diff --git a/shaders/forward.frag.glsl b/shaders/forward.frag.glsl index 3bfb81f..6ffc830 100644 --- a/shaders/forward.frag.glsl +++ b/shaders/forward.frag.glsl @@ -112,7 +112,7 @@ void main(void) { #endif #ifdef AMBIENT_LIGHT - outColor = vec4(diffuse*0, 1); + outColor = vec4(diffuse*0.1, 1); #else vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView); outColor = vec4(light*shadow*(1+cos(1.57 + att*1.57)), 1); diff --git a/src/mesh.cpp b/src/mesh.cpp index 2c88be1..8bc92f4 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -232,6 +232,8 @@ unsigned int Mesh::getFlags() flags |= 1 << MESH_TANGENT_SPACE; if(isBillboard) flags |= 1 << MESH_BILLBOARD; + if(isShadowCaster) + flags |= 1 << MESH_SHADOWED; } else flags |= 1 << MESH_2D; @@ -400,3 +402,23 @@ void Mesh::computeTangents() tangents[vertexId2] = {tangentDir, binormalDir}; } } + +void Mesh::computeBoundingBox(glm::vec3 &min, glm::vec3 &max) +{ + min = max = positions3D[0]; + for(const glm::vec3 &pos : positions3D) + { + if(pos.x < min.x) + min.x = pos.x; + else if(pos.x > max.x) + max.x = pos.x; + if(pos.y < min.y) + min.y = pos.y; + else if(pos.y > max.y) + max.y = pos.y; + if(pos.z < min.z) + min.z = pos.z; + else if(pos.z > max.z) + max.z = pos.z; + } +} diff --git a/src/mesh.h b/src/mesh.h index 7540467..b50dbd6 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -177,7 +177,12 @@ public: * compute tangent space from a textured indexed mesh (positions + normals + texcoords + indices) */ void computeTangents(); - + + /** + * compute the bounding box of the mesh based on the 3D positions + */ + void computeBoundingBox(glm::vec3 &min, glm::vec3 &max); + /*************************************************************/ /* ADVANCED CUSTOMISATION */ /*************************************************************/ diff --git a/src/texture.cpp b/src/texture.cpp index 857f631..22c0754 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -13,7 +13,8 @@ Texture::Texture(GLenum format, m_internal_format(internal_format), m_width(width), m_height(height), - m_dataType(dataType) + m_dataType(dataType), + m_hasMipMaps(false) { glGenTextures(1, &texId); glBindTexture(m_target, texId); @@ -38,24 +39,28 @@ Texture::Texture(GLenum format, } } -Texture::Texture(Image* myImage) : +Texture::Texture(Image* myImage, bool makeMipMaps) : m_target(GL_TEXTURE_2D), m_width(myImage->width), m_height(myImage->height), - m_dataType(GL_UNSIGNED_BYTE) + m_dataType(GL_UNSIGNED_BYTE), + m_hasMipMaps(makeMipMaps) { glGenTextures(1, &texId); glBindTexture(m_target, texId); initPixels(myImage, GL_TEXTURE_2D); setWrap(GL_REPEAT); setFiltering(GL_LINEAR); + if(makeMipMaps) + createMipMaps(); } -Texture::Texture(Image* myCubemapImages[6]) : +Texture::Texture(Image* myCubemapImages[6], bool makeMipMaps) : m_target(GL_TEXTURE_CUBE_MAP), m_width(myCubemapImages[0]->width), m_height(myCubemapImages[0]->height), - m_dataType(GL_UNSIGNED_BYTE) + m_dataType(GL_UNSIGNED_BYTE), + m_hasMipMaps(makeMipMaps) { glGenTextures(1, &texId); glBindTexture(m_target, texId); @@ -63,6 +68,8 @@ Texture::Texture(Image* myCubemapImages[6]) : initPixels(myCubemapImages[i], GL_TEXTURE_CUBE_MAP_POSITIVE_X + i); setWrap(GL_CLAMP_TO_EDGE); setFiltering(GL_LINEAR); + if(makeMipMaps) + createMipMaps(); } Texture::Texture(Texture* tex, bool halfDim) : @@ -71,7 +78,8 @@ Texture::Texture(Texture* tex, bool halfDim) : m_internal_format(tex->m_internal_format), m_width(tex->m_width), m_height(tex->m_height), - m_dataType(tex->m_dataType) + m_dataType(tex->m_dataType), + m_hasMipMaps(false) { glGenTextures(1, &texId); glBindTexture(m_target, texId); @@ -121,6 +129,13 @@ void Texture::setFiltering(GLint filter) glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, filter); } +void Texture::createMipMaps() +{ + m_hasMipMaps = true; + glGenerateMipmap(m_target); + glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); +} + void Texture::bind(int slot) { glActiveTexture(GL_TEXTURE0+slot); diff --git a/src/texture.h b/src/texture.h index 01be8c7..c7515fb 100644 --- a/src/texture.h +++ b/src/texture.h @@ -16,6 +16,7 @@ private: int m_width; int m_height; GLenum m_dataType; + bool m_hasMipMaps; void initPixels(Image* myImage, GLenum textureSlot); public: @@ -29,9 +30,9 @@ public: GLenum dataType = GL_UNSIGNED_BYTE, GLenum texTarget = GL_TEXTURE_2D); // creates a standard texture from an image - Texture(Image* myImage); + Texture(Image* myImage, bool makeMipMaps = true); // creates a cubeMap from 6 images - Texture(Image* myCubemapImages[6]); + Texture(Image* myCubemapImages[6], bool makeMipMaps = true); // creates a texture from another Texture(Texture* tex, bool halfDim = false); @@ -41,7 +42,9 @@ public: GLenum getTarget() {return m_target;} void setWrap(GLint wrap); void setFiltering(GLint filter); + void createMipMaps(); bool isCubeMap() {return m_target == GL_TEXTURE_CUBE_MAP;} + bool hasMipMaps() {return m_hasMipMaps;} int getWidth() {return m_width;} int getHeight() {return m_height;}