Developer Documentation
DrawMesh.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 
43 
44 #include <ACG/GL/acg_glew.hh>
45 #include "DrawMesh.hh"
46 
47 namespace ACG {
48 
49 DrawMeshBase::DrawMeshBase() :
50  vbo_(0), ibo_(0),
51  numTris_(0), numVerts_(0),
52  meshComp_(0),
53  lineIBO_(0),
54  heVBO_(0),
55  indexType_(0),
56  pickVertexIBO_(0) {
57 
58  vertexDecl_ = new VertexDeclaration;
59  vertexDeclEdgeCol_ = new VertexDeclaration;
60  vertexDeclHalfedgeCol_ = new VertexDeclaration;
61  vertexDeclHalfedgePos_ = new VertexDeclaration;
62 }
63 
64 DrawMeshBase::~DrawMeshBase() {
65  if (vbo_) glDeleteBuffers(1, &vbo_);
66  if (ibo_) glDeleteBuffers(1, &ibo_);
67  if (lineIBO_) glDeleteBuffers(1, &lineIBO_);
68  if (heVBO_) glDeleteBuffers(1, &heVBO_);
69 
70  delete vertexDecl_;
71  delete vertexDeclEdgeCol_;
72  delete vertexDeclHalfedgeCol_;
73  delete vertexDeclHalfedgePos_;
74 
75  if (pickVertexIBO_) glDeleteBuffers(1, &pickVertexIBO_);
76 }
77 
78 void DrawMeshBase::deleteIbo() {
79  if (ibo_)
80  glDeleteBuffers(1, &ibo_);
81  ibo_ = 0;
82 }
83 
84 void DrawMeshBase::bindVbo() {
85  if (!vbo_)
86  glGenBuffers(1, &vbo_);
87 
88  ACG::GLState::bindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_);
89 }
90 
91 void DrawMeshBase::bindIbo() {
92  if (!ibo_)
93  glGenBuffers(1, &ibo_);
94 
95  ACG::GLState::bindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo_);
96 }
97 
98 void DrawMeshBase::bindLineIbo() {
99  if (!lineIBO_)
100  glGenBuffers(1, &lineIBO_);
101 
102  ACG::GLState::bindBufferARB(GL_ELEMENT_ARRAY_BUFFER, lineIBO_);
103 }
104 
105 void DrawMeshBase::bindHEVbo() {
106  glGetIntegerv(GL_ARRAY_BUFFER_BINDING,&prevVBO_);
107  if (!heVBO_)
108  glGenBuffers(1, &heVBO_);
109 
110  ACG::GLState::bindBufferARB(GL_ARRAY_BUFFER_ARB, heVBO_);
111 }
112 
113 void DrawMeshBase::unbindHEVbo() {
114  ACG::GLState::bindBufferARB(GL_ARRAY_BUFFER_ARB, prevVBO_);
115 }
116 
117 void DrawMeshBase::bindPickVertexIbo() {
118  if (!pickVertexIBO_)
119  glGenBuffers(1, &pickVertexIBO_);
120 
121  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, pickVertexIBO_);
122 }
123 
124 void DrawMeshBase::createIndexBuffer() {
125  glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB,
126  numTris_ * 3 * sizeof(unsigned int),
127  meshComp_->getIndexBuffer(), GL_STATIC_DRAW_ARB);
128 }
129 
130 void DrawMeshBase::fillLineBuffer(size_t n_edges, void *data) {
131  // 2 or 4 byte indices:
132  if (indexType_ == GL_UNSIGNED_SHORT)
133  glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB,
134  n_edges * 2 * sizeof(unsigned short),
135  data, GL_STATIC_DRAW_ARB);
136  else
137  glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB,
138  n_edges * 2 * sizeof(unsigned int),
139  data, GL_STATIC_DRAW_ARB);
140 }
141 
142 void DrawMeshBase::fillHEVBO(size_t numberOfElements_, size_t sizeOfElements_, void* data_)
143 {
144  bindHEVbo();
145  glBufferData(GL_ARRAY_BUFFER,numberOfElements_ * sizeOfElements_, data_, GL_STATIC_DRAW);
146  unbindHEVbo();
147 }
148 
149 void DrawMeshBase::fillVertexBuffer() {
150  if (!vertices_.empty())
151  glBufferData(GL_ARRAY_BUFFER_ARB, numVerts_ * vertexDecl_->getVertexStride(), &vertices_[0], GL_STATIC_DRAW_ARB);
152 }
153 
154 void DrawMeshBase::fillInvVertexMap(size_t n_vertices, void *data) {
155  glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(int) * n_vertices, data, GL_STATIC_DRAW);
156 }
157 
158 } /* namespace ACG */
Namespace providing different geometric functions concerning angles.
static void bindBufferARB(GLenum _target, GLuint _buffer)
same function as bindBuffer
Definition: GLState.hh:573