-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpsviewer.cpp
3082 lines (2419 loc) · 124 KB
/
cpsviewer.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cpsviewer.h"
#include "pandora.h"
#include <OpenGL/glu.h>
#include <QGLFormat>
#include <QtGui>
#include <QtOpenGL>
#include <QGLBuffer>
#include <qmath.h>
#include <QDropEvent>
#include <QKeyEvent>
// Constructor
CPSviewer::CPSviewer(QWidget *parent) :
QGLWidget(parent)
{
setAutoFillBackground(false);
// Set the widget to Double Buffer mode. This will be needed for animation and efficient realtime rendering.
// This is technically not a OpenGL command because it depends on the windowing context (QGLWidget), but it has an important effect on all the other OpenGL commands.
setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer | QGL::Rgba | QGL::SampleBuffers));
// Sets up the first component of the vector prometheusModel as the active model for display.
activeModel = 0;
OpenGLMethod = 0;
// Set intital values for the camera's position for the viewer.
// Initialize with the Identity Quaternion to describe the camera's starting rotation.
accumulatedRotation.setVector(0, 0, 0);
accumulatedRotation.setScalar(1);
isStartStored = false;
xTrans = 0;
yTrans = 0;
zTrans = -20.0;
// Set up the viewer to accept files dropped on it.
setAcceptDrops(true);
// Determines the projection method for the viewer... 0 = Perspective and 1 = Orthogonal.
projectionMethod = 0;
// Initialize the flags that let the viewer know which tools the user wishes to use in the viewer.
viewTool = true;
selectTool = false;
atomTool = false;
deleteTool = false;
measureTool = false;
incrementBOTool = false;
decrementBOTool = false;
// Initialize the size of the Selection Buffer (see below for use with Tools).
selectionBufferSize = 512;
//selectRenderMode = false;
controlIsPressed = false;
doubleClick = false;
controlAll = false;
// These variables control atom placement for the Build Tool.
builderAtom = 6; // Initializes atomic number 6, Carbon.
// Initialize lable settings.
requestedLabelStyle = 0; // Default value requests no labels be drawn.
bitmapMode = false;
bitmapWidth = 1;
bitmapHeight = 1;
}
// Empty destructor
CPSviewer::~CPSviewer() {}
// Set minimum size of the viewer so that it's displayed reasonably.
QSize CPSviewer::minimumSizeHint() const
{
return QSize(200, 200);
}
// Set initial size of the viewer.
QSize CPSviewer::sizeHint() const
{
return QSize(600, 600);
}
// Initialize OpenGL. Quality settings and fixed-pipline lights are declared here.
void CPSviewer::initializeGL()
{
glShadeModel(GL_SMOOTH); // Enable smooth shading
qglClearColor(Qt::black); // Set the clear color to a black background
glClearDepth(1.0f); // Depth buffer setup
glEnable(GL_DEPTH_TEST); // Enable depth testing. This tells the GPU to perform hidden surface calculations for you.
glDepthFunc(GL_LEQUAL); // Set type of depth test
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really nice perspective calculations
// Blending breaks jaggie fragments and smears out the color more smoothly over an area.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// OpenGL can speed calculations by avoiding drawing surfaces that are hidden from the user's point of view.
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// Calls to OpenGL that set up the lighting.
glEnable(GL_LIGHTING);
GLfloat lightPos[] = {5, 5, 0.5, 0.0}; // This sets the light position as shining from the upper right hand side of the screen.
// The last zero in the lightPos vector specifies that it's a directional light. It is not a real light with a location,
// but a beam coming in from infinity.
GLfloat ambientLight[] = {0.3, 0.3, 0.3, 1.0}; // Medium ambient light (white in color).
GLfloat diffuseLight[] = {0.7, 0.7, 0.7, 1.0}; // Farily strong directed light (white in color).
GLfloat specularLight[] = {1.0, 1.0, 1.0, 1.0}; // Strong, specular light source (white in color).
// Tell OpenGL about the material we are lighting.
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
GLfloat specularReflectance[] = {1.0, 1.0, 1.0, 1.0}; // High specular reflectance.
glMaterialfv(GL_FRONT, GL_SPECULAR, specularReflectance);
glMateriali(GL_FRONT,GL_SHININESS,128); // Focuses the specular effects to a small area.
glLightfv(GL_LIGHT0,GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
//glEnable(GL_LIGHT0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if (OpenGLMethod == 2 | OpenGLMethod == 0)
{
// We have access to Vertex and Fragment Shaders... while the drawing must occur on the CPU, we can still leverage the GPU's abilities
// to improve quality and efficiency.
// Set up shaders under the GLSL.
QGLShader vShader (QGLShader::Vertex);
vShader.compileSourceFile(":/Shaders/vertexShader.vert");
QGLShader fShader (QGLShader::Fragment);
fShader.compileSourceFile(":/Shaders/fragmentShader.frag");
// Now create a shader program and add the shaders to it.
QGLShaderProgram cpsProgram(this);
cpsProgram.addShader(&vShader);
cpsProgram.addShader(&fShader);
// Link the various shaders together.
cpsProgram.link();
// And finally, bind the shader program to the QGLWidget context.
cpsProgram.bind();
}
else if (OpenGLMethod == 3)
{
// We have access to Vertex, Fragment, and, Geometry Shaders... so we may use some of the most advanced methods for drawing.
// Set up shaders under the GLSL.
QGLShader vShader (QGLShader::Vertex);
vShader.compileSourceFile(":/Shaders/vertexShader.vert");
QGLShader fShader (QGLShader::Fragment);
fShader.compileSourceFile(":/Shaders/fragmentShader.frag");
QGLShader gShader (QGLShader::Geometry);
gShader.compileSourceFile(":/Shaders/geometryShader.geom");
// Now create a shader program and add the shaders to it.
QGLShaderProgram cpsProgram(this);
cpsProgram.addShader(&vShader);
cpsProgram.addShader(&fShader);
cpsProgram.addShader(&gShader);
// Link the various shaders together.
cpsProgram.link();
// And finally, bind the shader program to the QGLWidget context.
cpsProgram.bind();
}
}
// This is called when the OpenGL window is resized. It handles redrawing the window,
// so we can resize the window without worry.
void CPSviewer::resizeGL(int width, int height)
{
// Prevent divide by zero (in the gluPerspective call)
if (height == 0)
height = 1;
glViewport(0,0, width, height);
}
// OpenGL painting code goes here.
void CPSviewer::paintGL()
{
float angle;
// First off, we have to decide how the model will be displayed. This integer will inform this function's drawing method.
// 1 = Ball-and-Stick, 2 = Dreiding (Licorice), 3 = Space Filling (CPK), 4 = Vector (Wireframe), 5 = Vector with Points
representationMethod = prometheusModel.getDrawingStyle();
// Pass this instruction to the shaders via a uniform variable.
glUniform1i(0, representationMethod);
glMatrixMode(GL_PROJECTION); // Select projection matrix
glLoadIdentity(); // Reset projection matrix
float aspect = this->width() / this->height();
if (projectionMethod == 0)
{
// The User desires a view using a perspective projection.
//gluPerspective(45.0f, this->width()/this->height(), 0.1f, 100.0f);
gluPerspective(45, aspect, 0.1, 1000);
}
else if (projectionMethod == 1)
{
// The User desires a view using an orthogonal projection.
glOrtho(-zTrans, zTrans, -zTrans*aspect, zTrans*aspect, -100, 100);
}
glMatrixMode(GL_MODELVIEW); // Select modelview matrix
glLoadIdentity(); // Reset modelview matrix
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer
glLoadIdentity(); // Reset current modelview matrix
// This line handles where you've dragged / zoomed the model.
// xTrans and yTrans are assigned from dragging functions.
// zTrans is assigned from the Zoom function.
if (projectionMethod == 0)
{
// In perspective, all three translations are model transformations.
glTranslatef(xTrans, yTrans, zTrans);
}
else
{
// In orthogonal, only x and y translations are model transformations... "zoom" is achieved by adjusting the viewers clipping planes.
glTranslatef(-xTrans, -yTrans, 0);
}
// Rotation... We ask OpenGL to rotate the model using the function glRotatef. The paramters are angle of rotation in degrees, and the x-y-z components of a vector describing
// the axis of rotation. These values are stored within the quaternion "accumulatedRotation". See the function "setRotation()" below for its calculation.
// Quaternions hold cos(angle / 2). In order to retrieve the angle in degrees, we multiply the conversion factor by 2.
// 2 * 180 / 3.14159 = 114.592750
angle = qAcos(accumulatedRotation.scalar()) * 114.592750;
glRotatef(angle, accumulatedRotation.x(), accumulatedRotation.y(), accumulatedRotation.z());
if (!bitmapMode)
{
if (OpenGLMethod == 0)
{
// This is basic calls using the glBegin() and glEnd() - fixed function paradigm... very old and very inefficient. OpenGL before v1.5
// Now that the view and orientation have been established, the OpenGL commands to draw the model are contained within this function.
drawModel();
/***************************************************************************************************************************************************************/
// Now that drawing is complete, what happens next is up to the User. The View Tool merely displays the model, so we wouldn't have to do any more in that case.
// If the User is using some other tool, we'll have more work to do.
/***************************************************************************************************************************************************************/
if (!SelectedAtoms.isEmpty() | !SelectedBonds.isEmpty())
{
// This informs the fragment shader that we wish for diffuse lighting on the selection "surface", but we do not want specular highlights.
// -1 is a special flag that shouldn't change, even if other representation methods are added to Prometheus.
glUniform1i(0, -1);
drawSelectedModel();
}
if (selectTool)
{
if (!singleSelect)
{
// If the User has triggered bulk selection, this function draws a rectangle representing the selection area.
// -2 is a special flag that shouldn't change even if other representation methods are added to Prometheus.
glUniform1i(0, -2);
drawSelectionBox(initialPos, finalPos);
}
}
// Draw any requested labels...
drawLabels();
}
else if (OpenGLMethod == 1)
{
// This features buffer objects, first introduced in OpenGL v1.5. All vertices are generated on the CPU, but it happens as infrequently as possible, and the results
// are stored for frquent drawing. Rotation and Translation occur on the CPU. Lighting is processed on the CPU. Applied for OpenGL v1.5 to v3.0
glColor3f(1, 0, 0);
#define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes))
GLuint buffers[2];
GLfloat vertices[][3] = {
{ -2.0, -2.0, -2.0 },
{ 2.0, -2.0, -2.0 },
{ 2.0, 2.0, -2.0 },
{ -2.0, 2.0, -2.0 },
{ -2.0, -2.0, 2.0 },
{ 2.0, -2.0, 2.0 },
{ 2.0, 2.0, 2.0 },
{ -2.0, 2.0, 2.0 }
};
GLubyte indices[] = {
0, 1, 2, 3,
4, 7, 6, 5,
0, 4, 5, 1,
3, 2, 6, 7,
0, 3, 7, 4,
1, 5, 6, 2
};
// Get numbers to ID our various buffers and store them in the array buffers
glGenBuffers(2, buffers);
// Start with our first buffer and bind (enable) it so we can give it data.
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
// Feed the data from the array vertices into the buffer object.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Because touching surfaces share vertices, we don't want OpenGL to draw them more than once... we tell OpenGL which vertices are
// part of the same surface by using indices. We bind and load the data, just as before.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// This buffer object will be rendered with glDrawElements... but first we have tell OpenGL what this data is and prepare the "client" for drawing.
glEnableClientState(GL_VERTEX_ARRAY);
// And how to iterate through it with this pointer.
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
// And now we can draw... let OpenGL know what we want to draw, and from where to pull the
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
}
else if (OpenGLMethod == 2)
{
// This is a hybrid method. All vertices are still generated on the CPU, but we make use of programmable vertex and fragment shaders.
// Rotation, translation, and lighting (Phong) all occur on the GPU. Applied for OpenGL v3.0 to v3.2
}
else if (OpenGLMethod == 3)
{
// This is the most advanced method and fastest. The GPU is passed a set of atomic coordinates, and makes use of programmable vertex, fragment, and geometry shaders...
// The GPU is able to draw the needed shapes, rotate and translate them into position, and apply Phong Lighting.
// Only for GPUs and implmentations supporting OpenGL beyond v3.2
}
}
else
{
//drawBitmapGrid();
}
}
//********************************************************************************************
// Functions in this section deal with drawing routines for the various model representations.
//*********************************************************************************************
void CPSviewer::drawModel()
{
// From here on, we need call OpenGL commands based on the representation method we're going after.
// 1 = Ball-and-Stick
// 2 = Licorice (Dreiding)
// 3 = Space Filling (CPK)
// 4 = Vector (Wireframe)
// 5 = Vector with Points
if (representationMethod == 0)
{
drawBallandStickRepresentation();
}
else if (representationMethod == 1)
{
drawDreidingRepresentation();
}
else if (representationMethod == 2)
{
drawSpaceFillingRepresentation();
}
else if (representationMethod == 3)
{
drawVectorRepresentation();
}
else if (representationMethod == 4)
{
drawEnhancedVectorRepresentation();
}
}
void CPSviewer::drawBallandStickRepresentation()
{
// Ball-and-Stick Model... A favorite staple of chemistry. We're going to render a lot of spheres and cylinders, so we're going to rely on the handy routines provided by the
// Graphics Library Utility (glu) to efficiently draw the model.
///////////////////////////////////////////////////////////////////////////
// SETUP
///////////////////////////////////////////////////////////////////////////
// Enable anti-aliasing techniques to improve image quality. For the Ball-and-Stick Representation, we need
// to make sure OpenGL uses techniques designed for 3D surfaces because the intersection of solids looks really
// bad, even when a lot of polygons are used. We can turn off anti-aliasing methods for lines and points for this representation.
if (glIsEnabled(GL_POINT_SMOOTH))
{
glDisable(GL_POINT_SMOOTH);
}
if (glIsEnabled(GL_LINE_SMOOTH))
{
glDisable(GL_LINE_SMOOTH);
}
glEnable(GL_MULTISAMPLE);
glHint(GL_MULTISAMPLE, GL_NICEST);
GLUquadricObj *sphere, *cylinder; // Quadric Object Pointers
sphere = gluNewQuadric(); // initialize objects for the glu routine
cylinder = gluNewQuadric();
// Tell OpenGL about the normals we'd like to use for vertex shading. We'd like OpenGL to smoothly interpolate the colors between vertices.
// This will help hide the polygon faces that approximate the curved surfaces. That means we can use bigger polygons to improve performace without sacrificing quality.
gluQuadricNormals(sphere, GLU_SMOOTH);
gluQuadricNormals(cylinder, GLU_SMOOTH);
//////////////////////////////////////////////////////////////////////////
// Drawing
//////////////////////////////////////////////////////////////////////////
// Begin drawing routine. We will begin by drawing the balls (atoms).
/* The Name Stack commands are only executed during Selection Mode. This code is ignored if we are just drawing the model.
When we are done drawing, this function is executed once more, and we ask OpenGL to track the shapes it draws and tell us which ones
are in a specific area (the ones the User is clicking on). */
// Cycle through the atoms in the Atlas Framework.
for (int i = 0; i < PosX.size(); ++i)
{
// Name this object for OpenGL's Selection Mode. Everything draw until this loop completes will be considered one object by OpenGL's selection
// mode. If any part of this object is within the viewing volume, it will return a hit to the selection buffer. This command is only valid
// during Selection Mode, otherwise it is ignored.
glLoadName(i + 1);
// Set the drawing to this color.
glColor3f(Colors[i][0], Colors[i][1], Colors[i][2]);
glPushMatrix();
// The glu routine renders spheres at the origin as a default, so we need to ensure we center it on the atom's coordinates.
glTranslatef(PosX[i], PosY[i], PosZ[i]);
// Call the glu routine to render the sphere.
// The parameters are: object_name, radius, # of slices, # of stacks
gluSphere(sphere, Radii[i]*0.25, 24, 12);
// Move back to the origin, so the next call will place the atom at the correct location.
glPopMatrix();
}
// We can move on to drawing the bonds.
// Cycle through the bonds listed in the Atlas Framework.
// We specify that the bonds are all composed of a neutral color. Here "Wheat" is specified.
glColor3f(0.96078, 0.870588, 0.7019607);
for (int i = 0; i < BondList.size(); ++i)
{
// Name this object for OpenGL's Selection Mode. Everything draw until this loop completes will be considered one object by OpenGL's selection
// mode. If any part of this object is within the viewing volume, it will return a hit to the selection buffer. This command is only valid
// during Selection Mode, otherwise it is ignored.
glLoadName(i + PosX.size() + 1);
if (!selectRenderMode)
{
if (BondOrders[i] == 1)
{
// Single Bond
glPushMatrix();
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][0] - 1], PosY[BondList[i][0] - 1], PosZ[BondList[i][0] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2], BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.1, 0.1, BondLengths[i], 10, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
}
else if (BondOrders[i] == 2)
{
// Double Bond... We draw two skinny cylinder next to one another.
glPushMatrix();
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][0] - 1] + 0.1, PosY[BondList[i][0] - 1] + 0.1, PosZ[BondList[i][0] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2], BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.075, 0.075, BondLengths[i], 10, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
glPushMatrix();
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][0] - 1] - 0.1, PosY[BondList[i][0] - 1] - 0.1, PosZ[BondList[i][0] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2], BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.075, 0.075, BondLengths[i], 10, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
}
else if (BondOrders[i] == 3)
{
// Triple Bond... We draw three skinny cylinders next to each other.
glPushMatrix();
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][0] - 1] + 0.15, PosY[BondList[i][0] - 1] + 0.15, PosZ[BondList[i][0] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2], BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.06, 0.06, BondLengths[i], 10, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
glPushMatrix();
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][0] - 1], PosY[BondList[i][0] - 1], PosZ[BondList[i][0] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2], BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.06, 0.06, BondLengths[i], 10, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
glPushMatrix();
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][0] - 1] - 0.15, PosY[BondList[i][0] - 1] - 0.15, PosZ[BondList[i][0] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2], BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.06, 0.06, BondLengths[i], 10, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
}
}
else
{
glPushMatrix();
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][0] - 1], PosY[BondList[i][0] - 1], PosZ[BondList[i][0] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2], BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.1, 0.1, BondLengths[i], 10, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
}
}
}
void CPSviewer::drawDreidingRepresentation()
{
// Dreiding Model... Prefered method among the biological sciences. We're going to render a lot of spheres and cylinders, so we're going to rely on the handy routines provided by the
// Graphics Library Utility (glu) to efficiently draw the model.
///////////////////////////////////////////////////////////////////////////
// SETUP
///////////////////////////////////////////////////////////////////////////
// Enable anti-aliasing techniques to improve image quality. For the Ball-and-Stick Representation, we need
// to make sure OpenGL uses techniques designed for 3D surfaces because the intersection of solids looks really
// bad, even when a lot of polygons are used. We can turn off anti-aliasing methods for lines and points for this representation.
if (glIsEnabled(GL_POINT_SMOOTH))
{
glDisable(GL_POINT_SMOOTH);
}
if (glIsEnabled(GL_LINE_SMOOTH))
{
glDisable(GL_LINE_SMOOTH);
}
glEnable(GL_MULTISAMPLE);
glHint(GL_MULTISAMPLE, GL_NICEST);
GLUquadricObj *sphere, *cylinder; // Quadric Object Pointers
sphere = gluNewQuadric(); // initialize objects for the glu routine
cylinder = gluNewQuadric();
// Tell OpenGL about the normals we'd like to use for vertex shading. We'd like OpenGL to smoothly interpolate the colors between vertices.
// This will help hide the polygon faces that approximate the curved surfaces. That means we can use bigger polygons to improve performace without sacrificing quality.
gluQuadricNormals(sphere, GLU_SMOOTH);
gluQuadricNormals(cylinder, GLU_SMOOTH);
//////////////////////////////////////////////////////////////////////////
// Drawing
//////////////////////////////////////////////////////////////////////////
/* The Name Stack commands are only executed during Selection Mode. This code is ignored if we are just drawing the model.
When we are done drawing, this function is executed once more, and we ask OpenGL to track the shapes it draws and tell us which ones
are in a specific area (the ones the User is clicking on). */
// Begin drawing routine. We will begin by drawing the balls (atoms).
// Cycle through the atoms in the Atlas Framework.
for (int i = 0; i < PosX.size(); ++i)
{
// Save current location so we can undo our transformations.
glPushMatrix();
// Set the drawing to this color.
glColor3f(Colors[i][0], Colors[i][1], Colors[i][2]);
// The glu routine renders spheres at the origin as a default, so we need to ensure we center it on the atom's coordinates.
glTranslatef(PosX[i], PosY[i], PosZ[i]);
// Call the glu routine to render the sphere.
// The parameters are: object_name, radius, # of slices, # of stacks
gluSphere(sphere, 0.2, 24, 12);
// Move back to the origin, so the next call will place the atom at the correct location.
glPopMatrix();
}
// We can move on to drawing the bonds.
// Cycle through the bonds listed in the Atlas Framework.
for (int i = 0; i < BondList.size(); ++i)
{
glPushMatrix();
// We specify that the bonds are colored to the midpoint according to the atom being represented.
// Set the color to the first atom.
glColor3f(Colors[BondList[i][0] - 1][0], Colors[BondList[i][0] - 1][1], Colors[BondList[i][0] - 1][2]);
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][0] - 1], PosY[BondList[i][0] - 1], PosZ[BondList[i][0] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2], BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.2, 0.2, BondLengths[i] / 2, 24, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
// Now draw the second half of the bond.
glPushMatrix();
// Set the color to the second atom.
glColor3f(Colors[BondList[i][1] - 1][0], Colors[BondList[i][1] - 1][1], Colors[BondList[i][1] - 1][2]);
// Cylinders are drawn at the origin and radiating toward the positive z-axis by default. Before drawing the bond, we have to move to the correct
// location before proceeding.
glTranslatef(PosX[BondList[i][1] - 1], PosY[BondList[i][1] - 1], PosZ[BondList[i][1] - 1]);
// We now rotate our orentation to align the z-axis with the bond vector.
glRotatef(BondNormals[i][2] + 180, BondNormals[i][0], BondNormals[i][1], 0);
// Now we draw the cylinder representing the bond by calling the glu routine.
// The parameters are: object_name, radius at bottom, radius at top, length of cylinder, # of slices, # of stacks
gluCylinder(cylinder, 0.2, 0.2, BondLengths[i] / 2, 24, 5);
// Move back to the origin and starting orientation, so the next call will function properly.
glPopMatrix();
}
}
void CPSviewer::drawSpaceFillingRepresentation()
{
// Space-Filling Model... We're going to need a lot of spheres. We're going to rely on the handy routines provided by the
// Graphics Library Utility (glu) to efficiently draw the model.
///////////////////////////////////////////////////////////////////////////
// SETUP
///////////////////////////////////////////////////////////////////////////
// Enable anti-aliasing techniques to improve image quality. For the Space Filling Representation, we need
// to make sure OpenGL uses techniques designed for 3D surfaces because the intersection of spheres looks really
// bad, even when a lot of polygons are used. We can turn off anti-aliasing methods for lines and points for this representation.
if (glIsEnabled(GL_POINT_SMOOTH))
{
glDisable(GL_POINT_SMOOTH);
}
if (glIsEnabled(GL_LINE_SMOOTH))
{
glDisable(GL_LINE_SMOOTH);
}
glEnable(GL_MULTISAMPLE);
glHint(GL_MULTISAMPLE, GL_NICEST);
GLUquadricObj *sphere; // Quadric Object Pointer
sphere = gluNewQuadric(); // initialize object for the glu routine
// Tell OpenGL about the normals we'd like to use for vertex shading. We'd like OpenGL to smoothly interpolate the colors between vertices.
// This will help hide the polygon faces that approximate the spheres. That means we can use bigger polygons to improve performace without sacrificing quality.
gluQuadricNormals(sphere, GLU_SMOOTH);
//////////////////////////////////////////////////////////////////////////
// Drawing
//////////////////////////////////////////////////////////////////////////
/* The Name Stack commands are only executed during Selection Mode. This code is ignored if we are just drawing the model.
When we are done drawing, this function is executed once more, and we ask OpenGL to track the shapes it draws and tell us which ones
are in a specific area (the ones the User is clicking on). */
// Begin drawing routine. Cycle through the atoms in the Atlas Framework.
for (int i = 0; i < PosX.size(); ++i)
{
// Name this object for OpenGL's Selection Mode. Everything draw until this loop completes will be considered one object by OpenGL's selection
// mode. If any part of this object is within the viewing volume, it will return a hit to the selection buffer. This command is only valid
// during Selection Mode, otherwise it is ignored.
glLoadName(i + 1);
// Save current location so we can undo our transformations.
glPushMatrix();
// Set the drawing to this color.
glColor3f(Colors[i][0], Colors[i][1], Colors[i][2]);
// The glu routine renders spheres at the origin as a default, so we need to ensure we center it on the atom's coordinates.
glTranslatef(PosX[i], PosY[i], PosZ[i]);
// Call the glu routine to render the sphere.
// The parameters are: object_name, radius, # of slices, # of stacks
gluSphere(sphere, Radii[i], 48, 24);
// Move back to the origin, so the next call will place the atom at the correct location.
glPopMatrix();
}
}
void CPSviewer::drawVectorRepresentation()
{
// Vector Representation... Lines represent the bonded atoms. The lines are painted to their center to show more clear
// demarcation of atom types.
///////////////////////////////////////////////////////////////////////////
// SETUP
///////////////////////////////////////////////////////////////////////////
glLineWidth(1.0);
// Enable anti-aliasing techniques to improve image quality. For the Vector Representation, we need
// to make sure OpenGL uses techniques designed for Lines because unaltered lines are rough to look at.
// We need to turn off Multi-sampling, an anti-aliasing method for solid surfaces, because it inhibits calls to line and point AA,
// and it is more expensive than the methods we'd like to use.
if (glIsEnabled(GL_MULTISAMPLE))
{
glDisable(GL_MULTISAMPLE);
}
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH, GL_NICEST);
//////////////////////////////////////////////////////////////////////////
// Drawing
//////////////////////////////////////////////////////////////////////////
// Loop over the vector that holds the paired atoms and draw lines between them.
// bondData holds the atom ID numbers, and their positions are contained within posX, posY, and posZ.
glBegin(GL_LINES); // Enter Drawing Routine.
for (int i = 0; i < BondList.size(); ++i)
{
// It is entered compactly below, and without holder variables, but the ends of the line are given as the
// positions of the atoms making the bonded pair. [PosX, PosY, PosZ]. The integer given as an index for
// PosX (or PosY or PosZ) is the integer atom ID stored within bondList.
glColor3f(Colors[BondList[i][0] - 1][0], Colors[BondList[i][0] - 1][1], Colors[BondList[i][0] - 1][2]); // First atom's color is set.
glVertex3f(PosX[BondList[i][0] - 1], PosY[BondList[i][0] - 1], PosZ[BondList[i][0] - 1]); // First Atom's Position
glVertex3f(BondCenters[i][0], BondCenters[i][1], BondCenters[i][2]); // Bond Center
glColor3f(Colors[BondList[i][1] - 1][0], Colors[BondList[i][1] - 1][1], Colors[BondList[i][1] - 1][2]); // Second atom's color is set.
glVertex3f(BondCenters[i][0], BondCenters[i][1], BondCenters[i][2]); // Bond Center
glVertex3f(PosX[BondList[i][1] - 1], PosY[BondList[i][1] - 1], PosZ[BondList[i][1] - 1]); // Second Atom's Position
}
glEnd(); // End Drawing Routine.
}
void CPSviewer::drawEnhancedVectorRepresentation()
{
// "Enhanced" Vector Representation... historically and techincally speaking, vetor representation should only include the lines
// that represent the bonded atoms. This has a little more flair since the atom coordinates are explicitly represented with points and
// not just line junctions.
///////////////////////////////////////////////////////////////////////////
// SETUP
///////////////////////////////////////////////////////////////////////////
glPointSize(5.0); // Sets the dot size of the atom
glLineWidth(1.0);
// Enable anti-aliasing techniques to improve image quality. For the Enhanced Vector Representation, we need
// to make sure OpenGL uses techniques designed for Lines and Points because unaltered lines are rough to look at, and points come out as squares.
// We need to turn off Multi-sampling, an anti-aliasing method for solid surfaces, because it inhibits calls to line and point AA,
// and it is more expensive than the methods we'd like to use here.
if (glIsEnabled(GL_MULTISAMPLE))
{
glDisable(GL_MULTISAMPLE);
}
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH, GL_NICEST);
//////////////////////////////////////////////////////////////////////////
// Drawing
//////////////////////////////////////////////////////////////////////////
// Begin drawing routine. Cycle through the atoms in the Atlas Framework. One point will be placed at every vertex listed between here and "glEnd"
glBegin(GL_POINTS);
for (int i = 0; i < PosX.size(); ++i)
{
// Set the color for drawing.
glColor3f(Colors[i][0], Colors[i][1], Colors[i][2]);
// Draw vertex.
glVertex3f(PosX[i], PosY[i], PosZ[i]);
}
glEnd(); // End drawing atoms.
// Now, we move on to bond vectors. Loop over the vector that holds the paired atoms and draw lines between them.
// bondData holds the atom ID numbers, and their positions are contained within posX, posY, and posZ.
glBegin(GL_LINES); // Enter Drawing Routine.
for (int i = 0; i < BondList.size(); ++i)
{
// It is entered compactly below, and without holder variables, but the ends of the line are given as the
// positions of the atoms making the bonded pair. [PosX, PosY, PosZ]. The integer given as an index for
// PosX (or PosY or PosZ) is the integer atom ID stored within bondList.
glColor3f(Colors[BondList[i][0] - 1][0], Colors[BondList[i][0] - 1][1], Colors[BondList[i][0] - 1][2]); // First atom's color is set.
glVertex3f(PosX[BondList[i][0] - 1], PosY[BondList[i][0] - 1], PosZ[BondList[i][0] - 1]); // First Atom's Position
glVertex3f(BondCenters[i][0], BondCenters[i][1], BondCenters[i][2]); // Bond Center
glColor3f(Colors[BondList[i][1] - 1][0], Colors[BondList[i][1] - 1][1], Colors[BondList[i][1] - 1][2]); // Second atom's color is set.
glVertex3f(BondCenters[i][0], BondCenters[i][1], BondCenters[i][2]); // Bond Center
glVertex3f(PosX[BondList[i][1] - 1], PosY[BondList[i][1] - 1], PosZ[BondList[i][1] - 1]); // Second Atom's Position
}
glEnd(); // End Drawing Routine.
}
//********************************************************************************************
// Functions in this section deal with drawing routines for the various model representations
// when part of the model is selected and highlighted on-screen.
//*********************************************************************************************
void CPSviewer::drawSelectedModel()
{
// From here on, we need call OpenGL commands based on the representation method we're going after.
// 1 = Ball-and-Stick
// 2 = Licorice (Dreiding)
// 3 = Space Filling (CPK)
// 4 = Vector (Wireframe)
// 5 = Vector with Points
if (representationMethod == 0)
{
drawSelectedBallandStickRepresentation();
}
else if (representationMethod == 1)
{
drawSelectedDreidingRepresentation();
}
else if (representationMethod == 2)
{
drawSelectedSpaceFillingRepresentation();
}
else if (representationMethod == 3 | representationMethod == 4)
{
drawSelectedVectorRepresentation();
}
}
void CPSviewer::drawSelectedBallandStickRepresentation()
{
// Ball-and-Stick Model... A favorite staple of chemistry. We're going to render a lot of spheres and cylinders, so we're going to rely on the handy routines provided by the
// Graphics Library Utility (glu) to efficiently draw the model.
///////////////////////////////////////////////////////////////////////////
// SETUP
///////////////////////////////////////////////////////////////////////////
QVector<int>::Iterator it;
// Enable anti-aliasing techniques to improve image quality. For the Ball-and-Stick Representation, we need
// to make sure OpenGL uses techniques designed for 3D surfaces because the intersection of solids looks really
// bad, even when a lot of polygons are used. We can turn off anti-aliasing methods for lines and points for this representation.
if (glIsEnabled(GL_POINT_SMOOTH))
{
glDisable(GL_POINT_SMOOTH);
}
if (glIsEnabled(GL_LINE_SMOOTH))
{
glDisable(GL_LINE_SMOOTH);
}
glEnable(GL_MULTISAMPLE);
glHint(GL_MULTISAMPLE, GL_NICEST);
GLUquadricObj *sphere, *cylinder; // Quadric Object Pointers
sphere = gluNewQuadric(); // initialize objects for the glu routine
cylinder = gluNewQuadric();