Developer Documentation
MeshNode2.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 "MeshNode2T.hh"
46 #include "MeshNode2T_impl.hh"
47 
48 namespace ACG {
49 namespace SceneGraph {
50 
51 MeshNodeBase::MeshNodeBase(BaseNode* _parent, std::string _name) :
52  BaseNode(_parent, _name),
53  drawMeshBase_(0),
54  polyEdgeBuf_(0),
55  polyEdgeBufSize_(0),
56  polyEdgeBufTex_(0) {
57 }
58 
59 void MeshNodeBase::supplyDrawMesh(DrawMeshBase *drawMeshBase) {
60  /*
61  * We take the luxury of checking these conditions even in release
62  * mode as this method is rarely called.
63  */
64  if (drawMeshBase_)
65  throw std::runtime_error("MeshNodeBase::supplyDrawMesh called "
66  "more than once.");
67  if (!drawMeshBase)
68  throw std::runtime_error("MeshNodeBase::supplyDrawMesh called "
69  "with NULL parameter.");
70 
71  drawMeshBase_ = drawMeshBase;
72 }
73 
74 void MeshNodeBase::updatePolyEdgeBuf()
75 {
77 {
78  // drawMeshBase_ must have been supplied.
79  assert(drawMeshBase_);
80 
81  MeshCompiler * const mc = drawMeshBase_->getMeshCompiler();
82  if (mc && !mc->isTriangleMesh())
83  {
84  // create/update the poly-edge buffer
85  if (!polyEdgeBuf_)
86  glGenBuffers(1, &polyEdgeBuf_);
87 
88  const int nTris = mc->getNumTriangles();
89 
90  const int newBufSize = (nTris/2+1);
91 
92  if (polyEdgeBufSize_ != newBufSize)
93  {
94  glBindBuffer(GL_TEXTURE_BUFFER, polyEdgeBuf_);
95 
96  // The poly-edge buffer is a texture buffer that stores one byte for each triangle, which encodes triangle edge properties.
97  // An inner edge is an edge that was added during the triangulation of a n-poly,
98  // whereas outer edges are edges that already exist in the input mesh object.
99  // This information is used in the wireframe/hiddenline geometry shader to identify edges, which should not be rendered.
100  // Buffer storage:
101  // each triangle uses 3 bits to mark edges as visible or hidden
102  // outer edge -> bit = 1 (visible)
103  // inner edge -> bit = 0 (hidden)
104  // each byte can store edges for two triangles and the remaining 2 bits are left unused
105 
106  polyEdgeBufSize_ = newBufSize;
107  unsigned char* polyEdgeBufData = new unsigned char[newBufSize];
108 
109  // set zero
110  memset(polyEdgeBufData, 0, newBufSize);
111 
112  // build buffer
113  for (int i = 0; i < nTris; ++i)
114  {
115  int byteidx = i>>1;
116  int bitidx = (i&1) * 3;
117 
118  for (int k = 0; k < 3; ++k)
119  if (mc->isFaceEdge(i, k))
120  polyEdgeBufData[byteidx] += 1 << (k + bitidx);
121  }
122 
123  glBufferData(GL_TEXTURE_BUFFER, polyEdgeBufSize_, polyEdgeBufData, GL_STATIC_DRAW);
124 
125 
126  delete [] polyEdgeBufData;
127  polyEdgeBufData = 0;
128 
129  // create texture object for the texture buffer
130 
131  if (!polyEdgeBufTex_)
132  {
133  glGenTextures(1, &polyEdgeBufTex_);
134 
135  glActiveTexture(GL_TEXTURE0);
136  glBindTexture(GL_TEXTURE_BUFFER, polyEdgeBufTex_);
137  glTexBuffer(GL_TEXTURE_BUFFER, GL_R8UI, polyEdgeBuf_);
138 
139  glBindTexture(GL_TEXTURE_2D, 0);
140  }
141 
143  }
144  }
145 }
146 }
147 
148 
149 } /* namespace SceneGraph */
150 } /* namespace ACG */
Namespace providing different geometric functions concerning angles.
void glCheckErrors()
Definition: GLError.hh:96
void compatibilityProfile(bool _enableCoreProfile)
Store opengl core profile setting.
Definition: gl.cc:166