From 4c10e99e2f83188bf75af14c63b4f65dc5767a7e Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Tue, 15 Oct 2019 22:07:46 -0400 Subject: [PATCH] Add example OpenGL project built within vscode g++ --- plates/cpp-opengl/.vscode/launch.json | 27 ++++ plates/cpp-opengl/.vscode/tasks.json | 30 ++++ plates/cpp-opengl/test-gl.cpp | 192 ++++++++++++++++++++++++++ 3 files changed, 249 insertions(+) create mode 100644 plates/cpp-opengl/.vscode/launch.json create mode 100644 plates/cpp-opengl/.vscode/tasks.json create mode 100644 plates/cpp-opengl/test-gl.cpp diff --git a/plates/cpp-opengl/.vscode/launch.json b/plates/cpp-opengl/.vscode/launch.json new file mode 100644 index 0000000..5bdbc15 --- /dev/null +++ b/plates/cpp-opengl/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/test-gl", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/plates/cpp-opengl/.vscode/tasks.json b/plates/cpp-opengl/.vscode/tasks.json new file mode 100644 index 0000000..4dbaa3a --- /dev/null +++ b/plates/cpp-opengl/.vscode/tasks.json @@ -0,0 +1,30 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "type": "shell", + "label": "cpp build active file", + "command": "/usr/bin/g++", + "args": [ + "${file}", + "-lGL", + "-lGLU", + "-lglut", + "-o", + "${fileDirname}/build/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "/usr/bin" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} \ No newline at end of file diff --git a/plates/cpp-opengl/test-gl.cpp b/plates/cpp-opengl/test-gl.cpp new file mode 100644 index 0000000..6ea2e18 --- /dev/null +++ b/plates/cpp-opengl/test-gl.cpp @@ -0,0 +1,192 @@ +/*############################################################################# +## Author: Shaun Reed ## +## ## +## Testing building OpenGL projects with source code from lazyfoo - ## +## https://lazyfoo.net/tutorials/OpenGL/ ## +############################################################################## +## test-gl.cpp +*/ +#include +#include +#include +#include + +//Screen constants +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; +const int SCREEN_FPS = 60; + +//Color modes +const int COLOR_MODE_CYAN = 0; +const int COLOR_MODE_MULTI = 1; + +//The current color rendering mode +int gColorMode = COLOR_MODE_CYAN; + +//The projection scale +GLfloat gProjectionScale = 1.f; + +bool initGL() +{ + //Initialize Projection Matrix + glMatrixMode( GL_PROJECTION ); + glLoadIdentity(); + glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0 ); + + //Initialize Modelview Matrix + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + //Initialize clear color + glClearColor( 0.f, 0.f, 0.f, 1.f ); + + //Check for error + GLenum error = glGetError(); + if( error != GL_NO_ERROR ) + { + printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) ); + return false; + } + + return true; +} + +void update() +{ + +} + +void render() +{ + //Clear color buffer + glClear( GL_COLOR_BUFFER_BIT ); + + //Reset modelview matrix + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + //Move to center of the screen + glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f ); + + //Render quad + if( gColorMode == COLOR_MODE_CYAN ) + { + //Solid Cyan + glBegin( GL_QUADS ); + glColor3f( 0.f, 1.f, 1.f ); + glVertex2f( -50.f, -50.f ); + glVertex2f( 50.f, -50.f ); + glVertex2f( 50.f, 50.f ); + glVertex2f( -50.f, 50.f ); + glEnd(); + } + else + { + //RYGB Mix + glBegin( GL_QUADS ); + glColor3f( 1.f, 0.f, 0.f ); glVertex2f( -50.f, -50.f ); + glColor3f( 1.f, 1.f, 0.f ); glVertex2f( 50.f, -50.f ); + glColor3f( 0.f, 1.f, 0.f ); glVertex2f( 50.f, 50.f ); + glColor3f( 0.f, 0.f, 1.f ); glVertex2f( -50.f, 50.f ); + glEnd(); + } + + //Update screen + glutSwapBuffers(); +} + +void handleKeys( unsigned char key, int x, int y ) +{ + //If the user presses q + if( key == 'q' ) + { + //Toggle color mode + if( gColorMode == COLOR_MODE_CYAN ) + { + gColorMode = COLOR_MODE_MULTI; + } + else + { + gColorMode = COLOR_MODE_CYAN; + } + } + else if( key == 'e' ) + { + //Cycle through projection scales + if( gProjectionScale == 1.f ) + { + //Zoom out + gProjectionScale = 2.f; + } + else if( gProjectionScale == 2.f ) + { + //Zoom in + gProjectionScale = 0.5f; + } + else if( gProjectionScale == 0.5f ) + { + //Regular zoom + gProjectionScale = 1.f; + } + + //Update projection matrix + glMatrixMode( GL_PROJECTION ); + glLoadIdentity(); + glOrtho( 0.0, SCREEN_WIDTH * gProjectionScale, SCREEN_HEIGHT * gProjectionScale, 0.0, 1.0, -1.0 ); + } +} + +void runMainLoop( int val ); +/* +Pre Condition: + -Initialized freeGLUT +Post Condition: + -Calls the main loop functions and sets itself to be called back in 1000 / SCREEN_FPS milliseconds +Side Effects: + -Sets glutTimerFunc +*/ + +int main( int argc, char* args[] ) +{ + //Initialize FreeGLUT + glutInit( &argc, args ); + + //Create OpenGL 2.1 context + glutInitContextVersion( 2, 1 ); + + //Create Double Buffered Window + glutInitDisplayMode( GLUT_DOUBLE ); + glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT ); + glutCreateWindow( "OpenGL" ); + + //Do post window/context creation initialization + if( !initGL() ) + { + printf( "Unable to initialize graphics library!\n" ); + return 1; + } + + //Set keyboard handler + glutKeyboardFunc( handleKeys ); + + //Set rendering function + glutDisplayFunc( render ); + + //Set main loop + glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, 0 ); + + //Start GLUT main loop + glutMainLoop(); + + return 0; +} + +void runMainLoop( int val ) +{ + //Frame logic + update(); + render(); + + //Run frame one more time + glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val ); +}