15#ifndef CSCI441_OBJECTS_HPP
16#define CSCI441_OBJECTS_HPP
21#ifdef CSCI441_USE_GLEW
27#include <glm/gtc/constants.hpp>
70 [[maybe_unused]]
void drawSolidCone( GLfloat base, GLfloat height, GLint stacks, GLint slices );
83 [[maybe_unused]]
void drawWireCone( GLfloat base, GLfloat height, GLint stacks, GLint slices );
118 [[maybe_unused]]
void drawWireCube( GLfloat sideLength );
126 [[maybe_unused]]
void drawCubeMap( GLfloat sideLength );
141 [[maybe_unused]]
void drawSolidCylinder( GLfloat base, GLfloat top, GLfloat height, GLint stacks, GLint slices );
155 [[maybe_unused]]
void drawWireCylinder( GLfloat base, GLfloat top, GLfloat height, GLint stacks, GLint slices );
170 [[maybe_unused]]
void drawSolidDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings );
184 [[maybe_unused]]
void drawWireDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings );
203 [[maybe_unused]]
void drawSolidPartialDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings, GLfloat start, GLfloat sweep );
221 [[maybe_unused]]
void drawWirePartialDisk( GLfloat inner, GLfloat outer, GLint slices, GLint rings, GLfloat start, GLfloat sweep );
233 [[maybe_unused]]
void drawSolidSphere( GLfloat radius, GLint stacks, GLint slices );
244 [[maybe_unused]]
void drawWireSphere( GLfloat radius, GLint stacks, GLint slices );
267 [[maybe_unused]]
void drawWireHalfSphere( GLfloat radius, GLint stacks, GLint slices );
279 [[maybe_unused]]
void drawSolidDome( GLfloat radius, GLint stacks, GLint slices );
290 [[maybe_unused]]
void drawWireDome( GLfloat radius, GLint stacks, GLint slices );
319 [[maybe_unused]]
void drawSolidTorus( GLfloat innerRadius, GLfloat outerRadius, GLint sides, GLint rings );
332 [[maybe_unused]]
void drawWireTorus( GLfloat innerRadius, GLfloat outerRadius, GLint sides, GLint rings );
346namespace CSCI441_INTERNAL {
361 void drawCube( GLfloat sideLength, GLenum renderMode );
367 void drawCubeIndexed( GLfloat sideLength, GLenum renderMode );
373 void drawCubeFlat( GLfloat sideLength, GLenum renderMode );
384 void drawCylinder( GLfloat base, GLfloat top, GLfloat height, GLuint stacks, GLuint slices, GLenum renderMode );
385 void drawPartialDisk(GLfloat innerRadius, GLfloat outerRadius, GLuint slices, GLuint rings, GLfloat startAngle, GLfloat sweepAngle, GLenum renderMode );
386 void drawSphere( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode );
387 void drawHalfSphere( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode );
388 void drawDome( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode );
389 void drawTorus( GLfloat innerRadius, GLfloat outerRadius, GLuint sides, GLuint rings, GLenum renderMode );
394 void drawTeapot( GLenum renderMode );
399 inline GLint _positionAttributeLocation = -1;
403 inline GLint _normalAttributeLocation = -1;
407 inline GLint _texCoordAttributeLocation = -1;
413 void generateCubeVAOFlat( GLfloat sideLength );
414 void generateCubeVAOIndexed( GLfloat sideLength );
415 inline std::map< GLfloat, GLuint > _cubeVAO;
416 inline std::map< GLfloat, GLuint > _cubeVBO;
417 inline std::map< GLfloat, GLuint > _cubeVAOIndexed;
418 inline std::map< GLfloat, GLuint > _cubeVBOIndexed;
419 inline std::map< GLfloat, GLuint > _cubeIBOIndexed;
422 struct CylinderData {
434 [[nodiscard]] GLuint64 numVertices()
const {
return stacks * (slices + 1) * 2; }
436 bool operator<(
const CylinderData rhs )
const {
437 if( radiusBase < rhs.radiusBase ) {
439 }
else if( fabs(radiusBase - rhs.radiusBase) <= 0.000001 ) {
440 if( top < rhs.top ) {
442 }
else if( fabs(top - rhs.top) <= 0.000001 ) {
443 if( height < rhs.height ) {
445 }
else if( fabs(height - rhs.height) <= 0.000001 ) {
446 if( stacks < rhs.stacks ) {
448 }
else if( stacks == rhs.stacks ) {
449 if( slices < rhs.slices ) {
459 void generateCylinderVAO( CylinderData cylData );
460 inline std::map< CylinderData, GLuint > _cylinderVAO;
461 inline std::map< CylinderData, GLuint > _cylinderVBO;
464 GLfloat innerRadius, outerRadius, startAngle, sweepAngle;
465 GLuint slices, rings;
466 [[nodiscard]] GLuint64 numVertices()
const {
return rings * (slices + 1) * 2; }
467 bool operator<(
const DiskData rhs )
const {
468 if( innerRadius < rhs.innerRadius ) {
470 }
else if( fabs(innerRadius - rhs.innerRadius) <= 0.000001 ) {
471 if( outerRadius < rhs.outerRadius ) {
473 }
else if( fabs(outerRadius - rhs.outerRadius) <= 0.000001 ) {
474 if( startAngle < rhs.startAngle ) {
476 }
else if( fabs(startAngle - rhs.startAngle) <= 0.000001 ) {
477 if( sweepAngle < rhs.sweepAngle ) {
479 }
else if( fabs(sweepAngle - rhs.sweepAngle) <= 0.000001 ) {
480 if( slices < rhs.slices ) {
482 }
else if( slices == rhs.slices ) {
483 if(rings < rhs.rings ) {
494 void generateDiskVAO( DiskData diskData );
495 inline std::map< DiskData, GLuint > _diskVAO;
496 inline std::map< DiskData, GLuint > _diskVBO;
500 GLuint stacks, slices;
501 [[nodiscard]] GLuint64 numVertices()
const {
return ((slices + 2) * 2) + (((stacks - 2) * (slices+1)) * 2); }
502 bool operator<(
const SphereData rhs )
const {
503 if( radius < rhs.radius ) {
505 }
else if( fabs(radius - rhs.radius) <= 0.000001 ) {
506 if( stacks < rhs.stacks ) {
508 }
else if( stacks == rhs.stacks ) {
509 if( slices < rhs.slices ) {
517 void generateSphereVAO( SphereData sphereData );
518 inline std::map< SphereData, GLuint > _sphereVAO;
519 inline std::map< SphereData, GLuint > _sphereVBO;
522 GLfloat innerRadius, outerRadius;
524 [[nodiscard]] GLuint64 numVertices()
const {
return sides * 4 * rings; }
525 bool operator<(
const TorusData rhs )
const {
526 if( innerRadius < rhs.innerRadius ) {
528 }
else if( fabs(innerRadius - rhs.innerRadius) <= 0.000001 ) {
529 if( outerRadius < rhs.outerRadius ) {
531 }
else if( fabs(outerRadius - rhs.outerRadius) <= 0.000001 ) {
532 if( sides < rhs.sides ) {
534 }
else if( sides == rhs.sides ) {
535 if( rings < rhs.rings ) {
544 void generateTorusVAO( TorusData torusData );
545 inline std::map< TorusData, GLuint > _torusVAO;
546 inline std::map< TorusData, GLuint > _torusVBO;
554 CSCI441_INTERNAL::_positionAttributeLocation = positionLocation;
555 CSCI441_INTERNAL::_normalAttributeLocation = normalLocation;
556 CSCI441_INTERNAL::_texCoordAttributeLocation = texCoordLocation;
557 CSCI441_INTERNAL::setTeapotAttributeLocations(positionLocation, normalLocation, texCoordLocation);
562 CSCI441_INTERNAL::deleteObjectVAOs();
567 CSCI441_INTERNAL::deleteObjectVBOs();
572 assert( base > 0.0f );
573 assert( height > 0.0f );
574 assert( stacks > 0 );
575 assert( slices > 2 );
577 CSCI441_INTERNAL::drawCylinder( base, 0.0f, height, stacks, slices, GL_FILL );
582 assert( base > 0.0f );
583 assert( height > 0.0f );
584 assert( stacks > 0 );
585 assert( slices > 2 );
587 CSCI441_INTERNAL::drawCylinder( base, 0.0f, height, stacks, slices, GL_LINE );
601 assert( sideLength > 0.0f );
603 CSCI441_INTERNAL::drawCube( sideLength, GL_FILL );
607 assert( sideLength > 0.0f );
609 CSCI441_INTERNAL::drawCubeFlat( sideLength, GL_FILL );
614 assert( sideLength > 0.0f );
616 CSCI441_INTERNAL::drawCube( sideLength, GL_LINE );
621 assert(sideLength > 0.0f);
623 CSCI441_INTERNAL::drawCube( sideLength, GL_FILL );
628 assert( (base >= 0.0f && top > 0.0f) || (base > 0.0f && top >= 0.0f) );
629 assert( height > 0.0f );
630 assert( stacks > 0 );
631 assert( slices > 2 );
633 CSCI441_INTERNAL::drawCylinder( base, top, height, stacks, slices, GL_FILL );
638 assert( (base >= 0.0f && top > 0.0f) || (base > 0.0f && top >= 0.0f) );
639 assert( height > 0.0f );
640 assert( stacks > 0 );
641 assert( slices > 2 );
643 CSCI441_INTERNAL::drawCylinder( base, top, height, stacks, slices, GL_LINE );
648 assert( inner >= 0.0f );
649 assert( outer > 0.0f );
650 assert( outer > inner );
651 assert( slices > 2 );
654 CSCI441_INTERNAL::drawPartialDisk( inner, outer, slices, rings, 0, glm::two_pi<float>(), GL_FILL );
659 assert( inner >= 0.0f );
660 assert( outer > 0.0f );
661 assert( outer > inner );
662 assert( slices > 2 );
665 CSCI441_INTERNAL::drawPartialDisk( inner, outer, slices, rings, 0, glm::two_pi<float>(), GL_LINE );
670 assert( inner >= 0.0f );
671 assert( outer > 0.0f );
672 assert( outer > inner );
673 assert( slices > 2 );
675 assert( start >= 0.0f && start <= 360.0f );
676 assert( sweep >= 0.0f && sweep <= 360.0f );
678 CSCI441_INTERNAL::drawPartialDisk( inner, outer, slices, rings, start * glm::pi<float>() / 180.0f, sweep * glm::pi<float>() / 180.0f, GL_FILL );
683 assert( inner >= 0.0f );
684 assert( outer > 0.0f );
685 assert( outer > inner );
686 assert( slices > 2 );
688 assert( start >= 0.0f && start <= 360.0f );
689 assert( sweep >= 0.0f && sweep <= 360.0f );
691 CSCI441_INTERNAL::drawPartialDisk( inner, outer, slices, rings, start * glm::pi<float>() / 180.0f, sweep * glm::pi<float>() / 180.0f, GL_LINE );
696 assert( radius > 0.0f );
697 assert( stacks > 1 );
698 assert( slices > 2 );
700 CSCI441_INTERNAL::drawSphere( radius, stacks, slices, GL_FILL );
705 assert( radius > 0.0f );
707 assert( slices > 2 );
709 CSCI441_INTERNAL::drawSphere( radius, stacks, slices, GL_LINE );
714 assert( radius > 0.0f );
715 assert( stacks > 1 );
716 assert( slices > 2 );
718 CSCI441_INTERNAL::drawHalfSphere( radius, stacks, slices, GL_FILL );
723 assert( radius > 0.0f );
725 assert( slices > 2 );
727 CSCI441_INTERNAL::drawHalfSphere( radius, stacks, slices, GL_LINE );
732 assert( radius > 0.0f );
733 assert( stacks > 1 );
734 assert( slices > 2 );
736 CSCI441_INTERNAL::drawDome( radius, stacks, slices, GL_FILL );
741 assert( radius > 0.0f );
743 assert( slices > 2 );
745 CSCI441_INTERNAL::drawDome( radius, stacks, slices, GL_LINE );
750 CSCI441_INTERNAL::drawTeapot(GL_FILL);
755 CSCI441_INTERNAL::drawTeapot(GL_LINE);
760 assert( innerRadius > 0.0f );
761 assert( outerRadius > 0.0f );
765 CSCI441_INTERNAL::drawTorus( innerRadius, outerRadius, sides, rings, GL_FILL );
770 assert( innerRadius > 0.0f );
771 assert( outerRadius > 0.0f );
775 CSCI441_INTERNAL::drawTorus( innerRadius, outerRadius, sides, rings, GL_LINE );
782inline void CSCI441_INTERNAL::deleteObjectVAOs() {
783 for(
auto &[sideLength, vaod] : _cubeVAO) {
784 glDeleteVertexArrays(1, &vaod);
788 for(
auto &[sideLength, vaod] : _cubeVAOIndexed) {
789 glDeleteVertexArrays(1, &vaod);
791 _cubeVAOIndexed.clear();
793 for(
auto &[cylData, vaod] : _cylinderVAO) {
794 glDeleteVertexArrays(1, &vaod);
796 _cylinderVAO.clear();
798 for(
auto &[diskData, vaod] : _diskVAO) {
799 glDeleteVertexArrays(1, &vaod);
803 for(
auto &[sphereData, vaod] : _sphereVAO) {
804 glDeleteVertexArrays(1, &vaod);
808 for(
auto &[torusData, vaod] : _torusVAO) {
809 glDeleteVertexArrays(1, &vaod);
814inline void CSCI441_INTERNAL::deleteObjectVBOs() {
815 for(
auto &[sideLength, vbod] : _cubeVBO) {
816 glDeleteBuffers(1, &vbod);
820 for(
auto &[sideLength, vbod] : _cubeVBOIndexed) {
821 glDeleteBuffers(1, &vbod);
823 _cubeVBOIndexed.clear();
825 for(
auto &[sideLength, vbod] : _cubeIBOIndexed) {
826 glDeleteBuffers(1, &vbod);
828 _cubeIBOIndexed.clear();
830 for(
auto &[cylData, vbod] : _cylinderVBO) {
831 glDeleteBuffers(1, &vbod);
833 _cylinderVBO.clear();
835 for(
auto &[diskData, vbod] : _diskVBO) {
836 glDeleteBuffers(1, &vbod);
840 for(
auto &[sphereData, vbod] : _sphereVBO) {
841 glDeleteBuffers(1, &vbod);
845 for(
auto &[torusData, vbod] : _torusVBO) {
846 glDeleteBuffers(1, &vbod);
851inline void CSCI441_INTERNAL::drawCube( GLfloat sideLength, GLenum renderMode ) {
852 drawCubeIndexed(sideLength, renderMode);
855inline void CSCI441_INTERNAL::drawCubeFlat( GLfloat sideLength, GLenum renderMode ) {
856 if( CSCI441_INTERNAL::_cubeVAO.count( sideLength ) == 0 ) {
857 CSCI441_INTERNAL::generateCubeVAOFlat( sideLength );
860 const GLuint64 NUM_VERTICES = 36;
862 GLint currentPolygonMode[2];
863 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
865 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
866 glBindVertexArray( CSCI441_INTERNAL::_cubeVAO.find( sideLength )->second );
867 glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_cubeVBO.find( sideLength )->second );
868 if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
869 glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
870 glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
872 if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
873 glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
874 glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES) );
876 if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
877 glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
878 glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES * 2) );
881 glDrawArrays( GL_TRIANGLES, 0, 36 );
883 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
886inline void CSCI441_INTERNAL::drawCubeIndexed( GLfloat sideLength, GLenum renderMode ) {
887 if( CSCI441_INTERNAL::_cubeVAOIndexed.count( sideLength ) == 0 ) {
888 CSCI441_INTERNAL::generateCubeVAOIndexed( sideLength );
891 const GLuint64 NUM_VERTICES = 8;
893 GLint currentPolygonMode[2];
894 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
896 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
897 glBindVertexArray( CSCI441_INTERNAL::_cubeVAOIndexed.find( sideLength )->second );
898 glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_cubeVBOIndexed.find( sideLength )->second );
899 if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
900 glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
901 glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
903 if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
904 glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
905 glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES) );
907 if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
908 glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
909 glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES * 2) );
912 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, (
void*)
nullptr);
914 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
917inline void CSCI441_INTERNAL::drawCylinder( GLfloat base, GLfloat top, GLfloat height, GLuint stacks, GLuint slices, GLenum renderMode ) {
918 CylinderData cylData = { base, top, height, stacks, slices };
919 if( CSCI441_INTERNAL::_cylinderVAO.count( cylData ) == 0 ) {
920 CSCI441_INTERNAL::generateCylinderVAO( cylData );
923 const GLuint64 NUM_VERTICES = cylData.numVertices();
925 GLint currentPolygonMode[2];
926 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
928 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
929 glBindVertexArray( CSCI441_INTERNAL::_cylinderVAO.find( cylData )->second );
930 glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_cylinderVBO.find( cylData )->second );
931 if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
932 glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
933 glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
935 if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
936 glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
937 glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES) );
939 if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
940 glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
941 glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES * 2) );
944 for(GLuint stackNum = 0; stackNum < stacks; stackNum++) {
945 glDrawArrays( GL_TRIANGLE_STRIP,
static_cast<GLint
>((slices+1)*2*stackNum),
static_cast<GLint
>((slices+1)*2) );
948 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
951inline void CSCI441_INTERNAL::drawPartialDisk(GLfloat innerRadius, GLfloat outerRadius, GLuint slices, GLuint rings, GLfloat startAngle, GLfloat sweepAngle, GLenum renderMode ) {
952 DiskData diskData = {innerRadius, outerRadius, startAngle, sweepAngle, slices, rings };
953 if( CSCI441_INTERNAL::_diskVAO.count( diskData ) == 0 ) {
954 CSCI441_INTERNAL::generateDiskVAO( diskData );
957 const GLuint64 NUM_VERTICES = diskData.numVertices();
959 GLint currentPolygonMode[2];
960 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
962 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
963 glBindVertexArray( CSCI441_INTERNAL::_diskVAO.find( diskData )->second );
964 glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_diskVBO.find( diskData )->second );
965 if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
966 glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
967 glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
969 if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
970 glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
971 glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES) );
973 if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
974 glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
975 glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES * 2) );
978 for(GLuint ringNum = 0; ringNum < rings; ringNum++) {
979 glDrawArrays( GL_TRIANGLE_STRIP,
static_cast<GLint
>((slices+1)*2*ringNum),
static_cast<GLint
>((slices+1)*2) );
982 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
985inline void CSCI441_INTERNAL::drawSphere( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode ) {
986 SphereData sphereData = { radius, stacks, slices };
987 if( CSCI441_INTERNAL::_sphereVAO.count( sphereData ) == 0 ) {
988 CSCI441_INTERNAL::generateSphereVAO( sphereData );
991 const GLuint64 NUM_VERTICES = sphereData.numVertices();
993 GLint currentPolygonMode[2];
994 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
996 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
997 glBindVertexArray( CSCI441_INTERNAL::_sphereVAO.find( sphereData )->second );
998 glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_sphereVBO.find( sphereData )->second );
999 if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
1000 glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
1001 glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
1003 if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
1004 glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
1005 glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES) );
1007 if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
1008 glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
1009 glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES * 2) );
1012 glDrawArrays( GL_TRIANGLE_FAN, 0,
static_cast<GLint
>(slices+2) );
1014 for(GLuint stackNum = 1; stackNum < stacks-1; stackNum++) {
1015 glDrawArrays( GL_TRIANGLE_STRIP,
static_cast<GLint
>((slices+2) + (stackNum-1)*((slices+1)*2)),
static_cast<GLint
>((slices+1)*2) );
1018 glDrawArrays( GL_TRIANGLE_FAN,
static_cast<GLint
>((slices+2) + (stacks-2)*(slices+1)*2),
static_cast<GLint
>(slices+2) );
1020 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
1023inline void CSCI441_INTERNAL::drawHalfSphere( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode ) {
1024 SphereData sphereData = { radius, stacks, slices };
1025 if( CSCI441_INTERNAL::_sphereVAO.count( sphereData ) == 0 ) {
1026 CSCI441_INTERNAL::generateSphereVAO( sphereData );
1029 const GLuint64 NUM_VERTICES = sphereData.numVertices();
1031 GLint currentPolygonMode[2];
1032 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
1034 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
1035 glBindVertexArray( CSCI441_INTERNAL::_sphereVAO.find( sphereData )->second );
1036 glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_sphereVBO.find( sphereData )->second );
1037 if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
1038 glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
1039 glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
1041 if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
1042 glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
1043 glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES) );
1045 if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
1046 glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
1047 glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES * 2) );
1050 glDrawArrays( GL_TRIANGLE_FAN,
static_cast<GLint
>((slices+2)/2),
static_cast<GLint
>((slices+2)/2) );
1052 for(GLuint stackNum = 1; stackNum < stacks-1; stackNum++) {
1053 glDrawArrays( GL_TRIANGLE_STRIP,
static_cast<GLint
>((slices+2) + (stackNum-1)*((slices+1)*2)),
static_cast<GLint
>(slices+2) );
1056 glDrawArrays( GL_TRIANGLE_FAN,
static_cast<GLint
>((slices+2) + (stacks-2)*(slices+1)*2),
static_cast<GLint
>((slices+2)/2) );
1058 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
1060 drawPartialDisk(0.0f, radius, slices, stacks, 0.0f, glm::two_pi<float>(), renderMode);
1063inline void CSCI441_INTERNAL::drawDome( GLfloat radius, GLuint stacks, GLuint slices, GLenum renderMode ) {
1064 SphereData sphereData = { radius, stacks, slices };
1065 if( CSCI441_INTERNAL::_sphereVAO.count( sphereData ) == 0 ) {
1066 CSCI441_INTERNAL::generateSphereVAO( sphereData );
1069 const GLuint64 NUM_VERTICES = sphereData.numVertices();
1071 GLint currentPolygonMode[2];
1072 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
1074 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
1075 glBindVertexArray( CSCI441_INTERNAL::_sphereVAO.find( sphereData )->second );
1076 glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_sphereVBO.find( sphereData )->second );
1077 if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
1078 glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
1079 glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
1081 if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
1082 glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
1083 glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES) );
1085 if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
1086 glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
1087 glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES * 2) );
1090 glDrawArrays( GL_TRIANGLE_FAN, 0,
static_cast<GLint
>(slices+2) );
1092 for(GLuint stackNum = (stacks-1)/2; stackNum < stacks-1; stackNum++) {
1093 glDrawArrays( GL_TRIANGLE_STRIP,
static_cast<GLint
>((slices+2) + (stackNum-1)*((slices+1)*2)),
static_cast<GLint
>((slices+1)*2) );
1096 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
1099inline void CSCI441_INTERNAL::drawTorus( GLfloat innerRadius, GLfloat outerRadius, GLuint sides, GLuint rings, GLenum renderMode ) {
1100 TorusData torusData = { innerRadius, outerRadius, sides, rings };
1101 if( CSCI441_INTERNAL::_torusVAO.count( torusData ) == 0 ) {
1102 CSCI441_INTERNAL::generateTorusVAO( torusData );
1105 const GLuint64 NUM_VERTICES = torusData.numVertices();
1107 GLint currentPolygonMode[2];
1108 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
1110 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
1111 glBindVertexArray( CSCI441_INTERNAL::_torusVAO.find( torusData )->second );
1112 glBindBuffer( GL_ARRAY_BUFFER, CSCI441_INTERNAL::_torusVBO.find( torusData )->second );
1113 if(CSCI441_INTERNAL::_positionAttributeLocation != -1) {
1114 glEnableVertexAttribArray( CSCI441_INTERNAL::_positionAttributeLocation );
1115 glVertexAttribPointer( CSCI441_INTERNAL::_positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)
nullptr );
1117 if(CSCI441_INTERNAL::_normalAttributeLocation != -1) {
1118 glEnableVertexAttribArray( CSCI441_INTERNAL::_normalAttributeLocation );
1119 glVertexAttribPointer( CSCI441_INTERNAL::_normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES) );
1121 if(CSCI441_INTERNAL::_texCoordAttributeLocation != -1) {
1122 glEnableVertexAttribArray( CSCI441_INTERNAL::_texCoordAttributeLocation );
1123 glVertexAttribPointer( CSCI441_INTERNAL::_texCoordAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, (
void*)(
sizeof(glm::vec3) * NUM_VERTICES * 2) );
1126 for(GLuint ringNum = 0; ringNum < rings; ringNum++) {
1127 glDrawArrays( GL_TRIANGLE_STRIP,
static_cast<GLint
>(ringNum*sides*4),
static_cast<GLint
>(sides*4) );
1130 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
1133inline void CSCI441_INTERNAL::drawTeapot( GLenum renderMode ) {
1134 GLint currentPolygonMode[2];
1135 glGetIntegerv(GL_POLYGON_MODE, currentPolygonMode);
1137 glPolygonMode( GL_FRONT_AND_BACK, renderMode );
1138 CSCI441_INTERNAL::teapot();
1139 glPolygonMode( GL_FRONT_AND_BACK, currentPolygonMode[0] );
1142inline void CSCI441_INTERNAL::generateCubeVAOFlat( GLfloat sideLength ) {
1144 glGenVertexArrays( 1, &vaod );
1145 glBindVertexArray( vaod );
1148 glGenBuffers( 1, &vbod );
1149 glBindBuffer( GL_ARRAY_BUFFER, vbod );
1151 const GLfloat CORNER_POINT = sideLength / 2.0f;
1153 const GLuint64 NUM_VERTICES = 36;
1155 glm::vec3 vertices[NUM_VERTICES] = {
1157 {-CORNER_POINT, -CORNER_POINT, -CORNER_POINT}, {-CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {-CORNER_POINT, CORNER_POINT, -CORNER_POINT},
1158 {-CORNER_POINT, CORNER_POINT, -CORNER_POINT}, {-CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {-CORNER_POINT, CORNER_POINT, CORNER_POINT},
1160 {CORNER_POINT, CORNER_POINT, CORNER_POINT}, {CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {CORNER_POINT, CORNER_POINT, -CORNER_POINT},
1161 {CORNER_POINT, CORNER_POINT, -CORNER_POINT}, {CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {CORNER_POINT, -CORNER_POINT, -CORNER_POINT},
1163 {-CORNER_POINT, CORNER_POINT, -CORNER_POINT}, {-CORNER_POINT, CORNER_POINT, CORNER_POINT}, {CORNER_POINT, CORNER_POINT, -CORNER_POINT},
1164 {CORNER_POINT, CORNER_POINT, -CORNER_POINT}, {-CORNER_POINT, CORNER_POINT, CORNER_POINT}, {CORNER_POINT, CORNER_POINT, CORNER_POINT},
1166 {CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {-CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {CORNER_POINT, -CORNER_POINT, -CORNER_POINT},
1167 {CORNER_POINT, -CORNER_POINT, -CORNER_POINT}, {-CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {-CORNER_POINT, -CORNER_POINT, -CORNER_POINT},
1169 {CORNER_POINT, CORNER_POINT, -CORNER_POINT}, {CORNER_POINT, -CORNER_POINT, -CORNER_POINT}, {-CORNER_POINT, CORNER_POINT, -CORNER_POINT},
1170 {-CORNER_POINT, CORNER_POINT, -CORNER_POINT}, {CORNER_POINT, -CORNER_POINT, -CORNER_POINT}, {-CORNER_POINT, -CORNER_POINT, -CORNER_POINT},
1172 {-CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {-CORNER_POINT, CORNER_POINT, CORNER_POINT},
1173 {-CORNER_POINT, CORNER_POINT, CORNER_POINT}, {CORNER_POINT, -CORNER_POINT, CORNER_POINT}, {CORNER_POINT, CORNER_POINT, CORNER_POINT}
1175 glm::vec3 normals[NUM_VERTICES] = {
1177 {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f},
1178 {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f},
1180 {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f},
1181 {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f},
1183 {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f},
1184 {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f},
1186 {0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f},
1187 {0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f},
1189 {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, -1.0f},
1190 {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, -1.0f},
1192 {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f},
1193 {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}
1195 glm::vec2 texCoords[NUM_VERTICES] = {
1197 {0.0f, 0.0f}, {1.0f, 0.0f}, {0.0f, 1.0f},
1198 {0.0f, 1.0f}, {1.0f, 0.0f}, {1.0f, 1.0f},
1200 {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f},
1201 {1.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f},
1203 {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 0.0f},
1204 {0.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f},
1206 {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 0.0f},
1207 {0.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f},
1209 {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f},
1210 {1.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f},
1212 {0.0f, 0.0f}, {1.0f, 0.0f}, {0.0f, 1.0f},
1213 {0.0f, 1.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}
1216 glBufferData(GL_ARRAY_BUFFER, (
sizeof(glm::vec3)*2 +
sizeof(glm::vec2)) * NUM_VERTICES,
nullptr, GL_STATIC_DRAW );
1217 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(glm::vec3) * NUM_VERTICES, vertices );
1218 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES,
sizeof(glm::vec3) * NUM_VERTICES, normals );
1219 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES * 2,
sizeof(glm::vec2) * NUM_VERTICES, texCoords );
1221 CSCI441_INTERNAL::_cubeVAO.insert( std::pair<GLfloat, GLuint>( sideLength, vaod ) );
1222 CSCI441_INTERNAL::_cubeVBO.insert( std::pair<GLfloat, GLuint>( sideLength, vbod ) );
1225inline void CSCI441_INTERNAL::generateCubeVAOIndexed( GLfloat sideLength ) {
1226 const GLfloat CORNER_POINT = sideLength / 2.0f;
1228 const GLuint64 NUM_VERTICES = 8;
1230 glm::vec3 vertices[NUM_VERTICES] = {
1231 { -CORNER_POINT, -CORNER_POINT, -CORNER_POINT },
1232 { CORNER_POINT, -CORNER_POINT, -CORNER_POINT },
1233 { CORNER_POINT, CORNER_POINT, -CORNER_POINT },
1234 { -CORNER_POINT, CORNER_POINT, -CORNER_POINT },
1235 { -CORNER_POINT, -CORNER_POINT, CORNER_POINT },
1236 { CORNER_POINT, -CORNER_POINT, CORNER_POINT },
1237 { CORNER_POINT, CORNER_POINT, CORNER_POINT },
1238 { -CORNER_POINT, CORNER_POINT, CORNER_POINT }
1240 glm::vec3 normals[NUM_VERTICES] = {
1241 {-1.0f, -1.0f, -1.0f},
1242 { 1.0f, -1.0f, -1.0f},
1243 { 1.0f, 1.0f, -1.0f},
1244 {-1.0f, 1.0f, -1.0f},
1245 {-1.0f, -1.0f, 1.0f},
1246 { 1.0f, -1.0f, 1.0f},
1247 { 1.0f, 1.0f, 1.0f},
1250 glm::vec3 texCoords[NUM_VERTICES] = {
1251 {-1.0f, -1.0f, -1.0f},
1252 { 1.0f, -1.0f, -1.0f},
1253 { 1.0f, 1.0f, -1.0f},
1254 {-1.0f, 1.0f, -1.0f},
1255 {-1.0f, -1.0f, 1.0f},
1256 { 1.0f, -1.0f, 1.0f},
1257 { 1.0f, 1.0f, 1.0f},
1260 GLushort indices[36] = {
1270 glGenVertexArrays( 1, &vaod );
1271 glBindVertexArray( vaod );
1274 glGenBuffers( 2, vbods );
1276 glBindBuffer( GL_ARRAY_BUFFER, vbods[0] );
1277 glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES * 3,
nullptr, GL_STATIC_DRAW );
1278 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(glm::vec3) * NUM_VERTICES, vertices );
1279 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES,
sizeof(glm::vec3) * NUM_VERTICES, normals );
1280 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES * 2,
sizeof(glm::vec3) * NUM_VERTICES, texCoords );
1282 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vbods[1] );
1283 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
sizeof(indices), indices, GL_STATIC_DRAW) ;
1285 CSCI441_INTERNAL::_cubeVAOIndexed.insert( std::pair<GLfloat, GLuint>( sideLength, vaod ) );
1286 CSCI441_INTERNAL::_cubeVBOIndexed.insert( std::pair<GLfloat, GLuint>( sideLength, vbods[0] ) );
1287 CSCI441_INTERNAL::_cubeIBOIndexed.insert( std::pair<GLfloat, GLuint>( sideLength, vbods[1] ) );
1290inline void CSCI441_INTERNAL::generateCylinderVAO( CylinderData cylData ) {
1292 glGenVertexArrays( 1, &vaod );
1293 glBindVertexArray( vaod );
1296 glGenBuffers( 1, &vbod );
1297 glBindBuffer( GL_ARRAY_BUFFER, vbod );
1299 const GLuint64 NUM_VERTICES = cylData.numVertices();
1301 GLfloat sliceStep = glm::two_pi<float>() / (GLfloat)cylData.slices;
1302 GLfloat stackStep = cylData.height / (GLfloat)cylData.stacks;
1304 auto vertices =
new glm::vec3[NUM_VERTICES];
1305 auto normals =
new glm::vec3[NUM_VERTICES];
1306 auto texCoords =
new glm::vec2[NUM_VERTICES];
1310 for(GLuint stackNum = 0; stackNum < cylData.stacks; stackNum++ ) {
1311 GLfloat botRadius = cylData.radiusBase * (GLfloat)(cylData.stacks - stackNum) / (GLfloat)cylData.stacks + cylData.top * (GLfloat)stackNum / (GLfloat)cylData.stacks;
1312 GLfloat topRadius = cylData.radiusBase * (GLfloat)(cylData.stacks - stackNum - 1) / (GLfloat)cylData.stacks + cylData.top * (GLfloat)(stackNum + 1) / (GLfloat)cylData.stacks;
1314 for(GLuint sliceNum = 0; sliceNum <= cylData.slices; sliceNum++ ) {
1315 vertices[ idx ].x = glm::cos( sliceNum * sliceStep )*botRadius;
1316 vertices[ idx ].y = (GLfloat)stackNum * stackStep;
1317 vertices[ idx ].z = glm::sin( sliceNum * sliceStep )*botRadius;
1319 normals[ idx ].x = glm::cos( sliceNum * sliceStep );
1320 normals[ idx ].y = 0.0f;
1321 normals[ idx ].z = glm::sin( sliceNum * sliceStep );
1323 texCoords[ idx ].s = (GLfloat)sliceNum / (GLfloat)cylData.slices;
1324 texCoords[ idx ].t = (GLfloat)stackNum / (GLfloat)cylData.stacks;
1328 vertices[ idx ].x = glm::cos( sliceNum * sliceStep )*topRadius;
1329 vertices[ idx ].y = (GLfloat)(stackNum+1) * stackStep;
1330 vertices[ idx ].z = glm::sin( sliceNum * sliceStep )*topRadius;
1332 normals[ idx ].x = glm::cos( sliceNum * sliceStep );
1333 normals[ idx ].y = 0.0f;
1334 normals[ idx ].z = glm::sin( sliceNum * sliceStep );
1336 texCoords[ idx ].s = (GLfloat)sliceNum / (GLfloat)cylData.slices;
1337 texCoords[ idx ].t = (GLfloat)(stackNum+1) / (GLfloat)cylData.stacks;
1343 glBufferData(GL_ARRAY_BUFFER, (
sizeof(glm::vec3)*2 +
sizeof(glm::vec2)) * NUM_VERTICES,
nullptr, GL_STATIC_DRAW );
1344 glBufferSubData(GL_ARRAY_BUFFER, 0,
static_cast<GLsizeiptr
>(
sizeof(glm::vec3) * NUM_VERTICES), vertices );
1345 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLintptr
>(
sizeof(glm::vec3) * NUM_VERTICES),
static_cast<GLsizeiptr
>(
sizeof(glm::vec3) * NUM_VERTICES), normals );
1346 glBufferSubData(GL_ARRAY_BUFFER,
static_cast<GLintptr
>(
sizeof(glm::vec3) * NUM_VERTICES * 2),
static_cast<GLsizeiptr
>(
sizeof(glm::vec2) * NUM_VERTICES), texCoords );
1348 CSCI441_INTERNAL::_cylinderVAO.insert( std::pair<CylinderData, GLuint>( cylData, vaod ) );
1349 CSCI441_INTERNAL::_cylinderVBO.insert( std::pair<CylinderData, GLuint>( cylData, vbod ) );
1356inline void CSCI441_INTERNAL::generateDiskVAO( DiskData diskData ) {
1358 glGenVertexArrays( 1, &vaod );
1359 glBindVertexArray( vaod );
1362 glGenBuffers( 1, &vbod );
1363 glBindBuffer( GL_ARRAY_BUFFER, vbod );
1365 const GLuint64 NUM_VERTICES = diskData.numVertices();
1367 GLfloat sliceStep = diskData.sweepAngle / (GLfloat)diskData.slices;
1368 GLfloat ringStep = (diskData.outerRadius - diskData.innerRadius) / (GLfloat)diskData.rings;
1370 auto vertices =
new glm::vec3[NUM_VERTICES];
1371 auto normals =
new glm::vec3[NUM_VERTICES];
1372 auto texCoords =
new glm::vec2[NUM_VERTICES];
1376 for(GLuint ringNum = 0; ringNum < diskData.rings; ringNum++ ) {
1377 GLfloat currRadius = diskData.innerRadius + (GLfloat)ringNum * ringStep;
1378 GLfloat nextRadius = diskData.innerRadius + (GLfloat)(ringNum + 1) * ringStep;
1380 GLfloat theta = diskData.startAngle;
1381 for(GLuint i = 0; i <= diskData.slices; i++ ) {
1382 vertices[ idx ].x = glm::cos(theta)*currRadius;
1383 vertices[ idx ].y = glm::sin(theta)*currRadius;
1384 vertices[ idx ].z = 0.0f;
1386 normals[ idx ].x = 0.0f;
1387 normals[ idx ].y = 0.0f;
1388 normals[ idx ].z = 1.0f;
1390 texCoords[ idx ].s = glm::cos(theta)*(currRadius/diskData.outerRadius);
1391 texCoords[ idx ].t = glm::sin(theta)*(currRadius/diskData.outerRadius);
1395 vertices[ idx ].x = glm::cos(theta)*nextRadius;
1396 vertices[ idx ].y = glm::sin(theta)*nextRadius;
1397 vertices[ idx ].z = 0.0f;
1399 normals[ idx ].x = 0.0f;
1400 normals[ idx ].y = 0.0f;
1401 normals[ idx ].z = 1.0f;
1403 texCoords[ idx ].s = glm::cos(theta)*(nextRadius/diskData.outerRadius);
1404 texCoords[ idx ].t = glm::sin(theta)*(nextRadius/diskData.outerRadius);
1411 glBufferData(GL_ARRAY_BUFFER, (
sizeof(glm::vec3)*2 +
sizeof(glm::vec2)) * NUM_VERTICES,
nullptr, GL_STATIC_DRAW );
1412 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(glm::vec3) * NUM_VERTICES, vertices );
1413 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES,
sizeof(glm::vec3) * NUM_VERTICES, normals );
1414 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES * 2,
sizeof(glm::vec2) * NUM_VERTICES, texCoords );
1416 CSCI441_INTERNAL::_diskVAO.insert( std::pair<DiskData, GLuint>( diskData, vaod ) );
1417 CSCI441_INTERNAL::_diskVBO.insert( std::pair<DiskData, GLuint>( diskData, vbod ) );
1424inline void CSCI441_INTERNAL::generateSphereVAO( SphereData sphereData ) {
1426 glGenVertexArrays( 1, &vaod );
1427 glBindVertexArray( vaod );
1430 glGenBuffers( 1, &vbod );
1431 glBindBuffer( GL_ARRAY_BUFFER, vbod );
1433 const GLuint64 NUM_VERTICES = sphereData.numVertices();
1435 GLfloat sliceStep = glm::two_pi<float>() / (GLfloat)sphereData.slices;
1436 GLfloat stackStep = glm::pi<float>() / (GLfloat)sphereData.stacks;
1438 auto vertices =
new glm::vec3[NUM_VERTICES];
1439 auto normals =
new glm::vec3[NUM_VERTICES];
1440 auto texCoords =
new glm::vec2[NUM_VERTICES];
1445 GLfloat phi = stackStep * (GLfloat)sphereData.stacks;
1446 GLfloat phiNext = stackStep * (GLfloat)(sphereData.stacks - 1);
1448 vertices[ idx ].x = 0.0f;
1449 vertices[ idx ].y = -glm::cos( phi )*sphereData.radius;
1450 vertices[ idx ].z = 0.0f;
1452 normals[ idx ].x = 0.0f;
1453 normals[ idx ].y = 1.0f;
1454 normals[ idx ].z = 0.0f;
1456 texCoords[ idx ].s = 0.5f;
1457 texCoords[ idx ].t = 1.0f;
1461 for(GLuint sliceNum = 0; sliceNum <= sphereData.slices; sliceNum++ ) {
1462 GLfloat theta = sliceStep * (GLfloat)sliceNum;
1464 vertices[ idx ].x = -glm::cos( theta )*glm::sin( phiNext )*sphereData.radius;
1465 vertices[ idx ].y = -glm::cos( phiNext )*sphereData.radius;
1466 vertices[ idx ].z = glm::sin( theta )*glm::sin( phiNext )*sphereData.radius;
1468 normals[ idx ].x = -glm::cos( theta )*glm::sin( phiNext );
1469 normals[ idx ].y = -glm::cos( phiNext );
1470 normals[ idx ].z = glm::sin( theta )*glm::sin( phiNext );
1472 texCoords[ idx ].s = (GLfloat)sliceNum / (GLfloat)sphereData.slices;
1473 texCoords[ idx ].t = 1.0f;
1479 for(GLuint stackNum = 1; stackNum < sphereData.stacks - 1; stackNum++ ) {
1480 phi = stackStep * (GLfloat)stackNum;
1481 phiNext = stackStep * (GLfloat)(stackNum + 1);
1483 for(GLuint sliceNum = sphereData.slices; sliceNum > 0; sliceNum-- ) {
1484 GLfloat theta = sliceStep * (GLfloat)sliceNum;
1486 vertices[ idx ].x = -glm::cos( theta )*glm::sin( phi )*sphereData.radius;
1487 vertices[ idx ].y = -glm::cos( phi )*sphereData.radius;
1488 vertices[ idx ].z = glm::sin( theta )*glm::sin( phi )*sphereData.radius;
1490 normals[ idx ].x = -glm::cos( theta )*glm::sin( phi );
1491 normals[ idx ].y = -glm::cos( phi );
1492 normals[ idx ].z = glm::sin( theta )*glm::sin( phi );
1494 texCoords[ idx ].s = (GLfloat)sliceNum / (GLfloat)sphereData.slices;
1495 texCoords[ idx ].t = (GLfloat)(stackNum-1) / (GLfloat)(sphereData.stacks - 2);
1499 vertices[ idx ].x = -glm::cos( theta )*glm::sin( phiNext )*sphereData.radius;
1500 vertices[ idx ].y = -glm::cos( phiNext )*sphereData.radius;
1501 vertices[ idx ].z = glm::sin( theta )*glm::sin( phiNext )*sphereData.radius;
1503 normals[ idx ].x = -glm::cos( theta )*glm::sin( phiNext );
1504 normals[ idx ].y = -glm::cos( phiNext );
1505 normals[ idx ].z = glm::sin( theta )*glm::sin( phiNext );
1507 texCoords[ idx ].s = (GLfloat)sliceNum / (GLfloat)sphereData.slices;
1508 texCoords[ idx ].t = (GLfloat)(stackNum) / (GLfloat)(sphereData.stacks - 2);
1513 vertices[ idx ].x = -glm::sin( phi )*sphereData.radius;
1514 vertices[ idx ].y = -glm::cos( phi )*sphereData.radius;
1515 vertices[ idx ].z = 0.0f;
1517 normals[ idx ].x = -glm::sin( phi );
1518 normals[ idx ].y = -glm::cos( phi );
1519 normals[ idx ].z = 0.0f;
1521 texCoords[ idx ].s = 0.0f;
1522 texCoords[ idx ].t = (GLfloat)(stackNum-1) / (GLfloat)(sphereData.stacks - 2);
1526 vertices[ idx ].x = -glm::sin( phiNext )*sphereData.radius;
1527 vertices[ idx ].y = -glm::cos( phiNext )*sphereData.radius;
1528 vertices[ idx ].z = 0.0f;
1530 normals[ idx ].x = -glm::sin( phiNext );
1531 normals[ idx ].y = -glm::cos( phiNext );
1532 normals[ idx ].z = 0.0f;
1534 texCoords[ idx ].s = 0.0f;
1535 texCoords[ idx ].t = (GLfloat)(stackNum) / (GLfloat)(sphereData.stacks - 2);
1542 phiNext = stackStep;
1544 vertices[ idx ].x = 0.0f;
1545 vertices[ idx ].y = -glm::cos( phi )*sphereData.radius;
1546 vertices[ idx ].z = 0.0f;
1548 normals[ idx ].x = 0.0f;
1549 normals[ idx ].y = -1.0f;
1550 normals[ idx ].z = 0.0f;
1552 texCoords[ idx ].s = 0.5f;
1553 texCoords[ idx ].t = 0.0f;
1557 for(GLuint sliceNum = sphereData.slices; sliceNum > 0; sliceNum--) {
1558 GLfloat theta = sliceStep * (GLfloat)sliceNum;
1560 vertices[ idx ].x = -glm::cos( theta )*glm::sin( phiNext )*sphereData.radius;
1561 vertices[ idx ].y = -glm::cos( phiNext )*sphereData.radius;
1562 vertices[ idx ].z = glm::sin( theta )*glm::sin( phiNext )*sphereData.radius;
1564 normals[ idx ].x = -glm::cos( theta )*glm::sin( phiNext );
1565 normals[ idx ].y = -glm::cos( phiNext );
1566 normals[ idx ].z = glm::sin( theta )*glm::sin( phiNext );
1568 texCoords[ idx ].s = (GLfloat)sliceNum / (GLfloat)sphereData.slices;
1569 texCoords[ idx ].t = 0.0f;
1574 vertices[ idx ].x = -glm::sin( phiNext )*sphereData.radius;
1575 vertices[ idx ].y = -glm::cos( phiNext )*sphereData.radius;
1576 vertices[ idx ].z = 0.0f;
1578 normals[ idx ].x = -glm::sin( phiNext );
1579 normals[ idx ].y = -glm::cos( phiNext );
1580 normals[ idx ].z = 0.0f;
1582 texCoords[ idx ].s = 0.0f;
1583 texCoords[ idx ].t = 0.0f;
1585 glBufferData(GL_ARRAY_BUFFER, (
sizeof(glm::vec3)*2 +
sizeof(glm::vec2)) * NUM_VERTICES,
nullptr, GL_STATIC_DRAW );
1586 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(glm::vec3) * NUM_VERTICES, vertices );
1587 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES,
sizeof(glm::vec3) * NUM_VERTICES, normals );
1588 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES * 2,
sizeof(glm::vec2) * NUM_VERTICES, texCoords );
1590 CSCI441_INTERNAL::_sphereVAO.insert( std::pair<SphereData, GLuint>( sphereData, vaod ) );
1591 CSCI441_INTERNAL::_sphereVBO.insert( std::pair<SphereData, GLuint>( sphereData, vbod ) );
1598inline void CSCI441_INTERNAL::generateTorusVAO( TorusData torusData ) {
1600 glGenVertexArrays( 1, &vaod );
1601 glBindVertexArray( vaod );
1604 glGenBuffers( 1, &vbod );
1605 glBindBuffer( GL_ARRAY_BUFFER, vbod );
1607 const GLuint64 NUM_VERTICES = torusData.numVertices();
1609 auto vertices =
new glm::vec3[NUM_VERTICES];
1610 auto normals =
new glm::vec3[NUM_VERTICES];
1611 auto texCoords =
new glm::vec2[NUM_VERTICES];
1615 GLfloat sideStep = glm::two_pi<float>() / (GLfloat)torusData.sides;
1616 GLfloat ringStep = glm::two_pi<float>() / (GLfloat)torusData.rings;
1618 for(GLuint ringNum = 0; ringNum < torusData.rings; ringNum++ ) {
1619 GLfloat currTheta = ringStep * (GLfloat)ringNum;
1620 GLfloat nextTheta = ringStep * (GLfloat)(ringNum+1);
1622 for(GLuint sideNum = 0; sideNum < torusData.sides; sideNum++ ) {
1623 GLfloat currPhi = sideStep * (GLfloat)sideNum;
1624 GLfloat nextPhi = sideStep * (GLfloat)(sideNum+1);
1626 vertices[ idx ].x = (torusData.outerRadius + torusData.innerRadius * glm::cos(currPhi ) ) * glm::cos(currTheta );
1627 vertices[ idx ].y = (torusData.outerRadius + torusData.innerRadius * glm::cos(currPhi ) ) * glm::sin(currTheta );
1628 vertices[ idx ].z = torusData.innerRadius * glm::sin(currPhi );
1630 normals[ idx ].x = glm::cos( currPhi ) * glm::cos( currTheta );
1631 normals[ idx ].y = glm::cos( currPhi ) * glm::sin( currTheta );
1632 normals[ idx ].z = glm::sin( currPhi );
1634 texCoords[ idx ].s = (GLfloat)sideNum / (GLfloat)torusData.sides;
1635 texCoords[ idx ].t = (GLfloat)ringNum / (GLfloat)torusData.rings;
1639 vertices[ idx ].x = (torusData.outerRadius + torusData.innerRadius * glm::cos(currPhi ) ) * glm::cos(nextTheta );
1640 vertices[ idx ].y = (torusData.outerRadius + torusData.innerRadius * glm::cos(currPhi ) ) * glm::sin(nextTheta );
1641 vertices[ idx ].z = torusData.innerRadius * glm::sin(currPhi );
1643 normals[ idx ].x = glm::cos( currPhi ) * glm::cos( nextTheta );
1644 normals[ idx ].y = glm::cos( currPhi ) * glm::sin( nextTheta );
1645 normals[ idx ].z = glm::sin( currPhi );
1647 texCoords[ idx ].s = (GLfloat)sideNum / (GLfloat)torusData.sides;
1648 texCoords[ idx ].t = (GLfloat)(ringNum+1) / (GLfloat)torusData.rings;
1652 vertices[ idx ].x = (torusData.outerRadius + torusData.innerRadius * glm::cos(nextPhi ) ) * glm::cos(currTheta );
1653 vertices[ idx ].y = (torusData.outerRadius + torusData.innerRadius * glm::cos(nextPhi ) ) * glm::sin(currTheta );
1654 vertices[ idx ].z = torusData.innerRadius * glm::sin(nextPhi );
1656 normals[ idx ].x = glm::cos( nextPhi ) * glm::cos( currTheta );
1657 normals[ idx ].y = glm::cos( nextPhi ) * glm::sin( currTheta );
1658 normals[ idx ].z = glm::sin( nextPhi );
1660 texCoords[ idx ].s = (GLfloat)(sideNum+1) / (GLfloat)torusData.sides;
1661 texCoords[ idx ].t = (GLfloat)ringNum / (GLfloat)torusData.rings;
1665 vertices[ idx ].x = (torusData.outerRadius + torusData.innerRadius * glm::cos(nextPhi ) ) * glm::cos(nextTheta );
1666 vertices[ idx ].y = (torusData.outerRadius + torusData.innerRadius * glm::cos(nextPhi ) ) * glm::sin(nextTheta );
1667 vertices[ idx ].z = torusData.innerRadius * glm::sin(nextPhi );
1669 normals[ idx ].x = glm::cos( nextPhi ) * glm::cos( nextTheta );
1670 normals[ idx ].y = glm::cos( nextPhi ) * glm::sin( nextTheta );
1671 normals[ idx ].z = glm::sin( nextPhi );
1673 texCoords[ idx ].s = (GLfloat)(sideNum+1) / (GLfloat)torusData.sides;
1674 texCoords[ idx ].t = (GLfloat)(ringNum+1) / (GLfloat)torusData.rings;
1680 glBufferData(GL_ARRAY_BUFFER, (
sizeof(glm::vec3)*2 +
sizeof(glm::vec2)) * NUM_VERTICES,
nullptr, GL_STATIC_DRAW );
1681 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(glm::vec3) * NUM_VERTICES, vertices );
1682 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES,
sizeof(glm::vec3) * NUM_VERTICES, normals );
1683 glBufferSubData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * NUM_VERTICES * 2,
sizeof(glm::vec2) * NUM_VERTICES, texCoords );
1685 CSCI441_INTERNAL::_torusVAO.insert( std::pair<TorusData, GLuint>( torusData, vaod ) );
1686 CSCI441_INTERNAL::_torusVBO.insert( std::pair<TorusData, GLuint>( torusData, vbod ) );
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17
void drawSolidTeapot(GLfloat unused=1.0f)
Draws a solid teapot.
Definition: objects.hpp:749
void drawWireTeapot(GLfloat unused=1.0f)
Draws a wireframe teapot.
Definition: objects.hpp:754
void drawSolidCube(GLfloat sideLength)
Calls through to drawSolidCubeIndexed()
Definition: objects.hpp:591
void drawSolidCylinder(GLfloat base, GLfloat top, GLfloat height, GLint stacks, GLint slices)
Draws a solid open ended cylinder.
Definition: objects.hpp:627
void drawWireCube(GLfloat sideLength)
Draws a wireframe cube.
Definition: objects.hpp:613
void drawSolidPartialDisk(GLfloat inner, GLfloat outer, GLint slices, GLint rings, GLfloat start, GLfloat sweep)
Draws part of a solid disk.
Definition: objects.hpp:669
void drawSolidDome(GLfloat radius, GLint stacks, GLint slices)
Draws a solid dome.
Definition: objects.hpp:731
void deleteObjectVBOs()
deletes the VBOs stored for all object types
Definition: objects.hpp:566
void deleteObjectVAOs()
deletes the VAOs stored for all object types
Definition: objects.hpp:561
void setVertexAttributeLocations(GLint positionLocation, GLint normalLocation=-1, GLint texCoordLocation=-1)
Sets the attribute locations for vertex positions, normals, and texture coordinates.
Definition: objects.hpp:553
void drawWireTorus(GLfloat innerRadius, GLfloat outerRadius, GLint sides, GLint rings)
Draws a wireframe torus.
Definition: objects.hpp:769
void drawWireDome(GLfloat radius, GLint stacks, GLint slices)
Draws a wireframe dome.
Definition: objects.hpp:740
void drawSolidCone(GLfloat base, GLfloat height, GLint stacks, GLint slices)
Draws a solid cone.
Definition: objects.hpp:571
void drawWireHalfSphere(GLfloat radius, GLint stacks, GLint slices)
Draws a wireframe half sphere with a bottom.
Definition: objects.hpp:722
void drawSolidCubeTextured(GLfloat sideLength)
Draws a solid textured cube. Calls through to drawSolidCubeFlat()
Definition: objects.hpp:596
void drawSolidTorus(GLfloat innerRadius, GLfloat outerRadius, GLint sides, GLint rings)
Draws a solid torus.
Definition: objects.hpp:759
void drawSolidHalfSphere(GLfloat radius, GLint stacks, GLint slices)
Draws a solid half sphere with a bottom.
Definition: objects.hpp:713
void drawSolidCubeFlat(GLfloat sideLength)
Draws a solid cube with normals aligned with cube face.
Definition: objects.hpp:606
void drawWireSphere(GLfloat radius, GLint stacks, GLint slices)
Draws a wireframe sphere.
Definition: objects.hpp:704
void drawSolidSphere(GLfloat radius, GLint stacks, GLint slices)
Draws a solid sphere.
Definition: objects.hpp:695
void drawWireCone(GLfloat base, GLfloat height, GLint stacks, GLint slices)
Draws a wireframe cone.
Definition: objects.hpp:581
void drawSolidDisk(GLfloat inner, GLfloat outer, GLint slices, GLint rings)
Draws a solid disk.
Definition: objects.hpp:647
void drawSolidCubeIndexed(GLfloat sideLength)
Draws a solid cube.
Definition: objects.hpp:600
void drawCubeMap(GLfloat sideLength)
Draws a cube with 3D Texture Coordinates to map a cube map texture to it.
Definition: objects.hpp:620
void drawWireDisk(GLfloat inner, GLfloat outer, GLint slices, GLint rings)
Draws a wireframe disk.
Definition: objects.hpp:658
void drawWirePartialDisk(GLfloat inner, GLfloat outer, GLint slices, GLint rings, GLfloat start, GLfloat sweep)
Draws part of a wireframe disk.
Definition: objects.hpp:682
void drawWireCylinder(GLfloat base, GLfloat top, GLfloat height, GLint stacks, GLint slices)
Draws a wireframe open ended cylinder.
Definition: objects.hpp:637
Helper functions to draw teapot with OpenGL 3.0+.