diff --git a/shaders/forward.frag.glsl b/shaders/forward.frag.glsl index 1cd6dfe..ce955a9 100644 --- a/shaders/forward.frag.glsl +++ b/shaders/forward.frag.glsl @@ -42,8 +42,15 @@ in vec4 posInLightSpace; in vec2 varTexCoord; +#ifndef AMBIENT_LIGHT in vec3 lightDirInView; in vec3 halfVecInView; +#endif + +#ifdef POINT_LIGHT +in vec3 lightDistInView; +uniform float attenuation; +#endif layout(location = 0)out vec4 outColor; @@ -95,10 +102,17 @@ void main(void) { float shadow = 1; #endif + float att = 0; +#ifdef POINT_LIGHT + att = length(lightDistInView)*attenuation; + if(att > 1) + discard; +#endif + #ifdef AMBIENT_LIGHT - outColor = vec4(0, 0, 0, 1); + outColor = vec4(diffuse*0.1f, 1); #else vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView); - outColor = vec4(shadow*light, 1); + outColor = vec4(light*shadow*(1+cos(1.57 + att*1.57)), 1); #endif } diff --git a/shaders/forward.vert.glsl b/shaders/forward.vert.glsl index e4459ef..a75f137 100644 --- a/shaders/forward.vert.glsl +++ b/shaders/forward.vert.glsl @@ -34,6 +34,10 @@ out vec3 lightDirInView; out vec3 halfVecInView; #endif +#ifdef POINT_LIGHT +out vec3 lightDistInView; +#endif + #ifdef SHADOWMAP uniform mat4 lightMVP; out vec4 posInLightSpace; @@ -52,8 +56,9 @@ void main(void) { #endif #ifdef POINT_LIGHT - vec4 lightPosInView = viewMatrix*vec4(pointLight, 1); - lightDirInView = normalize(lightPosInView.xyz - posInView); + vec3 lightPosInView = vec3(viewMatrix*vec4(pointLight, 1)); + lightDistInView = lightPosInView - posInView; + lightDirInView = normalize(lightDistInView); halfVecInView = normalize(lightDirInView - normalize(posInView)); #endif diff --git a/shaders/hdr.frag.glsl b/shaders/hdr.frag.glsl index ebf27ac..866232b 100644 --- a/shaders/hdr.frag.glsl +++ b/shaders/hdr.frag.glsl @@ -2,7 +2,7 @@ uniform sampler2DRect colorSampler; -uniform float exposition; +uniform vec3 minMaxMean; uniform float gamma; layout(location = 0)out vec4 outColor; @@ -11,8 +11,9 @@ void main(void) { ivec2 pos = ivec2(gl_FragCoord.xy); vec3 color = texelFetch(colorSampler, pos).xyz; + vec3 mapped = (color-minMaxMean.r)/(minMaxMean.g-minMaxMean.r); // Exposure tone mapping - vec3 mapped = vec3(1.0) - exp(-color * 1); + //vec3 mapped = vec3(1.0) - exp(-color * exposition); // Gamma correction mapped = pow(mapped, vec3(1.0 / gamma)); diff --git a/src/forwardmodule.cpp b/src/forwardmodule.cpp index c128339..65c9323 100644 --- a/src/forwardmodule.cpp +++ b/src/forwardmodule.cpp @@ -43,6 +43,11 @@ void ForwardModule::renderGL(Camera* myCamera, Scene* scene) void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light) { + if(isWireframe){ + glAssert(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); + }else{ + glAssert(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)); + } // loop over all types of geometry for(int i=0; ibindVec3(shader->getLocation("pointLight"), light->getPos()); shader->bindVec3(shader->getLocation("lightColor"), light->getColor()); - // TODO add attenuation + shader->bindFloat(shader->getLocation("attenuation"), light->getAttenuation()); break; case Light::SPOT: shader->bindVec3(shader->getLocation("lightColor"), light->getColor()); diff --git a/src/forwardmodule.h b/src/forwardmodule.h index 65a28f5..f027e86 100644 --- a/src/forwardmodule.h +++ b/src/forwardmodule.h @@ -18,7 +18,8 @@ class ForwardModule : public Module public: ForwardModule() : shaderSources(NULL), - renderTarget(FrameBuffer::screen) + renderTarget(FrameBuffer::screen), + isWireframe(false) {} virtual void renderGL(Camera* myCamera, Scene* scene); @@ -30,6 +31,7 @@ public: void compileShaders(Scene* scene); void setRenderTarget(const FrameBuffer* target); + void setWireframe(bool wireframe) {isWireframe = wireframe;} private: static const char* const flagStr[NB_FLAGS]; @@ -40,6 +42,8 @@ private: std::vector lightFlagList; const FrameBuffer* renderTarget; + bool isWireframe; + void lightPass(Camera* myCamera, Scene* scene, Light* light); }; diff --git a/src/light.cpp b/src/light.cpp index fcc19a5..32d51ec 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -39,7 +39,7 @@ void Light::initDirectionnalLight(glm::vec3 dir, glm::vec3 lightColor) viewMatrix = glm::lookAt(position, position+direction, glm::vec3(0, 1, 0)); } -void Light::initPointLight(glm::vec3 pos, glm::vec3 lightColor) +void Light::initPointLight(glm::vec3 pos, glm::vec3 lightColor, float att) { type = POINT; position = pos; @@ -48,6 +48,7 @@ void Light::initPointLight(glm::vec3 pos, glm::vec3 lightColor) shadowCaster = false; isDir = false; viewMatrix = glm::mat4(); + attenuation = att; } void Light::initSpotLight(glm::vec3 pos, glm::vec3 dir, float spotAngle, glm::vec3 lightColor) @@ -95,6 +96,7 @@ void Light::initShadowMap(int resWidth, int resHeight, glm::vec3 dim) void Light::generateShadowMap(Scene* scene) { + glAssert(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)); glAssert(glViewport(0, 0, shadowMapWidth, shadowMapHeight)); shadowMap->bindFBO(); glAssert(glClearDepth(1.0)); diff --git a/src/light.h b/src/light.h index 6157c38..bd34e3c 100644 --- a/src/light.h +++ b/src/light.h @@ -33,7 +33,7 @@ public: Light(); void initDirectionnalLight(glm::vec3 dir = glm::vec3(1, 0, 0), glm::vec3 lightColor = glm::vec3(1)); - void initPointLight(glm::vec3 pos = glm::vec3(0), glm::vec3 lightColor = glm::vec3(1)); + void initPointLight(glm::vec3 pos = glm::vec3(0), glm::vec3 lightColor = glm::vec3(1), float att = 1); void initSpotLight(glm::vec3 pos = glm::vec3(0), glm::vec3 dir = glm::vec3(1, 0, 0), float spotAngle = 360, glm::vec3 lightColor = glm::vec3(1)); bool isDirectionnal(); @@ -41,12 +41,15 @@ public: glm::vec3 getDir() {return direction;} glm::vec3 getPos() {return position;} glm::vec3 getColor() {return color;} + float getAttenuation() {return attenuation;} bool isShadowCaster() {return shadowCaster;} void initShadowMap(int resWidth, int resHeight, glm::vec3 dim = glm::vec3(1)); void generateShadowMap(Scene* scene); Texture* getShadowMap(); + void setAttenuation(float a) {attenuation = a;} + // camera inheritance virtual glm::mat4 getProjectionMatrix() {return projectionMatrix;} virtual glm::mat4 getViewMatrix() {return viewMatrix;} diff --git a/src/mesh.cpp b/src/mesh.cpp index 63f06ee..e508d3e 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -191,45 +191,57 @@ Mesh* VertexComparator::mesh = NULL; void Mesh::mergeVertices() { - std::vector swapped; + bool *deleted = new bool[positions.size()]; + int *offsets = new int[positions.size()]; std::set vertexSet; VertexComparator::setMesh(this); - int swappedOffset = positions.size() - 1; - for(int i=0; i= positions.size()) - indices[i] = swapped[swappedOffset - indices[i]]; std::pair::iterator,bool> ret = vertexSet.insert(indices[i]); - if(!ret.second) // duplicate found + deleted[indices[i]] = !ret.second && *(ret.first) != indices[i]; + if(deleted[indices[i]]) { - // updating indices references - int toDelete = indices[i]; - swapped.push_back(toDelete); - indices[i] = *(ret.first); - // deleting the duplicate - positions[toDelete] = positions.back(); - positions.pop_back(); - if(hasTexCoords()) - { - texCoords[toDelete] = texCoords.back(); - texCoords.pop_back(); - } - if(hasNormals()) - { - normals[toDelete] = normals.back(); - normals.pop_back(); - } if(hasTangents()) { - tangents[indices[i]].tangent += tangents[toDelete].tangent; - tangents[indices[i]].binormal += tangents[toDelete].binormal; - tangents[toDelete] = tangents.back(); - tangents.pop_back(); + tangents[*(ret.first)].tangent += tangents[indices[i]].tangent; + tangents[*(ret.first)].binormal += tangents[indices[i]].binormal; } + indices[i] = *(ret.first); } } + int offset = 0; + int pos = 0; + for(int i=0; iredux(); - float exposition = 1/(2*minMaxMean.z); float gamma = 2.2f; glAssert(glViewport(0, 0, width, height)); @@ -189,7 +189,7 @@ void PostEffectModule::hdrStep() shaders[HDR_SHADER]->bindInteger(shaders[HDR_SHADER]->getLocation("colorSampler"), 0); frameBuffers[BLOOM_FBO].getTexture(1)->bind(0); - shaders[HDR_SHADER]->bindFloat(shaders[HDR_SHADER]->getLocation("exposition"), exposition); + shaders[HDR_SHADER]->bindVec3(shaders[HDR_SHADER]->getLocation("minMaxMean"), minMaxMean); shaders[HDR_SHADER]->bindFloat(shaders[HDR_SHADER]->getLocation("gamma"), gamma); glAssert(glDrawArrays(GL_TRIANGLES, 0, 3)); diff --git a/src/scene.h b/src/scene.h index 124f389..90a12ac 100644 --- a/src/scene.h +++ b/src/scene.h @@ -59,6 +59,7 @@ public: void clearScene() {clearLights(); clearEntities();} void addMesh(GeometryNode* node) {geometry.push_back(node);} void addLight(Light* myLight) {lights.push_back(myLight);} + Mesh* getMesh(int id) {return geometry[id]->mesh;} virtual SceneIterator* getLights() {return new ArraySceneIterator(lights);} virtual SceneIterator* getGeometry() {return new ArraySceneIterator(geometry);}