From a00d1ee77744bc25c06219c8c0467772672dc623 Mon Sep 17 00:00:00 2001 From: anselme Date: Sun, 29 Nov 2015 22:32:44 +0100 Subject: [PATCH] fixed some bugs, forward module is working --- forward.frag | 6 +++++- forward.vert | 4 ++-- forwardmodule.cpp | 39 +++++++++++++++++++++++++++++++-------- forwardmodule.h | 4 ++-- glassert.h | 4 +--- phongentity.cpp | 8 ++++---- phongmodule.cpp | 2 ++ shader.cpp | 7 ++++++- shader.h | 3 ++- shadersource.cpp | 3 --- skyboxmodule.cpp | 2 +- sparrowrenderer.cpp | 2 -- 12 files changed, 56 insertions(+), 28 deletions(-) diff --git a/forward.frag b/forward.frag index 24dd4ba..b9f7a94 100644 --- a/forward.frag +++ b/forward.frag @@ -79,6 +79,10 @@ void main(void) { vec3 specular = materialKs; #endif +#ifdef AMBIENT_LIGHT + outColor = vec4(ambient + lightColor*diffuse, 1); +#else vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView); - outColor = vec4(ambient + light, 1); + outColor = vec4(light, 1); +#endif } diff --git a/forward.vert b/forward.vert index 199df81..3fefc81 100644 --- a/forward.vert +++ b/forward.vert @@ -22,8 +22,8 @@ uniform vec3 pointLight; #endif layout(location = 0)in vec3 inPosition; -layout(location = 1)in vec2 inTexCoord; -layout(location = 2)in vec3 inNormal; +layout(location = 2)in vec2 inTexCoord; +layout(location = 1)in vec3 inNormal; #ifdef NORMAL_MAP layout(location = 3)in vec3 inTangent; layout(location = 4)in vec3 inBinormal; diff --git a/forwardmodule.cpp b/forwardmodule.cpp index 63b0301..45a3f96 100644 --- a/forwardmodule.cpp +++ b/forwardmodule.cpp @@ -4,6 +4,7 @@ #include "mesh.h" #include "shader.h" #include "light.h" +#include "glassert.h" #include const char* const ForwardModule::flagStr[] = @@ -25,19 +26,34 @@ const char* const ForwardModule::lightStr[] = void ForwardModule::renderGL(Camera* myCamera, Scene* scene) { // render ambient lighting - lightPass(NULL, myCamera, scene, AMBIENT_LIGHT); + glAssert(glClearColor(0, 0.1f, 0.05f, 1.)); // add attribute, and setter for clear color + glAssert(glClearDepth(1.0)); + glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); + glAssert(glDepthFunc(GL_LESS)); + glAssert(glDisable(GL_BLEND)); + + lightPass(myCamera, scene, NULL, AMBIENT_LIGHT); // render directionnal lighting and point lighting + glAssert(glDepthFunc(GL_LEQUAL)); + glAssert(glEnable(GL_BLEND)); + glAssert(glBlendFunc(GL_ONE, GL_ONE)); + glAssert(glDepthMask(GL_FALSE)); + for(SceneIterator* lightIt = scene->getLights(); lightIt->isValid(); lightIt->next()) { Light* l = lightIt->getItem(); unsigned int type = l->isDirectionnal() ? DIRECTIONNAL_LIGHT : POINT_LIGHT; - lightPass(l, myCamera, scene, type); + lightPass(myCamera, scene, l, type); } + + glAssert(glDisable(GL_BLEND)); + glAssert(glDepthFunc(GL_LESS)); + glAssert(glDepthMask(GL_TRUE)); } -void ForwardModule::lightPass(Light* light, Camera* myCamera, Scene* scene, unsigned int type) +void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light, unsigned int type) { // loop over all types of geometry for(int i=0; igetMesh()->indiceGroups.size(); ++j) { - Material* mat = entity->getMesh()->indiceGroups[i].material; + Material* mat = entity->getMesh()->indiceGroups[j].material; if(mat->getFlags() == flags[i]) { // bind material attributes mat->bindAttributes(shader); - shader->bindMatrix(shader->getLocation("viewMatrix"), myCamera->getViewMatrix()); - shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); - shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix); - shader->bindMatrix(shader->getLocation("MVP"), mvp); + shader->bindMat4(shader->getLocation("viewMatrix"), myCamera->getViewMatrix()); + shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix); + shader->bindMat3(shader->getLocation("normalMatrix"), glm::mat3(normalMatrix)); + shader->bindMat4(shader->getLocation("MVP"), mvp); // draw geometry entity->drawGroup(j); } @@ -88,11 +104,18 @@ void ForwardModule::lightPass(Light* light, Camera* myCamera, Scene* scene, unsi void ForwardModule::setShaderSource(ShaderSource* source) { + if(shaderSources != NULL) + delete(shaderSources); shaderSources = source; } void ForwardModule::compileShaders(Scene* scene) { + flags.clear(); + for(Shader* s : shaders) + delete(s); + shaders.clear(); + int size = 1 << NB_FLAGS; bool geometryFlags[size]; for(int i=0; i shaders; std::vector flags; - void lightPass(Light* light, Camera* myCamera, Scene* scene, unsigned int type); + void lightPass(Camera* myCamera, Scene* scene, Light* light, unsigned int type); }; #endif // FORWARDMODULE_H diff --git a/glassert.h b/glassert.h index 4be8ad0..442f5dc 100644 --- a/glassert.h +++ b/glassert.h @@ -2,9 +2,7 @@ #define GLASSERT /** - * * OpenGL error management class. - * */ #include @@ -17,7 +15,7 @@ {\ GLuint err = glGetError(); \ if (err != GL_NO_ERROR) { \ - std::cerr<<"Erreur OpenGL ("<<__FILE__<<":"<<__LINE__<<", "<indiceGroups[i].material; mat->bindAttributes(); Shader* shader = mat->getShader(); - shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix); - shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); - shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix); - shader->bindMatrix(shader->getLocation("MVP"), mvp); + shader->bindMat4(shader->getLocation("viewMatrix"), viewMatrix); + shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix); + shader->bindMat4(shader->getLocation("normalMatrix"), normalMatrix); + shader->bindMat4(shader->getLocation("MVP"), mvp); shader->bindVec3Array(shader->getLocation("dirLight"), (glm::vec3*)dirLight, 2); pointLights->bind(shader->getLocation("pointLights"), shader->getLocation("nbPointLights"), shader); drawGroup(i); diff --git a/phongmodule.cpp b/phongmodule.cpp index 713642d..2d1838e 100644 --- a/phongmodule.cpp +++ b/phongmodule.cpp @@ -36,6 +36,8 @@ void PhongModule::clearEntities() void PhongModule::renderGL(Camera* myCamera, Scene* scene) { + glAssert(glClearColor(0, 0, 0, 1.0)); + glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); if(!SparrowRenderer::isModernOpenGLAvailable()) { glLoadIdentity(); diff --git a/shader.cpp b/shader.cpp index 0d9ec0d..0003a5c 100644 --- a/shader.cpp +++ b/shader.cpp @@ -160,7 +160,12 @@ void Shader::bindFloat(GLuint location, float val) glAssert(glUniform1f(location, val)); } -void Shader::bindMatrix(GLuint location, glm::mat4 mat) +void Shader::bindMat3(GLuint location, glm::mat3 mat) +{ + glAssert(glUniformMatrix3fv(location, 1, GL_FALSE, glm::value_ptr(mat))); +} + +void Shader::bindMat4(GLuint location, glm::mat4 mat) { glAssert(glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(mat))); } diff --git a/shader.h b/shader.h index 7bfa3d0..e376b92 100644 --- a/shader.h +++ b/shader.h @@ -21,7 +21,8 @@ public: void bind(); void unbind(); void bindFloat(GLuint location, float val); - void bindMatrix(GLuint location, glm::mat4 mat); + void bindMat3(GLuint location, glm::mat3 mat); + void bindMat4(GLuint location, glm::mat4 mat); void bindVec3(GLuint location, glm::vec3 vec); void bindVec4(GLuint location, glm::vec4 vec); void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements); diff --git a/shadersource.cpp b/shadersource.cpp index 66d6133..6d27e2d 100644 --- a/shadersource.cpp +++ b/shadersource.cpp @@ -3,8 +3,6 @@ #include #include "shader.h" -#include - ShaderSource::ShaderSource() { for(int i=0; ibind(); - shader->bindMatrix(mvpLocation, projectionMatrix * viewMatrix); + shader->bindMat4(mvpLocation, projectionMatrix * viewMatrix); } cubeMap->bind(0); glAssert(glBindVertexArray(vao)); diff --git a/sparrowrenderer.cpp b/sparrowrenderer.cpp index b8f5c24..bdb4cb5 100644 --- a/sparrowrenderer.cpp +++ b/sparrowrenderer.cpp @@ -57,8 +57,6 @@ void SparrowRenderer::resizeGL(int width, int height) void SparrowRenderer::renderGL() { - glAssert(glClearColor(0, 0, 0, 1.0)); - glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); for(ModuleNode &m : modules) { if(m.isEnabled)