diff --git a/CMakeLists.txt b/CMakeLists.txt index 6521222..e7de0b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,6 @@ set(LIB_SRC_LIST shader.cpp skyboxmodule.cpp sparrowrenderer.cpp - crappymodule.cpp sphere.cpp texture.cpp ) diff --git a/crappymodule.cpp b/crappymodule.cpp deleted file mode 100644 index dd29e8a..0000000 --- a/crappymodule.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "crappymodule.h" -#include "camera.h" -#include "phongentity.h" -#include - -CrappyModule::CrappyModule(Lights::Light* myDirLight, Lights* myPointLights) : - dirLight(myDirLight), - pointLights(myPointLights) -{ - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glLightfv(GL_LIGHT0, GL_AMBIENT, glm::value_ptr(glm::vec4(glm::vec3(0.1f), 1))); - glLightfv(GL_LIGHT0, GL_POSITION, glm::value_ptr(glm::vec4(dirLight->position, 0))); - // TODO add point lights -} - -void CrappyModule::renderGL(Camera* myCamera) -{ - glLightfv(GL_LIGHT0, GL_DIFFUSE, glm::value_ptr(glm::vec4(dirLight->color, 1))); - glLightfv(GL_LIGHT0, GL_SPECULAR, glm::value_ptr(glm::vec4(dirLight->color, 1))); - for(PhongEntity* e : entities) - e->crappyDraw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix(), dirLight, pointLights); -} - -void CrappyModule::addEntity(PhongEntity* myEntity) -{ - entities.push_back(myEntity); -} - -void CrappyModule::clearEntities() -{ - entities.clear(); -} diff --git a/crappymodule.h b/crappymodule.h deleted file mode 100644 index 6478fa6..0000000 --- a/crappymodule.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef CRAPPYMODULE_H -#define CRAPPYMODULE_H - -#include "module.h" -#include "lights.h" - -class PhongEntity; - -class CrappyModule : public Module -{ -public: - CrappyModule(Lights::Light* myDirLight, Lights* myPointLights); - - void addEntity(PhongEntity* myEntity); - void clearEntities(); - - virtual void renderGL(Camera* myCamera); - virtual bool requiresModernOpenGL() {return false;} - -private: - Lights::Light* dirLight; - Lights* pointLights; - GLuint dirLightLocation; - GLuint nbPointLightsLocation; - GLuint pointLightsLocation; - std::vector entities; -}; - -#endif // CRAPPYMODULE_H diff --git a/meshbuilder.h b/meshbuilder.h index 2eb092f..0f9b978 100644 --- a/meshbuilder.h +++ b/meshbuilder.h @@ -23,7 +23,7 @@ public: void addGroup(Material* myMaterial); void setCurrentGroup(int groupId); - void setCurrentGroupMaterial(Material* myMaterial); + void setCurrentGroupMaterial(Material* myMaterial); int getNbGroups(); // require normals and texCoord diff --git a/phongentity.cpp b/phongentity.cpp index 837bbb1..f927043 100644 --- a/phongentity.cpp +++ b/phongentity.cpp @@ -5,12 +5,21 @@ #include "mesh.h" #include #include "glassert.h" +#include "sparrowrenderer.h" #define BUFFER_OFFSET(i) ((char *)NULL + (i)) PhongEntity::PhongEntity(Mesh* myMesh) : mesh(myMesh) {} -void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix, Lights::Light* dirLight, Lights* pointLights) +void PhongEntity::draw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights) +{ + if(SparrowRenderer::isModernOpenGLAvailable()) + modernDraw(viewMatrix, projectionMatrix, dirLight, pointLights); + else + crappyDraw(viewMatrix, projectionMatrix, dirLight, pointLights); +} + +void PhongEntity::modernDraw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights) { glm::mat4 modelViewMatrix = viewMatrix * modelMatrix; glm::mat4 mvp = projectionMatrix * modelViewMatrix; @@ -30,30 +39,17 @@ void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMat } } -void PhongEntity::crappyDraw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix, Lights::Light* dirLight, Lights* pointLights) +void PhongEntity::crappyDraw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights) { glMatrixMode(GL_MODELVIEW); glm::mat4 modelViewMatrix = viewMatrix * modelMatrix; glLoadMatrixf(glm::value_ptr(modelViewMatrix)); glMatrixMode(GL_PROJECTION); glLoadMatrixf(glm::value_ptr(projectionMatrix)); - for(int i=0; iindiceGroups.size(); ++i) - { - PhongMaterial* mat = (PhongMaterial*)(mesh->indiceGroups[i].material); - mat->crappyBindAttributes(); - glBegin(GL_TRIANGLES); - for(int j=0; jindiceGroups[i].indices.size(); ++j) - { - int vid = mesh->indiceGroups[i].indices[j]; - glNormal3fv(glm::value_ptr(mesh->normals[vid])); - glTexCoord2fv(glm::value_ptr(mesh->texCoords[vid])); - glVertex3fv(glm::value_ptr(mesh->positions[vid])); - } - glEnd(); - } + glCallList(displayList); } -void PhongEntity::initGL(bool isDynamic) +void PhongEntity::modernInit(bool isDynamic) { GLenum buffer_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW; @@ -62,11 +58,11 @@ void PhongEntity::initGL(bool isDynamic) glAssert(glBindVertexArray(vao)); nb_buffers = 1; // positions buffer - if(mesh->hasNormals()) + if(mesh->hasNormals()) ++nb_buffers; - if(mesh->hasTexCoords()) + if(mesh->hasTexCoords()) ++nb_buffers; - if(mesh->hasTangents()) + if(mesh->hasTangents()) ++nb_buffers; nb_buffers += mesh->indiceGroups.size(); @@ -86,21 +82,21 @@ void PhongEntity::initGL(bool isDynamic) glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER])); glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->positions.size() * sizeof(glm::vec3), mesh->positions.data(), buffer_type)); - if(mesh->hasNormals()) + if(mesh->hasNormals()) { // init normals vbo glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER])); glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->normals.size() * sizeof(glm::vec3), mesh->normals.data(), buffer_type)); } - if(mesh->hasNormals()) + if(mesh->hasNormals()) { // init texCoords vbo glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER])); glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->texCoords.size() * sizeof(glm::vec2), mesh->texCoords.data(), buffer_type)); } - if(mesh->hasTangents()) + if(mesh->hasTangents()) { // init tangents vbo glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TANGENT_BUFFER])); @@ -111,11 +107,45 @@ void PhongEntity::initGL(bool isDynamic) glAssert(glBindVertexArray(0)); } +void PhongEntity::crappyInit() +{ + displayList = glAssert(glGenLists(1)); + glAssert(glNewList(displayList, GL_COMPILE)); + for(int i=0; iindiceGroups.size(); ++i) + { + PhongMaterial* mat = (PhongMaterial*)(mesh->indiceGroups[i].material); + mat->crappyBindAttributes(); + glAssert(glBegin(GL_TRIANGLES)); + for(int j=0; jindiceGroups[i].indices.size(); ++j) + { + int vid = mesh->indiceGroups[i].indices[j]; + glNormal3fv(glm::value_ptr(mesh->normals[vid])); + glTexCoord2fv(glm::value_ptr(mesh->texCoords[vid])); + glVertex3fv(glm::value_ptr(mesh->positions[vid])); + } + glAssert(glEnd()); + } + glAssert(glEndList()); +} + +void PhongEntity::initGL(bool isDynamic) +{ + if(SparrowRenderer::isModernOpenGLAvailable()) + modernInit(isDynamic); + else + crappyInit(); +} + void PhongEntity::destroyGL() { - glAssert(glDeleteVertexArrays(1, &vao)); - glAssert(glDeleteBuffers(nb_buffers, vbo)); - delete[] vbo; + if(SparrowRenderer::isModernOpenGLAvailable()) + { + glAssert(glDeleteVertexArrays(1, &vao)); + glAssert(glDeleteBuffers(nb_buffers, vbo)); + delete[] vbo; + } + else + glAssert(glDeleteLists(displayList, 1)); } void PhongEntity::drawGroup(int groupId) @@ -146,3 +176,4 @@ void PhongEntity::drawGroup(int groupId) } glAssert(glDrawElements(GL_TRIANGLES, mesh->indiceGroups[groupId].indices.size(), GL_UNSIGNED_INT, mesh->indiceGroups[groupId].indices.data())); } + diff --git a/phongentity.h b/phongentity.h index fe1ce39..8c34e28 100644 --- a/phongentity.h +++ b/phongentity.h @@ -13,6 +13,10 @@ class Shader; class PhongEntity { protected: + Mesh* mesh; + + // modern opengl : + enum { // required buffers POSITION_BUFFER, @@ -22,20 +26,27 @@ protected: INDICES_BUFFER }; - Mesh* mesh; - GLuint vao; int nb_buffers; GLuint* vbo; + void modernInit(bool isDynamic); void drawGroup(int groupId); + void modernDraw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights); + + // old opengl : + + GLuint displayList; + + void crappyInit(); + void crappyDraw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights); + public: glm::mat4 modelMatrix; PhongEntity(Mesh* myMesh); - void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix, Lights::Light* dirLight, Lights* pointLights); - void crappyDraw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix, Lights::Light* dirLight, Lights* pointLights); + void draw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights); void initGL(bool isDynamic = false); void destroyGL(); diff --git a/phongmodule.cpp b/phongmodule.cpp index f54a856..8384a45 100644 --- a/phongmodule.cpp +++ b/phongmodule.cpp @@ -4,6 +4,9 @@ #include "phongentity.h" #include "camera.h" #include "mesh.h" +#include "sparrowrenderer.h" +#include "glassert.h" +#include Shader* PhongModule::shaders[NB_SHADERS] = {0, 0}; @@ -11,7 +14,14 @@ PhongModule::PhongModule(Lights::Light* myDirLight, Lights* myPointLights) : dirLight(myDirLight), pointLights(myPointLights) { - + if(!SparrowRenderer::isModernOpenGLAvailable()) + { + glAssert(glEnable(GL_LIGHTING)); + glAssert(glEnable(GL_LIGHT0)); + glAssert(glLightfv(GL_LIGHT0, GL_AMBIENT, glm::value_ptr(glm::vec4(glm::vec3(0.1f), 1)))); + glAssert(glLightfv(GL_LIGHT0, GL_POSITION, glm::value_ptr(glm::vec4(dirLight->position, 0)))); + // TODO add point lights + } } void PhongModule::addEntity(PhongEntity* myEntity) @@ -26,6 +36,11 @@ void PhongModule::clearEntities() void PhongModule::renderGL(Camera* myCamera) { + if(!SparrowRenderer::isModernOpenGLAvailable()) + { + glAssert(glLightfv(GL_LIGHT0, GL_DIFFUSE, glm::value_ptr(glm::vec4(dirLight->color, 1)))); + glAssert(glLightfv(GL_LIGHT0, GL_SPECULAR, glm::value_ptr(glm::vec4(dirLight->color, 1)))); + } for(PhongEntity* e : entities) e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix(), dirLight, pointLights); } diff --git a/phongmodule.h b/phongmodule.h index 263686f..d3d8462 100644 --- a/phongmodule.h +++ b/phongmodule.h @@ -13,7 +13,12 @@ class PhongEntity; class PhongModule : public Module { public: - enum ShaderType {PHONG_COLOR, PHONG_TEXTURE, NB_SHADERS}; + enum ShaderType { + PHONG_COLOR, // rendering untextured mesh + PHONG_TEXTURE, // rendering textured mesh + //TODO : rendering bump mapped mesh + NB_SHADERS + }; PhongModule(Lights::Light* myDirLight, Lights* myPointLights); @@ -21,7 +26,13 @@ public: void clearEntities(); virtual void renderGL(Camera* myCamera); + virtual bool requiresModernOpenGL() {return false;} + // modern opengl methods + + /** + * @brief all used shaders shall be provided through this method before rendering + */ static void setShader(ShaderType slot, Shader* myShader); static Shader* getShader(ShaderType slot); private: diff --git a/sparrowrenderer.cpp b/sparrowrenderer.cpp index 754d9c2..6b3caf3 100644 --- a/sparrowrenderer.cpp +++ b/sparrowrenderer.cpp @@ -7,6 +7,8 @@ // main methods +bool SparrowRenderer::modernOpenglAvailable = false; + void SparrowRenderer::initGL(int width, int height) { glewExperimental = GL_TRUE; diff --git a/sparrowrenderer.h b/sparrowrenderer.h index 6760f69..a60c967 100644 --- a/sparrowrenderer.h +++ b/sparrowrenderer.h @@ -15,7 +15,7 @@ public: void destroyGL(); void resizeGL(int width, int height); void renderGL(); - bool isModernOpenGLAvailable(); + static bool isModernOpenGLAvailable(); // modules methods void addModule(Module* myModule, std::string name); @@ -36,7 +36,7 @@ protected: Camera* camera; std::list modules; - bool modernOpenglAvailable; + static bool modernOpenglAvailable; std::list::iterator getModuleNode(std::string name); };