forked from nvpro-samples/gl_cadscene_rendertechniques
/
cadscene.hpp
229 lines (180 loc) · 6.27 KB
/
cadscene.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*-----------------------------------------------------------------------
Copyright (c) 2014, NVIDIA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Neither the name of its contributors may be used to endorse
or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------*/
#ifndef CADSCENE_H__
#define CADSCENE_H__
#include <GL/glew.h>
#include <nv_math/nv_math.h>
#include <vector>
#include "nodetree.hpp"
class CadScene {
public:
struct BBox {
nv_math::vec4f min;
nv_math::vec4f max;
BBox() : min(FLT_MAX), max(-FLT_MAX) {}
inline void merge( const nv_math::vec4f& point )
{
min = nv_math::nv_min(min, point);
max = nv_math::nv_max(max, point);
}
inline void merge( const BBox& bbox )
{
min = nv_math::nv_min(min, bbox.min);
max = nv_math::nv_max(max, bbox.max);
}
inline BBox transformed ( const nv_math::mat4f &matrix, int dim=3)
{
int i;
nv_math::vec4f box[16];
// create box corners
box[0] = nv_math::vec4f(min.x,min.y,min.z,min.w);
box[1] = nv_math::vec4f(max.x,min.y,min.z,min.w);
box[2] = nv_math::vec4f(min.x,max.y,min.z,min.w);
box[3] = nv_math::vec4f(max.x,max.y,min.z,min.w);
box[4] = nv_math::vec4f(min.x,min.y,max.z,min.w);
box[5] = nv_math::vec4f(max.x,min.y,max.z,min.w);
box[6] = nv_math::vec4f(min.x,max.y,max.z,min.w);
box[7] = nv_math::vec4f(max.x,max.y,max.z,min.w);
box[8] = nv_math::vec4f(min.x,min.y,min.z,max.w);
box[9] = nv_math::vec4f(max.x,min.y,min.z,max.w);
box[10] = nv_math::vec4f(min.x,max.y,min.z,max.w);
box[11] = nv_math::vec4f(max.x,max.y,min.z,max.w);
box[12] = nv_math::vec4f(min.x,min.y,max.z,max.w);
box[13] = nv_math::vec4f(max.x,min.y,max.z,max.w);
box[14] = nv_math::vec4f(min.x,max.y,max.z,max.w);
box[15] = nv_math::vec4f(max.x,max.y,max.z,max.w);
// transform box corners
// and find new mins,maxs
BBox bbox;
for (i = 0; i < (1<<dim) ; i++){
nv_math::vec4f point = matrix * box[i];
bbox.merge(point);
}
return bbox;
}
};
struct MaterialSide {
nv_math::vec4f ambient;
nv_math::vec4f diffuse;
nv_math::vec4f specular;
nv_math::vec4f emissive;
};
// need to keep this 256 byte aligned (UBO range)
struct Material {
MaterialSide sides[2];
GLuint64 texturesADDR[4];
GLuint textures[4];
GLuint _pad[4+16];
Material() {
memset(this,0,sizeof(Material));
}
};
// need to keep this 256 byte aligned (UBO range)
struct MatrixNode {
nv_math::mat4f worldMatrix;
nv_math::mat4f worldMatrixIT;
nv_math::mat4f objectMatrix;
nv_math::mat4f objectMatrixIT;
};
struct Vertex {
nv_math::vec4f position;
nv_math::vec4f normal;
};
struct DrawRange {
size_t offset;
int count;
DrawRange() : offset(0) , count(0) {}
};
struct DrawStateInfo {
int materialIndex;
int matrixIndex;
friend bool operator != ( const DrawStateInfo &lhs, const DrawStateInfo &rhs){
return lhs.materialIndex != rhs.materialIndex || lhs.matrixIndex != rhs.matrixIndex;
}
friend bool operator == ( const DrawStateInfo &lhs, const DrawStateInfo &rhs){
return lhs.materialIndex == rhs.materialIndex && lhs.matrixIndex == rhs.matrixIndex;
}
};
struct DrawRangeCache {
std::vector<DrawStateInfo> state;
std::vector<int> stateCount;
std::vector<size_t> offsets;
std::vector<int> counts;
};
struct GeometryPart {
DrawRange indexSolid;
DrawRange indexWire;
};
struct Geometry {
GLuint vboGL;
GLuint iboGL;
GLuint64 vboADDR;
GLuint64 iboADDR;
size_t vboSize;
size_t iboSize;
std::vector<GeometryPart> parts;
int numVertices;
int numIndexSolid;
int numIndexWire;
int cloneIdx;
};
struct ObjectPart {
int active;
int materialIndex;
int matrixIndex;
};
struct Object {
int matrixIndex;
int geometryIndex;
std::vector<ObjectPart> parts;
DrawRangeCache cacheSolid;
DrawRangeCache cacheWire;
};
std::vector<Material> m_materials;
std::vector<BBox> m_geometryBboxes;
std::vector<Geometry> m_geometry;
std::vector<MatrixNode> m_matrices;
std::vector<Object> m_objects;
std::vector<nv_math::vec2i> m_objectAssigns;
BBox m_bbox;
GLuint m_materialsGL;
GLuint64 m_materialsADDR;
GLuint m_matricesGL;
GLuint64 m_matricesADDR;
GLuint m_matricesTexGL;
GLuint64 m_matricesTexGLADDR;
GLuint m_geometryBboxesGL;
GLuint m_geometryBboxesTexGL;
GLuint m_objectAssignsGL;
GLuint m_parentIDsGL;
GLuint m_matricesOrigGL;
GLuint m_matricesOrigTexGL;
NodeTree m_nodeTree;
void updateObjectDrawCache(Object& object);
bool loadCSF(const char* filename, int clones = 0, int cloneaxis=3);
void unload();
static void enableVertexFormat(int attrPos, int attrNormal);
static void disableVertexFormat(int attrPos, int attrNormal);
void resetMatrices();
};
#endif