Commit f989188b authored by Jan Möbius's avatar Jan Möbius
Browse files

Hopefully final switch to drawmsh

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@11717 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 9df1e6ec
......@@ -52,6 +52,7 @@
#define ACG_MESHNODE_C
#include <ACG/Geometry/GPUCacheOptimizer.hh>
#include <ACG/GL/DrawMesh.hh>
//== NAMESPACES ===============================================================
......@@ -72,20 +73,9 @@ MeshNodeT(Mesh& _mesh,
std::string _name ):
BaseNode(_parent, _name),
mesh_(_mesh),
stripProcessor_(_mesh),
indexBuffer_(0),
indexBufferInitialized_(false),
vertexBuffer_(0),
vertexBufferInitialized_(false),
drawMesh_(0),
enableNormals_(true),
normalVertexBuffer_(0),
normalVertexBufferInitialized_(false),
enableColors_(true),
colorVertexbuffer_(0),
colorVertexBufferInitialized_(false),
enableTexCoords_(true),
lineIndexBuffer_(0),
lineIndexBufferInitialized_(false),
enabled_arrays_(0),
updateVertexPickingList_(true),
vertexPickingBaseIndex_(0),
......@@ -109,6 +99,7 @@ MeshNodeT(Mesh& _mesh,
std::cerr << "Error! Vertex buffer objects are not supported! The meshNode will not work without them!" << std::endl;
}
drawMesh_ = new DrawMeshT<Mesh>(mesh_);
vertexPickingList_ = glGenLists(1);
edgePickingList_ = glGenLists(1);
......@@ -122,21 +113,7 @@ MeshNodeT<Mesh>::
~MeshNodeT()
{
// Delete all allocated buffers
if (vertexBuffer_)
glDeleteBuffersARB(1, &vertexBuffer_);
if (normalVertexBuffer_)
glDeleteBuffersARB(1, &normalVertexBuffer_);
if (colorVertexbuffer_)
glDeleteBuffersARB(1, &colorVertexbuffer_);
if (lineIndexBuffer_)
glDeleteBuffersARB(1, &lineIndexBuffer_);
if (indexBuffer_)
glDeleteBuffersARB(1, &indexBuffer_);
if (vertexPickingList_)
glDeleteLists (vertexPickingList_, 1);
......@@ -149,6 +126,7 @@ MeshNodeT<Mesh>::
if (anyPickingList_)
glDeleteLists (anyPickingList_, 3);
delete drawMesh_;
}
template<class Mesh>
......@@ -225,16 +203,33 @@ template<class Mesh>
void
MeshNodeT<Mesh>::
draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
// Update strips if necessary
stripProcessor_.stripify();
/*
if ( ( _drawMode & DrawModes::SOLID_FLAT_SHADED ) ||
( _drawMode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED) ||
( _drawMode & DrawModes::SOLID_TEXTURED) ||
( _drawMode & DrawModes::SOLID_2DTEXTURED_FACE))
{
drawMesh_->setFlatShading();
}
else
drawMesh_->setSmoothShading();
if ( (_drawMode & DrawModes::SOLID_FACES_COLORED ||
_drawMode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED))
{
drawMesh_->usePerFaceColors();
}
else
drawMesh_->usePerVertexColors();
*/
GLenum prev_depth = _state.depthFunc();
unsigned int arrays = VERTEX_ARRAY;
unsigned int arrays = NONE;
glPushAttrib(GL_ENABLE_BIT);
if ( (_drawMode & DrawModes::POINTS) || (_drawMode & DrawModes::POINTS_COLORED) || (_drawMode & DrawModes::POINTS_SHADED ) ) {
glShadeModel(GL_FLAT);
......@@ -246,21 +241,20 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
// Use Colors in this mode if allowed
if ( enableColors_ && (_drawMode & DrawModes::POINTS_COLORED) )
arrays |= COLOR_VERTEX_ARRAY;
// Use Normals in this mode if allowed
if ( enableNormals_ && (_drawMode & DrawModes::POINTS_SHADED ) ) {
arrays |= NORMAL_VERTEX_ARRAY;
{
drawMesh_->usePerVertexColors();
// If we have colors and lighting with normals, we have to use colormaterial
if ( (arrays & COLOR_VERTEX_ARRAY) )
if ( enableNormals_ && (_drawMode & DrawModes::POINTS_SHADED ) )
glEnable(GL_COLOR_MATERIAL);
else
else
glDisable(GL_COLOR_MATERIAL);
}
else
drawMesh_->disableColors();
// Bring the arrays online
enable_arrays(arrays);
// enable_arrays(arrays);
// Draw vertices
draw_vertices();
......@@ -270,15 +264,18 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
/// \todo We can render also wireframe shaded and with vertex colors
if (_drawMode & DrawModes::WIREFRAME)
{
enable_arrays( VERTEX_ARRAY | LINE_INDEX_ARRAY );
// enable_arrays( VERTEX_ARRAY | LINE_INDEX_ARRAY );
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
drawMesh_->disableColors();
draw_lines();
}
if (_drawMode & DrawModes::HIDDENLINE)
{
enable_arrays(VERTEX_ARRAY);
// enable_arrays(VERTEX_ARRAY);
// First:
// Render all faces in background color to initialize z-buffer
......@@ -290,14 +287,17 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
_state.set_base_color(clear_color);
// drawMesh_->SetFlatShading();
drawMesh_->disableColors();
glDepthRange(0.01, 1.0);
draw_faces(PER_VERTEX);
draw_faces();
glDepthRange(0.0, 1.0);
// Second
// Render the lines. All lines not on the front will be skipped in z-test
enable_arrays(VERTEX_ARRAY|LINE_INDEX_ARRAY);
// enable_arrays(VERTEX_ARRAY|LINE_INDEX_ARRAY);
glDepthFunc(GL_LEQUAL);
_state.set_base_color(base_color);
draw_lines();
......@@ -339,12 +339,11 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
glDepthRange(0.01, 1.0);
if ( enableNormals_ ) {
glEnable(GL_COLOR_MATERIAL);
enable_arrays( VERTEX_ARRAY | COLOR_VERTEX_ARRAY | NORMAL_VERTEX_ARRAY );
} else {
glDisable(GL_COLOR_MATERIAL);
enable_arrays( VERTEX_ARRAY | COLOR_VERTEX_ARRAY );
}
draw_faces(PER_VERTEX);
draw_faces();
glDepthRange(0.0, 1.0);
}
......@@ -353,19 +352,24 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
glEnable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glDepthRange(0.01, 1.0);
enable_arrays(PER_FACE_VERTEX_ARRAY | PER_FACE_NORMAL_ARRAY);
draw_faces(PER_FACE);
drawMesh_->setFlatShading();
drawMesh_->disableColors();
draw_faces();
glDepthRange(0.0, 1.0);
}
if ( ( _drawMode & DrawModes::SOLID_SMOOTH_SHADED ) && mesh_.has_vertex_normals() )
{
enable_arrays( VERTEX_ARRAY | NORMAL_VERTEX_ARRAY );
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glDepthRange(0.01, 1.0);
draw_faces(PER_VERTEX);
drawMesh_->setSmoothShading();
drawMesh_->disableColors();
draw_faces();
glDepthRange(0.0, 1.0);
}
......@@ -381,12 +385,15 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
//
// // Enable own Phong shader
// program->use();
enable_arrays(VERTEX_ARRAY | NORMAL_VERTEX_ARRAY );
// enable_arrays(VERTEX_ARRAY | NORMAL_VERTEX_ARRAY );
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glDepthRange(0.01, 1.0);
draw_faces(PER_VERTEX);
drawMesh_->setSmoothShading();
drawMesh_->disableColors();
draw_faces();
glDepthRange(0.0, 1.0);
//disable own Phong shader
......@@ -403,8 +410,11 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glDepthRange(0.01, 1.0);
enable_arrays(PER_FACE_VERTEX_ARRAY | PER_FACE_COLOR_ARRAY);
draw_faces(PER_FACE);
// enable_arrays(PER_FACE_VERTEX_ARRAY | PER_FACE_COLOR_ARRAY);
drawMesh_->usePerFaceColors();
draw_faces();
glDepthRange(0.0, 1.0);
_state.set_base_color(base_color_backup);
......@@ -418,8 +428,12 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
glShadeModel(GL_FLAT);
glDepthRange(0.01, 1.0);
enable_arrays(PER_FACE_VERTEX_ARRAY | PER_FACE_COLOR_ARRAY | PER_FACE_NORMAL_ARRAY );
draw_faces(PER_FACE);
// enable_arrays(PER_FACE_VERTEX_ARRAY | PER_FACE_COLOR_ARRAY | PER_FACE_NORMAL_ARRAY );
drawMesh_->setFlatShading();
drawMesh_->usePerFaceColors();
draw_faces();
glDepthRange(0.0, 1.0);
_state.set_base_color(base_color_backup);
......@@ -429,41 +443,50 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
if ( ( _drawMode & DrawModes::SOLID_TEXTURED ) && mesh_.has_vertex_texcoords2D())
{
///\todo enableTexCoords_
enable_arrays(VERTEX_ARRAY | TEXCOORD_VERTEX_ARRAY );
// enable_arrays(VERTEX_ARRAY | TEXCOORD_VERTEX_ARRAY );
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glDepthRange(0.01, 1.0);
draw_faces(PER_VERTEX);
drawMesh_->disableColors();
draw_faces();
glDepthRange(0.0, 1.0);
glDisable(GL_TEXTURE_2D);
}
if ( ( _drawMode & DrawModes::SOLID_TEXTURED_SHADED ) && mesh_.has_vertex_texcoords2D() && mesh_.has_vertex_normals())
{
enable_arrays(VERTEX_ARRAY | NORMAL_VERTEX_ARRAY | TEXCOORD_VERTEX_ARRAY);
// enable_arrays(VERTEX_ARRAY | NORMAL_VERTEX_ARRAY | TEXCOORD_VERTEX_ARRAY);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glDepthRange(0.01, 1.0);
draw_faces(PER_VERTEX);
drawMesh_->setSmoothShading();
drawMesh_->disableColors();
draw_faces();
glDepthRange(0.0, 1.0);
glDisable(GL_TEXTURE_2D);
}
// Textured by using coordinates stored in halfedges ... arrays generated by stripprocessor
if ( (_drawMode & DrawModes::SOLID_2DTEXTURED_FACE) && mesh_.n_faces() > 0 )
{
glEnable(GL_TEXTURE_2D);
enable_arrays( PER_FACE_VERTEX_ARRAY | PER_FACE_TEXCOORD_ARRAY );
// enable_arrays( PER_FACE_VERTEX_ARRAY | PER_FACE_TEXCOORD_ARRAY );
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glDepthRange(0.01, 1.0);
draw_faces(PER_FACE);
drawMesh_->disableColors();
draw_faces();
glDepthRange(0.0, 1.0);
glDisable(GL_TEXTURE_2D);
......@@ -474,12 +497,16 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
{
glEnable(GL_TEXTURE_2D);
enable_arrays( PER_FACE_VERTEX_ARRAY | PER_FACE_TEXCOORD_ARRAY | PER_FACE_PER_VERTEX_NORMAL_ARRAY );
// enable_arrays( PER_FACE_VERTEX_ARRAY | PER_FACE_TEXCOORD_ARRAY | PER_FACE_PER_VERTEX_NORMAL_ARRAY );
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glDepthRange(0.01, 1.0);
draw_faces(PER_FACE);
drawMesh_->setSmoothShading();
drawMesh_->disableColors();
draw_faces();
glDepthRange(0.0, 1.0);
glDisable(GL_TEXTURE_2D);
......@@ -497,11 +524,7 @@ template<class Mesh>
void
MeshNodeT<Mesh>::
draw_vertices() {
if ( !vertexBufferInitialized_ || ( mesh_.n_vertices() == 0 ))
return;
glDrawArrays(GL_POINTS, 0, mesh_.n_vertices());
drawMesh_->drawVertices();
}
template<class Mesh>
......@@ -509,21 +532,13 @@ void
MeshNodeT<Mesh>::
draw_lines() {
if ( enabled_arrays_ & LINE_INDEX_ARRAY )
// Check if array is set up correctly
if ( lineIndexBufferInitialized_ )
glDrawElements(GL_LINES, mesh_.n_edges() * 2, GL_UNSIGNED_INT, 0 );
else
return;
// If we are rendering per edge per vertex attributes, we need to use a seperated vertex buffer!
else if ( enabled_arrays_ & PER_EDGE_VERTEX_ARRAY )
if ((enabled_arrays_ & PER_EDGE_COLOR_ARRAY) && (enabled_arrays_ & PER_EDGE_VERTEX_ARRAY))
{
// colored edges still slow
glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);
// Something went wrong here!
}
else
std::cerr << "Unable to Draw! array configuration is invalid!!" << std::endl;
drawMesh_->drawLines();
}
......@@ -542,165 +557,19 @@ draw_halfedges() {
template<class Mesh>
void
MeshNodeT<Mesh>::
draw_faces(FaceMode _mode) {
// If we have all properties per Vertex, we can render with index array from triangle strips!
if (_mode == PER_VERTEX) {
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexBuffer_);
glDrawElements(GL_TRIANGLES, tris_.size(), GL_UNSIGNED_INT, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
} else if ( _mode == PER_FACE ) {
if ( stripProcessor_.perFaceTextureIndexAvailable() && (textureMap_ != 0) ) {
int lastTexture = -1;
for ( uint i = 0 ; i < stripProcessor_.textureRenderData()->size() ; ++i ) {
int texture = (*stripProcessor_.textureRenderData())[i].textureId;
if ( texture != lastTexture) {
if ( textureMap_->find(texture) == textureMap_->end() ) {
std::cerr << "Illegal texture index ... trying to access " << texture << std::endl;
lastTexture = -1;
continue;
}
glBindTexture( GL_TEXTURE_2D, (*textureMap_)[texture] );
lastTexture = texture;
}
// We need per face attributes so we have to use seperate vertices per face
glDrawArrays(GL_TRIANGLES, (*stripProcessor_.textureRenderData())[i].startOffset , (*stripProcessor_.textureRenderData())[i].faceCount * 3 );
}
// Unbind Texture again
glBindTexture( GL_TEXTURE_2D, 0 );
} else {
// We need per face attributes so we have to use seperate vertices per face
glDrawArrays(GL_TRIANGLES, 0, stripProcessor_.perFaceVertexBufferSize() );
}
}
draw_faces() {
drawMesh_->draw(textureMap_);
}
template<class Mesh>
void
MeshNodeT<Mesh>::
enable_arrays(unsigned int _arrays) {
// Unbind everything to ensure sane settings
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
//===================================================================
// Vertex Array
//===================================================================
// Check if we should enable the vertex array
if ( (_arrays & VERTEX_ARRAY ) && vertexBufferInitialized_ ) {
// Check if its already enabled
if (!(enabled_arrays_ & VERTEX_ARRAY)) {
// Enable the vertex buffer
enabled_arrays_ |= VERTEX_ARRAY;
// Bind the Vertex buffer
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer_);
// As we ensure that buffers are converted to float before using them, use Float here
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
}
} else if (enabled_arrays_ & VERTEX_ARRAY) {
// Disable the Vertex array
enabled_arrays_ &= ~VERTEX_ARRAY;
glDisableClientState(GL_VERTEX_ARRAY);
}
//===================================================================
// Normal Array
//===================================================================
// Check if we should enable the normal array
if ( mesh_.has_vertex_normals() && ( _arrays & NORMAL_VERTEX_ARRAY ) && normalVertexBufferInitialized_ ) {
// Check if its already enabled
if (!(enabled_arrays_ & NORMAL_VERTEX_ARRAY)) {
enabled_arrays_ |= NORMAL_VERTEX_ARRAY;
glBindBufferARB(GL_ARRAY_BUFFER_ARB, normalVertexBuffer_);
// As we ensure that buffers are converted to float before using them, use Float here
glNormalPointer(GL_FLOAT, 0 , 0);
glEnableClientState(GL_NORMAL_ARRAY);
}
} else if (enabled_arrays_ & NORMAL_VERTEX_ARRAY) {
// Disable Normal array
enabled_arrays_ &= ~NORMAL_VERTEX_ARRAY;
glDisableClientState(GL_NORMAL_ARRAY);
}
//===================================================================
// per Vertex Color Array
//===================================================================
/// \todo This is different to normal and vertex buffer since it uses openmesh colors directly! Check for different color representations in OpenMesh!
// Check if we should enable the color array
if ( mesh_.has_vertex_colors() && ( _arrays & COLOR_VERTEX_ARRAY )) {
// Check if its already enabled
if (!(enabled_arrays_ & COLOR_VERTEX_ARRAY)) {
enabled_arrays_ |= COLOR_VERTEX_ARRAY;
glBindBufferARB(GL_ARRAY_BUFFER_ARB, colorVertexbuffer_);
// Explicitly give the pointer as we uploaded the data ourself!
glColorPointer(4, GL_FLOAT , 0 , 0);
glEnableClientState(GL_COLOR_ARRAY);
}
} else if (enabled_arrays_ & COLOR_VERTEX_ARRAY) {
// Disable Color array
enabled_arrays_ &= ~COLOR_VERTEX_ARRAY;
glDisableClientState(GL_COLOR_ARRAY);
}
//===================================================================
// per Vertex Texture coordinate Array
//===================================================================
// Check if we should enable the color array
if ( mesh_.has_vertex_texcoords2D() && ( _arrays & TEXCOORD_VERTEX_ARRAY )) {
// Check if its already enabled
if (!(enabled_arrays_ & TEXCOORD_VERTEX_ARRAY)) {
enabled_arrays_ |= TEXCOORD_VERTEX_ARRAY;
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
// Explicitly give the pointer as we uploaded the data ourself!
glTexCoordPointer(2, GL_FLOAT , 0 , mesh_.texcoords2D() );
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
} else if (enabled_arrays_ & TEXCOORD_VERTEX_ARRAY) {
// Disable TexCoord array
enabled_arrays_ &= ~TEXCOORD_VERTEX_ARRAY;
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
//===================================================================
// per Edge Vertex Array
//===================================================================
......@@ -714,7 +583,7 @@ enable_arrays(unsigned int _arrays) {
// For this version we load the colors directly not from vbo
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glVertexPointer( stripProcessor_.perEdgeVertexBuffer() );
glVertexPointer( drawMesh_->perEdgeVertexBuffer() );
glEnableClientState(GL_VERTEX_ARRAY);
......@@ -738,7 +607,7 @@ enable_arrays(unsigned int _arrays) {
// For this version we load the colors directly not from vbo
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glColorPointer( stripProcessor_.perEdgeColorBuffer() );
glColorPointer( drawMesh_->perEdgeColorBuffer() );
glEnableClientState(GL_COLOR_ARRAY);
......@@ -763,7 +632,7 @@ enable_arrays(unsigned int _arrays) {
// For this version we load the colors directly not from vbo
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glVertexPointer( stripProcessor_.perHalfedgeVertexBuffer() );
glVertexPointer( drawMesh_->perHalfedgeVertexBuffer() );
glEnableClientState(GL_VERTEX_ARRAY);
......@@ -787,7 +656,7 @@ enable_arrays(unsigned int _arrays) {
// For this version we load the colors directly not from vbo
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glColorPointer( stripProcessor_.perHalfedgeColorBuffer() );
glColorPointer( drawMesh_->perHalfedgeColorBuffer() );
glEnableClientState(GL_COLOR_ARRAY);
......@@ -797,147 +666,7 @@ enable_arrays(unsigned int _arrays) {
enabled_arrays_ &= ~PER_HALFEDGE_COLOR_ARRAY;
glDisableClientState(GL_COLOR_ARRAY);
}
//===================================================================
// per Face Vertex Array
//===================================================================
// Check if we should enable the per face vertex array
if (_arrays & PER_FACE_VERTEX_ARRAY) {
// Check if its already enabled
if (!(enabled_arrays_ & PER_FACE_VERTEX_ARRAY)) {
enabled_arrays_ |= PER_FACE_VERTEX_ARRAY;
// For this version we load the colors directly not from vbo
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glVertexPointer( stripProcessor_.perFaceVertexBuffer() );
glEnableClientState(GL_VERTEX_ARRAY);
}
} else if (enabled_arrays_ & PER_FACE_VERTEX_ARRAY) {
// Disable Vertex array
enabled_arrays_ &= ~PER_FACE_VERTEX_ARRAY;
glDisableClientState(GL_VERTEX_ARRAY);
}
//===================================================================
// per Face Normal Array
//===================================================================
// Check if we should enable the per face normal array
if (mesh_.has_face_normals() && (_arrays & PER_FACE_NORMAL_ARRAY) ) {
// Check if its already enabled
if (!(enabled_arrays_ & PER_FACE_NORMAL_ARRAY)) {
enabled_arrays_ |= PER_FACE_NORMAL_ARRAY;
// For this version we load the colors directly not from vbo
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glNormalPointer( stripProcessor_.perFaceNormalBuffer() );
glEnableClientState(GL_NORMAL_ARRAY);
}
} else if (enabled_arrays_ & PER_FACE_NORMAL_ARRAY) {
// Disable Normal array