-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloVrui.cpp
443 lines (396 loc) · 15.4 KB
/
HelloVrui.cpp
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#define CLIP_SCREEN 0
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <vector>
#include <Misc/FunctionCalls.h>
#include <Misc/ThrowStdErr.h>
#include <Math/Math.h>
#include <Math/Constants.h>
#include <Geometry/LinearUnit.h>
#include <GL/gl.h>
#include <GL/GLColorTemplates.h>
#include <GL/GLMatrixTemplates.h>
#include <GL/GLMaterial.h>
#include <GL/GLColorMap.h>
#include <GL/GLContextData.h>
#include <GL/GLModels.h>
#include <GL/GLGeometryWrappers.h>
#include <GL/GLExtensionManager.h>
#include <GL/Extensions/GLARBVertexBufferObject.h>
#include <GL/GLTransformationWrappers.h>
#include <GL/GLFrustum.h>
#include <GLMotif/StyleSheet.h>
#include <GLMotif/WidgetManager.h>
#include <GLMotif/Blind.h>
#include <GLMotif/Label.h>
#include <GLMotif/Button.h>
#include <GLMotif/CascadeButton.h>
#include <GLMotif/Menu.h>
#include <GLMotif/SubMenu.h>
#include <GLMotif/Popup.h>
#include <GLMotif/PopupMenu.h>
#include <GLMotif/PopupWindow.h>
#include <GLMotif/RowColumn.h>
#include <GLMotif/TextField.h>
#include <SceneGraph/NodeCreator.h>
#include <SceneGraph/GroupNode.h>
#include <SceneGraph/VRMLFile.h>
#include <SceneGraph/GLRenderState.h>
#include <Vrui/Vrui.h>
#include <Vrui/CoordinateManager.h>
#include <Vrui/Viewer.h>
#if CLIP_SCREEN
#include <Vrui/VRScreen.h>
#endif
#include <Vrui/Application.h>
#include <Vrui/SceneGraphSupport.h>
#include <Vrui/VRWindow.h>
#include "HelloVrui.h"
// VTK includes
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCubeSource.h>
#include <vtkExternalOpenGLCamera.h>
#include <vtkExternalOpenGLRenderer.h>
#include "vtkExternalOpenGLRenderWindow.h"
#include <vtkLightCollection.h>
#include <vtkLight.h>
#include <vtkMatrix4x4.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkSphereSource.h>
#include <vtkLightActor.h>
/*****************************************
Methods of class HelloVrui::DataItem:
*****************************************/
HelloVrui::DataItem::DataItem(void)
:hasVertexBufferObjectExtension(false), // GLARBVertexBufferObject::isSupported()),
surfaceVertexBufferObjectId(0),surfaceIndexBufferObjectId(0),
surfaceTextureObjectId(0),
displayListIdBase(0)
{
/* Use buffer objects for the Earth surface if supported: */
if(hasVertexBufferObjectExtension)
{
/* Initialize the vertex buffer object extension: */
GLARBVertexBufferObject::initExtension();
/* Create vertex buffer objects: */
GLuint bufferObjectIds[2];
glGenBuffersARB(2,bufferObjectIds);
surfaceVertexBufferObjectId=bufferObjectIds[0];
surfaceIndexBufferObjectId=bufferObjectIds[1];
}
/* Generate a texture object for the Earth's surface texture: */
glGenTextures(1,&surfaceTextureObjectId);
/* Generate display lists for the Earth model components: */
displayListIdBase=glGenLists(4);
}
HelloVrui::DataItem::~DataItem(void)
{
if(hasVertexBufferObjectExtension)
{
/* Delete vertex buffer objects: */
GLuint bufferObjectIds[2];
bufferObjectIds[0]=surfaceVertexBufferObjectId;
bufferObjectIds[1]=surfaceIndexBufferObjectId;
glDeleteBuffersARB(2,bufferObjectIds);
}
/* Delete the Earth surface texture object: */
glDeleteTextures(1,&surfaceTextureObjectId);
/* Delete the Earth model components display lists: */
glDeleteLists(displayListIdBase,4);
}
/*******************************
Methods of class HelloVrui:
*******************************/
GLMotif::Popup* HelloVrui::createRenderTogglesMenu(void)
{
/* Create the submenu's top-level shell: */
GLMotif::Popup* renderTogglesMenuPopup=new GLMotif::Popup("RenderTogglesMenuPopup",Vrui::getWidgetManager());
/* Create the array of render toggle buttons inside the top-level shell: */
GLMotif::SubMenu* renderTogglesMenu=new GLMotif::SubMenu("RenderTogglesMenu",renderTogglesMenuPopup,false);
/* Create a toggle button to render the Earth's surface: */
GLMotif::ToggleButton* showSurfaceToggle=new GLMotif::ToggleButton("ShowSurfaceToggle",renderTogglesMenu,"Show Surface");
showSurfaceToggle->setToggle(showSurface);
showSurfaceToggle->getValueChangedCallbacks().add(this,&HelloVrui::menuToggleSelectCallback);
/* Create a toggle button to render the Earth's surface transparently: */
GLMotif::ToggleButton* surfaceTransparentToggle=new GLMotif::ToggleButton("SurfaceTransparentToggle",renderTogglesMenu,"Surface Transparent");
surfaceTransparentToggle->setToggle(surfaceTransparent);
surfaceTransparentToggle->getValueChangedCallbacks().add(this,&HelloVrui::menuToggleSelectCallback);
/* Create a toggle button to render the lat/long grid: */
GLMotif::ToggleButton* showGridToggle=new GLMotif::ToggleButton("ShowGridToggle",renderTogglesMenu,"Show Grid");
showGridToggle->setToggle(showGrid);
showGridToggle->getValueChangedCallbacks().add(this,&HelloVrui::menuToggleSelectCallback);
/* Calculate the submenu's proper layout: */
renderTogglesMenu->manageChild();
/* Return the created top-level shell: */
return renderTogglesMenuPopup;
}
GLMotif::PopupMenu* HelloVrui::createMainMenu(void)
{
/* Create a top-level shell for the main menu: */
GLMotif::PopupMenu* mainMenuPopup=new GLMotif::PopupMenu("MainMenuPopup",Vrui::getWidgetManager());
mainMenuPopup->setTitle("Interactive Globe");
/* Create the actual menu inside the top-level shell: */
GLMotif::Menu* mainMenu=new GLMotif::Menu("MainMenu",mainMenuPopup,false);
/* Create a cascade button to show the "Rendering Modes" submenu: */
GLMotif::CascadeButton* renderTogglesCascade=new GLMotif::CascadeButton("RenderTogglesCascade",mainMenu,"Rendering Modes");
renderTogglesCascade->setPopup(createRenderTogglesMenu());
/* Create a button to reset the navigation coordinates to the default (showing the entire Earth): */
GLMotif::Button* centerDisplayButton=new GLMotif::Button("CenterDisplayButton",mainMenu,"Center Display");
centerDisplayButton->getSelectCallbacks().add(this,&HelloVrui::centerDisplayCallback);
/* Calculate the main menu's proper layout: */
mainMenu->manageChild();
/* Return the created top-level shell: */
return mainMenuPopup;
}
HelloVrui::HelloVrui(int& argc,char**& argv)
:Vrui::Application(argc,argv),
showSurface(true),surfaceTransparent(false),
surfaceMaterial(GLMaterial::Color(1.0f,1.0f,1.0f,0.333f),GLMaterial::Color(0.333f,0.333f,0.333f),10.0f),
showGrid(true),
mainMenu(0),
drawGLCube(false)
{
if(argc > 1)
{
if(std::string(argv[1]) == "--gl")
{
this->drawGLCube = true;
}
}
/* Create the user interface: */
mainMenu=createMainMenu();
Vrui::setMainMenu(mainMenu);
/* Initialize Vrui navigation transformation: */
centerDisplayCallback(0);
this->renWin =
vtkSmartPointer<vtkExternalOpenGLRenderWindow>::New();
this->ren = vtkSmartPointer<vtkExternalOpenGLRenderer>::New();
// this->cam = vtkSmartPointer<vtkExternalOpenGLCamera>::New();
// this->ren->SetActiveCamera(this->cam);
}
HelloVrui::~HelloVrui(void)
{
/* Delete the user interface: */
delete mainMenu;
}
void HelloVrui::initContext(GLContextData& contextData) const
{
/* Create a new context data item: */
DataItem* dataItem=new DataItem();
contextData.addDataItem(this,dataItem);
this->renWin->AddRenderer(this->ren.GetPointer());
vtkNew<vtkPolyDataMapper> mapper;
vtkNew<vtkPolyDataMapper> mapper1;
vtkNew<vtkActor> actor;
vtkNew<vtkActor> actor1;
actor->SetMapper(mapper.GetPointer());
actor1->SetMapper(mapper1.GetPointer());
this->ren->AddActor(actor.GetPointer());
this->ren->AddActor(actor1.GetPointer());
this->ren->AutomaticLightCreationOff();
this->ren->PreserveColorBufferOn();
this->ren->PreserveDepthBufferOn();
this->ren->RemoveAllLights();
vtkNew<vtkLight> light;
light->SetLightTypeToSceneLight();
// light->SetPositional(true);
// vtkNew<vtkLightActor> lightActor;
// lightActor->SetLight(light.GetPointer());
this->ren->AddLight(light.GetPointer());
// this->ren->AddViewProp(lightActor.GetPointer());
vtkNew<vtkCubeSource> ss;
vtkNew<vtkSphereSource> ss1;
ss1->SetRadius(1.5);
ss1->SetCenter(2,0,1);
mapper->SetInputConnection(ss->GetOutputPort());
mapper1->SetInputConnection(ss1->GetOutputPort());
}
void HelloVrui::toolCreationCallback(Vrui::ToolManager::ToolCreationCallbackData* cbData)
{
}
void HelloVrui::frame(void)
{
// this->renWin->SetSize(const_cast<int*>(Vrui::getWindow(0)->getViewportSize()));
// std::cout << (const_cast<int*>(Vrui::getWindow(0)->getViewportSize()))[0] << " " << (const_cast<int*>(Vrui::getWindow(0)->getViewportSize()))[1] <<std::endl;
#if 0 // Printing matrices
GLdouble mv[16],p[16];
glGetDoublev(GL_MODELVIEW_MATRIX,mv);
glGetDoublev(GL_PROJECTION_MATRIX,p);
std::cout << "Frame matrices " << std::endl;
std::cout << "VRUI MV:" << std::endl;
for(int i=0;i<4;++i)
{
for(int j=0;j<4;++j)
std::cout<<" "<<std::setw(12)<<mv[i+j*4];
std::cout << std::endl;
}
std::cout << std::endl << "VRUI P:" << std::endl;
// std::cout<<" ";
for(int i=0;i<4;++i)
{
for(int j=0;j<4;++j)
std::cout<<" "<<std::setw(12)<<p[i+j*4];
std::cout<<std::endl;
}
std::cout<<std::endl;
#endif
}
void HelloVrui::display(GLContextData& contextData) const
{
/* Print the modelview and projection matrices: */
// GLdouble mv[16],p[16];
// glGetDoublev(GL_MODELVIEW_MATRIX,mv);
// glGetDoublev(GL_PROJECTION_MATRIX,p);
#if 0 // Printing matrices
std::cout << "Display matrices " << std::endl;
std::cout << "VRUI MV:" << std::endl;
for(int i=0;i<4;++i)
{
for(int j=0;j<4;++j)
std::cout<<" "<<std::setw(12)<<mv[i+j*4];
std::cout << std::endl;
}
std::cout << std::endl << "VRUI P:" << std::endl;
// std::cout<<" ";
for(int i=0;i<4;++i)
{
for(int j=0;j<4;++j)
std::cout<<" "<<std::setw(12)<<p[i+j*4];
std::cout<<std::endl;
}
std::cout<<std::endl;
#endif
/* Save OpenGL state: */
glPushAttrib(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ENABLE_BIT|GL_LIGHTING_BIT|GL_POLYGON_BIT);
if(this->drawGLCube)
{
drawCube();
}
else
{
/* Directly set the projection and model view matrices for the camera
* This syncs up the VRUI and VTK camera
* NOTE: Since, VTK takes a transpose of the matrices internally
* before rendering, the matrices are transposed here first and then set
* on the camera to nullify the transpose.
*/
// double p1[16], mv1[16];
// this->transposeMatrix4x4(p,p1);
// this->cam->SetProjectionTransformMatrix(p);
// this->transposeMatrix4x4(mv,mv1);
// this->cam->SetViewTransformMatrix(mv);
/* Render the scene */
renWin->Render();
/* It is observed that in the VRUI coordinate space, VTK's +Z axis
* becomes the -Y axis. This causes, the light to always be at an
* inverse position to the camera. To counter-act this association, the
* light's transform matrix is set to an inverse of the camera's model
* view matrix.
*/
// vtkMatrix4x4* mat = 0;
// mat->DeepCopy(this->cam->GetModelViewTransformMatrix());
// if(!mat)
// {
// std::cout << "mat is null" << std::endl;
// }
// mat->Invert();
// vtkLightCollection* lC = this->ren->GetLights();
// lC->InitTraversal();
// vtkLight* light = lC->GetNextItem();
// light->SetTransformMatrix(mat);
}
glPopAttrib();
}
void HelloVrui::transposeMatrix4x4(double* inArr, double* outArr) const
{
for(int i = 0 ; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
outArr[i*4+j] = inArr[j*4+i];
outArr[j*4+i] = inArr[i*4+j];
}
}
}
void HelloVrui::drawCube(void) const
{
glBegin(GL_QUADS); // Draw The Cube Using quads
glColor3f(0.0f,1.0f,0.0f); // Color Blue
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3f(1.0f,0.5f,0.0f); // Color Orange
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f,0.0f,0.0f); // Color Red
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3f(1.0f,1.0f,0.0f); // Color Yellow
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glColor3f(0.0f,0.0f,1.0f); // Color Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3f(1.0f,0.0f,1.0f); // Color Violet
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd(); // End Drawing The Cube
}
void HelloVrui::menuToggleSelectCallback(GLMotif::ToggleButton::ValueChangedCallbackData* cbData)
{
/* Adjust program state based on which toggle button changed state: */
if(strcmp(cbData->toggle->getName(),"ShowSurfaceToggle")==0)
showSurface=cbData->set;
else if(strcmp(cbData->toggle->getName(),"SurfaceTransparentToggle")==0)
surfaceTransparent=cbData->set;
else if(strcmp(cbData->toggle->getName(),"ShowGridToggle")==0)
showGrid=cbData->set;
}
void HelloVrui::centerDisplayCallback(Misc::CallbackData* cbData)
{
Vrui::NavTransform nav=Vrui::NavTransform::identity;
nav*=Vrui::NavTransform::translateFromOriginTo(Vrui::getDisplayCenter());
nav*=Vrui::NavTransform::rotate(Vrui::Rotation::rotateFromTo(Vrui::Vector(0,0,1),Vrui::getUpDirection()));
nav*=Vrui::NavTransform::scale(Vrui::Scalar(8)*Vrui::getInchFactor()/Vrui::Scalar(2.0));
Vrui::setNavigationTransformation(nav);
}
/* Create and execute an application object: */
/*
* main - The application main method.
*
* parameter argc - int
* parameter argv - char**
*
* Calling the application with --gl as the argument draws the GL cube and there
* is no VTK.
*/
int main(int argc, char* argv[]) {
try {
char** applicationDefaults = 0;
HelloVrui application(argc, argv);
application.run();
return 0;
} catch (std::runtime_error e) {
std::cerr << "Error: Exception " << e.what() << "!" << std::endl;
return 1;
}
}