-
Notifications
You must be signed in to change notification settings - Fork 3
/
objects.hpp
executable file
·1668 lines (1402 loc) · 71.6 KB
/
objects.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
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
/** @file objects.hpp
* @brief Helper functions to draw 3D OpenGL 3.0+ objects
* @author Dr. Jeffrey Paone
*
* @copyright MIT License Copyright (c) 2017 Dr. Jeffrey Paone
*
* These functions draw solid (or wireframe) 3D closed OpenGL
* objects. All objects are constructed using triangles that
* have normals and texture coordinates properly set.
*
* @warning NOTE: This header file will only work with OpenGL 3.0+
* @warning NOTE: This header file depends upon GLAD (or alternatively GLEW)
*/
#ifndef CSCI441_OBJECTS_HPP
#define CSCI441_OBJECTS_HPP
#include "teapot.hpp" // for teapot()
#ifdef CSCI441_USE_GLEW
#include <GL/glew.h>
#else
#include <glad/gl.h>
#endif
#include <glm/gtc/constants.hpp>
#include <cassert> // for assert()
#include <map> // for map
////////////////////////////////////////////////////////////////////////////////////
/**
* @namespace CSCI441
* @brief CSCI441 Helper Functions for OpenGL
*/
namespace CSCI441 {
/**
* @brief Sets the attribute locations for vertex positions, normals, and texture coordinates
* @param positionLocation location of the vertex position attribute
* @param normalLocation location of the vertex normal attribute
* @param texCoordLocation location of the vertex texture coordinate attribute
* @note Needs to be called after a shader program is being used and before drawing geometry
*/
void setVertexAttributeLocations( GLint positionLocation, GLint normalLocation = -1, GLint texCoordLocation = -1 );
/**
* @brief deletes the VAOs stored for all object types
*/
[[maybe_unused]] void deleteObjectVAOs();
/**
* @brief deletes the VBOs stored for all object types
*/
[[maybe_unused]] void deleteObjectVBOs();
/**
* @brief Draws a solid cone
* @param base radius of the base of the cone
* @param height height of the cone from the base to the tip
* @param stacks resolution of the number of steps rotated around the central axis of the cone
* @param slices resolution of the number of steps to take along the height
* @pre base must be greater than zero
* @pre height must be greater than zero
* @pre stacks must be greater than zero
* @pre slices must be greater than two
* @note Cone is oriented along the y-axis with the origin along the base of the cone
*/
[[maybe_unused]] void drawSolidCone( GLfloat base, GLfloat height, GLint stacks, GLint slices );
/**
* @brief Draws a wireframe cone
* @param base radius of the base of the cone
* @param height height of the cone from the base to the tip
* @param stacks resolution of the number of steps rotated around the central axis of the cone
* @param slices resolution of the number of steps to take along the height
* @pre base must be greater than zero
* @pre height must be greater than zero
* @pre stacks must be greater than zero
* @pre slices must be greater than two
* @note Cone is oriented along the y-axis with the origin along the base of the cone
*/
[[maybe_unused]] void drawWireCone( GLfloat base, GLfloat height, GLint stacks, GLint slices );
/**
* @brief Calls through to drawSolidCubeIndexed()
* @param sideLength length of the edge of the cube
* @pre sideLength must be greater than zero
*/
[[maybe_unused]] void drawSolidCube( GLfloat sideLength );
/**
* @brief Draws a solid cube with normals aligned with cube face
* @param sideLength length of the edge of the cube
* @pre sideLength must be greater than zero
* @note The origin is at the cube's center of mass. Cube is oriented with our XYZ axes
*/
void drawSolidCubeFlat( GLfloat sideLength );
/**
* @brief Draws a solid cube
* @param sideLength length of the edge of the cube
* @pre sideLength must be greater than zero
* @note The origin is at the cube's center of mass. Cube is oriented with our XYZ axes
*/
void drawSolidCubeIndexed( GLfloat sideLength );
/**
* @brief Draws a solid textured cube. Calls through to drawSolidCubeFlat()
* @param sideLength length of the edge of the cube
* @pre sideLength must be greater than zero
* @note The origin is at the cube's center of mass. Cube is oriented with our XYZ axes
*/
[[maybe_unused]] void drawSolidCubeTextured( GLfloat sideLength );
/**
* @brief Draws a wireframe cube
* @param sideLength length of the edge of the cube
* @pre sideLength must be greater than zero
* @note The origin is at the cube's center of mass. Cube is oriented with our XYZ axes
*/
[[maybe_unused]] void drawWireCube( GLfloat sideLength );
/**
* @brief Draws a cube with 3D Texture Coordinates to map a cube map texture to it
* @param sideLength length of the edge of the cube
* @pre sideLength must be greater than zero
* @note The origin is at the cube's center of mass. Cube is oriented with our XYZ axes
*/
[[maybe_unused]] void drawCubeMap( GLfloat sideLength );
/**
* @brief Draws a solid open ended cylinder
* @param base radius of the base of the cylinder
* @param top radius of the top of the cylinder
* @param height height of the cylinder from the base to the top
* @param stacks resolution of the number of steps rotated around the central axis of the cylinder
* @param slices resolution of the number of steps to take along the height
* @pre either: (1) base is greater than zero and top is greater than or equal to zero or (2) base is greater than or equal to zero and top is greater than zero
* @pre height must be greater than zero
* @pre stacks must be greater than zero
* @pre slices must be greater than two
* @note Cylinder is oriented along the y-axis with the origin along the base
*/
[[maybe_unused]] void drawSolidCylinder( GLfloat base, GLfloat top, GLfloat height, GLint stacks, GLint slices );
/**
* @brief Draws a wireframe open ended cylinder
* @param base radius of the base of the cylinder
* @param top radius of the top of the cylinder
* @param height height of the cylinder from the base to the top
* @param stacks resolution of the number of steps rotated around the central axis of the cylinder
* @param slices resolution of the number of steps to take along the height
* @pre either: (1) base is greater than zero and top is greater than or equal to zero or (2) base is greater than or equal to zero and top is greater than zero
* @pre height must be greater than zero
* @pre stacks must be greater than zero
* @pre slices must be greater than two
* @note Cylinder is oriented along the y-axis with the origin along the base
*/
[[maybe_unused]] void drawWireCylinder( GLfloat base, GLfloat top, GLfloat height, GLint stacks, GLint slices );
/**
* @brief Draws a solid disk
* @param inner equivalent to the width of the disk
* @param outer radius from the center of the disk to the center of the ring
* @param slices resolution of the number of steps rotated along the disk
* @param rings resolution of the number of steps to take along the disk width
* @pre inner is greater than or equal to zero
* @pre outer is greater than zero
* @pre outer is greater than inner
* @pre slices is greater than two
* @pre rings is greater than zero
* @note Disk is drawn in the XY plane with the origin at its center
*/
[[maybe_unused]] void drawSolidDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings );
/**
* @brief Draws a wireframe disk
* @param inner equivalent to the width of the disk
* @param outer radius from the center of the disk to the center of the ring
* @param slices resolution of the number of steps rotated along the disk
* @param rings resolution of the number of steps to take along the disk width
* @pre inner is greater than or equal to zero
* @pre outer is greater than zero
* @pre outer is greater than inner
* @pre slices is greater than two
* @pre rings is greater than zero
* @note Disk is drawn in the XY plane with the origin at its center
*/
[[maybe_unused]] void drawWireDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings );
/**
* @brief Draws part of a solid disk
* @param inner equivalent to the width of the disk
* @param outer radius from the center of the disk to the center of the ring
* @param slices resolution of the number of steps rotated along the disk
* @param rings resolution of the number of steps to take along the disk width
* @param start angle in degrees to start the disk at
* @param sweep distance in degrees to rotate through
* @pre inner is greater than or equal to zero
* @pre outer is greater than zero
* @pre outer is greater than inner
* @pre slices is greater than two
* @pre rings is greater than zero
* @pre start is between [0, 360]
* @pre sweep is between [0, 360]
* @note Disk is drawn in the XY plane with the origin at its center
*/
[[maybe_unused]] void drawSolidPartialDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings, GLfloat start, GLfloat sweep );
/**
* @brief Draws part of a wireframe disk
* @param inner equivalent to the width of the disk
* @param outer radius from the center of the disk to the center of the ring
* @param slices resolution of the number of steps rotated along the disk
* @param rings resolution of the number of steps to take along the disk width
* @param start angle in degrees to start the disk at
* @param sweep distance in degrees to rotate through
* @pre inner is greater than or equal to zero
* @pre outer is greater than zero
* @pre outer is greater than inner
* @pre slices is greater than two
* @pre rings is greater than zero
* @pre start is between [0, 360]
* @pre sweep is between [0, 360]
* @note Disk is drawn in the XY plane with the origin at its center
*/
[[maybe_unused]] void drawWirePartialDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings, GLfloat start, GLfloat sweep );
/**
* @brief Draws a solid sphere
* @param radius radius of the sphere
* @param stacks resolution of the number of steps to take along theta (rotate around Y-axis)
* @param slices resolution of the number of steps to take along phi (rotate around X- or Z-axis)
* @pre radius must be greater than 0
* @pre stacks must be greater than 2
* @pre slices must be greater than 2
* @note Origin is at the center of the sphere
*/
[[maybe_unused]] void drawSolidSphere( GLfloat radius, GLint stacks, GLint slices );
/**
* @brief Draws a wireframe sphere
* @param radius radius of the sphere
* @param stacks resolution of the number of steps to take along theta (rotate around Y-axis)
* @param slices resolution of the number of steps to take along phi (rotate around X- or Z-axis)
* @pre radius must be greater than 0
* @pre stacks must be greater than 2
* @pre slices must be greater than 2
* @note Origin is at the center of the sphere
*/
[[maybe_unused]] void drawWireSphere( GLfloat radius, GLint stacks, GLint slices );
/**
* @brief Draws a solid half sphere with a bottom
* @param radius radius of the sphere
* @param stacks resolution of the number of steps to take along theta (rotate around Y-axis)
* @param slices resolution of the number of steps to take along phi (rotate around X- or Z-axis)
* @pre radius must be greater than 0
* @pre stacks must be greater than 2
* @pre slices must be greater than 2
* @note Origin is at the center of the sphere
*/
[[maybe_unused]] void drawSolidHalfSphere( GLfloat radius, GLint stacks, GLint slices );
/**
* @brief Draws a wireframe half sphere with a bottom
* @param radius radius of the sphere
* @param stacks resolution of the number of steps to take along theta (rotate around Y-axis)
* @param slices resolution of the number of steps to take along phi (rotate around X- or Z-axis)
* @pre radius must be greater than 0
* @pre stacks must be greater than 2
* @pre slices must be greater than 2
* @note Origin is at the center of the sphere
*/
[[maybe_unused]] void drawWireHalfSphere( GLfloat radius, GLint stacks, GLint slices );
/**
* @brief Draws a solid dome
* @param radius radius of the dome
* @param stacks resolution of the number of steps to take along theta (rotate around Y-axis)
* @param slices resolution of the number of steps to take along phi (rotate around X- or Z-axis)
* @pre radius must be greater than 0
* @pre stacks must be greater than 2
* @pre slices must be greater than 2
* @note Origin is at the center of the dome
*/
[[maybe_unused]] void drawSolidDome( GLfloat radius, GLint stacks, GLint slices );
/**
* @brief Draws a wireframe dome
* @param radius radius of the dome
* @param stacks resolution of the number of steps to take along theta (rotate around Y-axis)
* @param slices resolution of the number of steps to take along phi (rotate around X- or Z-axis)
* @pre radius must be greater than 0
* @pre stacks must be greater than 2
* @pre slices must be greater than 2
* @note Origin is at the center of the dome
*/
[[maybe_unused]] void drawWireDome( GLfloat radius, GLint stacks, GLint slices );
/**
* @brief Draws a solid teapot
* @param unused present for historical compatability
* @pre size must be greater than zero
* @note Oriented with spout and handle running along X-axis, cap and bottom along Y-axis. Origin is at the center of the teapot
*/
[[maybe_unused]] void drawSolidTeapot( GLfloat unused = 1.0f );
/**
* @brief Draws a wireframe teapot
* @param unused present for historical compatability
* @pre size must be greater than zero
* @note Oriented with spout and handle running along X-axis, cap and bottom along Y-axis. Origin is at the center of the teapot
*/
[[maybe_unused]] void drawWireTeapot( GLfloat unused = 1.0f );
/**
* @brief Draws a solid torus
* @param innerRadius equivalent to the width of the torus ring
* @param outerRadius radius from the center of the torus to the center of the ring
* @param sides resolution of steps to take around the band of the ring
* @param rings resolution of steps to take around the torus
* @pre innerRadius must be greater than zero
* @pre outerRadius must be greater than zero
* @pre sides must be greater than two
* @pre rings must be greater than two
* @note Torus is oriented in the XY-plane with the origin at its center
*/
[[maybe_unused]] void drawSolidTorus( GLfloat innerRadius, GLfloat outerRadius, GLint sides, GLint rings );
/**
* @brief Draws a wireframe torus
* @param innerRadius equivalent to the width of the torus ring
* @param outerRadius radius from the center of the torus to the center of the ring
* @param sides resolution of steps to take around the band of the ring
* @param rings resolution of steps to take around the torus
* @pre innerRadius must be greater than zero
* @pre outerRadius must be greater than zero
* @pre sides must be greater than two
* @pre rings must be greater than two
* @note Torus is oriented in the XY-plane with the origin at its center
*/
[[maybe_unused]] void drawWireTorus( GLfloat innerRadius, GLfloat outerRadius, GLint sides, GLint rings );
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// Internal rendering implementations to stay consistent with solid and wire modes
//
// Cone is drawn with a cylinder
// Disk is drawn with a partial disk
/**
* @namespace CSCI441_INTERNAL
* @brief implementations of public facing methods
*/
namespace CSCI441_INTERNAL {
/**
* @brief delete all the VAOs from the GPU
*/
void deleteObjectVAOs();
/**
* @brief delete all the VBOs from the GPU
*/
void deleteObjectVBOs();
/**
* @brief draws an indexed cube where vertex normals are the sum of the three faces
* @param sideLength side length
* @param renderMode filled polygon (GL_FILL) or line edges (GL_LINES)
*/
void drawCube( GLfloat sideLength, GLenum renderMode );
/**
* @brief draws an indexed cube where vertex normals are the sum of the three faces
* @param sideLength side length
* @param renderMode filled polygon (GL_FILL) or line edges (GL_LINES)
*/
void drawCubeIndexed( GLfloat sideLength, GLenum renderMode );
/**
* @brief draws a flat cube where vertex normals are aligned with the face of the cube
* @param sideLength side length
* @param renderMode filled polygon (GL_FILL) or line edges (GL_LINES)
*/
void drawCubeFlat( GLfloat sideLength, GLenum renderMode );
/**
* @brief draws a cylinder
* @note iff one of the base or top is zero and the other is non-zero, then a cone can be drawn
* @param base radius of the base of the cylinder
* @param top radius of the top of the cylinder
* @param height height of the cylinder
* @param stacks resolution of the number of steps rotated around the central axis of the cylinder
* @param slices resolution of the number of steps to take along the height
* @param renderMode filled polygon (GL_FILL) or line edges (GL_LINES)
*/
void drawCylinder( GLfloat base, GLfloat top, GLfloat height, GLuint stacks, GLuint slices, GLenum renderMode );
void drawPartialDisk(GLfloat innerRadius, GLfloat outerRadius, GLuint slices, GLuint rings, GLfloat startAngle, GLfloat sweepAngle, GLenum renderMode );
void drawSphere( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode );
void drawHalfSphere( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode );
void drawDome( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode );
void drawTorus( GLfloat innerRadius, GLfloat outerRadius, GLuint sides, GLuint rings, GLenum renderMode );
/**
* @brief draws the Utah Teapot
* @param renderMode filled polygon (GL_FILL) or line edges (GL_LINES)
*/
void drawTeapot( GLenum renderMode );
/**
* @brief location of the vertex position attribute within the shader being rendered with
*/
inline GLint _positionAttributeLocation = -1;
/**
* @brief location of the vertex normal attribute within the shader being rendered with
*/
inline GLint _normalAttributeLocation = -1;
/**
* @brief location of the vertex texture coordinate attribute within the shader being rendered with
*/
inline GLint _texCoordAttributeLocation = -1;
/**
* @brief creates the VAO + VBO
* @param sideLength
*/
void generateCubeVAOFlat( GLfloat sideLength );
void generateCubeVAOIndexed( GLfloat sideLength );
inline std::map< GLfloat, GLuint > _cubeVAO;
inline std::map< GLfloat, GLuint > _cubeVBO;
inline std::map< GLfloat, GLuint > _cubeVAOIndexed;
inline std::map< GLfloat, GLuint > _cubeVBOIndexed;
inline std::map< GLfloat, GLuint > _cubeIBOIndexed;
/// \desc stores data necessary to specify a unique cylinder
struct CylinderData {
/// \desc radius of the base
GLfloat radiusBase;
/// \desc radius of the top
GLfloat top;
/// \desc height
GLfloat height;
/// \desc number of stacks
GLuint stacks;
/// \desc number of slices
GLuint slices;
/// \desc compute number of vertices
[[nodiscard]] GLuint64 numVertices() const { return stacks * (slices + 1) * 2; }
/// \desc less than comparison operator used to sort
bool operator<( const CylinderData rhs ) const {
if( radiusBase < rhs.radiusBase ) {
return true;
} else if( fabs(radiusBase - rhs.radiusBase) <= 0.000001 ) {
if( top < rhs.top ) {
return true;
} else if( fabs(top - rhs.top) <= 0.000001 ) {
if( height < rhs.height ) {
return true;
} else if( fabs(height - rhs.height) <= 0.000001 ) {
if( stacks < rhs.stacks ) {
return true;
} else if( stacks == rhs.stacks ) {
if( slices < rhs.slices ) {
return true;
}
}
}
}
}
return false;
}
};
void generateCylinderVAO( CylinderData cylData );
inline std::map< CylinderData, GLuint > _cylinderVAO;
inline std::map< CylinderData, GLuint > _cylinderVBO;
struct DiskData {
GLfloat innerRadius, outerRadius, startAngle, sweepAngle;
GLuint slices, rings;
[[nodiscard]] GLuint64 numVertices() const { return rings * (slices + 1) * 2; }
bool operator<( const DiskData rhs ) const {
if( innerRadius < rhs.innerRadius ) {
return true;
} else if( fabs(innerRadius - rhs.innerRadius) <= 0.000001 ) {
if( outerRadius < rhs.outerRadius ) {
return true;
} else if( fabs(outerRadius - rhs.outerRadius) <= 0.000001 ) {
if( startAngle < rhs.startAngle ) {
return true;
} else if( fabs(startAngle - rhs.startAngle) <= 0.000001 ) {
if( sweepAngle < rhs.sweepAngle ) {
return true;
} else if( fabs(sweepAngle - rhs.sweepAngle) <= 0.000001 ) {
if( slices < rhs.slices ) {
return true;
} else if( slices == rhs.slices ) {
if(rings < rhs.rings ) {
return true;
}
}
}
}
}
}
return false;
}
};
void generateDiskVAO( DiskData diskData );
inline std::map< DiskData, GLuint > _diskVAO;
inline std::map< DiskData, GLuint > _diskVBO;
struct SphereData {
GLfloat radius;
GLuint stacks, slices;
[[nodiscard]] GLuint64 numVertices() const { return ((slices + 2) * 2) + (((stacks - 2) * (slices+1)) * 2); }
bool operator<( const SphereData rhs ) const {
if( radius < rhs.radius ) {
return true;
} else if( fabs(radius - rhs.radius) <= 0.000001 ) {
if( stacks < rhs.stacks ) {
return true;
} else if( stacks == rhs.stacks ) {
if( slices < rhs.slices ) {
return true;
}
}
}
return false;
}
};
void generateSphereVAO( SphereData sphereData );
inline std::map< SphereData, GLuint > _sphereVAO;
inline std::map< SphereData, GLuint > _sphereVBO;
struct TorusData {
GLfloat innerRadius, outerRadius;
GLuint sides, rings;
[[nodiscard]] GLuint64 numVertices() const { return sides * 4 * rings; }
bool operator<( const TorusData rhs ) const {
if( innerRadius < rhs.innerRadius ) {
return true;
} else if( fabs(innerRadius - rhs.innerRadius) <= 0.000001 ) {
if( outerRadius < rhs.outerRadius ) {
return true;
} else if( fabs(outerRadius - rhs.outerRadius) <= 0.000001 ) {
if( sides < rhs.sides ) {
return true;
} else if( sides == rhs.sides ) {
if( rings < rhs.rings ) {
return true;
}
}
}
}
return false;
}
};
void generateTorusVAO( TorusData torusData );
inline std::map< TorusData, GLuint > _torusVAO;
inline std::map< TorusData, GLuint > _torusVBO;
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// Outward facing function implementations
inline void CSCI441::setVertexAttributeLocations( GLint positionLocation, GLint normalLocation, GLint texCoordLocation ) {
CSCI441_INTERNAL::_positionAttributeLocation = positionLocation;
CSCI441_INTERNAL::_normalAttributeLocation = normalLocation;
CSCI441_INTERNAL::_texCoordAttributeLocation = texCoordLocation;
CSCI441_INTERNAL::setTeapotAttributeLocations(positionLocation, normalLocation, texCoordLocation);
}
[[maybe_unused]]
inline void CSCI441::deleteObjectVAOs() {
CSCI441_INTERNAL::deleteObjectVAOs();
}
[[maybe_unused]]
inline void CSCI441::deleteObjectVBOs() {
CSCI441_INTERNAL::deleteObjectVBOs();
}
[[maybe_unused]]
inline void CSCI441::drawSolidCone( GLfloat base, GLfloat height, GLint stacks, GLint slices ) {
assert( base > 0.0f );
assert( height > 0.0f );
assert( stacks > 0 );
assert( slices > 2 );
CSCI441_INTERNAL::drawCylinder( base, 0.0f, height, stacks, slices, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWireCone( GLfloat base, GLfloat height, GLint stacks, GLint slices ) {
assert( base > 0.0f );
assert( height > 0.0f );
assert( stacks > 0 );
assert( slices > 2 );
CSCI441_INTERNAL::drawCylinder( base, 0.0f, height, stacks, slices, GL_LINE );
}
[[maybe_unused]]
inline void CSCI441::drawSolidCube( GLfloat sideLength ) {
drawSolidCubeIndexed(sideLength);
}
[[maybe_unused]]
inline void CSCI441::drawSolidCubeTextured( GLfloat sideLength ) {
drawSolidCubeFlat(sideLength);
}
inline void CSCI441::drawSolidCubeIndexed(GLfloat sideLength) {
assert( sideLength > 0.0f );
CSCI441_INTERNAL::drawCube( sideLength, GL_FILL );
}
inline void CSCI441::drawSolidCubeFlat(GLfloat sideLength) {
assert( sideLength > 0.0f );
CSCI441_INTERNAL::drawCubeFlat( sideLength, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWireCube( GLfloat sideLength ) {
assert( sideLength > 0.0f );
CSCI441_INTERNAL::drawCube( sideLength, GL_LINE );
}
[[maybe_unused]]
inline void CSCI441::drawCubeMap(GLfloat sideLength) {
assert(sideLength > 0.0f);
CSCI441_INTERNAL::drawCube( sideLength, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawSolidCylinder( GLfloat base, GLfloat top, GLfloat height, GLint stacks, GLint slices ) {
assert( (base >= 0.0f && top > 0.0f) || (base > 0.0f && top >= 0.0f) );
assert( height > 0.0f );
assert( stacks > 0 );
assert( slices > 2 );
CSCI441_INTERNAL::drawCylinder( base, top, height, stacks, slices, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWireCylinder( GLfloat base, GLfloat top, GLfloat height, GLint stacks, GLint slices ) {
assert( (base >= 0.0f && top > 0.0f) || (base > 0.0f && top >= 0.0f) );
assert( height > 0.0f );
assert( stacks > 0 );
assert( slices > 2 );
CSCI441_INTERNAL::drawCylinder( base, top, height, stacks, slices, GL_LINE );
}
[[maybe_unused]]
inline void CSCI441::drawSolidDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings ) {
assert( inner >= 0.0f );
assert( outer > 0.0f );
assert( outer > inner );
assert( slices > 2 );
assert( rings > 0 );
CSCI441_INTERNAL::drawPartialDisk( inner, outer, slices, rings, 0, glm::two_pi<float>(), GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWireDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings ) {
assert( inner >= 0.0f );
assert( outer > 0.0f );
assert( outer > inner );
assert( slices > 2 );
assert( rings > 0 );
CSCI441_INTERNAL::drawPartialDisk( inner, outer, slices, rings, 0, glm::two_pi<float>(), GL_LINE );
}
[[maybe_unused]]
inline void CSCI441::drawSolidPartialDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings, GLfloat start, GLfloat sweep ) {
assert( inner >= 0.0f );
assert( outer > 0.0f );
assert( outer > inner );
assert( slices > 2 );
assert( rings > 0 );
assert( start >= 0.0f && start <= 360.0f );
assert( sweep >= 0.0f && sweep <= 360.0f );
CSCI441_INTERNAL::drawPartialDisk( inner, outer, slices, rings, start * glm::pi<float>() / 180.0f, sweep * glm::pi<float>() / 180.0f, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWirePartialDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings, GLfloat start, GLfloat sweep ) {
assert( inner >= 0.0f );
assert( outer > 0.0f );
assert( outer > inner );
assert( slices > 2 );
assert( rings > 0 );
assert( start >= 0.0f && start <= 360.0f );
assert( sweep >= 0.0f && sweep <= 360.0f );
CSCI441_INTERNAL::drawPartialDisk( inner, outer, slices, rings, start * glm::pi<float>() / 180.0f, sweep * glm::pi<float>() / 180.0f, GL_LINE );
}
[[maybe_unused]]
inline void CSCI441::drawSolidSphere( GLfloat radius, GLint stacks, GLint slices ) {
assert( radius > 0.0f );
assert( stacks > 1 );
assert( slices > 2 );
CSCI441_INTERNAL::drawSphere( radius, stacks, slices, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWireSphere( GLfloat radius, GLint stacks, GLint slices ) {
assert( radius > 0.0f );
assert( stacks > 1);
assert( slices > 2 );
CSCI441_INTERNAL::drawSphere( radius, stacks, slices, GL_LINE );
}
[[maybe_unused]]
inline void CSCI441::drawSolidHalfSphere( GLfloat radius, GLint stacks, GLint slices ) {
assert( radius > 0.0f );
assert( stacks > 1 );
assert( slices > 2 );
CSCI441_INTERNAL::drawHalfSphere( radius, stacks, slices, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWireHalfSphere( GLfloat radius, GLint stacks, GLint slices ) {
assert( radius > 0.0f );
assert( stacks > 1);
assert( slices > 2 );
CSCI441_INTERNAL::drawHalfSphere( radius, stacks, slices, GL_LINE );
}
[[maybe_unused]]
inline void CSCI441::drawSolidDome( GLfloat radius, GLint stacks, GLint slices ) {
assert( radius > 0.0f );
assert( stacks > 1 );
assert( slices > 2 );
CSCI441_INTERNAL::drawDome( radius, stacks, slices, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWireDome( GLfloat radius, GLint stacks, GLint slices ) {
assert( radius > 0.0f );
assert( stacks > 1);
assert( slices > 2 );
CSCI441_INTERNAL::drawDome( radius, stacks, slices, GL_LINE );
}
[[maybe_unused]]
inline void CSCI441::drawSolidTeapot( [[maybe_unused]] GLfloat unused ) {
CSCI441_INTERNAL::drawTeapot(GL_FILL);
}
[[maybe_unused]]
inline void CSCI441::drawWireTeapot( [[maybe_unused]] GLfloat unused ) {
CSCI441_INTERNAL::drawTeapot(GL_LINE);
}
[[maybe_unused]]
inline void CSCI441::drawSolidTorus( GLfloat innerRadius, GLfloat outerRadius, GLint sides, GLint rings ) {
assert( innerRadius > 0.0f );
assert( outerRadius > 0.0f );
assert( sides > 2 );
assert( rings > 2 );
CSCI441_INTERNAL::drawTorus( innerRadius, outerRadius, sides, rings, GL_FILL );
}
[[maybe_unused]]
inline void CSCI441::drawWireTorus( GLfloat innerRadius, GLfloat outerRadius, GLint sides, GLint rings ) {
assert( innerRadius > 0.0f );
assert( outerRadius > 0.0f );
assert( sides > 2 );
assert( rings > 2 );
CSCI441_INTERNAL::drawTorus( innerRadius, outerRadius, sides, rings, GL_LINE );
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// Internal function rendering implementations
inline void CSCI441_INTERNAL::deleteObjectVAOs() {
for(auto & iter : _cubeVAO) {
glDeleteVertexArrays(1, &(iter.second));
}
for(auto & iter : _cubeVAOIndexed) {
glDeleteVertexArrays(1, &(iter.second));
}
for(auto & iter : _cylinderVAO) {
glDeleteVertexArrays(1, &(iter.second));
}
for(auto & iter : _diskVAO) {
glDeleteVertexArrays(1, &(iter.second));
}
for(auto & iter : _sphereVAO) {
glDeleteVertexArrays(1, &(iter.second));
}
for(auto & iter : _torusVAO) {
glDeleteVertexArrays(1, &(iter.second));
}
}
inline void CSCI441_INTERNAL::deleteObjectVBOs() {
for(auto & iter : _cubeVBO) {
glDeleteBuffers(1, &(iter.second));
}
for(auto & iter : _cubeVBOIndexed) {
glDeleteBuffers(1, &(iter.second));
}
for(auto & iter : _cubeIBOIndexed) {
glDeleteBuffers(1, &(iter.second));
}
for(auto & iter : _cylinderVBO) {
glDeleteBuffers(1, &(iter.second));
}
for(auto & iter : _diskVBO) {
glDeleteBuffers(1, &(iter.second));
}
for(auto & iter : _sphereVBO) {
glDeleteBuffers(1, &(iter.second));
}
for(auto & iter : _torusVBO) {
glDeleteBuffers(1, &(iter.second));
}
}
inline void CSCI441_INTERNAL::drawCube( GLfloat sideLength, GLenum renderMode ) {
drawCubeIndexed(sideLength, renderMode);
}
inline void CSCI441_INTERNAL::drawCubeFlat( GLfloat sideLength, GLenum renderMode ) {
if( CSCI441_INTERNAL::_cubeVAO.count( sideLength ) == 0 ) {
CSCI441_INTERNAL::generateCubeVAOFlat( sideLength );
}
const GLuint64 NUM_VERTICES = 36;
GLint currentPolygonMode[2];
glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
glPolygonMode( GL_FRONT_AND_BACK, renderMode );
glBindVertexArray( CSCI441_INTERNAL::_cubeVAO.find( sideLength )->second );
glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_cubeVBO.find( sideLength )->second );
if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)nullptr );
}
if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES) );
}
if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES * 2) );
}
glDrawArrays( GL_TRIANGLES, 0, 36 );
glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
}
inline void CSCI441_INTERNAL::drawCubeIndexed( GLfloat sideLength, GLenum renderMode ) {
if( CSCI441_INTERNAL::_cubeVAOIndexed.count( sideLength ) == 0 ) {
CSCI441_INTERNAL::generateCubeVAOIndexed( sideLength );
}
const GLuint64 NUM_VERTICES = 8;
GLint currentPolygonMode[2];
glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
glPolygonMode( GL_FRONT_AND_BACK, renderMode );
glBindVertexArray( CSCI441_INTERNAL::_cubeVAOIndexed.find( sideLength )->second );
glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_cubeVBOIndexed.find( sideLength )->second );
if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)nullptr );
}
if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES) );
}
if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES * 2) );
}
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, (void*)nullptr);
glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
}
inline void CSCI441_INTERNAL::drawCylinder( GLfloat base, GLfloat top, GLfloat height, GLuint stacks, GLuint slices, GLenum renderMode ) {
CylinderData cylData = { base, top, height, stacks, slices };
if( CSCI441_INTERNAL::_cylinderVAO.count( cylData ) == 0 ) {
CSCI441_INTERNAL::generateCylinderVAO( cylData );
}
const GLuint64 NUM_VERTICES = cylData.numVertices();
GLint currentPolygonMode[2];
glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
glPolygonMode( GL_FRONT_AND_BACK, renderMode );
glBindVertexArray( CSCI441_INTERNAL::_cylinderVAO.find( cylData )->second );
glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_cylinderVBO.find( cylData )->second );
if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)nullptr );
}
if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES) );
}
if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES * 2) );
}
for(GLuint stackNum = 0; stackNum < stacks; stackNum++) {
glDrawArrays( GL_TRIANGLE_STRIP, static_cast<GLint>((slices+1)*2*stackNum), static_cast<GLint>((slices+1)*2) );
}
glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
}
inline void CSCI441_INTERNAL::drawPartialDisk(GLfloat innerRadius, GLfloat outerRadius, GLuint slices, GLuint rings, GLfloat startAngle, GLfloat sweepAngle, GLenum renderMode ) {
DiskData diskData = {innerRadius, outerRadius, startAngle, sweepAngle, slices, rings };
if( CSCI441_INTERNAL::_diskVAO.count( diskData ) == 0 ) {
CSCI441_INTERNAL::generateDiskVAO( diskData );
}
const GLuint64 NUM_VERTICES = diskData.numVertices();
GLint currentPolygonMode[2];
glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
glPolygonMode( GL_FRONT_AND_BACK, renderMode );
glBindVertexArray( CSCI441_INTERNAL::_diskVAO.find( diskData )->second );
glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_diskVBO.find( diskData )->second );
if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)nullptr );
}
if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES) );
}
if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES * 2) );
}
for(GLuint ringNum = 0; ringNum < rings; ringNum++) {
glDrawArrays( GL_TRIANGLE_STRIP, static_cast<GLint>((slices+1)*2*ringNum), static_cast<GLint>((slices+1)*2) );
}
glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
}
inline void CSCI441_INTERNAL::drawSphere( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode ) {
SphereData sphereData = { radius, stacks, slices };
if( CSCI441_INTERNAL::_sphereVAO.count( sphereData ) == 0 ) {
CSCI441_INTERNAL::generateSphereVAO( sphereData );
}
const GLuint64 NUM_VERTICES = sphereData.numVertices();
GLint currentPolygonMode[2];
glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
glPolygonMode( GL_FRONT_AND_BACK, renderMode );
glBindVertexArray( CSCI441_INTERNAL::_sphereVAO.find( sphereData )->second );
glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_sphereVBO.find( sphereData )->second );
if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)nullptr );
}
if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES) );
}
if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(glm::vec3) * NUM_VERTICES * 2) );
}
glDrawArrays( GL_TRIANGLE_FAN, 0, static_cast<GLint>(slices+2) );
for(GLuint stackNum = 1; stackNum < stacks-1; stackNum++) {
glDrawArrays( GL_TRIANGLE_STRIP, static_cast<GLint>((slices+2) + (stackNum-1)*((slices+1)*2)), static_cast<GLint>((slices+1)*2) );
}
glDrawArrays( GL_TRIANGLE_FAN, static_cast<GLint>((slices+2) + (stacks-2)*(slices+1)*2), static_cast<GLint>(slices+2) );
glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
}
inline void CSCI441_INTERNAL::drawHalfSphere( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode ) {
SphereData sphereData = { radius, stacks, slices };
if( CSCI441_INTERNAL::_sphereVAO.count( sphereData ) == 0 ) {