00001
00002
00003 #include <MC_opengl_drawer.hpp>
00004
00005 #include <MC_string_helper.hpp>
00006 #include <MC_mesh_index_vector.hpp>
00007 #include <MC_mesh_index_vector_draw.hpp>
00008 #include <MC_mesh_fast_draw.hpp>
00009 #include <MC_polygon.hpp>
00010 #include <MC_v3d.hpp>
00011 #include <MC_segment.hpp>
00012 #include <MC_polygon.hpp>
00013
00014
00015
00016
00017 namespace mesh_conv
00018 {
00019 MC_opengl_drawer::MC_opengl_drawer(){}
00020
00021 void MC_opengl_drawer::draw_per_polygon_color(const MC_mesh_index_vector& mesh,const MC_v3d_vector& color_per_polygon)
00022 {
00023 if(color_per_polygon.size()!=mesh.polygon_number())
00024 {std::cout<<"Error in MC_opengl_drawer::draw_per_polygon_color(), size of color="<<color_per_polygon.size()<<" while polygon number="<<mesh.polygon_number()<<std::endl;exit(-1);}
00025
00026
00027 glEnable(GL_BLEND);
00028 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00029
00030 int N_polygon=mesh.polygon_number();
00031 for(int k_polygon=0;k_polygon<N_polygon;k_polygon++)
00032 {
00033 int poly_size=mesh.polygon_size(k_polygon);
00034 MC_polygon p=mesh.get_polygon(k_polygon);
00035
00036
00037
00038 MC_v3d n=p.normal();
00039 glNormal3d(n[0],n[1],n[2]);
00040
00041 const MC_v3d& col=color_per_polygon[k_polygon];
00042 glColor3dv(col.pointer());
00043
00044 glBegin(GL_POLYGON);
00045 for(int k_vertex=0;k_vertex<poly_size;k_vertex++)
00046 {
00047 const MC_v3d& x=p[k_vertex];
00048 glVertex3d(x[0],x[1],x[2]);
00049 }
00050 glEnd();
00051 }
00052
00053
00054 glDisable(GL_BLEND);
00055 }
00056 const MC_mesh_index_vector& MC_opengl_drawer::draw(const MC_mesh_index_vector& mesh)
00057 {
00058
00059
00060 glEnable(GL_BLEND);
00061 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00062
00063 int N_polygon=mesh.polygon_number();
00064 for(int k_polygon=0;k_polygon<N_polygon;k_polygon++)
00065 {
00066 int poly_size=mesh.polygon_size(k_polygon);
00067 MC_polygon p=mesh.get_polygon(k_polygon);
00068
00069
00070
00071 MC_v3d n=p.normal();
00072 glNormal3d(n[0],n[1],n[2]);
00073
00074 glBegin(GL_POLYGON);
00075 for(int k_vertex=0;k_vertex<poly_size;k_vertex++)
00076 {
00077 MC_v3d x=p[k_vertex];
00078 glVertex3d(x[0],x[1],x[2]);
00079 }
00080 glEnd();
00081 }
00082
00083
00084 glDisable(GL_BLEND);
00085
00086 return mesh;
00087 }
00088
00089
00090 const MC_mesh_fast_draw& MC_opengl_drawer::draw(const MC_mesh_fast_draw& mesh)
00091 {
00092 const MC_double_vector& vertex=mesh.point_set();
00093 const MC_double_vector& color=mesh.color();
00094 const MC_double_vector& texture=mesh.texture();
00095 const MC_double_vector& normal=mesh.normal();
00096 const std::map <int,MC_int_vector>& connectivity=mesh.connectivity();
00097
00098 const double* p_vertex=vertex.pointer();
00099 const double* p_color=color.pointer();
00100 const double* p_texture=texture.pointer();
00101 const double* p_normal=normal.pointer();
00102
00103 glEnable(GL_LIGHTING);
00104 glEnable(GL_BLEND);
00105 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00106 glEnable(GL_COLOR_MATERIAL);
00107
00108
00109
00110 bool is_color=(color.size()==vertex.size());
00111 bool is_texture=(texture.size()!=0);
00112
00113
00114 std::map <int,MC_int_vector>:: const_iterator it;
00115 for(it=connectivity.begin();it!=connectivity.end();++it)
00116 {
00117 int current_size=it->first;
00118 const MC_int_vector& current_connectivity=it->second;
00119 const int* p_connectivity=current_connectivity.pointer();
00120
00121 if(current_size==3 || current_size==4)
00122 {
00123 glEnableClientState(GL_VERTEX_ARRAY);
00124 glEnableClientState(GL_NORMAL_ARRAY);
00125 if(is_color)
00126 glEnableClientState(GL_COLOR_ARRAY);
00127 if(is_texture)
00128 {
00129
00130 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00131 }
00132
00133 glVertexPointer(3,GL_DOUBLE,0,p_vertex);
00134 glNormalPointer(GL_DOUBLE,0,p_normal);
00135 if(is_color)
00136 glColorPointer(3,GL_DOUBLE,0,p_color);
00137 if(is_texture)
00138 glTexCoordPointer(2,GL_DOUBLE,0,p_texture);
00139
00140
00141
00142 if(current_size==3)
00143 glDrawElements(GL_TRIANGLES,current_connectivity.size(),GL_UNSIGNED_INT,p_connectivity);
00144 else
00145 glDrawElements(GL_QUADS,current_connectivity.size(),GL_UNSIGNED_INT,p_connectivity);
00146
00147 glDisableClientState(GL_VERTEX_ARRAY);
00148 glDisableClientState(GL_NORMAL_ARRAY);
00149 glDisableClientState(GL_COLOR_ARRAY);
00150 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00151 }
00152 else
00153 {
00154 for(int k_polygon=0;k_polygon<current_connectivity.size();k_polygon+=current_size)
00155 {
00156 glBegin(GL_POLYGON);
00157 for(int k_size=0;k_size<current_size;++k_size)
00158 {
00159 if(is_color==true)
00160 glColor3dv( p_color+3*current_connectivity[k_polygon+k_size] );
00161 if(is_texture==true)
00162 glTexCoord2dv( p_texture+2*current_connectivity[k_polygon+k_size] );
00163 glNormal3dv( p_normal+3*current_connectivity[k_polygon+k_size] );
00164 glVertex3dv( p_vertex+3*current_connectivity[k_polygon+k_size] );
00165
00166 }
00167 glEnd();
00168 }
00169
00170 }
00171 }
00172
00173
00174 glDisable(GL_LIGHTING);
00175 glDisable(GL_BLEND);
00176
00177 return mesh;
00178 }
00179
00180
00181 const MC_mesh_index_vector_draw& MC_opengl_drawer::draw(const MC_mesh_index_vector_draw& mesh)
00182 {
00183
00184 bool is_vertex_normal = (mesh.normal().size()==mesh.point_set().size());
00185 bool is_vertex_color = (mesh.color().size()==mesh.point_set().size());
00186 bool is_texture = (mesh.texture().size()!=0 && mesh.texture_connectivity().size()==mesh.polygon_number());
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 glEnable(GL_BLEND);
00198 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00199 glEnable(GL_COLOR_MATERIAL);
00200 glEnable(GL_LIGHTING);
00201
00202 int N_polygon=mesh.polygon_number();
00203 for(int k_polygon=0;k_polygon<N_polygon;k_polygon++)
00204 {
00205 MC_int_vector index=mesh.connectivity()(k_polygon);
00206
00207 MC_int_vector index_texture;
00208 if(is_texture==true)
00209 index_texture=mesh.texture_connectivity()(k_polygon);
00210
00211 int poly_size=index.size();
00212
00213 glBegin(GL_POLYGON);
00214 for(int k_vertex=0;k_vertex<poly_size;++k_vertex)
00215 {
00216 int u=index(k_vertex);
00217
00218 MC_v3d x=mesh.point_set()[u];
00219 if(is_vertex_normal)
00220 {MC_v3d n=mesh.normal()[u];glNormal3dv(n.pointer());}
00221 if(is_vertex_color)
00222 {MC_v3d c=mesh.color()[u];glColor3dv(c.pointer());}
00223 if(is_texture)
00224 {
00225 int utex=index_texture(k_vertex);
00226 MC_v3d t=mesh.texture()[utex];
00227
00228 glTexCoord2dv(t.pointer());
00229
00230 }
00231
00232 glVertex3dv(x.pointer());
00233 }
00234 glEnd();
00235 }
00236
00237
00238 glDisable(GL_BLEND);
00239
00240 return mesh;
00241 }
00242
00243 GLuint MC_opengl_drawer::send_gpu(const MC_mesh_fast_draw& mesh)
00244 {
00245 GLuint new_draw_list=glGenLists(1);
00246 glNewList(new_draw_list,GL_COMPILE);
00247 MC_opengl_drawer::draw(mesh);
00248 glEndList();
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 return new_draw_list;
00275 }
00276
00277 const MC_segment& MC_opengl_drawer::draw(const MC_segment& segment)
00278 {
00279 glBegin(GL_LINES);
00280 glVertex3dv(segment[0].pointer());
00281 glVertex3dv(segment[1].pointer());
00282 glEnd();
00283 return segment;
00284 }
00285 const std::vector<MC_segment>& MC_opengl_drawer::draw(const std::vector<MC_segment>& segments)
00286 {
00287 glBegin(GL_LINES);
00288 for(int k=0,N=segments.size();k<N;++k)
00289 {
00290 glVertex3dv(segments[k][0].pointer());
00291 glVertex3dv(segments[k][1].pointer());
00292 }
00293 glEnd();
00294 return segments;
00295 }
00296
00297 const MC_curve& MC_opengl_drawer::draw(const MC_curve& curve)
00298 {
00299 int N=curve.size();
00300 glBegin(GL_LINE_STRIP);
00301 for(int k=0;k<N;++k)
00302 glVertex3dv(curve[k].pointer());
00303 glEnd();
00304
00305 return curve;
00306 }
00307
00308
00309 void MC_opengl_drawer::draw_sphere(const MC_v3d& position,const double& radius,const int& N_subdiv)
00310 {
00311 draw(MC_mesh_fast_draw(MC_mesh_index_vector::build_sphere(N_subdiv)*radius+position));
00312 }
00313 void MC_opengl_drawer::draw_points(const MC_v3d_vector& position)
00314 {
00315 glBegin(GL_POINTS);
00316 for(int k=0,N=position.size();k<N;++k)
00317 glVertex3dv(position[k].pointer());
00318 glEnd();
00319 }
00320 void MC_opengl_drawer::draw_sphere(const MC_v3d_vector& position,const double& radius,const int& N_subdiv)
00321 {
00322 MC_mesh_fast_draw sph=MC_mesh_fast_draw(MC_mesh_index_vector::build_sphere(N_subdiv)*radius);
00323 int N=position.size();
00324 glMatrixMode(GL_MODELVIEW);
00325
00326 for(int k=0;k<N;++k)
00327 {
00328 glPushMatrix();
00329 glTranslated(position[k][0],position[k][1],position[k][2]);
00330 draw(sph);
00331 glPopMatrix();
00332 }
00333 }
00334
00335
00336 GLuint MC_opengl_drawer::send_gpu_grid(const MC_mesh_index_vector& mesh)
00337 {
00338 GLuint new_draw_list=glGenLists(1);
00339 glNewList(new_draw_list,GL_COMPILE);
00340 MC_opengl_drawer::draw_grid(mesh);
00341 glEndList();
00342 return new_draw_list;
00343 }
00344 const MC_mesh_index_vector& MC_opengl_drawer::draw_grid(const MC_mesh_index_vector& mesh)
00345 {
00346 int N=mesh.polygon_number();
00347 for(int k=0;k<N;++k)
00348 {
00349 glBegin(GL_LINE_STRIP);
00350 MC_int_vector index=mesh.connectivity()(k);
00351 int N_size=index.size();
00352 for(int k_poly=0;k_poly<=N_size;++k_poly)
00353 glVertex3dv(mesh.point_set()(index(k_poly%N_size)).pointer());
00354 glEnd();
00355 }
00356
00357 return mesh;
00358 }
00359 const MC_mesh_fast_draw& MC_opengl_drawer::draw_grid(const MC_mesh_fast_draw& mesh)
00360 {
00361 std::map<int,MC_int_vector> connec=mesh.connectivity();
00362 std::map<int,MC_int_vector> ::const_iterator it_connec=connec.begin();
00363 std::map<int,MC_int_vector> ::const_iterator it_connec_end=connec.end();
00364
00365 const double* v=mesh.point_set().pointer();
00366
00367 for(;it_connec!=it_connec_end;it_connec++)
00368 {
00369
00370 int N_size=it_connec->first;
00371 int N_poly=it_connec->second.size()/N_size;
00372
00373 for(int k_poly=0;k_poly<N_poly;++k_poly)
00374 {
00375 glBegin(GL_LINE_STRIP);
00376 for(int k_vertex=0;k_vertex<=N_size;++k_vertex)
00377 {
00378 int index=it_connec->second(k_poly*N_size+k_vertex%N_size);
00379 glVertex3dv(v+3*index);
00380 }
00381 glEnd();
00382 }
00383
00384
00385
00386 }
00387
00388
00389 return mesh;
00390 }
00391 const std::vector <MC_curve>& MC_opengl_drawer::draw(const std::vector <MC_curve>& curves)
00392 {
00393 int N=curves.size();
00394 for(int k=0;k<N;++k)
00395 draw(curves[k]);
00396 return curves;
00397 }
00398 const MC_polygon& MC_opengl_drawer::draw(const MC_polygon& polygon)
00399 {
00400 int N=polygon.size();
00401 MC_v3d n=polygon.normal();
00402
00403 glBegin(GL_POLYGON);
00404 glNormal3dv(n.pointer());
00405 for(int k=0;k<N;++k)
00406 glVertex3dv(polygon[k].pointer());
00407 glEnd();
00408
00409 return polygon;
00410 }
00411
00412 const MC_mesh_index_vector& MC_opengl_drawer::draw_normal_per_polygon(const MC_mesh_index_vector& mesh,const double& L)
00413 {
00414 MC_v3d_vector nn=mesh.normal_polygon();
00415 MC_v3d_vector bar=MC_v3d_vector::zeros(nn.size());
00416 for(int k=0,N=nn.size();k<N;++k)
00417 bar[k]=mesh.get_polygon(k).barycenter();
00418 for(int k=0,N=nn.size();k<N;++k)
00419 draw(MC_segment(bar[k],bar[k]+L*nn[k]));
00420 return mesh;
00421 }
00422
00423 MC_opengl_drawer MC_opengl_drawer::draw_triangular(const mesh::cgal::MC_mesh_cgal& mesh)
00424 {
00425 std::cout<<"Warning MC_opengl_drawer::draw_triangular not implemented"<<std::endl;
00426 exit(-1);
00427
00428 return MC_opengl_drawer();
00429 }
00430
00431
00432 }