From 32c430ec83503f99a70efbb86be3b55b2b3931ad Mon Sep 17 00:00:00 2001 From: Sylvain Thery Date: Fri, 4 Oct 2013 13:08:54 +0200 Subject: [PATCH] minor bugs & optim --- Apps/Examples/viewerOBJ.cpp | 108 +++++++++++++++++++-------- Apps/Examples/viewerOBJ.h | 5 ++ src/Container/attributeContainer.cpp | 10 ++- src/Utils/drawer.cpp | 8 +- 4 files changed, 93 insertions(+), 38 deletions(-) diff --git a/Apps/Examples/viewerOBJ.cpp b/Apps/Examples/viewerOBJ.cpp index 12aafe6b..b65a98b0 100644 --- a/Apps/Examples/viewerOBJ.cpp +++ b/Apps/Examples/viewerOBJ.cpp @@ -27,13 +27,15 @@ #include "Algo/Modelisation/polyhedron.h" #include "Utils/vbo.h" + ObjView::ObjView(): m_obj(myMap), m_positionVBO(NULL), m_normalVBO(NULL), m_texcoordVBO(NULL), m_shader(NULL), - m_shader2(NULL) + m_shader2(NULL), + m_RenderStyle(2) {} ObjView::~ObjView() @@ -53,6 +55,27 @@ ObjView::~ObjView() delete m_texcoordVBO; } + +void ObjView::cb_keyPress(int k) +{ + switch(k) + { + case 'm': + m_RenderStyle=0; + break; + case 'c': + m_RenderStyle=1; + break; + case 't': + m_RenderStyle=2; + break; + default: + break; + } + updateGL(); +} + + void ObjView::cb_initGL() { // choose to use GL version 2 @@ -63,12 +86,6 @@ void ObjView::cb_initGL() m_texcoordVBO = new Utils::VBO; m_normalVBO = new Utils::VBO; -// m_shader = new Utils::ShaderSimpleTexture(); -// m_shader->setAttributePosition(m_positionVBO); -// m_shader->setAttributeTexCoord(m_texcoordVBO); -// m_shader->setTextureUnit(GL_TEXTURE0); -// registerShader(m_shader); - m_shader2 = new Utils::ShaderPhongTexture(); m_shader2->setAttributePosition(m_positionVBO); m_shader2->setAttributeTexCoord(m_texcoordVBO); @@ -100,30 +117,61 @@ void ObjView::cb_redraw() for (unsigned int i=0; itextureDiffuse != NULL) - { - m_shader2->setTexture(mats[i]->textureDiffuse); - m_shader2->setShininess(mats[i]->shininess); - m_shader2->setAmbient(0.8f); - m_shader2->activeTexture(); - m_shader2->enableVertexAttribs(); - glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i)); - m_shader2->disableVertexAttribs(); - } - else + switch(m_RenderStyle) { - Geom::Vec4f v; -// v[0] = mats[i]->ambiantColor[0]; v[1] = mats[i]->ambiantColor[1]; v[2] = mats[i]->ambiantColor[2]; v[3] = 0.0f; - v[0] = 0.5f; v[1] = 0.5f; v[2] = 0.5f; v[3] = 0.0f; - m_phongShader->setAmbiant(v) ; - v[0] = mats[i]->diffuseColor[0]; v[1] = mats[i]->diffuseColor[1]; v[2] = mats[i]->diffuseColor[2]; v[3] = 0.0f; - m_phongShader->setDiffuse(v) ; - v[0] = mats[i]->specularColor[0]; v[1] = mats[i]->specularColor[1]; v[2] = mats[i]->specularColor[2]; v[3] = 0.0f; - m_phongShader->setSpecular(v) ; - m_phongShader->setShininess(mats[i]->shininess) ; - m_phongShader->enableVertexAttribs(); - glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i)); - m_phongShader->disableVertexAttribs(); + case 0: // MONO + { + Geom::Vec4f v(0.2f,1.0f,0.4f,0.0f); // color here green + m_phongShader->setAmbiant(0.2f*v) ; + m_phongShader->setDiffuse(v) ; + // v[0] = 1.0f; v[1] = 1.0f; v[2] = 1.0f; v[3] = 0.0f; // use this for specular effected + m_phongShader->setSpecular(v) ; + m_phongShader->setShininess(10000.0) ; + m_phongShader->enableVertexAttribs(); + glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i)); + m_phongShader->disableVertexAttribs(); + } + break; + case 1: // COLOR + { + Geom::Vec4f v(mats[i]->diffuseColor[0],mats[i]->diffuseColor[1],mats[i]->diffuseColor[2],0.0f); + m_phongShader->setAmbiant(0.2f*v) ; + m_phongShader->setDiffuse(v) ; + // v[0] = 1.0f; v[1] = 1.0f; v[2] = 1.0f; v[3] = 0.0f; // use this for specular effected + m_phongShader->setSpecular(v) ; + m_phongShader->setShininess(10000.0) ; + m_phongShader->enableVertexAttribs(); + glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i)); + m_phongShader->disableVertexAttribs(); + + } + default: + { + if (mats[i]->textureDiffuse != NULL) + { + m_shader2->setTexture(mats[i]->textureDiffuse); + m_shader2->setShininess(mats[i]->shininess); + m_shader2->setAmbient(0.8f); + m_shader2->activeTexture(); + m_shader2->enableVertexAttribs(); + glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i)); + m_shader2->disableVertexAttribs(); + } + else + { + Geom::Vec4f v; + v[0] = 0.5f; v[1] = 0.5f; v[2] = 0.5f; v[3] = 0.0f; + m_phongShader->setAmbiant(v) ; + v[0] = mats[i]->diffuseColor[0]; v[1] = mats[i]->diffuseColor[1]; v[2] = mats[i]->diffuseColor[2]; v[3] = 0.0f; + m_phongShader->setDiffuse(v) ; + v[0] = mats[i]->specularColor[0]; v[1] = mats[i]->specularColor[1]; v[2] = mats[i]->specularColor[2]; v[3] = 0.0f; + m_phongShader->setSpecular(v) ; + m_phongShader->setShininess(mats[i]->shininess) ; + m_phongShader->enableVertexAttribs(); + glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i)); + m_phongShader->disableVertexAttribs(); + } + } } } } diff --git a/Apps/Examples/viewerOBJ.h b/Apps/Examples/viewerOBJ.h index 052d2d40..753b696a 100644 --- a/Apps/Examples/viewerOBJ.h +++ b/Apps/Examples/viewerOBJ.h @@ -77,6 +77,8 @@ public: Utils::ShaderPhongTexture* m_shader2; Utils::ShaderPhong* m_phongShader; + int m_RenderStyle; + ObjView(); ~ObjView(); @@ -88,6 +90,9 @@ public: void cb_initGL(); + void cb_keyPress(int k); + + }; #endif diff --git a/src/Container/attributeContainer.cpp b/src/Container/attributeContainer.cpp index 7f3cb66c..091634ff 100644 --- a/src/Container/attributeContainer.cpp +++ b/src/Container/attributeContainer.cpp @@ -174,11 +174,13 @@ void AttributeContainer::clear(bool removeAttrib) for (std::vector::iterator it = m_holesBlocks.begin(); it != m_holesBlocks.end(); ++it) delete (*it); - std::vector bf; - m_holesBlocks.swap(bf); + { // add bracket just for scope of temporary vectors + std::vector bf; + m_holesBlocks.swap(bf); - std::vector bwf; - m_tableBlocksWithFree.swap(bwf); + std::vector bwf; + m_tableBlocksWithFree.swap(bwf); + } // detruit les données for (std::vector::iterator it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it) diff --git a/src/Utils/drawer.cpp b/src/Utils/drawer.cpp index b1229486..e4d4b9fd 100644 --- a/src/Utils/drawer.cpp +++ b/src/Utils/drawer.cpp @@ -185,7 +185,7 @@ void Drawer::callList(float opacity) { if (pp->mode == GL_POINTS) glPointSize(pp->width); - if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP)) + if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP)) glLineWidth(pp->width); glDrawArrays(pp->mode, pp->begin, pp->nb); } @@ -206,7 +206,7 @@ void Drawer::callSubList(int index, float opacity) if (pp->mode == GL_POINTS) glPointSize(pp->width); - if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP)) + if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP)) glLineWidth(pp->width); glDrawArrays(pp->mode, pp->begin, pp->nb); @@ -226,7 +226,7 @@ void Drawer::callSubLists(int first, int nb, float opacity) if (pp->mode == GL_POINTS) glPointSize(pp->width); - if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP)) + if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP)) glLineWidth(pp->width); glDrawArrays(pp->mode, pp->begin, pp->nb); } @@ -247,7 +247,7 @@ void Drawer::callSubLists(std::vector indices, float opacity) if (pp->mode == GL_POINTS) glPointSize(pp->width); - if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP)) + if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP)) glLineWidth(pp->width); glDrawArrays(pp->mode, pp->begin, pp->nb); } -- GitLab