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
376
377
378
379
380
381
382
383
384
|
// Copyright 2008 Nanorex, Inc. See LICENSE file for details.
#include "NXOpenGLRendererPlugin.h"
#include <Nanorex/Interface/NXNanoVisionResultCodes.h>
#include "GLT/glt_error.h"
#include <sstream>
using namespace std;
namespace Nanorex {
NXCommandResult NXOpenGLRendererPlugin::_s_commandResult;
NXSGOpenGLRenderable *NXOpenGLRendererPlugin::_s_canonicalSphereNode(NULL);
NXSGOpenGLRenderable *NXOpenGLRendererPlugin::_s_canonicalCylinderNode(NULL);
/// Initializes the plugin by rendering the canonical sphere and cylinder
/// Must be called after the calling engine makes its OpenGL context current
NXCommandResult* NXOpenGLRendererPlugin::initialize(void)
{
InitializeCanonicalSphereNode();
InitializeCanonicalCylinderNode();
if(_s_canonicalSphereNode == NULL || _s_canonicalCylinderNode == NULL) {
// copy static context error to instance
commandResult = _s_commandResult;
// deallocate which went through
if(_s_canonicalSphereNode != NULL) {
delete _s_canonicalSphereNode;
_s_canonicalSphereNode = NULL;
}
if(_s_canonicalCylinderNode != NULL) {
delete _s_canonicalCylinderNode;
_s_canonicalCylinderNode = NULL;
}
return &commandResult;
}
// assign a minimum ref count of 1. If all nodes that access these canonical
// nodes as parents cleanup, then these nodes will be deleted when their
// ref count becomes zero. This initial increment will ensure that the
// ref count is at least one if all other nodes behave correctly
canonicalSphereNodeGuard.addChild(_s_canonicalSphereNode);
canonicalCylinderNodeGuard.addChild(_s_canonicalCylinderNode);
if(_s_canonicalSphereNode->getRefCount() != 1 ||
_s_canonicalCylinderNode->getRefCount() != 1)
{
SetWarning(commandResult,
"Reference-counting error in plugin initialization");
}
else {
commandResult.setResult(NX_CMD_SUCCESS);
}
return &commandResult;
}
/// Cleanup
NXCommandResult* NXOpenGLRendererPlugin::cleanup(void)
{
commandResult.setResult(NX_CMD_SUCCESS);
vector<QString> message;
commandResult.setParamVector(message);
#if 0
if(_s_canonicalSphereNode != NULL) {
if(_s_canonicalSphereNode->getRefCount() != 1) {
SetWarning(commandResult,
"Reference-counting audit failed at plugin cleanup");
}
delete _s_canonicalSphereNode;
_s_canonicalSphereNode = NULL;
}
if(_s_canonicalCylinderNode != NULL) {
if(_s_canonicalCylinderNode->getRefCount() != 1) {
SetWarning(commandResult,
"Reference-counting audit failed at plugin cleanup");
}
delete _s_canonicalCylinderNode;
_s_canonicalCylinderNode = NULL;
}
#endif
return &commandResult;
}
/*static*/
void NXOpenGLRendererPlugin::InitializeCanonicalSphereNode(void)
{
// quick return if node is already created
if(_s_canonicalSphereNode != NULL)
return;
try {
_s_canonicalSphereNode = new NXSGOpenGLRenderable;
}
catch (...) {
// fail silently if unable to create for any reason
_s_canonicalSphereNode = NULL;
}
if(_s_canonicalSphereNode == NULL)
return;
bool beginRenderOK = _s_canonicalSphereNode->beginRender();
if(!beginRenderOK) {
NXCommandResult *scenegraphCtxtError = NXSGOpenGLNode::GetCommandResult();
_s_commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
_s_commandResult.setParamVector(scenegraphCtxtError->getParamVector());
delete _s_canonicalSphereNode;
_s_canonicalSphereNode = NULL;
return;
}
DrawOpenGLCanonicalSphere();
bool const ok = (_s_commandResult.getResult() == (int) NX_CMD_SUCCESS);
if(!ok) {
delete _s_canonicalSphereNode;
_s_canonicalSphereNode = NULL;
return;
}
bool endRenderOK = _s_canonicalSphereNode->endRender();
if(!endRenderOK) {
NXCommandResult *scenegraphCtxtError = NXSGOpenGLNode::GetCommandResult();
_s_commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
_s_commandResult.setParamVector(scenegraphCtxtError->getParamVector());
delete _s_canonicalSphereNode;
_s_canonicalSphereNode = NULL;
}
}
/*static*/
void NXOpenGLRendererPlugin::InitializeCanonicalCylinderNode(void)
{
// quick return if already initialized
if(_s_canonicalCylinderNode != NULL)
return;
try {
_s_canonicalCylinderNode = new NXSGOpenGLRenderable;
}
catch (...) {
// fail silently if unable to create for any reason
_s_canonicalCylinderNode = NULL;
}
// extra check for NULL before performing ops on it
if(_s_canonicalCylinderNode == NULL)
return;
bool beginRenderOK = _s_canonicalCylinderNode->beginRender();
if(!beginRenderOK) {
NXCommandResult *scenegraphCtxtError = NXSGOpenGLNode::GetCommandResult();
_s_commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
_s_commandResult.setParamVector(scenegraphCtxtError->getParamVector());
delete _s_canonicalCylinderNode;
_s_canonicalCylinderNode = NULL;
return;
}
DrawOpenGLCanonicalCylinder();
bool const ok = (_s_commandResult.getResult() == (int) NX_CMD_SUCCESS);
if(!ok) {
delete _s_canonicalCylinderNode;
_s_canonicalCylinderNode = NULL;
return;
}
bool endRenderOK = _s_canonicalCylinderNode->endRender();
if(!endRenderOK) {
NXCommandResult *scenegraphCtxtError = NXSGOpenGLNode::GetCommandResult();
_s_commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
_s_commandResult.setParamVector(scenegraphCtxtError->getParamVector());
delete _s_canonicalCylinderNode;
_s_canonicalCylinderNode = NULL;
}
}
void NXOpenGLRendererPlugin::SetError(NXCommandResult& commandResult,
char const *const errMsg)
{
commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
vector<QString> message;
message.push_back(QObject::tr(errMsg));
commandResult.setParamVector(message);
}
void NXOpenGLRendererPlugin::SetWarning(NXCommandResult& commandResult,
char const *const warnMsg)
{
commandResult.setResult(NX_PLUGIN_REPORTS_WARNING);
vector<QString> message;
message.push_back(QObject::tr(warnMsg));
commandResult.setParamVector(message);
}
/* static */
void NXOpenGLRendererPlugin::DrawOpenGLCanonicalSphere(void)
{
const double r = 1.0; /* radius */
double theta, rSinTheta, phi, sinPhi, cosPhi;
double theta2, rSinTheta2;
GLint iTheta, iPhi;
GLdouble x,y,z,z1,z2;
const int ALPHA = 5;
/* Automatic normalization of normals */
glEnable(GL_NORMALIZE);
/* Top cap - draw triangles instead of quads */
glBegin(GL_TRIANGLE_FAN);
/* Top pole */
glNormal3d(0,0,1);
glVertex3d(0,0,r);
theta = ALPHA * M_PI/180.0;
rSinTheta = r*sin(theta);
z = r*cos(theta);
for(iPhi = 0; iPhi <= 360; iPhi += ALPHA) {
phi = M_PI/180.0 * (GLdouble) iPhi;
x = rSinTheta*cos(phi);
y = rSinTheta*sin(phi);
/* normal to point on sphere is ray from center to point */
glNormal3d(x, y, z);
glVertex3d(x, y, z);
}
glEnd();
/* Sphere body - draw quad strips */
for(iTheta = ALPHA; iTheta <= 180-(2*ALPHA); iTheta += ALPHA) {
theta = M_PI/180.0 * (double) iTheta;
theta2 = M_PI/180.0 * (double) (iTheta+10);
z1 = (GLdouble) (r*cos(theta));
z2 = (GLdouble) (r*cos(theta2));
rSinTheta = r*sin(theta);
rSinTheta2 = r*sin(theta2);
glBegin(GL_QUAD_STRIP);
for(iPhi = 0; iPhi <= 360; iPhi += 10) {
phi = M_PI/180.00 * (double)(iPhi);
cosPhi = cos(phi);
sinPhi = sin(phi);
x = (GLdouble) (rSinTheta*cosPhi);
y = (GLdouble) (rSinTheta*sinPhi);
glNormal3d(x, y, z1);
glVertex3d(x, y, z1);
x = (GLdouble) (rSinTheta2*cosPhi);
y = (GLdouble) (rSinTheta2*sinPhi);
glNormal3d(x, y, z2);
glVertex3d(x, y, z2);
}
glEnd();
}
/* Bottom cap - draw triangle fan */
iTheta = 180-ALPHA;
theta = M_PI/180.0 * (GLdouble) iTheta;
z = r*cos(theta);
rSinTheta = r*sin(theta);
glBegin(GL_TRIANGLE_FAN);
/* Bottom pole */
glNormal3d(0,0,-1);
glVertex3d(0,0,-r);
for(iPhi = 0; iPhi <= 360; iPhi += ALPHA) {
phi = M_PI/180.0 * (GLdouble) iPhi;
x = rSinTheta*cos(phi);
y = rSinTheta*sin(phi);
glNormal3d(x, y, z);
glVertex3d(x, y, z);
}
glEnd();
ostringstream errMsgStream;
GLenum const err = GLERROR(errMsgStream);
if(err == GL_NO_ERROR) {
_s_commandResult.setResult(NX_CMD_SUCCESS);
}
else {
SetError(_s_commandResult,
("Error drawing openGL unit sphere"+errMsgStream.str()).c_str());
}
}
void NXOpenGLRendererPlugin::DrawOpenGLCanonicalCylinder(void)
{
int const NUM_FACETS = 72;
const double DELTA_PHI = 360.0 / (double) NUM_FACETS;
GLdouble vertex[NUM_FACETS][2]; // store (x,y) values
double x=0.0, y=0.0;
// GLdouble const z = 0.5;
// generate vertices
int iFacet = 0; // counter
double phi = 0.0;
for(iFacet=0; iFacet<NUM_FACETS; ++iFacet, phi+=DELTA_PHI) {
double const phi_rad = M_PI/180.0 * phi;
#ifdef _GNU_SOURCE
sincos(phi_rad, &y, &x);
#else
x = cos(phi_rad);
y = sin(phi_rad);
#endif
vertex[iFacet][0] = (GLdouble)(x);
vertex[iFacet][1] = (GLdouble)(y);
}
/* Automatic normalization of normals */
glEnable(GL_NORMALIZE);
/* Fill polygons */
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT, GL_FILL);
/* Top cap - draw triangles instead of quads */
glBegin(GL_TRIANGLE_FAN);
glNormal3d(0,0,1);
glVertex3d(0.0, 0.0, 1.0);
for(iFacet=0; iFacet<NUM_FACETS; ++iFacet) {
glVertex3d(vertex[iFacet][0], vertex[iFacet][1], 1.0);
}
// close top-cap
glVertex3d(vertex[0][0], vertex[0][1], 1.0);
glEnd();
/* Cylinder body - draw triangle strips */
glBegin(GL_TRIANGLE_STRIP);
for(iFacet=0; iFacet<NUM_FACETS; ++iFacet) {
glNormal3d(vertex[iFacet][0], vertex[iFacet][1], 0.0);
glVertex3d(vertex[iFacet][0], vertex[iFacet][1], 1.0);
glVertex3d(vertex[iFacet][0], vertex[iFacet][1], 0.0);
}
// close the side surface
glNormal3d(vertex[0][0], vertex[0][1], 0.0);
glVertex3d(vertex[0][0], vertex[0][1], 1.0);
glVertex3d(vertex[0][0], vertex[0][1], 0.0);
glEnd();
/* Bottom cap - draw triangle fan */
glBegin(GL_TRIANGLE_FAN);
/* Bottom pole */
glNormal3d(0,0,-1);
glVertex3d(0.0, 0.0, 0.0);
for(iFacet=0; iFacet<NUM_FACETS; ++iFacet) {
glVertex3d(vertex[iFacet][0], vertex[iFacet][1], 0.0);
}
// close bottom-cap
glVertex3d(vertex[0][0], vertex[0][1], 0.0);
glEnd();
ostringstream errMsgStream;
GLenum const err = GLERROR(errMsgStream);
if(err == GL_NO_ERROR) {
_s_commandResult.setResult(NX_CMD_SUCCESS);
}
else {
SetError(_s_commandResult,
("Error drawing openGL unit cylinder"+errMsgStream.str()).c_str());
}
}
} // Nanorex
|