fixed some bugs, forward module is working

This commit is contained in:
anselme 2015-11-29 22:32:44 +01:00
parent 2381318a09
commit a00d1ee777
12 changed files with 56 additions and 28 deletions

View File

@ -79,6 +79,10 @@ void main(void) {
vec3 specular = materialKs; vec3 specular = materialKs;
#endif #endif
#ifdef AMBIENT_LIGHT
outColor = vec4(ambient + lightColor*diffuse, 1);
#else
vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView); vec3 light = phongLighting(diffuse, specular, materialNs, lightColor, normal, lightDirInView, halfVecInView);
outColor = vec4(ambient + light, 1); outColor = vec4(light, 1);
#endif
} }

View File

@ -22,8 +22,8 @@ uniform vec3 pointLight;
#endif #endif
layout(location = 0)in vec3 inPosition; layout(location = 0)in vec3 inPosition;
layout(location = 1)in vec2 inTexCoord; layout(location = 2)in vec2 inTexCoord;
layout(location = 2)in vec3 inNormal; layout(location = 1)in vec3 inNormal;
#ifdef NORMAL_MAP #ifdef NORMAL_MAP
layout(location = 3)in vec3 inTangent; layout(location = 3)in vec3 inTangent;
layout(location = 4)in vec3 inBinormal; layout(location = 4)in vec3 inBinormal;

View File

@ -4,6 +4,7 @@
#include "mesh.h" #include "mesh.h"
#include "shader.h" #include "shader.h"
#include "light.h" #include "light.h"
#include "glassert.h"
#include <glm/ext.hpp> #include <glm/ext.hpp>
const char* const ForwardModule::flagStr[] = const char* const ForwardModule::flagStr[] =
@ -25,19 +26,34 @@ const char* const ForwardModule::lightStr[] =
void ForwardModule::renderGL(Camera* myCamera, Scene* scene) void ForwardModule::renderGL(Camera* myCamera, Scene* scene)
{ {
// render ambient lighting // 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 // 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<Light*>* lightIt = scene->getLights(); for(SceneIterator<Light*>* lightIt = scene->getLights();
lightIt->isValid(); lightIt->next()) lightIt->isValid(); lightIt->next())
{ {
Light* l = lightIt->getItem(); Light* l = lightIt->getItem();
unsigned int type = l->isDirectionnal() ? DIRECTIONNAL_LIGHT : POINT_LIGHT; 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 // loop over all types of geometry
for(int i=0; i<flags.size(); ++i) for(int i=0; i<flags.size(); ++i)
@ -67,15 +83,15 @@ void ForwardModule::lightPass(Light* light, Camera* myCamera, Scene* scene, unsi
// loop over material groups // loop over material groups
for(int j=0; j<entity->getMesh()->indiceGroups.size(); ++j) for(int j=0; j<entity->getMesh()->indiceGroups.size(); ++j)
{ {
Material* mat = entity->getMesh()->indiceGroups[i].material; Material* mat = entity->getMesh()->indiceGroups[j].material;
if(mat->getFlags() == flags[i]) if(mat->getFlags() == flags[i])
{ {
// bind material attributes // bind material attributes
mat->bindAttributes(shader); mat->bindAttributes(shader);
shader->bindMatrix(shader->getLocation("viewMatrix"), myCamera->getViewMatrix()); shader->bindMat4(shader->getLocation("viewMatrix"), myCamera->getViewMatrix());
shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix);
shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix); shader->bindMat3(shader->getLocation("normalMatrix"), glm::mat3(normalMatrix));
shader->bindMatrix(shader->getLocation("MVP"), mvp); shader->bindMat4(shader->getLocation("MVP"), mvp);
// draw geometry // draw geometry
entity->drawGroup(j); entity->drawGroup(j);
} }
@ -88,11 +104,18 @@ void ForwardModule::lightPass(Light* light, Camera* myCamera, Scene* scene, unsi
void ForwardModule::setShaderSource(ShaderSource* source) void ForwardModule::setShaderSource(ShaderSource* source)
{ {
if(shaderSources != NULL)
delete(shaderSources);
shaderSources = source; shaderSources = source;
} }
void ForwardModule::compileShaders(Scene* scene) void ForwardModule::compileShaders(Scene* scene)
{ {
flags.clear();
for(Shader* s : shaders)
delete(s);
shaders.clear();
int size = 1 << NB_FLAGS; int size = 1 << NB_FLAGS;
bool geometryFlags[size]; bool geometryFlags[size];
for(int i=0; i<size; ++i) for(int i=0; i<size; ++i)

View File

@ -18,7 +18,7 @@ public:
ForwardModule() : shaderSources(NULL) {} ForwardModule() : shaderSources(NULL) {}
virtual void renderGL(Camera* myCamera, Scene* scene); virtual void renderGL(Camera* myCamera, Scene* scene);
virtual bool requiresModernOpenGL() {return false;} virtual bool requiresModernOpenGL() {return true;} // write some compatibility code to change that to false
// modern opengl methods // modern opengl methods
@ -42,7 +42,7 @@ private:
std::vector<Shader*> shaders; std::vector<Shader*> shaders;
std::vector<unsigned int> flags; std::vector<unsigned int> 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 #endif // FORWARDMODULE_H

View File

@ -2,9 +2,7 @@
#define GLASSERT #define GLASSERT
/** /**
*
* OpenGL error management class. * OpenGL error management class.
*
*/ */
#include <iostream> #include <iostream>
@ -17,7 +15,7 @@
{\ {\
GLuint err = glGetError(); \ GLuint err = glGetError(); \
if (err != GL_NO_ERROR) { \ if (err != GL_NO_ERROR) { \
std::cerr<<"Erreur OpenGL ("<<__FILE__<<":"<<__LINE__<<", "<<STR(code)<<") : "<<(const char*)gluErrorString (err)<<"("<<err<<")"<<std::endl; \ std::cerr<<"Erreur OpenGL ("<<__FILE__<<":"<<__LINE__<<", "<<STR(code)<<") : "<<(const char*)gluErrorString (err)<<"("<<err<<")"<<std::endl; \
} \ } \
} }
#else #else

View File

@ -36,10 +36,10 @@ void PhongEntity::modernDraw(const glm::mat4 &viewMatrix, const glm::mat4 &proje
PhongMaterial* mat = (PhongMaterial*)mesh->indiceGroups[i].material; PhongMaterial* mat = (PhongMaterial*)mesh->indiceGroups[i].material;
mat->bindAttributes(); mat->bindAttributes();
Shader* shader = mat->getShader(); Shader* shader = mat->getShader();
shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix); shader->bindMat4(shader->getLocation("viewMatrix"), viewMatrix);
shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix);
shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix); shader->bindMat4(shader->getLocation("normalMatrix"), normalMatrix);
shader->bindMatrix(shader->getLocation("MVP"), mvp); shader->bindMat4(shader->getLocation("MVP"), mvp);
shader->bindVec3Array(shader->getLocation("dirLight"), (glm::vec3*)dirLight, 2); shader->bindVec3Array(shader->getLocation("dirLight"), (glm::vec3*)dirLight, 2);
pointLights->bind(shader->getLocation("pointLights"), shader->getLocation("nbPointLights"), shader); pointLights->bind(shader->getLocation("pointLights"), shader->getLocation("nbPointLights"), shader);
drawGroup(i); drawGroup(i);

View File

@ -36,6 +36,8 @@ void PhongModule::clearEntities()
void PhongModule::renderGL(Camera* myCamera, Scene* scene) 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()) if(!SparrowRenderer::isModernOpenGLAvailable())
{ {
glLoadIdentity(); glLoadIdentity();

View File

@ -160,7 +160,12 @@ void Shader::bindFloat(GLuint location, float val)
glAssert(glUniform1f(location, 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))); glAssert(glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(mat)));
} }

View File

@ -21,7 +21,8 @@ public:
void bind(); void bind();
void unbind(); void unbind();
void bindFloat(GLuint location, float val); 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 bindVec3(GLuint location, glm::vec3 vec);
void bindVec4(GLuint location, glm::vec4 vec); void bindVec4(GLuint location, glm::vec4 vec);
void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements); void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements);

View File

@ -3,8 +3,6 @@
#include <sstream> #include <sstream>
#include "shader.h" #include "shader.h"
#include <iostream>
ShaderSource::ShaderSource() ShaderSource::ShaderSource()
{ {
for(int i=0; i<NB_TYPES; ++i) for(int i=0; i<NB_TYPES; ++i)
@ -78,6 +76,5 @@ std::string ShaderSource::preprocess(std::string source, int nbDefines, const ch
else if(allowed) else if(allowed)
compiled.append(line+'\n'); compiled.append(line+'\n');
} }
std::cout << compiled << std::endl;
return compiled; return compiled;
} }

View File

@ -66,7 +66,7 @@ void SkyboxModule::renderGL(Camera* myCamera, Scene* scene)
else else
{ {
shader->bind(); shader->bind();
shader->bindMatrix(mvpLocation, projectionMatrix * viewMatrix); shader->bindMat4(mvpLocation, projectionMatrix * viewMatrix);
} }
cubeMap->bind(0); cubeMap->bind(0);
glAssert(glBindVertexArray(vao)); glAssert(glBindVertexArray(vao));

View File

@ -57,8 +57,6 @@ void SparrowRenderer::resizeGL(int width, int height)
void SparrowRenderer::renderGL() void SparrowRenderer::renderGL()
{ {
glAssert(glClearColor(0, 0, 0, 1.0));
glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
for(ModuleNode &m : modules) for(ModuleNode &m : modules)
{ {
if(m.isEnabled) if(m.isEnabled)