From 0fbea0f85c3512cca7b9533db4b720d7c8b5ac7b Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Thu, 18 Jun 2015 14:20:33 +0200 Subject: [PATCH] cut_plugin refactored --- .../Polyhedron/Polyhedron_demo_cut_plugin.cpp | 4 +- .../demo/Polyhedron/Scene_plane_item.cpp | 221 ++++-------------- Polyhedron/demo/Polyhedron/Scene_plane_item.h | 45 +--- 3 files changed, 52 insertions(+), 218 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/Polyhedron_demo_cut_plugin.cpp b/Polyhedron/demo/Polyhedron/Polyhedron_demo_cut_plugin.cpp index 73a507a2ac0..02d52bdfa8e 100644 --- a/Polyhedron/demo/Polyhedron/Polyhedron_demo_cut_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Polyhedron_demo_cut_plugin.cpp @@ -105,7 +105,6 @@ private: program->enableAttributeArray("vertex"); program->setAttributeBuffer("vertex",GL_FLOAT,0,3); buffers[0].release(); - program->setAttributeValue("colors",this->color()); program->release(); vaos[0].release(); @@ -129,6 +128,7 @@ private: program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); program->bind(); + program->setAttributeValue("colors",this->color()); qFunc.glDrawArrays(GL_LINES, 0, positions_lines.size()/3); program->release(); vaos[0].release(); @@ -221,7 +221,6 @@ private: program->enableAttributeArray("vertex"); program->setAttributeBuffer("vertex",GL_FLOAT,0,3); buffers[0].release(); - program->setAttributeValue("colors",this->color()); program->release(); vaos[0].release(); @@ -248,6 +247,7 @@ private: program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); program->bind(); + program->setAttributeValue("colors",this->color()); qFunc.glDrawArrays(GL_LINES, 0, positions_lines.size()/3); vaos[0].release(); program->release(); diff --git a/Polyhedron/demo/Polyhedron/Scene_plane_item.cpp b/Polyhedron/demo/Polyhedron/Scene_plane_item.cpp index fe08863cf16..90963b62d00 100644 --- a/Polyhedron/demo/Polyhedron/Scene_plane_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_plane_item.cpp @@ -3,169 +3,33 @@ #include "Scene_plane_item.moc" -void Scene_plane_item::initialize_buffers() +void Scene_plane_item::initialize_buffers(Viewer_interface *viewer) const { - qFunc.glBindVertexArray(vao[0]); + program = getShaderProgram(PROGRAM_WITHOUT_LIGHT, viewer); + program->bind(); + vaos[0].bind(); - qFunc.glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); - qFunc.glBufferData(GL_ARRAY_BUFFER, - (positions_quad.size())*sizeof(float), - positions_quad.data(), - GL_STATIC_DRAW); - qFunc.glVertexAttribPointer(0, //number of the buffer - 3, //number of floats to be taken - GL_FLOAT, // type of data - GL_FALSE, //not normalized - 0, //compact data (not in a struct) - NULL //no offset (seperated in several buffers) - ); - qFunc.glEnableVertexAttribArray(0); - - qFunc.glBindBuffer(GL_ARRAY_BUFFER, buffer[1]); - qFunc.glBufferData(GL_ARRAY_BUFFER, - (positions_lines.size())*sizeof(float), - positions_lines.data(), - GL_STATIC_DRAW); - qFunc.glVertexAttribPointer(1, //number of the buffer - 3, //number of floats to be taken - GL_FLOAT, // type of data - GL_FALSE, //not normalized - 0, //compact data (not in a struct) - NULL //no offset (seperated in several buffers) - ); - qFunc.glEnableVertexAttribArray(1); -} -void Scene_plane_item::compile_shaders(void) -{ - //fill the vertex shader - static const GLchar* vertex_shader_source[] = - { - "#version 300 es \n" - " \n" - "layout (location = 0) in vec3 positions; \n" - - "uniform mat4 mvp_matrix; \n" - "uniform mat4 f_matrix; \n" - "uniform vec3 color; \n" - "out highp vec3 fColors; \n" - - "vec4 positions_quad = vec4 (positions, 1.0); \n" - " \n" - "void main(void) \n" - "{ \n" - " fColors = color; \n" - " gl_Position = mvp_matrix * f_matrix * positions_quad; \n" - "} \n" - }; - //fill the fragment shader - static const GLchar* fragment_shader_source[]= - { - "#version 300 es \n" - " \n" - "in highp vec3 fColors; \n" - "out highp vec4 color; \n" - " \n" - "void main(void) \n" - "{ \n" - " color = vec4(fColors, 1.0); \n" - "} \n" - }; - - GLuint vertex_shader = qFunc.glCreateShader(GL_VERTEX_SHADER); - qFunc.glShaderSource(vertex_shader, 1, vertex_shader_source, NULL); - qFunc.glCompileShader(vertex_shader); + buffers[0].bind(); + buffers[0].allocate(positions_quad.data(), positions_quad.size()*sizeof(float)); + program->enableAttributeArray("vertex"); + program->setAttributeBuffer("vertex",GL_FLOAT,0,3); + buffers[0].release(); + vaos[0].release(); - GLuint fragment_shader = qFunc.glCreateShader(GL_FRAGMENT_SHADER); - qFunc.glShaderSource(fragment_shader, 1, fragment_shader_source, NULL); - qFunc.glCompileShader(fragment_shader); + vaos[1].bind(); + buffers[1].bind(); + buffers[1].allocate(positions_lines.data(), positions_lines.size()*sizeof(float)); + program->enableAttributeArray("vertex"); + program->setAttributeBuffer("vertex",GL_FLOAT,0,3); + buffers[1].release(); + vaos[1].release(); - - //creates the program, attaches and links the shaders - GLuint program= qFunc.glCreateProgram(); - qFunc.glAttachShader(program, vertex_shader); - qFunc.glAttachShader(program, fragment_shader); - qFunc.glLinkProgram(program); - - //Clean-up - qFunc.glDeleteShader(vertex_shader); - - rendering_program_quad = program; - - - //For the edges - static const GLchar* vertex_shader_source_lines[] = - { - "#version 300 es \n" - " \n" - "layout (location = 1) in vec3 positions; \n" - "uniform mat4 mvp_matrix; \n" - "uniform mat4 f_matrix; \n" - "vec4 positions_lines = vec4(positions,1.0); \n" - "out highp vec3 fColors; \n" - " \n" - "void main(void) \n" - "{ \n" - " fColors = vec3(0.0,0.0,0.0); \n" - " gl_Position = mvp_matrix * f_matrix * positions_lines; \n" - "} \n" - }; - - vertex_shader = qFunc.glCreateShader(GL_VERTEX_SHADER); - qFunc.glShaderSource(vertex_shader, 1, vertex_shader_source_lines, NULL); - qFunc.glCompileShader(vertex_shader); - - - qFunc.glShaderSource(fragment_shader, 1, fragment_shader_source, NULL); - qFunc.glCompileShader(fragment_shader); - - - program = qFunc.glCreateProgram(); - qFunc.glAttachShader(program, vertex_shader); - qFunc.glAttachShader(program, fragment_shader); - qFunc.glLinkProgram(program); - - - //Clean-up - qFunc.glDeleteShader(vertex_shader); - qFunc.glDeleteShader(fragment_shader); - rendering_program_lines = program; + program->release(); + are_buffers_filled = true; } -void Scene_plane_item::uniform_attrib(Viewer_interface* viewer, int mode) const -{ - GLfloat mvp_mat[16]; - GLdouble d_mat[16]; - GLfloat f_mat[16]; - viewer->camera()->getModelViewProjectionMatrix(d_mat); - //Convert the GLdoubles matrices in GLfloats - for (int i=0; i<16; ++i){ - mvp_mat[i] = GLfloat(d_mat[i]); - f_mat[i] = GLfloat(frame->matrix()[i]); - } - GLfloat colors[3]; - - colors[0] =this->color().redF(); - colors[1] =this->color().greenF(); - colors[2] =this->color().blueF(); - - if(mode ==0) - { - qFunc.glUseProgram(rendering_program_quad); - qFunc.glUniformMatrix4fv(location[0], 1, GL_FALSE, mvp_mat); - qFunc.glUniform3fv(location[1], 1, colors); - qFunc.glUniformMatrix4fv(location[2], 1, GL_FALSE, f_mat); - - } - else if(mode ==1) - { - qFunc.glUseProgram(rendering_program_lines); - qFunc.glUniformMatrix4fv(location[3], 1, GL_FALSE, mvp_mat); - qFunc.glUniformMatrix4fv(location[4], 1, GL_FALSE, f_mat); - } - -} void Scene_plane_item::compute_normals_and_vertices(void) { positions_quad.clear(); @@ -224,32 +88,41 @@ void Scene_plane_item::compute_normals_and_vertices(void) } } - - - location[0] = qFunc.glGetUniformLocation(rendering_program_quad, "mvp_matrix"); - location[1] = qFunc.glGetUniformLocation(rendering_program_quad, "color"); - location[2] = qFunc.glGetUniformLocation(rendering_program_quad, "f_matrix"); - location[3] = qFunc.glGetUniformLocation(rendering_program_lines, "mvp_matrix"); - location[4] = qFunc.glGetUniformLocation(rendering_program_lines, "f_matrix"); } void Scene_plane_item::draw(Viewer_interface* viewer)const { - qFunc.glBindVertexArray(vao[0]); - qFunc.glUseProgram(rendering_program_quad); - uniform_attrib(viewer,0); - qFunc.glDrawArrays(GL_TRIANGLES, 0, positions_quad.size()/3);\ - qFunc.glUseProgram(0); - qFunc.glBindVertexArray(0); + if(!are_buffers_filled); + initialize_buffers(viewer); + vaos[0].bind(); + program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); + attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); + QMatrix4x4 f_matrix; + for(int i=0; i<16; i++) + f_matrix.data()[i] = (float)frame->matrix()[i]; + program->bind(); + program->setUniformValue("f_matrix", f_matrix); + program->setAttributeValue("colors",this->color()); + qFunc.glDrawArrays(GL_TRIANGLES, 0, positions_quad.size()/3); + program->release(); + vaos[0].release(); } void Scene_plane_item::draw_edges(Viewer_interface* viewer)const { - qFunc.glBindVertexArray(vao[0]); - qFunc.glUseProgram(rendering_program_lines); - uniform_attrib(viewer,1); - qFunc.glDrawArrays(GL_LINES, 0, positions_lines.size()/3);\ - qFunc.glUseProgram(0); - qFunc.glBindVertexArray(0); + if(!are_buffers_filled); + initialize_buffers(viewer); + vaos[1].bind(); + program = getShaderProgram(PROGRAM_WITHOUT_LIGHT); + attrib_buffers(viewer, PROGRAM_WITHOUT_LIGHT); + QMatrix4x4 f_matrix; + for(int i=0; i<16; i++) + f_matrix.data()[i] = (float)frame->matrix()[i]; + program->bind(); + program->setUniformValue("f_matrix", f_matrix); + program->setAttributeValue("colors",QVector3D(0,0,0)); + qFunc.glDrawArrays(GL_LINES, 0, positions_lines.size()/3); + program->release(); + vaos[1].release(); } diff --git a/Polyhedron/demo/Polyhedron/Scene_plane_item.h b/Polyhedron/demo/Polyhedron/Scene_plane_item.h index 02119641b12..27d033ce31b 100644 --- a/Polyhedron/demo/Polyhedron/Scene_plane_item.h +++ b/Polyhedron/demo/Polyhedron/Scene_plane_item.h @@ -34,18 +34,11 @@ public: { setNormal(0., 0., 1.); qFunc.initializeOpenGLFunctions(); - qFunc.glGenVertexArrays(1, vao); //Generates an integer which will be used as ID for each buffer - qFunc.glGenBuffers(2, buffer); - compile_shaders(); changed(); } ~Scene_plane_item() { - qFunc.glDeleteBuffers(2, buffer); - qFunc.glDeleteVertexArrays(1, vao); - qFunc.glDeleteProgram(rendering_program_lines); - qFunc.glDeleteProgram(rendering_program_quad); delete frame; } @@ -103,33 +96,7 @@ public: bool supportsRenderingMode(RenderingMode m) const { return (m == Wireframe || m == Flat); } - - // Flat OpenGL drawing - void draw() const { - const double diag = scene_diag(); - ::glPushMatrix(); - ::glMultMatrixd(frame->matrix()); - GLboolean lighting; - ::glGetBooleanv(GL_LIGHTING, &lighting); - ::glDisable(GL_LIGHTING); - ::glBegin(GL_POLYGON); - ::glVertex3d(-diag, -diag, 0.); - ::glVertex3d(-diag, diag, 0.); - ::glVertex3d( diag, diag, 0.); - ::glVertex3d( diag, -diag, 0.); - ::glEnd(); - if(lighting) - ::glEnable(GL_LIGHTING); - ::glPopMatrix(); - }; virtual void draw(Viewer_interface*) const; - // Wireframe OpenGL drawing - void draw_edges() const { - ::glPushMatrix(); - ::glMultMatrixd(frame->matrix()); - QGLViewer::drawGrid((float)scene_diag()); - ::glPopMatrix(); - } virtual void draw_edges(Viewer_interface* viewer)const; Plane_3 plane() const { const qglviewer::Vec& pos = frame->position(); @@ -154,7 +121,7 @@ public slots: virtual void changed() { compute_normals_and_vertices(); -initialize_buffers(); +are_buffers_filled = false; } void setPosition(float x, float y, float z) { @@ -188,17 +155,11 @@ private: mutable std::vector positions_lines; mutable std::vector positions_quad; - mutable GLuint rendering_program_quad; - mutable GLuint rendering_program_lines; - mutable GLint location[10]; mutable GLint sampler_location; - mutable GLuint vao[1]; - mutable GLuint buffer[2]; mutable bool smooth_shading; + mutable QOpenGLShaderProgram *program; - void initialize_buffers(); - void compile_shaders(void); - void uniform_attrib(Viewer_interface*, int) const; + void initialize_buffers(Viewer_interface*)const; void compute_normals_and_vertices(void); };