From a68f7107e12141b9cc5f365af81d5a00a49733d0 Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 8 Sep 2015 17:49:34 +0200 Subject: [PATCH] temp commit of crappymodule implementation --- CMakeLists.txt | 1 + crappymodule.cpp | 23 +++++++++++++++++++++++ crappymodule.h | 20 ++++++++++++++++++++ module.h | 1 + phongentity.cpp | 22 ++++++++++++++++++++++ phongentity.h | 4 +++- phongmaterial.cpp | 17 ++++++++++++++--- phongmaterial.h | 1 + phongmodule.h | 3 ++- skyboxmodule.cpp | 27 +++++++++++++++------------ skyboxmodule.h | 1 + sparrowrenderer.cpp | 15 +++++++++------ 12 files changed, 112 insertions(+), 23 deletions(-) create mode 100644 crappymodule.cpp create mode 100644 crappymodule.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e7de0b8..6521222 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ set(LIB_SRC_LIST shader.cpp skyboxmodule.cpp sparrowrenderer.cpp + crappymodule.cpp sphere.cpp texture.cpp ) diff --git a/crappymodule.cpp b/crappymodule.cpp new file mode 100644 index 0000000..3d7d258 --- /dev/null +++ b/crappymodule.cpp @@ -0,0 +1,23 @@ +#include "crappymodule.h" +#include "camera.h" +#include "phongentity.h" + +CrappyModule::CrappyModule(Lights::Light* myDirLight, Lights* myPointLights) +{ + +} + +void CrappyModule::renderGL(Camera* myCamera) +{ + +} + +void CrappyModule::addEntity(PhongEntity* myEntity) +{ + +} + +void CrappyModule::clearEntities() +{ + +} diff --git a/crappymodule.h b/crappymodule.h new file mode 100644 index 0000000..41e98b3 --- /dev/null +++ b/crappymodule.h @@ -0,0 +1,20 @@ +#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); +}; + +#endif // CRAPPYMODULE_H diff --git a/module.h b/module.h index aaad992..8cbc2b0 100644 --- a/module.h +++ b/module.h @@ -7,6 +7,7 @@ class Module { public: virtual void renderGL(Camera* myCamera) = 0; + virtual bool isAvailable() {return true;} }; #endif // MODULE diff --git a/phongentity.cpp b/phongentity.cpp index dfc4c30..59a4e57 100644 --- a/phongentity.cpp +++ b/phongentity.cpp @@ -35,6 +35,28 @@ 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) +{ + 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) + { + //Material* mat = (PhongMaterial*)(mesh->indiceGroups[i].material); + 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(); + } +} + void PhongEntity::initGL(bool isDynamic) { GLenum buffer_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW; diff --git a/phongentity.h b/phongentity.h index 3b4f533..bd3487e 100644 --- a/phongentity.h +++ b/phongentity.h @@ -29,13 +29,15 @@ protected: GLuint* vbo; void drawGroup(int groupId); + + void crappyDraw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix, Lights::Light* dirLight, Lights* pointLights); public: glm::mat4 modelMatrix; PhongEntity(Mesh* myMesh); ~PhongEntity(); - void draw(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/phongmaterial.cpp b/phongmaterial.cpp index 7a9d7f4..7b3d8a4 100644 --- a/phongmaterial.cpp +++ b/phongmaterial.cpp @@ -1,6 +1,7 @@ #include "phongmaterial.h" #include "texture.h" #include "phongmodule.h" +#include #define TEX_ID 0 @@ -11,9 +12,9 @@ void PhongMaterial::updateShader() void PhongMaterial::bindAttributes() { - shader->bind(); - shader->bindVec3(shader->getLocation("materialAmbient"), emission); - shader->bindVec3(shader->getLocation("materialKd"), diffuse); + shader->bind(); + shader->bindVec3(shader->getLocation("materialAmbient"), emission); + shader->bindVec3(shader->getLocation("materialKd"), diffuse); shader->bindVec3(shader->getLocation("materialKs"), specular); shader->bindFloat(shader->getLocation("materialNs"), shininess); if(diffuse_texture != NULL) @@ -23,6 +24,16 @@ void PhongMaterial::bindAttributes() } } +void PhongMaterial::crappyBindAttributes() +{ + glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, glm::value_ptr(glm::vec4(emission, 1))); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glm::value_ptr(glm::vec4(diffuse, 1))); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, glm::value_ptr(glm::vec4(specular, 1))); + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess); + if(diffuse_texture != NULL) + diffuse_texture->bind(TEX_ID); +} + void PhongMaterial::setTexture(Texture* myTexture) { diffuse_texture = myTexture; diff --git a/phongmaterial.h b/phongmaterial.h index 4a9e2e1..e08d99f 100644 --- a/phongmaterial.h +++ b/phongmaterial.h @@ -37,6 +37,7 @@ public: updateShader(); } virtual void bindAttributes(); + void crappyBindAttributes(); void setTexture(Texture* myTexture); diff --git a/phongmodule.h b/phongmodule.h index 4919368..21fb4f1 100644 --- a/phongmodule.h +++ b/phongmodule.h @@ -20,7 +20,8 @@ public: void addEntity(PhongEntity* myEntity); void clearEntities(); - void virtual renderGL(Camera* myCamera); + virtual void renderGL(Camera* myCamera); + virtual bool isAvailable() {return GLEW_VERSION_3_3;} static void setShader(ShaderType slot, Shader* myShader); static Shader* getShader(ShaderType slot); diff --git a/skyboxmodule.cpp b/skyboxmodule.cpp index 9407292..c89a1c7 100644 --- a/skyboxmodule.cpp +++ b/skyboxmodule.cpp @@ -9,20 +9,23 @@ SkyboxModule::SkyboxModule(Texture* myCubeMap) { - shader = new Shader(vertSource, fragSource); - cubeMap = myCubeMap; + if(isAvailable()) + { + shader = new Shader(vertSource, fragSource); + cubeMap = myCubeMap; - // set up vao - glAssert(glGenVertexArrays(1, &vao)); - glAssert(glBindVertexArray(vao)); - glAssert(glGenBuffers(1, &vbo)); - glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo)); - glAssert(glBufferData(GL_ARRAY_BUFFER, 108 * sizeof(GLfloat), skyboxVertices, GL_STATIC_DRAW)); - glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, NULL)); - glAssert(glEnableVertexAttribArray(0)); - glAssert(glBindVertexArray(0)); + // set up vao + glAssert(glGenVertexArrays(1, &vao)); + glAssert(glBindVertexArray(vao)); + glAssert(glGenBuffers(1, &vbo)); + glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo)); + glAssert(glBufferData(GL_ARRAY_BUFFER, 108 * sizeof(GLfloat), skyboxVertices, GL_STATIC_DRAW)); + glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, NULL)); + glAssert(glEnableVertexAttribArray(0)); + glAssert(glBindVertexArray(0)); - mvpLocation = shader->getLocation("MVP"); + mvpLocation = shader->getLocation("MVP"); + } } SkyboxModule::~SkyboxModule() diff --git a/skyboxmodule.h b/skyboxmodule.h index 7868434..67d119f 100644 --- a/skyboxmodule.h +++ b/skyboxmodule.h @@ -23,6 +23,7 @@ public: SkyboxModule(Texture* myCubeMap); ~SkyboxModule(); virtual void renderGL(Camera* myCamera); + virtual bool isAvailable() {return GLEW_VERSION_3_3;} }; #endif // SKYBOXMODULE_H diff --git a/sparrowrenderer.cpp b/sparrowrenderer.cpp index 4024b3b..bb0e8ad 100644 --- a/sparrowrenderer.cpp +++ b/sparrowrenderer.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include "sparrowrenderer.h" #include "glassert.h" #include "camera.h" @@ -12,16 +12,18 @@ void SparrowRenderer::initGL(int width, int height) glewExperimental = GL_TRUE; GLenum err = glewInit(); if (GLEW_OK != err) - { - std::cerr << "Warning: glewInit failed!" << std::endl; - } + fprintf(stderr, "Warning: glewInit failed!\n"); if (!GLEW_ARB_vertex_program || !GLEW_ARB_fragment_program || !GLEW_ARB_texture_float || !GLEW_ARB_draw_buffers || !GLEW_ARB_framebuffer_object) { - std::cerr << "Warning: Shaders not supported!" << std::endl; + fprintf(stderr, "Warning: Shaders not supported!\n"); + } + if(!GLEW_VERSION_3_3) + { + fprintf(stderr, "Warning: Crappy rendering mode enabled!\n"); } std::cout << "OpenGL version " << glGetString(GL_VERSION) << std::endl; @@ -59,7 +61,8 @@ void SparrowRenderer::renderGL() void SparrowRenderer::addModule(Module* myModule, std::string name) { - modules.push_back(ModuleNode(myModule, name)); + if(myModule->isAvailable()) + modules.push_back(ModuleNode(myModule, name)); } int SparrowRenderer::getNbModules()