-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPolydata.cpp
More file actions
375 lines (338 loc) · 14 KB
/
Polydata.cpp
File metadata and controls
375 lines (338 loc) · 14 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
hale: support for minimalist scientific visualization
Copyright (C) 2014, 2015 University of Chicago
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software. Permission is granted to anyone to
use this software for any purpose, including commercial applications, and
to alter it and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "Hale.h"
#include "privateHale.h"
#include <teem/ell.h>
namespace Hale {
void Polydata::_buffer(bool newaddr) {
static const char me[] = "Hale::Polydata::_buffer";
const limnPolyData *lpd = this->lpld();
unsigned int ibits = limnPolyDataInfoBitFlag(lpd);
if (debugging)
printf("!%s(%s): ____________________________________________ \n", me,
_name.c_str());
glBindVertexArray(_vao);
if (debugging) printf("# glBindVertexArray(%u);\n", _vao);
glBindBuffer(GL_ARRAY_BUFFER, _buff[_buffIdx[vertAttrIdxXYZW]]);
if (debugging)
printf("# glBindBuffer(GL_ARRAY_BUFFER, %u);\n", _buff[_buffIdx[vertAttrIdxXYZW]]);
if (newaddr) {
glBufferData(GL_ARRAY_BUFFER, lpd->xyzwNum * sizeof(float) * 4, lpd->xyzw,
GL_DYNAMIC_DRAW);
if (debugging)
printf("# glBufferData(GL_ARRAY_BUFFER, %u, lpd->xyzw, GL_DYNAMIC_DRAW);\n",
(unsigned int)(lpd->xyzwNum * sizeof(float) * 4));
} else {
glBufferSubData(GL_ARRAY_BUFFER, 0, lpd->xyzwNum * sizeof(float) * 4, lpd->xyzw);
}
glVertexAttribPointer(Hale::vertAttrIdxXYZW, 4, GL_FLOAT, GL_FALSE, 0, 0);
if (debugging)
printf("# glVertexAttribPointer(%u, 4, GL_FLOAT, GL_FALSE, 0, 0);\n",
Hale::vertAttrIdxXYZW);
if (ibits & (1 << limnPolyDataInfoNorm)) {
glBindBuffer(GL_ARRAY_BUFFER, _buff[_buffIdx[vertAttrIdxNorm]]);
if (debugging)
printf("# glBindBuffer(GL_ARRAY_BUFFER, %u);\n",
_buff[_buffIdx[vertAttrIdxNorm]]);
if (newaddr) {
glBufferData(GL_ARRAY_BUFFER, lpd->normNum * sizeof(float) * 3, lpd->norm,
GL_DYNAMIC_DRAW);
if (debugging)
printf("# glBufferData(GL_ARRAY_BUFFER, %u, lpd->norm, "
"GL_DYNAMIC_DRAW);\n",
(unsigned int)(lpd->normNum * sizeof(float) * 3));
} else {
glBufferSubData(GL_ARRAY_BUFFER, 0, lpd->normNum * sizeof(float) * 3, lpd->norm);
}
glVertexAttribPointer(Hale::vertAttrIdxNorm, 3, GL_FLOAT, GL_FALSE, 0, 0);
if (debugging)
printf("# glVertexAttribPointer(%u, 3, GL_FLOAT, GL_FALSE, 0, 0);\n",
Hale::vertAttrIdxNorm);
}
if (ibits & (1 << limnPolyDataInfoRGBA)) {
glBindBuffer(GL_ARRAY_BUFFER, _buff[_buffIdx[vertAttrIdxRGBA]]);
if (debugging)
printf("# glBindBuffer(GL_ARRAY_BUFFER, %u);\n",
_buff[_buffIdx[vertAttrIdxRGBA]]);
if (newaddr) {
glBufferData(GL_ARRAY_BUFFER, lpd->rgbaNum * sizeof(char) * 4, lpd->rgba,
GL_DYNAMIC_DRAW);
if (debugging)
printf("# glBufferData(GL_ARRAY_BUFFER, %u, lpd->rgba, "
"GL_DYNAMIC_DRAW);\n",
(unsigned int)(lpd->rgbaNum * sizeof(char) * 4));
} else {
glBufferSubData(GL_ARRAY_BUFFER, 0, lpd->rgbaNum * sizeof(char) * 4, lpd->rgba);
}
glVertexAttribPointer(Hale::vertAttrIdxRGBA, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
if (debugging)
printf("# glVertexAttribPointer(%u, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);\n",
Hale::vertAttrIdxRGBA);
}
/* HEY: tang and tex2 */
if (!_elms || newaddr) {
if (_elms) {
glDeleteBuffers(1, &_elms);
if (debugging) printf("# glDeleteBuffers(1, %u);\n", _elms);
}
glGenBuffers(1, &_elms);
if (debugging) printf("# glGenBuffers(1, &); -> %u\n", _elms);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _elms);
if (debugging) printf("# glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, %u);\n", _elms);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, lpd->indxNum * sizeof(unsigned int),
lpd->indx, GL_DYNAMIC_DRAW);
if (debugging)
printf("# glBufferData(GL_ELEMENT_ARRAY_BUFFER, %u, lpd->indx, "
"GL_DYNAMIC_DRAW);\n",
(unsigned int)(lpd->indxNum * sizeof(unsigned int)));
}
return;
}
void Polydata::model(glm::mat4 mat) {
_model = mat;
glm::mat3 model3x3(_model);
// Compute the inverse transpose of upper 3x3
_model3IT = glm::transpose(glm::inverse(model3x3));
}
glm::mat4 Polydata::model() const { return _model; }
void Polydata::_init(std::string name) {
static const char me[] = "Hale::Polydata::_init";
if (name.empty()) {
std::ostringstream address;
address << (void const *)this;
_name = address.str();
} else {
_name = name;
}
if (debugging)
printf("!%s(%s): ____________________________________________ \n", me,
_name.c_str());
_colorSolid = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
_model = glm::mat4(1.0f);
_model3IT = glm::mat3(1.0f);
const limnPolyData *lpld = this->lpld();
unsigned int aa, ibits = limnPolyDataInfoBitFlag(lpld);
_buffNum = 1 + airBitsSet(ibits); /* lpld->xyzw is always set */
if (debugging)
printf("!%s: %p|%p %u buffers to set\n", me, _lpld, _lpldOwn, _buffNum);
_buff = AIR_CALLOC(_buffNum, GLuint);
if (debugging) printf("!%s: _buff = %p\n", me, _buff);
glGenBuffers(_buffNum, _buff);
if (debugging) {
printf("# glGenBuffers(%u, &); -> %u", _buffNum, _buff[0]);
for (int bi = 1; bi < _buffNum; bi++) {
printf(" %u", _buff[bi]);
}
printf("\n");
}
glGenVertexArrays(1, &_vao);
if (debugging) printf("# glGenVertexArrays(1, &); -> %u\n", _vao);
glBindVertexArray(_vao);
if (debugging) printf("# glBindVertexArray(%u);\n", _vao);
aa = 0;
glEnableVertexAttribArray(Hale::vertAttrIdxXYZW);
if (debugging) printf("# glEnableVertexAttribArray(%u);\n", Hale::vertAttrIdxXYZW);
_buffIdx[Hale::vertAttrIdxXYZW] = aa++;
for (int ii = limnPolyDataInfoUnknown + 1; ii < limnPolyDataInfoLast; ii++) {
/* HEY assumption of limnPolyDataInfo, Hale::vertAttrIdx mirroring */
int hva = ii - limnPolyDataInfoRGBA + Hale::vertAttrIdxRGBA;
if (ibits & (1 << ii)) {
glEnableVertexAttribArray(hva);
if (debugging) printf("# glEnableVertexAttribArray(%u);\n", hva);
_buffIdx[hva] = aa++;
} else {
_buffIdx[hva] = -1;
}
}
_elms = 0;
memcpy(&_lpldCopy, lpld, sizeof(limnPolyData));
_buffer(true);
return;
}
Polydata::Polydata(const limnPolyData *poly, const Program *prog, std::string name) {
_lpld = poly;
_lpldOwn = NULL;
_program = prog;
_init(name);
}
Polydata::Polydata(limnPolyData * poly, bool own, const Program *prog,
std::string name) {
if (own) {
_lpld = NULL;
_lpldOwn = poly;
} else {
_lpld = poly;
_lpldOwn = NULL;
}
_program = prog;
_init(name);
}
// to make a PolyData from something that has the parts of a limnPolyData but isn't
// one
Polydata::Polydata(
bool fpIsDouble, // following two void* are double* (else they are float *)
const void *_xyzw, // (float or double) (always allocated) vertNum position 4-tuples
const void *_norm, // (float or double) if non-NULL, vertNum (x,y,z) unit normals
uint vertNum, // logical size of _xyzw (and _norm, if it non-NULL)
uint indxNum, // there are indxNum vertex indices in indx[]
const uint *indx, /* all indices (into above arrays) for all primitives,
concatenated together into one array */
uint primNum, // there are primNum primitives (e.g. tristrips)
const uchar *type, // primitive ii (0<=i<primNum) is a type[ii] (limnPrimitive* enum)
const uint *icnt, // primitive ii (0<=i<primNum) has icnt[ii] vertex indices
const Program *prog, std::string name) {
static const char me[] = "Polydata::Polydata(from parts)";
// printf("%s: Polydata from parts!\n", me);
_lpld = NULL;
_lpldOwn = limnPolyDataNew();
uint infoBitFlag = _norm ? (1 << limnPolyDataInfoNorm) : 0;
// printf("%s: Polydata from parts! (infoBitFlag=%u)\n", me, infoBitFlag);
if (limnPolyDataAlloc(_lpldOwn, infoBitFlag, vertNum, indxNum, primNum)) {
char *err = biffGetDone(LIMN);
fprintf(stderr, "%s: problem creating own limnPolyData\n%s", me, err);
free(err);
}
if (fpIsDouble) {
float *xyzwDst = _lpldOwn->xyzw;
float *normDst = _lpldOwn->norm;
const double *xyzwSrc = (const double *)_xyzw;
const double *normSrc = (const double *)_norm;
for (uint vi = 0; vi < vertNum; vi++) {
ELL_4V_COPY(xyzwDst + 4 * vi, xyzwSrc + 4 * vi);
if (normDst) {
ELL_3V_COPY(normDst + 3 * vi, normSrc + 3 * vi);
}
}
} else {
memcpy(_lpldOwn->xyzw, _xyzw, 4 * vertNum * sizeof(float));
if (_lpldOwn->norm) {
memcpy(_lpldOwn->norm, _norm, 3 * vertNum * sizeof(float));
}
}
// for now not touching _lpldOwn->rgba
// for now not touching _lpldOwn->tex2
// for now not touching _lpldOwn->tang
memcpy(_lpldOwn->indx, indx, indxNum * sizeof(uint));
// for (uint ii = 0; ii < indxNum; ii++) _lpldOwn->indx[ii] = indx[ii];
memcpy(_lpldOwn->type, type, primNum * sizeof(uchar));
memcpy(_lpldOwn->icnt, icnt, primNum * sizeof(uint));
/*
for (uint pi = 0; pi < primNum; pi++) {
_lpldOwn->type[ii] = type[ii];
_lpldOwn->icnt[ii] = icnt[ii];
}
*/
_program = prog;
_init(name);
}
void Polydata::rebuffer() {
static const char me[] = "Polydata::rebuffer";
const limnPolyData *lpld = this->lpld();
unsigned int cbits = limnPolyDataInfoBitFlag(&_lpldCopy),
ibits = limnPolyDataInfoBitFlag(lpld);
bool newaddr;
if (cbits != ibits) {
newaddr = true;
} else {
newaddr = (_lpldCopy.xyzw != lpld->xyzw || _lpldCopy.xyzwNum != lpld->xyzwNum
|| _lpldCopy.rgba != lpld->rgba || _lpldCopy.rgbaNum != lpld->rgbaNum
|| _lpldCopy.norm != lpld->norm || _lpldCopy.normNum != lpld->normNum
|| _lpldCopy.tex2 != lpld->tex2 || _lpldCopy.tex2Num != lpld->tex2Num
|| _lpldCopy.tang != lpld->tang || _lpldCopy.tangNum != lpld->tangNum);
}
if (debugging)
printf("!%s: calling _buffer(newaddr=%s)\n", me, newaddr ? "true" : "false");
_buffer(newaddr);
memcpy(&_lpldCopy, lpld, sizeof(limnPolyData));
return;
}
Polydata::~Polydata() {
// static const char me[]="Hale::Polydata::~Polydata";
if (_lpldOwn) {
limnPolyDataNix(_lpldOwn);
}
glDeleteVertexArrays(1, &_vao);
glDeleteBuffers(1, &_elms);
glDeleteBuffers(_buffNum, _buff);
free(_buff);
}
void Polydata::colorSolid(float rr, float gg, float bb) {
_colorSolid = glm::vec4(rr, gg, bb, 1.0f);
}
void Polydata::colorSolid(glm::vec3 rgb) { _colorSolid = glm::vec4(rgb, 1.0f); }
void Polydata::colorSolid(glm::vec4 rgba) { _colorSolid = rgba; }
glm::vec4 Polydata::colorSolid() const { return _colorSolid; }
void Polydata::program(const Program *prog) {
static const std::string me = "Hale::Polydata::program";
if (!prog) {
throw std::runtime_error(me + ": got NULL program");
}
_program = prog;
}
const Program *Polydata::program() const { return _program; }
void Polydata::bounds(glm::vec3 & finalmin, glm::vec3 & finalmax) const {
const limnPolyData *lpd = lpld();
glm::vec4 wmin, wmax, wpos;
const float *_wpos = lpd->xyzw;
wmin = wmax = glm::vec4(_wpos[0], _wpos[1], _wpos[2], _wpos[3]);
for (unsigned int ii = 1; ii < lpd->xyzwNum; ii++) {
_wpos += 4;
wpos = _model * glm::vec4(_wpos[0], _wpos[1], _wpos[2], _wpos[3]);
wpos /= wpos[3];
wmin = glm::min(wmin, wpos);
wmax = glm::max(wmax, wpos);
}
finalmin = glm::vec3(wmin);
finalmax = glm::vec3(wmax);
}
void Polydata::name(std::string nm) { _name = nm; }
std::string Polydata::name() const { return _name; }
void Polydata::draw() const {
static const char me[] = "Hale::Polydata::draw";
if (debugging)
printf("!%s(%s): ____________________________________________ \n", me,
_name.c_str());
_program->use();
const limnPolyData *lpld = this->lpld();
int ibits = limnPolyDataInfoBitFlag(lpld);
if (!(ibits & (1 << limnPolyDataInfoRGBA))) {
_program->uniform("colorSolid", _colorSolid);
}
_program->uniform("modelMat", _model);
_program->uniform("model3IT", _model3IT);
/* would be nice to call this only if the values have changed;
but the Program pointer is to a const Program, so we can't easily
make this into a stateful/conditional call to uniform() */
_program->uniform("phongKa", 0.2);
_program->uniform("phongKd", 0.8);
if (debugging) printf("!%s: (done setting uniforms)\n", me);
glBindVertexArray(_vao);
if (debugging) printf("# glBindVertexArray(%u);\n", _vao);
// int offset = 0;
for (unsigned int ii = 0; ii < lpld->primNum; ii++) {
glDrawElements(Hale::limnToGLPrim(lpld->type[ii]), lpld->icnt[ii], GL_UNSIGNED_INT,
((void *)0));
if (debugging)
printf("# glDrawElements(%u, %u, GL_UNSIGNED_INT, 0);\n",
Hale::limnToGLPrim(lpld->type[ii]), lpld->icnt[ii]);
Hale::glErrorCheck(me, "glDrawElements(prim " + std::to_string(ii) + ")");
// offset += lpld->icnt[ii]; // HEY why is not used/needed?
}
return;
}
} // namespace Hale