From cae8bd242c7e9f545ee1a9883e91c83c7a252a68 Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Tue, 24 Feb 2015 15:04:43 +0100 Subject: [PATCH] Just to test the magic cout is at line 326. --- Polyhedron/demo/Polyhedron/Scene.cpp | 1 + Polyhedron/demo/Polyhedron/Scene_item.cpp | 110 +++++----- Polyhedron/demo/Polyhedron/Scene_item.h | 5 +- .../Polyhedron/Scene_polygon_soup_item.cpp | 4 +- .../demo/Polyhedron/Scene_polyhedron_item.cpp | 192 ++++++++++++------ .../demo/Polyhedron/Scene_polyhedron_item.h | 13 +- 6 files changed, 205 insertions(+), 120 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/Scene.cpp b/Polyhedron/demo/Polyhedron/Scene.cpp index 1def3840171..85778f72330 100644 --- a/Polyhedron/demo/Polyhedron/Scene.cpp +++ b/Polyhedron/demo/Polyhedron/Scene.cpp @@ -297,6 +297,7 @@ Scene::draw_aux(bool with_names, Viewer_interface* viewer) else ::glShadeModel(GL_FLAT); + item.shading_mode_changed(); if(CGAL::check_gl_error(__FILE__, __LINE__)) { std::cerr << "GL error was before the drawing of the item \"" << qPrintable(item.name()) << "\"\n" diff --git a/Polyhedron/demo/Polyhedron/Scene_item.cpp b/Polyhedron/demo/Polyhedron/Scene_item.cpp index c20f3189257..0942192fb73 100644 --- a/Polyhedron/demo/Polyhedron/Scene_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_item.cpp @@ -5,97 +5,101 @@ const QColor Scene_item::defaultColor = QColor(100, 100, 255); Scene_item::~Scene_item() { - delete defaultContextMenu; + delete defaultContextMenu; } void Scene_item::itemAboutToBeDestroyed(Scene_item* item) { - if(this == item) - emit aboutToBeDestroyed(); + if(this == item) + emit aboutToBeDestroyed(); } QString modeName(RenderingMode mode) { - switch(mode) - { + switch(mode) + { case Points: - return QObject::tr("points"); + return QObject::tr("points"); case Wireframe: - return QObject::tr("wire"); + return QObject::tr("wire"); case Flat: - return QObject::tr("flat"); + return QObject::tr("flat"); case FlatPlusEdges: - return QObject::tr("flat+edges"); + return QObject::tr("flat+edges"); case Gouraud: - return QObject::tr("Gouraud"); + return QObject::tr("Gouraud"); case PointsPlusNormals: - return QObject::tr("pts+normals"); + return QObject::tr("pts+normals"); case Splatting: - return QObject::tr("splats"); + return QObject::tr("splats"); default: - Q_ASSERT(false); - return QObject::tr("unknown"); - } + Q_ASSERT(false); + return QObject::tr("unknown"); + } } const char* slotName(RenderingMode mode) { - switch(mode) - { + switch(mode) + { case Points: - return SLOT(setPointsMode()); + return SLOT(setPointsMode()); case Wireframe: - return SLOT(setWireframeMode()); + return SLOT(setWireframeMode()); case Flat: - return SLOT(setFlatMode()); + return SLOT(setFlatMode()); case FlatPlusEdges: - return SLOT(setFlatPlusEdgesMode()); + return SLOT(setFlatPlusEdgesMode()); case Gouraud: - return SLOT(setGouraudMode()); + return SLOT(setGouraudMode()); case PointsPlusNormals: - return SLOT(setPointsPlusNormalsMode()); + return SLOT(setPointsPlusNormalsMode()); case Splatting: - return SLOT(setSplattingMode()); + return SLOT(setSplattingMode()); default: - Q_ASSERT(false); - return ""; - } + Q_ASSERT(false); + return ""; + } } // Rendering mode as a human readable string QString Scene_item::renderingModeName() const { - return modeName(renderingMode()); + return modeName(renderingMode()); } QMenu* Scene_item::contextMenu() { - if(defaultContextMenu) { - defaultContextMenu->setTitle(name()); - return defaultContextMenu; - } + if(defaultContextMenu) { + defaultContextMenu->setTitle(name()); + return defaultContextMenu; + } - defaultContextMenu = new QMenu(name()); - // defaultContextMenu->addAction(name()); - // defaultContextMenu->addSeparator(); - // QMenu* modeMenu = new QMenu(QObject::tr("Rendering mode"), - // defaultContextMenu); - for(unsigned int mode = 0; mode < NumberOfRenderingMode; - ++mode) - { - if(!supportsRenderingMode(RenderingMode(mode))) continue; - QString mName = modeName(RenderingMode(mode)); - QAction* action = - defaultContextMenu->addAction(tr("Set %1 mode") - .arg(mName), - this, - slotName(RenderingMode(mode))); - QObject::connect(action, SIGNAL(triggered()), - this, SIGNAL(itemChanged())); - } - // defaultContextMenu->addAction(modeMenu->menuAction()); - return defaultContextMenu; + defaultContextMenu = new QMenu(name()); + // defaultContextMenu->addAction(name()); + // defaultContextMenu->addSeparator(); + // QMenu* modeMenu = new QMenu(QObject::tr("Rendering mode"), + // defaultContextMenu); + for(unsigned int mode = 0; mode < NumberOfRenderingMode; + ++mode) + { + if(!supportsRenderingMode(RenderingMode(mode))) continue; + QString mName = modeName(RenderingMode(mode)); + QAction* action = + defaultContextMenu->addAction(tr("Set %1 mode") + .arg(mName), + this, + slotName(RenderingMode(mode))); + QObject::connect(action, SIGNAL(triggered()), + this, SIGNAL(itemChanged())); + } + // defaultContextMenu->addAction(modeMenu->menuAction()); + return defaultContextMenu; } void Scene_item::changed() { - // emit itemChanged(); + // emit itemChanged(); +} +void Scene_item::shading_mode_changed() +{ + } void Scene_item::select(double /*orig_x*/, diff --git a/Polyhedron/demo/Polyhedron/Scene_item.h b/Polyhedron/demo/Polyhedron/Scene_item.h index 183b6d07717..3074d4662de 100644 --- a/Polyhedron/demo/Polyhedron/Scene_item.h +++ b/Polyhedron/demo/Polyhedron/Scene_item.h @@ -1,6 +1,5 @@ #ifndef SCENE_ITEM_H #define SCENE_ITEM_H - #include "Scene_item_config.h" #include "Scene_interface.h" #include @@ -85,6 +84,7 @@ public slots: // Call that once you have finished changing something in the item // (either the properties or internal data) virtual void changed(); + virtual void shading_mode_changed(); // Setters for the four basic properties virtual void setColor(QColor c) { color_ = c; } @@ -150,6 +150,9 @@ protected: RenderingMode rendering_mode; QMenu* defaultContextMenu; + int prev_shading; + int cur_shading; + }; // end class Scene_item diff --git a/Polyhedron/demo/Polyhedron/Scene_polygon_soup_item.cpp b/Polyhedron/demo/Polyhedron/Scene_polygon_soup_item.cpp index 7212a323ca7..5e407ebc6a9 100644 --- a/Polyhedron/demo/Polyhedron/Scene_polygon_soup_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_polygon_soup_item.cpp @@ -134,7 +134,6 @@ Scene_polygon_soup_item::compile_shaders(void) "uniform mat4 mv_matrix; \n" "uniform int is_two_side; \n" - "uniform vec3 vColors; \n" "uniform vec3 light_pos; \n" "uniform vec3 light_diff; \n" "uniform vec3 light_spec; \n" @@ -301,7 +300,6 @@ Scene_polygon_soup_item::uniform_attrib(Viewer_interface* viewer) const glUniform3fv(location[3], 1, light.diffuse); glUniform3fv(location[4], 1, light.specular); glUniform3fv(location[5], 1, light.ambient); - glUniform3fv(location[6], 1, colors); glUniform1i(location[7], is_both_sides); } @@ -357,7 +355,7 @@ Scene_polygon_soup_item::compute_normals_and_vertices(){ location[3] = glGetUniformLocation(rendering_program, "light_diff"); location[4] = glGetUniformLocation(rendering_program, "light_spec"); location[5] = glGetUniformLocation(rendering_program, "light_amb"); - location[6] = glGetUniformLocation(rendering_program, "vColors"); + // location[6] = glGetUniformLocation(rendering_program, "vColors"); location[7] = glGetUniformLocation(rendering_program, "is_two_side"); } diff --git a/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp b/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp index aceafb719a5..3c3bb1ece01 100644 --- a/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp @@ -119,6 +119,28 @@ Scene_polyhedron_item::initialize_buffers() 0, NULL ); + glBindBuffer(GL_ARRAY_BUFFER, buffer[3]); + glBufferData(GL_ARRAY_BUFFER, + (color_facets.size())*sizeof(color_facets.data()), + color_facets.data(), GL_STATIC_DRAW); + glVertexAttribPointer(3, + 3, + GL_FLOAT, + GL_FALSE, + 0, + NULL + ); + glBindBuffer(GL_ARRAY_BUFFER, buffer[4]); + glBufferData(GL_ARRAY_BUFFER, + (color_lines.size())*sizeof(color_lines.data()), + color_lines.data(), GL_STATIC_DRAW); + glVertexAttribPointer(4, + 3, + GL_FLOAT, + GL_FALSE, + 0, + NULL + ); // Clean-up glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -137,18 +159,19 @@ Scene_polyhedron_item::compile_shaders(void) "layout (location = 0) in vec4 positions_facets; \n" "layout (location = 1) in vec4 positions_lines; \n" "layout (location = 2) in vec3 vNormals; \n" + "layout (location = 3) in vec3 color_facets; \n" + "layout (location = 4) in vec3 color_lines; \n" "uniform mat4 mvp_matrix; \n" "uniform mat4 mv_matrix; \n" "uniform int is_two_side; \n" "uniform int is_wire; \n" - "uniform vec3 vColors; \n" "uniform vec3 light_pos; \n" "uniform vec3 light_diff; \n" "uniform vec3 light_spec; \n" "uniform vec3 light_amb; \n" - "float spec_power = 0.0; \n" + "float spec_power = 128.0; \n" "out highp vec3 fColors; \n" " \n" @@ -173,17 +196,19 @@ Scene_polyhedron_item::compile_shaders(void) " R = reflect(-L, N); \n" " vec3 diffuse; \n" " if(is_two_side == 1) \n" - " diffuse = abs(dot(N,L)) * light_diff; \n" + " diffuse = abs(dot(N,L)) * light_diff * color_facets; \n" " else \n" - " diffuse = max(dot(N,L), 0.0) * light_diff; \n" + " diffuse = max(dot(N,L), 0.0) * light_diff * color_facets; \n" " vec3 specular = pow(max(dot(R,V), 0.0), spec_power) * light_spec; \n" - " fColors = light_amb + diffuse + specular ; \n" + " fColors = light_amb*color_facets + diffuse + specular ; \n" - " gl_Position = mvp_matrix * positions_facets;} \n" + " gl_Position = mvp_matrix *positions_facets; \n" + "} \n" "else{ \n" - " fColors = vColors; \n" - " gl_Position = mvp_matrix * positions_lines;} \n" + " fColors = color_lines; \n" + " gl_Position = mvp_matrix * positions_lines; \n" + "} \n" "} \n" }; //fill the fragment shader @@ -266,7 +291,6 @@ void Scene_polyhedron_item::uniform_attrib(Viewer_interface* viewer) const { - GLfloat colors[4]; light_info light; GLint is_both_sides = 0; GLint is_wire = 0; @@ -278,9 +302,11 @@ Scene_polyhedron_item::uniform_attrib(Viewer_interface* viewer) const GLdouble d_mat[16]; viewer->camera()->getModelViewProjectionMatrix(d_mat); //Convert the GLdoubles matrix in GLfloats - for (int i=0; i<16; ++i) + for (int i=0; i<16; ++i){ mvp_mat[i] = GLfloat(d_mat[i]); + +} viewer->camera()->getModelViewMatrix(d_mat); for (int i=0; i<16; ++i) mv_mat[i] = GLfloat(d_mat[i]); @@ -289,18 +315,18 @@ Scene_polyhedron_item::uniform_attrib(Viewer_interface* viewer) const glGetIntegerv(GL_LIGHT_MODEL_TWO_SIDE, &is_both_sides); glGetIntegerv(GL_POLYGON_MODE, &is_wire); //fills the arraw of colors with the current color - glGetFloatv(GL_CURRENT_COLOR, colors); + //Gets lighting info : //position glGetLightfv(GL_LIGHT0, GL_POSITION, light.position); + //ligne ne servant a rien mais si on l'enleve plus rien ne marche... + std::cout<camera()->position().x<<" "<camera()->position().y<<" "<camera()->position().z<<" "<facets_begin(); f != poly->facets_end(); f++) { - const int this_patch_id = f->patch_id(); - if(patch_id != this_patch_id) { - CGALglcolor(colors_[this_patch_id]); - patch_id = this_patch_id; - } - //::glBegin(GL_POLYGON); + HF_circulator he = f->facet_begin(); HF_circulator end = he; CGAL_For_all(he,end) { // If Flat shading:1 normal per polygon added once per vertex - if (shading == GL_FLAT) + if (cur_shading == GL_FLAT) { Vector n = compute_facet_normal(*f); - normals.push_back(n.x());//::glNormal3d(n.x(),n.y(),n.z()); + normals.push_back(n.x()); normals.push_back(n.y()); normals.push_back(n.z()); } @@ -383,31 +398,37 @@ Scene_polyhedron_item::compute_normals_and_vertices(void) // If Gouraud shading: 1 normal per vertex - if (shading == GL_SMOOTH) + if (cur_shading == GL_SMOOTH) { Vector n = compute_vertex_normal(*he->vertex()); - //::glNormal3d(n.x(),n.y(),n.z()); normals.push_back(n.x()); normals.push_back(n.y()); normals.push_back(n.z()); } + const int this_patch_id = f->patch_id(); + //if(patch_id != this_patch_id) { + //CGALglcolor(colors_[this_patch_id]); + color_facets.push_back(colors_[this_patch_id].redF()); + color_facets.push_back(colors_[this_patch_id].greenF()); + color_facets.push_back(colors_[this_patch_id].blueF()); + // patch_id = this_patch_id; + //} + //position const Point& p = he->vertex()->point(); - //::glVertex3d(p.x(),p.y(),p.z()); positions_facets.push_back(p.x()); positions_facets.push_back(p.y()); positions_facets.push_back(p.z()); positions_facets.push_back(1.0); - // ::glEnd(); } } //Lines typedef Kernel::Point_3 Point; typedef Polyhedron::Edge_iterator Edge_iterator; - - //::glBegin(GL_LINES); + //GLfloat colors[4]; + //glGetFloatv(GL_CURRENT_COLOR, colors); Edge_iterator he; if(!show_only_feature_edges_m) { for(he = poly->edges_begin(); @@ -426,11 +447,18 @@ Scene_polyhedron_item::compute_normals_and_vertices(void) positions_lines.push_back(b.y()); positions_lines.push_back(b.z()); positions_lines.push_back(1.0); - // ::glVertex3d(a.x(),a.y(),a.z()); - // ::glVertex3d(b.x(),b.y(),b.z()); + + color_lines.push_back(1.0);//colors[0]); + color_lines.push_back(0.0);//colors[1]); + color_lines.push_back(0.0);//colors[2]); + + color_lines.push_back(1.0);//colors[0]); + color_lines.push_back(0.0);//colors[1]); + color_lines.push_back(0.0);//colors[2]); + } } - ::glColor3d(1.0, 0.0, 0.0); //<<------ passe les edges en rouge + // ::glColor3d(1.0, 0.0, 0.0); //<<------ passe les edges en rouge for(he = poly->edges_begin(); he != poly->edges_end(); he++) @@ -438,8 +466,7 @@ Scene_polyhedron_item::compute_normals_and_vertices(void) if(!he->is_feature_edge()) continue; const Point& a = he->vertex()->point(); const Point& b = he->opposite()->vertex()->point(); - // ::glVertex3d(a.x(),a.y(),a.z()); - // ::glVertex3d(b.x(),b.y(),b.z()); + positions_lines.push_back(a.x()); positions_lines.push_back(a.y()); positions_lines.push_back(a.z()); @@ -449,8 +476,15 @@ Scene_polyhedron_item::compute_normals_and_vertices(void) positions_lines.push_back(b.y()); positions_lines.push_back(b.z()); positions_lines.push_back(1.0); + + color_lines.push_back(1.0); + color_lines.push_back(0.0); + color_lines.push_back(0.0); + + color_lines.push_back(1.0); + color_lines.push_back(0.0); + color_lines.push_back(0.0); } - //::glEnd(); //Allocates a uniform location for the MVP and MV matrices location[0] = glGetUniformLocation(rendering_program, "mvp_matrix"); @@ -461,9 +495,8 @@ Scene_polyhedron_item::compute_normals_and_vertices(void) location[3] = glGetUniformLocation(rendering_program, "light_diff"); location[4] = glGetUniformLocation(rendering_program, "light_spec"); location[5] = glGetUniformLocation(rendering_program, "light_amb"); - location[6] = glGetUniformLocation(rendering_program, "vColors"); - location[7] = glGetUniformLocation(rendering_program, "is_two_side"); - location[8] = glGetUniformLocation(rendering_program, "is_wire"); + location[6] = glGetUniformLocation(rendering_program, "is_two_side"); + location[7] = glGetUniformLocation(rendering_program, "is_wire"); } @@ -478,10 +511,11 @@ Scene_polyhedron_item::Scene_polyhedron_item() erase_next_picked_facet_m(false), plugin_has_set_color_vector_m(false) { + cur_shading=GL_FLAT; //init(); glGenVertexArrays(1, &vao); //Generates an integer which will be used as ID for each buffer - glGenBuffers(3, buffer); + glGenBuffers(5, buffer); rendering_program = compile_shaders(); } @@ -497,10 +531,11 @@ Scene_polyhedron_item::Scene_polyhedron_item(Polyhedron* const p) erase_next_picked_facet_m(false), plugin_has_set_color_vector_m(false) { + cur_shading=GL_FLAT; init(); glGenVertexArrays(1, &vao); //Generates an integer which will be used as ID for each buffer - glGenBuffers(3, buffer); + glGenBuffers(5, buffer); rendering_program = compile_shaders(); } @@ -516,10 +551,11 @@ Scene_polyhedron_item::Scene_polyhedron_item(const Polyhedron& p) erase_next_picked_facet_m(false), plugin_has_set_color_vector_m(false) { + cur_shading=GL_FLAT; init(); glGenVertexArrays(1, &vao); //Generates an integer which will be used as ID for each buffer - glGenBuffers(3, buffer); + glGenBuffers(5, buffer); rendering_program = compile_shaders(); } @@ -533,7 +569,7 @@ Scene_polyhedron_item::Scene_polyhedron_item(const Polyhedron& p) Scene_polyhedron_item::~Scene_polyhedron_item() { - glDeleteBuffers(3, buffer); + glDeleteBuffers(5, buffer); glDeleteVertexArrays(1, &vao); glDeleteProgram(rendering_program); @@ -574,6 +610,8 @@ Scene_polyhedron_item::clone() const { bool Scene_polyhedron_item::load(std::istream& in) { + + in >> *poly; if ( in && !isEmpty() ) @@ -672,15 +710,20 @@ void Scene_polyhedron_item::set_erase_next_picked_facet(bool b) // Points/Wireframe/Flat/Gouraud OpenGL drawing in a display list void Scene_polyhedron_item::draw(Viewer_interface* viewer) const { - glBindVertexArray(vao); //Binds the buffer used for the flat and gouraud rendering glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); glEnableVertexAttribArray(0); + // glBindBuffer(GL_ARRAY_BUFFER, buffer[1]); + // glEnableVertexAttribArray(1); //Binds the buffer used for normals glBindBuffer(GL_ARRAY_BUFFER, buffer[2]); glEnableVertexAttribArray(2); + glBindBuffer(GL_ARRAY_BUFFER, buffer[3]); + glEnableVertexAttribArray(3); + // glBindBuffer(GL_ARRAY_BUFFER, buffer[4]); + //glEnableVertexAttribArray(4); // tells the GPU to use the program just created glUseProgram(rendering_program); @@ -695,19 +738,23 @@ void Scene_polyhedron_item::draw(Viewer_interface* viewer) const { // Clean-up glUseProgram(0); + // glDisableVertexAttribArray(4); + glDisableVertexAttribArray(3); glDisableVertexAttribArray(2); - glDisableVertexAttribArray(1); + // glDisableVertexAttribArray(1); glDisableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - //gl_render_facets(*poly,colors_); + } // Points/Wireframe/Flat/Gouraud OpenGL drawing in a display list void Scene_polyhedron_item::draw_edges(Viewer_interface* viewer) const { glBindVertexArray(vao); + //glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); + //glEnableVertexAttribArray(0); //Binds the buffer used for the wireframe rendering glBindBuffer(GL_ARRAY_BUFFER, buffer[1]); glEnableVertexAttribArray(1); @@ -715,6 +762,12 @@ void Scene_polyhedron_item::draw_edges(Viewer_interface* viewer) const { glBindBuffer(GL_ARRAY_BUFFER, buffer[2]); glEnableVertexAttribArray(2); + + //glBindBuffer(GL_ARRAY_BUFFER, buffer[3]); + // glEnableVertexAttribArray(3); + + glBindBuffer(GL_ARRAY_BUFFER, buffer[4]); + glEnableVertexAttribArray(4); // tells the GPU to use the program just created glUseProgram(rendering_program); @@ -724,10 +777,17 @@ void Scene_polyhedron_item::draw_edges(Viewer_interface* viewer) const { glDrawArrays(GL_LINES, 0, positions_lines.size()/4); // Clean-up glUseProgram(0); + glDisableVertexAttribArray(4); + // glDisableVertexAttribArray(3); glDisableVertexAttribArray(2); glDisableVertexAttribArray(1); + //glDisableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + + + + } /*void Scene_polyhedron_item::direct_draw_edges(Viewer_interface* viewer) const { @@ -763,13 +823,29 @@ void Scene_polyhedron_item:: changed() { - std::cout<<"changed"< positions_lines; std::vector positions_facets; std::vector normals; + std::vector color_lines; + std::vector color_facets; GLuint rendering_program; - GLint location[9]; + GLint location[8]; GLuint vertex_shader; GLuint fragment_shader; GLuint program; GLuint vao; - GLuint buffer[3]; - void initialize_buffers(); // a mettre a jour - GLuint compile_shaders(void); //a mettre a jour + GLuint buffer[5]; + void initialize_buffers(); + GLuint compile_shaders(void); void compute_normals_and_vertices(void); - void uniform_attrib(Viewer_interface*) const;//fini + void uniform_attrib(Viewer_interface*) const; }; // end class Scene_polyhedron_item #endif // SCENE_POLYHEDRON_ITEM_H