Reorganize cpp/opengl-cmake

+ So project structure is closer to that of cpp/sdl-cmake
This commit is contained in:
Shaun Reed 2021-05-24 17:36:57 -04:00
parent 006f77ad95
commit 82effe5203
4 changed files with 114 additions and 75 deletions

View File

@ -15,15 +15,15 @@ project(
LANGUAGES CXX LANGUAGES CXX
) )
# Add test executable add_library(lib-opengl-test "src/lib-opengl-test.cpp")
add_executable(opengl-test "test-gl.cpp") target_include_directories(lib-opengl-test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
# Find OpenGL package # Find OpenGL package
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
if (OPENGL_FOUND) if (OPENGL_FOUND)
# Link opengl-test executable to OpenGL # Link opengl-test executable to OpenGL
target_include_directories(opengl-test PUBLIC ${OPENGL_INCLUDE_DIR}) target_include_directories(lib-opengl-test PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(opengl-test PUBLIC ${OPENGL_LIBRARIES}) target_link_libraries(lib-opengl-test PUBLIC ${OPENGL_LIBRARIES})
else() else()
message( message(
@ -35,13 +35,18 @@ endif()
# Find GLUT package # Find GLUT package
find_package(GLUT REQUIRED) find_package(GLUT REQUIRED)
if (GLUT_FOUND) if (GLUT_FOUND)
# Link opengl-test executable to GLUT # Link lib-opengl-test executable to GLUT
target_include_directories(opengl-test PUBLIC ${GLUT_INCLUDE_DIR}) target_include_directories(lib-opengl-test PUBLIC ${GLUT_INCLUDE_DIR})
target_link_libraries(opengl-test PUBLIC ${GLUT_LIBRARIES}) target_link_libraries(lib-opengl-test PUBLIC ${GLUT_LIBRARIES})
else() else()
message( message(
"Error: CMake was unable to find the OpenGL package\n" "Error: CMake was unable to find the GLUT package\n"
"Please install OpenGL and try again\n" "Please install GLUT (freeglut3-dev) and try again\n"
) )
endif() endif()
# Add test executable
add_executable(opengl-test "apps/test-gl.cpp")
target_link_libraries(opengl-test PRIVATE lib-opengl-test)
target_include_directories(opengl-test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)

View File

@ -0,0 +1,60 @@
/*#############################################################################
## Requires freeglut3-dev to be installed with your package manager ##
## To build an executable: `g++ test-gl.cpp -w -lGL -lGLU -lglut -o test` ##
## ##
## Testing building OpenGL projects with source code from lazyfoo - ##
## https://lazyfoo.net/tutorials/OpenGL/ ##
##############################################################################
## test-gl.cpp
*/
#include <lib-opengl-test.hpp>
#include <GL/freeglut.h>
#include <cstdio>
//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;
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;
}

View File

@ -1,18 +1,11 @@
/*#############################################################################
## Author: Shaun Reed ## #include <lib-opengl-test.hpp>
## Requires freeglut3-dev to be installed with your package manager ##
## To build an executable: `g++ test-gl.cpp -w -lGL -lGLU -lglut -o test` ##
## ##
## Testing building OpenGL projects with source code from lazyfoo - ##
## https://lazyfoo.net/tutorials/OpenGL/ ##
##############################################################################
## test-gl.cpp
*/
#include <GL/freeglut.h> #include <GL/freeglut.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
#include <stdio.h>
#include <cstdio>
//Screen constants //Screen constants
const int SCREEN_WIDTH = 640; const int SCREEN_WIDTH = 640;
@ -24,11 +17,21 @@ const int COLOR_MODE_CYAN = 0;
const int COLOR_MODE_MULTI = 1; const int COLOR_MODE_MULTI = 1;
//The current color rendering mode //The current color rendering mode
int gColorMode = COLOR_MODE_CYAN; int gColorMode = 0;
//The projection scale //The projection scale
GLfloat gProjectionScale = 1.f; GLfloat gProjectionScale = 1.f;
void runMainLoop( int val )
{
//Frame logic
update();
render();
//Run frame one more time
glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val );
}
bool initGL() bool initGL()
{ {
//Initialize Projection Matrix //Initialize Projection Matrix
@ -139,57 +142,3 @@ void handleKeys( unsigned char key, int x, int y )
} }
} }
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 );
}

View File

@ -0,0 +1,25 @@
#ifndef LIB_OPENGL_TEST_HPP
#define LIB_OPENGL_TEST_HPP
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
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
*/
bool initGL();
void update();
void render();
void handleKeys( unsigned char key, int x, int y );
#endif // LIB_OPENGL_TEST_HPP