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