Update glCode in 3D demos

This commit is contained in:
Maxime Gimeno 2018-07-06 12:08:22 +02:00
parent ec0f5657db
commit 4086888964
5 changed files with 149 additions and 390 deletions

View File

@ -39,12 +39,14 @@ const char vertex_source[] =
"uniform highp mat4 mvp_matrix;\n" "uniform highp mat4 mvp_matrix;\n"
"uniform highp mat4 mv_matrix; \n" "uniform highp mat4 mv_matrix; \n"
"uniform highp float point_size; \n"
"varying highp vec4 fP; \n" "varying highp vec4 fP; \n"
"varying highp vec3 fN; \n" "varying highp vec3 fN; \n"
"varying highp vec4 fColor; \n" "varying highp vec4 fColor; \n"
"void main(void)\n" "void main(void)\n"
"{\n" "{\n"
" gl_PointSize = point_size; \n"
" fP = mv_matrix * vertex; \n" " fP = mv_matrix * vertex; \n"
" fN = mat3(mv_matrix)* normal; \n" " fN = mat3(mv_matrix)* normal; \n"
" fColor = vec4(color, 1.0); \n" " fColor = vec4(color, 1.0); \n"
@ -89,8 +91,10 @@ const char vertex_source_p_l[] =
"#version 120 \n" "#version 120 \n"
"attribute highp vec4 vertex;\n" "attribute highp vec4 vertex;\n"
"uniform highp mat4 mvp_matrix;\n" "uniform highp mat4 mvp_matrix;\n"
"uniform highp float point_size; \n"
"void main(void)\n" "void main(void)\n"
"{\n" "{\n"
" gl_PointSize = point_size; \n"
" gl_Position = mvp_matrix * vertex;\n" " gl_Position = mvp_matrix * vertex;\n"
"}" "}"
}; };
@ -714,12 +718,12 @@ void Viewer::draw()
} }
if(vertices) if(vertices)
{ {
glPointSize(size_points);
vao[3].bind(); vao[3].bind();
attrib_buffers(this); attrib_buffers(this);
color.setRgbF(.2f,.2f,.6f); color.setRgbF(.2f,.2f,.6f);
rendering_program_p_l.bind(); rendering_program_p_l.bind();
rendering_program_p_l.setAttributeValue(colorLocation,color); rendering_program_p_l.setAttributeValue(colorLocation,color);
rendering_program_p_l.setUniformValue("point_size", GLfloat(size_points));
glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(pos_points.size()/3)); glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(pos_points.size()/3));
rendering_program_p_l.release(); rendering_program_p_l.release();
vao[3].release(); vao[3].release();
@ -749,17 +753,10 @@ void Viewer::init()
// Light default parameters // Light default parameters
glLineWidth(size_edges); glLineWidth(size_edges);
glPointSize(size_points);
glEnable(GL_POLYGON_OFFSET_FILL); glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f,1.0f); glPolygonOffset(1.0f,1.0f);
glClearColor(1.0f,1.0f,1.0f,0.0f); glClearColor(1.0f,1.0f,1.0f,0.0f);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glEnable(GL_LIGHTING);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glShadeModel(GL_FLAT);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glDisable(GL_LINE_SMOOTH); glDisable(GL_LINE_SMOOTH);
glDisable(GL_POLYGON_SMOOTH_HINT); glDisable(GL_POLYGON_SMOOTH_HINT);
@ -777,12 +774,10 @@ void Viewer::keyPressEvent(QKeyEvent *e)
wireframe = !wireframe; wireframe = !wireframe;
if (wireframe) if (wireframe)
{ {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
displayMessage("Wireframe."); displayMessage("Wireframe.");
} }
else else
{ {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
displayMessage("Filled faces."); displayMessage("Filled faces.");
} }
update(); update();

View File

@ -861,36 +861,6 @@ protected:
QColor color; QColor color;
/* if(m_draw_vertices)
{
::glPointSize(m_size_points);
vao[3].bind();
attrib_buffers(this);
color.setRgbF((double)m_vertices_mono_color.red()/(double)255,
(double)m_vertices_mono_color.green()/(double)255,
(double)m_vertices_mono_color.blue()/(double)255);
rendering_program_p_l.bind();
rendering_program_p_l.setAttributeValue(colorLocation,color);
// glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(pos_points.size()/3));
rendering_program_p_l.release();
vao[3].release();
}
if(m_draw_edges)
{
vao[2].bind();
attrib_buffers(this);
color.setRgbF((double)m_edges_mono_color.red()/(double)255,
(double)m_edges_mono_color.green()/(double)255,
(double)m_edges_mono_color.blue()/(double)255);
rendering_program_p_l.bind();
rendering_program_p_l.setAttributeValue(colorLocation,color);
::glLineWidth(m_size_edges);
// glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(pos_segments.size()/3));
rendering_program_p_l.release();
vao[2].release();
}*/
if (m_draw_faces) if (m_draw_faces)
{ {
vao[0].bind(); vao[0].bind();
@ -945,14 +915,9 @@ protected:
// Light default parameters // Light default parameters
::glLineWidth(m_size_edges); ::glLineWidth(m_size_edges);
::glPointSize(m_size_points);
::glEnable(GL_POLYGON_OFFSET_FILL); ::glEnable(GL_POLYGON_OFFSET_FILL);
::glPolygonOffset(1.f,1.f); ::glPolygonOffset(1.f,1.f);
::glClearColor(1.0f,1.0f,1.0f,0.0f); ::glClearColor(1.0f,1.0f,1.0f,0.0f);
::glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
::glEnable(GL_LIGHTING);
::glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
::glShadeModel(GL_FLAT);
::glDisable(GL_BLEND); ::glDisable(GL_BLEND);
::glEnable(GL_LINE_SMOOTH); ::glEnable(GL_LINE_SMOOTH);
::glDisable(GL_POLYGON_SMOOTH_HINT); ::glDisable(GL_POLYGON_SMOOTH_HINT);

View File

@ -51,16 +51,9 @@ void Viewer::init()
setFont(QFont("Arial Black", 16, QFont::Bold)); setFont(QFont("Arial Black", 16, QFont::Bold));
/* OpenGL inits */ /* OpenGL inits */
// Increase the material shininess, so that the difference between
// the two versions of the spiral is more visible.
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0);
GLfloat specular_color[4] = { 0.8f, 0.8f, 0.8f, 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular_color);
// Set Smooth Shading
glShadeModel(GL_SMOOTH);
// depth buffer setup // depth buffer setup
glClearDepth(1.0f); glClearDepthf(1.0f);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
@ -173,8 +166,10 @@ void Viewer::compile_shaders()
"#version 120 \n" "#version 120 \n"
"attribute highp vec4 vertex;\n" "attribute highp vec4 vertex;\n"
"uniform highp mat4 mvp_matrix;\n" "uniform highp mat4 mvp_matrix;\n"
"uniform highp float point_size; \n"
"void main(void)\n" "void main(void)\n"
"{\n" "{\n"
" gl_PointSize = point_size; \n"
" gl_Position = mvp_matrix * vertex; \n" " gl_Position = mvp_matrix * vertex; \n"
"}" "}"
}; };
@ -212,7 +207,6 @@ void Viewer::compile_shaders()
{ {
std::cerr<<"linking Program FAILED"<<std::endl; std::cerr<<"linking Program FAILED"<<std::endl;
} }
rendering_program.bind();
// sphere program // sphere program
//Vertex source code //Vertex source code
@ -290,7 +284,6 @@ void Viewer::compile_shaders()
{ {
std::cerr<<"linking Program FAILED"<<std::endl; std::cerr<<"linking Program FAILED"<<std::endl;
} }
rendering_program_spheres.bind();
// cylinder program // cylinder program
//Vertex source code //Vertex source code
@ -336,7 +329,6 @@ void Viewer::compile_shaders()
{ {
std::cerr<<"linking Program FAILED"<<std::endl; std::cerr<<"linking Program FAILED"<<std::endl;
} }
rendering_program_cylinders.bind();
} }
@ -1193,9 +1185,9 @@ void Viewer::draw()
if(m_showVertex) if(m_showVertex)
{ {
rendering_program.bind(); rendering_program.bind();
glPointSize(8.0);
vao[0].bind(); vao[0].bind();
rendering_program.setUniformValue(colorLocation[0], m_colorVertex); rendering_program.setUniformValue(colorLocation[0], m_colorVertex);
rendering_program.setUniformValue("point_size", 8.0f);
glDrawArrays(GL_POINTS, 0, pos_points->size()/3); glDrawArrays(GL_POINTS, 0, pos_points->size()/3);
vao[0].release(); vao[0].release();
rendering_program.release(); rendering_program.release();
@ -1221,7 +1213,7 @@ void Viewer::draw()
// Insert point mode // Insert point mode
if( m_curMode == INSERT_PT) { if( m_curMode == INSERT_PT) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Insert a point"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Insert a point"), fontPrompt );
drawText( width()-200, 40, tr("Hold Left: Move the point"), fontPrompt ); drawText( width()-200, 40, tr("Hold Left: Move the point"), fontPrompt );
drawText( width()-200, 60, tr("Return: Insert to DT"), fontPrompt ); drawText( width()-200, 60, tr("Return: Insert to DT"), fontPrompt );
@ -1245,7 +1237,7 @@ void Viewer::draw()
} }
else if( m_curMode == SELECT) { else if( m_curMode == SELECT) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Select"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Select"), fontPrompt );
drawText( width()-200, 40, tr("Ctrl+Left: Add selection"), drawText( width()-200, 40, tr("Ctrl+Left: Add selection"),
QFont("Arial", 14) ); QFont("Arial", 14) );
@ -1261,7 +1253,7 @@ void Viewer::draw()
} }
else if( m_curMode == MOVE ) { else if( m_curMode == MOVE ) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Left Click: Select"), fontPrompt ); drawText( width()-200, 20, tr("Left Click: Select"), fontPrompt );
if(m_isMoving) if(m_isMoving)
drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt ); drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt );
@ -1275,7 +1267,7 @@ void Viewer::draw()
} }
else if( m_curMode == FINDNB ) { else if( m_curMode == FINDNB ) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Place query point"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Place query point"), fontPrompt );
drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt ); drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt );
rendering_program.bind(); rendering_program.bind();
@ -1294,7 +1286,7 @@ void Viewer::draw()
} }
else if(m_curMode == EMPTYSPH){ else if(m_curMode == EMPTYSPH){
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Place query point"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Place query point"), fontPrompt );
drawText( width()-200, 40, tr("Press S: Show/Hide trackball"), fontPrompt ); drawText( width()-200, 40, tr("Press S: Show/Hide trackball"), fontPrompt );
drawText( width()-200, 60, tr("Shift+Wheel: Resize trackball"), fontPrompt ); drawText( width()-200, 60, tr("Shift+Wheel: Resize trackball"), fontPrompt );
@ -1314,39 +1306,39 @@ void Viewer::draw()
if( !m_incrementalPts.isEmpty() ) { if( !m_incrementalPts.isEmpty() ) {
// draw the rest to-be-inserted vertices // draw the rest to-be-inserted vertices
rendering_program.bind(); rendering_program.bind();
glPointSize(8.0);
vao[24].bind(); vao[24].bind();
color.setRgbF(0.7,0.7,0.7); color.setRgbF(0.7,0.7,0.7);
rendering_program.setUniformValue(colorLocation[0],color); rendering_program.setUniformValue(colorLocation[0],color);
rendering_program.setUniformValue("point_size", 8.0f);
glDrawArrays(GL_POINTS, 0, incremental_points->size()/3); glDrawArrays(GL_POINTS, 0, incremental_points->size()/3);
vao[24].release(); vao[24].release();
rendering_program.release(); rendering_program.release();
switch( m_curStep ) { switch( m_curStep ) {
case NEWPT: case NEWPT:
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( 10, 20, tr("Highlight the next-to-insert point"), fontPrompt ); drawText( 10, 20, tr("Highlight the next-to-insert point"), fontPrompt );
// Highlight the next-to-insert point // Highlight the next-to-insert point
rendering_program.bind(); rendering_program.bind();
glPointSize(8.0);
vao[21].bind(); vao[21].bind();
color.setRgbF(1.0,0.0,0.0); color.setRgbF(1.0,0.0,0.0);
rendering_program.setUniformValue(colorLocation[0], color); rendering_program.setUniformValue(colorLocation[0], color);
rendering_program.setUniformValue("point_size", 8.0f);
glDrawArrays(GL_POINTS, 0, incremental_next_point->size()/3); glDrawArrays(GL_POINTS, 0, incremental_next_point->size()/3);
vao[21].release(); vao[21].release();
rendering_program.release(); rendering_program.release();
break; break;
case CELL: // show the tetrahedron that contains the point case CELL: // show the tetrahedron that contains the point
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( 10, 20, tr("Show the tetrahedron containing the point"), fontPrompt ); drawText( 10, 20, tr("Show the tetrahedron containing the point"), fontPrompt );
drawText( 10, 40, tr("(Only finite facets are drawn)"), fontPrompt ); drawText( 10, 40, tr("(Only finite facets are drawn)"), fontPrompt );
// Highlight the next-to-insert vertex // Highlight the next-to-insert vertex
rendering_program.bind(); rendering_program.bind();
glPointSize(8.0);
vao[21].bind(); vao[21].bind();
color.setRgbF(1.0,0.0,0.0); color.setRgbF(1.0,0.0,0.0);
rendering_program.setUniformValue(colorLocation[0], color); rendering_program.setUniformValue(colorLocation[0], color);
rendering_program.setUniformValue("point_size", 8.0f);
glDrawArrays(GL_POINTS, 0, incremental_next_point->size()/3); glDrawArrays(GL_POINTS, 0, incremental_next_point->size()/3);
vao[21].release(); vao[21].release();
rendering_program.release(); rendering_program.release();
@ -1360,14 +1352,14 @@ void Viewer::draw()
break; break;
case CONFLICT: // show the conflict region case CONFLICT: // show the conflict region
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( 10, 20, tr("Show the conflict region"), fontPrompt ); drawText( 10, 20, tr("Show the conflict region"), fontPrompt );
// Highlight the next-to-insert vertex // Highlight the next-to-insert vertex
rendering_program.bind(); rendering_program.bind();
glPointSize(8.0);
vao[21].bind(); vao[21].bind();
color.setRgbF(1.0,0.0,0.0); color.setRgbF(1.0,0.0,0.0);
rendering_program.setUniformValue(colorLocation[0], color); rendering_program.setUniformValue(colorLocation[0], color);
rendering_program.setUniformValue("point_size", 8.0f);
glDrawArrays(GL_POINTS, 0, incremental_next_point->size()/3); glDrawArrays(GL_POINTS, 0, incremental_next_point->size()/3);
vao[21].release(); vao[21].release();
rendering_program.release(); rendering_program.release();
@ -1419,7 +1411,7 @@ void Viewer::draw()
if( m_curMode == INSERT_PT) { if( m_curMode == INSERT_PT) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Insert a point"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Insert a point"), fontPrompt );
drawText( width()-200, 40, tr("Hold Left: Move the point"), fontPrompt ); drawText( width()-200, 40, tr("Hold Left: Move the point"), fontPrompt );
drawText( width()-200, 60, tr("Return: Insert to DT"), fontPrompt ); drawText( width()-200, 60, tr("Return: Insert to DT"), fontPrompt );
@ -1444,7 +1436,7 @@ void Viewer::draw()
} }
else if( m_curMode == SELECT) { else if( m_curMode == SELECT) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Select"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Select"), fontPrompt );
drawText( width()-200, 40, tr("Ctrl+Left: Add selection"), drawText( width()-200, 40, tr("Ctrl+Left: Add selection"),
QFont("Arial", 14) ); QFont("Arial", 14) );
@ -1460,7 +1452,7 @@ void Viewer::draw()
} }
else if( m_curMode == MOVE ) { else if( m_curMode == MOVE ) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Left Click: Select"), fontPrompt ); drawText( width()-200, 20, tr("Left Click: Select"), fontPrompt );
if(m_isMoving) if(m_isMoving)
drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt ); drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt );
@ -1474,7 +1466,7 @@ void Viewer::draw()
} }
else if( m_curMode == FINDNB ) { else if( m_curMode == FINDNB ) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Place query point"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Place query point"), fontPrompt );
drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt ); drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt );
rendering_program_spheres.bind(); rendering_program_spheres.bind();
@ -1494,7 +1486,7 @@ void Viewer::draw()
} }
else if(m_curMode == EMPTYSPH){ else if(m_curMode == EMPTYSPH){
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Place query point"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Place query point"), fontPrompt );
drawText( width()-200, 40, tr("Press S: Show/Hide trackball"), fontPrompt ); drawText( width()-200, 40, tr("Press S: Show/Hide trackball"), fontPrompt );
drawText( width()-200, 60, tr("Shift+Wheel: Resize trackball"), fontPrompt ); drawText( width()-200, 60, tr("Shift+Wheel: Resize trackball"), fontPrompt );
@ -1526,7 +1518,7 @@ void Viewer::draw()
switch( m_curStep ) { switch( m_curStep ) {
case NEWPT: case NEWPT:
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( 10, 20, tr("Highlight the next-to-insert point"), fontPrompt ); drawText( 10, 20, tr("Highlight the next-to-insert point"), fontPrompt );
// Highlight the next-to-insert point // Highlight the next-to-insert point
rendering_program_spheres.bind(); rendering_program_spheres.bind();
@ -1539,7 +1531,7 @@ void Viewer::draw()
break; break;
case CELL: // show the tetrahedron that contains the point case CELL: // show the tetrahedron that contains the point
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( 10, 20, tr("Show the tetrahedron containing the point"), fontPrompt ); drawText( 10, 20, tr("Show the tetrahedron containing the point"), fontPrompt );
drawText( 10, 40, tr("(Only finite facets are drawn)"), fontPrompt ); drawText( 10, 40, tr("(Only finite facets are drawn)"), fontPrompt );
// Highlight the next-to-insert vertex // Highlight the next-to-insert vertex
@ -1560,7 +1552,7 @@ void Viewer::draw()
break; break;
case CONFLICT: // show the conflict region case CONFLICT: // show the conflict region
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( 10, 20, tr("Show the conflict region"), fontPrompt ); drawText( 10, 20, tr("Show the conflict region"), fontPrompt );
// Highlight the next-to-insert vertex // Highlight the next-to-insert vertex
rendering_program_spheres.bind(); rendering_program_spheres.bind();
@ -1597,7 +1589,7 @@ void Viewer::draw()
} }
if( m_curMode == INSERT_V ) { if( m_curMode == INSERT_V ) {
// Show prompt messages // Show prompt messages
glColor3d(0,0,0);
drawText( width()-200, 20, tr("Shift+Left: Insert a vertex"), fontPrompt ); drawText( width()-200, 20, tr("Shift+Left: Insert a vertex"), fontPrompt );
drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt ); drawText( width()-200, 40, tr("Shift+Wheel: Resize trackball"), fontPrompt );
@ -1668,58 +1660,130 @@ void Viewer::drawFacet(const Triangle_3& t, std::vector<float> *vertices)
void Viewer::drawWithNames() void Viewer::drawWithNames()
{ {
for(int i=0; i<m_pScene->m_vhArray.size(); ++i) { for(int i=0; i<m_pScene->m_vhArray.size(); ++i) {
// push a name for each point onto the name stack //clear depth
// note: it can NOT be used between glBegin and glEnd glClearDepthf(1.0f);
glPushName( i ); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//draw point
// draw the point
glBegin(GL_POINTS);
Point_3& p = m_pScene->m_vhArray.at(i)->point(); Point_3& p = m_pScene->m_vhArray.at(i)->point();
glVertex3f(p.x(), p.y(), p.z()); GLfloat buf[3];
glEnd(); buf[0]=p.x();
buf[1]=p.y();
buf[2]=p.z();
rendering_program.bind();
vao[27].bind();
buffers[33].bind();
buffers[33].allocate(buf, 3*sizeof(GLfloat));
rendering_program.enableAttributeArray("vertex");
rendering_program.setAttributeArray("vertex",GL_FLOAT,0,3);
buffers[33].release();
vao[3].release();
QMatrix4x4 mvpMatrix;
double mat[16];
camera()->getModelViewProjectionMatrix(mat);
for(int i=0; i < 16; i++)
{
mvpMatrix.data()[i] = (float)mat[i];
}
rendering_program.bind();
vao[27].bind();
rendering_program.setUniformValue("mvp_matrix", mvpMatrix);
rendering_program.setUniformValue("point_size", 8.0f);
glDrawArrays(GL_POINTS,0,1);
vao[27].release();
rendering_program.release();
//read depth and store in map
GLfloat depth = 1.0f;
glReadPixels(picking_pos.x(),camera()->screenHeight()-1-picking_pos.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
if (depth != 1.0)
{
picked_IDs[depth] = i;
}
// pop one name off the top of the name stack
glPopName();
}//end-for-points }//end-for-points
// push a name for the newly inserted point // push a name for the newly inserted point
if( m_curMode == INSERT_PT && m_hasNewPt ) { if( m_curMode == INSERT_PT && m_hasNewPt ) {
glPushName( GLuint(-1) ); //clear depth
glBegin(GL_POINTS); glClearDepthf(1.0f);
glVertex3f(m_newPt.x(), m_newPt.y(), m_newPt.z()); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnd(); //draw point
glPopName(); GLfloat buf[3];
}//end-if-newPt buf[0]=m_newPt.x();
} buf[1]=m_newPt.y();
buf[2]=m_newPt.z();
rendering_program.bind();
vao[27].bind();
buffers[33].bind();
buffers[33].allocate(buf, 3*sizeof(GLfloat));
rendering_program.enableAttributeArray("vertex");
rendering_program.setAttributeArray("vertex",GL_FLOAT,0,3);
buffers[33].release();
void Viewer::endSelection(const QPoint& /*point*/) QMatrix4x4 mvpMatrix;
double mat[16];
camera()->getModelViewProjectionMatrix(mat);
for(int i=0; i < 16; i++)
{
mvpMatrix.data()[i] = (float)mat[i];
}
rendering_program.bind();
rendering_program.setUniformValue("mvp_matrix", mvpMatrix);
rendering_program.setUniformValue("point_size", 8.0f);
glDrawArrays(GL_POINTS,0,1);
vao[3].release();
rendering_program.release();
//read depth and store in map
GLfloat depth = 1.0f;
glReadPixels(picking_pos.x(),camera()->screenHeight()-1-picking_pos.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
if (depth != 1.0)
{
picked_IDs[depth] = -1;
}
}//end-if-newPt
}
void Viewer::beginSelection(const QPoint &point)
{
picking_pos = point;
CGAL::QGLViewer::beginSelection(point);
};
void Viewer::endSelection(const QPoint&)
{ {
// flush GL buffers // flush GL buffers
glFlush(); glFlush();
// reset GL_RENDER mode (was GL_SELECT) and get the number of selected points bool picked = false;
size_t nSel = glRenderMode(GL_RENDER); int id = 0;
QList<float> depths = picked_IDs.keys();
if(!depths.isEmpty())
{
qSort(depths);
id = picked_IDs[depths.first()];
picked = true;
}
/* No selection */ /* No selection */
if( nSel <= 0 ) { if( !picked) {
if( m_curMode == SELECT ) if( m_curMode == SELECT )
m_isPress = false; m_isPress = false;
}//end-if-notselected }//end-if-notselected
// each hit record has 4 data: # of names in name stack, min and max depth of old hits,
// name stack contents [see glSelectBuffer man page for more details]
// i.e. (selectBuffer())[4*i+3] is the id pushed on the stack
/* Check whether the new point is clicked on */ /* Check whether the new point is clicked on */
else if( m_curMode == INSERT_PT ) { else if( m_curMode == INSERT_PT ) {
if( m_hasNewPt && (selectBuffer())[3] == GLuint(-1) ) if( m_hasNewPt && (id == -1 ))
m_isMoving = true; m_isMoving = true;
}//end-if-inspt }//end-if-inspt
/* Check whether vertex is clicked on */ /* Check whether vertex is clicked on */
else if( m_curMode == MOVE ) { else if( m_curMode == MOVE ) {
m_isMoving = true; m_isMoving = true;
m_vidMoving = (selectBuffer())[3]; m_vidMoving = id;
// compute the corresponding size of trackball, i.e. selectedV is on the ball // compute the corresponding size of trackball, i.e. selectedV is on the ball
Point_3 p = m_pScene->m_vhArray.at( m_vidMoving )->point(); Point_3 p = m_pScene->m_vhArray.at( m_vidMoving )->point();
m_fRadius = sqrt( p.x()*p.x() + p.y()*p.y() + p.z()*p.z() ); m_fRadius = sqrt( p.x()*p.x() + p.y()*p.y() + p.z()*p.z() );
@ -1736,19 +1800,17 @@ void Viewer::endSelection(const QPoint& /*point*/)
m_vidSeled.clear(); m_vidSeled.clear();
// record the new selections // record the new selections
for(std::size_t i=0; i<nSel; ++i) {
m_vidSeled.push_back( (selectBuffer())[4*i+3] ); m_vidSeled.push_back(id);
m_pScene->m_vhArray.at( m_vidSeled.back() )->setSeled(); m_pScene->m_vhArray.at( m_vidSeled.back() )->setSeled();
}
} else { } else {
for(std::size_t i=0; i<nSel; ++i) { if( !m_vidSeled.contains(id)) {
if( !m_vidSeled.contains( (selectBuffer())[4*i+3] ) ) { m_vidSeled.push_back( id );
m_vidSeled.push_back( (selectBuffer())[4*i+3] ); m_pScene->m_vhArray.at( id )->setSeled();
m_pScene->m_vhArray.at( (selectBuffer())[4*i+3] )->setSeled();
}//end-if-contain }//end-if-contain
}//end-for
}//end-if-add }//end-if-add
}//end-if-sel }//end-if-sel
changed();
} }
/*************************************************************/ /*************************************************************/
@ -2420,142 +2482,6 @@ void Viewer::draw_cylinder(float R, int prec, std::vector<float> *vertices, std:
{ {
vertices->resize(0); vertices->resize(0);
normals->resize(0); normals->resize(0);
// int rings=360/prec, sectors=360/prec;
// float T, P;
// float x[4],y[4],z[4];
//Closing nicely the tubes will cause z-fighting and the spherical parts will get all messy
/*
//top of the cylinder
for(int t=0; t<360; t+=sectors)
{
vertices->push_back(0);
vertices->push_back(R+1);
vertices->push_back(0);
normals->push_back(0);
normals->push_back(1);
normals->push_back(0);
P = rings*CGAL_PI/180.0;
T = t*CGAL_PI/180.0;
x[1] = sin(P) * cos(T) ;
z[1] = sin(P) * sin(T) ;
y[1] = cos(P);
vertices->push_back(R * x[1]);
vertices->push_back(R * y[1]+1);
vertices->push_back(R * z[1]);
normals->push_back(x[1]);
normals->push_back(y[1]);
normals->push_back(z[1]);
//
P = rings*CGAL_PI/180.0;
T = (t+sectors)*CGAL_PI/180.0;
x[2] = sin(P) * cos(T) ;
z[2] = sin(P) * sin(T) ;
y[2] = cos(P);
vertices->push_back(R * x[2]);
vertices->push_back(R * y[2]+1);
vertices->push_back(R * z[2]);
normals->push_back(x[2]);
normals->push_back(y[2]);
normals->push_back(z[2]);
}
//Body of the sphere
for (int p=rings; p<90; p+=rings)
for(int t=0; t<360; t+=sectors)
{
//A
P = p*CGAL_PI/180.0;
T = t*CGAL_PI/180.0;
x[0] = sin(P) * cos(T) ;
z[0] = sin(P) * sin(T) ;
y[0] = cos(P);
vertices->push_back(R * x[0]);
vertices->push_back(R * y[0]+1);
vertices->push_back(R * z[0]);
normals->push_back(x[0]);
normals->push_back(y[0]);
normals->push_back(z[0]);
//B
P = (p+rings)*CGAL_PI/180.0;
T = t*CGAL_PI/180.0;
x[1] = sin(P) * cos(T) ;
z[1] = sin(P) * sin(T) ;
y[1] = cos(P);
vertices->push_back(R * x[1]);
vertices->push_back(R * y[1]+1);
vertices->push_back(R * z[1]);
normals->push_back(x[1]);
normals->push_back(y[1]);
normals->push_back(z[1]);
//C
P = p*CGAL_PI/180.0;
T = (t+sectors)*CGAL_PI/180.0;
x[2] = sin(P) * cos(T) ;
z[2] = sin(P) * sin(T) ;
y[2] = cos(P);
vertices->push_back(R * x[2]);
vertices->push_back(R * y[2]+1);
vertices->push_back(R * z[2]);
normals->push_back(x[2]);
normals->push_back(y[2]);
normals->push_back(z[2]);
//D
P = (p+rings)*CGAL_PI/180.0;
T = (t+sectors)*CGAL_PI/180.0;
x[3] = sin(P) * cos(T) ;
z[3] = sin(P) * sin(T) ;
y[3] = cos(P);
vertices->push_back(R * x[3]);
vertices->push_back(R * y[3]+1);
vertices->push_back(R * z[3]);
normals->push_back(x[3]);
normals->push_back(y[3]);
normals->push_back(z[3]);
vertices->push_back(R * x[1]);
vertices->push_back(R * y[1]+1);
vertices->push_back(R * z[1]);
normals->push_back(x[1]);
normals->push_back(y[1]);
normals->push_back(z[1]);
vertices->push_back(R * x[2]);
vertices->push_back(R * y[2]+1);
vertices->push_back(R * z[2]);
normals->push_back(x[2]);
normals->push_back(y[2]);
normals->push_back(z[2]);
}
*/
//body of the cylinder //body of the cylinder
for(int d = 0; d<360; d+= 360/prec) for(int d = 0; d<360; d+= 360/prec)
{ {
@ -2619,138 +2545,6 @@ void Viewer::draw_cylinder(float R, int prec, std::vector<float> *vertices, std:
normals->push_back(cos(D)); normals->push_back(cos(D));
} }
/*
//bottom of the cylinder
for(int t=0; t<360; t+=sectors)
{
vertices->push_back(0);
vertices->push_back(-R);
vertices->push_back(0);
normals->push_back(0);
normals->push_back(-1);
normals->push_back(0);
P = rings*CGAL_PI/180.0;
T = t*CGAL_PI/180.0;
x[1] = sin(P) * cos(T) ;
z[1] = sin(P) * sin(T) ;
y[1] = cos(P);
vertices->push_back(R * x[1]);
vertices->push_back(R * y[1]);
vertices->push_back(R * z[1]);
normals->push_back(x[1]);
normals->push_back(y[1]);
normals->push_back(z[1]);
//
P = rings*CGAL_PI/180.0;
T = (t+sectors)*CGAL_PI/180.0;
x[2] = sin(P) * cos(T) ;
z[2] = sin(P) * sin(T) ;
y[2] = cos(P);
vertices->push_back(R * x[2]);
vertices->push_back(R * y[2]);
vertices->push_back(R * z[2]);
normals->push_back(x[2]);
normals->push_back(y[2]);
normals->push_back(z[2]);
}
//Body of the sphere
for (int p=90; p<180; p+=rings)
for(int t=0; t<360; t+=sectors)
{
//A
P = p*CGAL_PI/180.0;
T = t*CGAL_PI/180.0;
x[0] = sin(P) * cos(T) ;
z[0] = sin(P) * sin(T) ;
y[0] = cos(P);
vertices->push_back(R * x[0]);
vertices->push_back(R * y[0]);
vertices->push_back(R * z[0]);
normals->push_back(x[0]);
normals->push_back(y[0]);
normals->push_back(z[0]);
//B
P = (p+rings)*CGAL_PI/180.0;
T = t*CGAL_PI/180.0;
x[1] = sin(P) * cos(T) ;
z[1] = sin(P) * sin(T) ;
y[1] = cos(P);
vertices->push_back(R * x[1]);
vertices->push_back(R * y[1]);
vertices->push_back(R * z[1]);
normals->push_back(x[1]);
normals->push_back(y[1]);
normals->push_back(z[1]);
//C
P = p*CGAL_PI/180.0;
T = (t+sectors)*CGAL_PI/180.0;
x[2] = sin(P) * cos(T) ;
z[2] = sin(P) * sin(T) ;
y[2] = cos(P);
vertices->push_back(R * x[2]);
vertices->push_back(R * y[2]);
vertices->push_back(R * z[2]);
normals->push_back(x[2]);
normals->push_back(y[2]);
normals->push_back(z[2]);
//D
P = (p+rings)*CGAL_PI/180.0;
T = (t+sectors)*CGAL_PI/180.0;
x[3] = sin(P) * cos(T) ;
z[3] = sin(P) * sin(T) ;
y[3] = cos(P);
vertices->push_back(R * x[3]);
vertices->push_back(R * y[3]);
vertices->push_back(R * z[3]);
normals->push_back(x[3]);
normals->push_back(y[3]);
normals->push_back(z[3]);
vertices->push_back(R * x[1]);
vertices->push_back(R * y[1]);
vertices->push_back(R * z[1]);
normals->push_back(x[1]);
normals->push_back(y[1]);
normals->push_back(z[1]);
vertices->push_back(R * x[2]);
vertices->push_back(R * y[2]);
vertices->push_back(R * z[2]);
normals->push_back(x[2]);
normals->push_back(y[2]);
normals->push_back(z[2]);
}*/
} }
void Viewer::draw_sphere(float R, int prec, std::vector<float> *vertices, std::vector<float> *normals) void Viewer::draw_sphere(float R, int prec, std::vector<float> *vertices, std::vector<float> *normals)

View File

@ -254,8 +254,9 @@ protected:
void draw(); void draw();
// customize selection process // customize selection process
void beginSelection(const QPoint &point);
void drawWithNames(); void drawWithNames();
void endSelection(const QPoint& point); void endSelection(const QPoint&);
// customize mouse events // customize mouse events
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event);
@ -344,7 +345,7 @@ private:
QColor color; QColor color;
static const int vaoSize = 29; static const int vaoSize = 29;
static const int vboSize = 33; static const int vboSize = 34;
// define material // define material
QVector4D ambient; QVector4D ambient;
QVector4D diffuse; QVector4D diffuse;
@ -396,6 +397,10 @@ private:
std::vector<float> *incremental_facet; std::vector<float> *incremental_facet;
std::vector<float> *incremental_conflict; std::vector<float> *incremental_conflict;
//picking
QMap<float, int> picked_IDs;
QPoint picking_pos;
QOpenGLBuffer buffers[vboSize]; QOpenGLBuffer buffers[vboSize];
QOpenGLVertexArrayObject vao[vaoSize]; QOpenGLVertexArrayObject vao[vaoSize];
QOpenGLShaderProgram rendering_program; QOpenGLShaderProgram rendering_program;