From f6f5cb524d134eacf6830b438f55d428d1f9e524 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 8 Jun 2008 15:09:45 +0000 Subject: [PATCH] Initial check-in of OpenGL example. Doesn't do anything yet. --- CMakeLists.txt | 2 + examples/OpenGL/CMakeLists.txt | 33 +++++++++++ examples/OpenGL/main.cpp | 100 +++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 examples/OpenGL/CMakeLists.txt create mode 100644 examples/OpenGL/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9cb5c9a2..75059ded 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ SET(POLYVOX_VERSION_MINOR "1") SET(POLYVOX_VERSION_PATCH "0") SET(POLYVOX_VERSION "${POLYVOX_VERSION_MAJOR}.${POLYVOX_VERSION_MINOR}.${POLYVOX_VERSION_PATCH}") +ADD_SUBDIRECTORY(examples/OpenGL) + #Projects source files SET(SRC_FILES source/IndexedSurfacePatch.cpp diff --git a/examples/OpenGL/CMakeLists.txt b/examples/OpenGL/CMakeLists.txt new file mode 100644 index 00000000..3eee064c --- /dev/null +++ b/examples/OpenGL/CMakeLists.txt @@ -0,0 +1,33 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) + +PROJECT(OpenGLExample) + +#Projects source files +SET(SRC_FILES main.cpp) + +#Projects headers files +#SET(INC_FILES) + +#Appends "_d" to the generated library when in debug mode +SET(CMAKE_DEBUG_POSTFIX "_d") + +#"Sources" and "Headers" are the group names in Visual Studio. +#They may have other uses too... +SOURCE_GROUP("Sources" FILES ${SRC_FILES}) +#SOURCE_GROUP("Headers" FILES ${INC_FILES}) + +FIND_PACKAGE(OpenGL REQUIRED) + +IF (WIN32) + #NOTE: In Windows I haven't had much luck getting the FindGLUT script to work correctly. + #The easiest solution has been to install GLUT alongside OpenGL, which is already in the system path. + #This means glut.h and glut32.lib go in the 'include' and 'lib' folders within Microsoft Visual Studio 8\VC\PlatformSDK\ + #Also, glut32.dll goes in C:\Windows\System. Using C:\Windows\System32 doesn't seem to work + + #Tell CMake the paths + INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR}) + + #Build + ADD_EXECUTABLE(OpenGLExample ${SRC_FILES}) + TARGET_LINK_LIBRARIES(OpenGLExample glut32.lib ${OPENGL_gl_LIBRARY}) +ENDIF (WIN32) \ No newline at end of file diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp new file mode 100644 index 00000000..86016318 --- /dev/null +++ b/examples/OpenGL/main.cpp @@ -0,0 +1,100 @@ +/* +When creating your project, uncheck OWL, +uncheck Class Library, select Static +instead of Dynamic and change the target +model to Console from GUI. +Also link glut.lib to your project once its done. +*/ + +#include // Standard Header For Most Programs +#include // The GL Header File +#include // The GL Utility Toolkit (Glut) Header + +void init ( GLvoid ) // Create Some Everyday Functions +{ + + glShadeModel(GL_SMOOTH); // Enable Smooth Shading + glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background + glClearDepth(1.0f); // Depth Buffer Setup + glEnable(GL_DEPTH_TEST); // Enables Depth Testing + glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do + glEnable ( GL_COLOR_MATERIAL ); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); +} + +void display ( void ) // Create The Display Function +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer + glLoadIdentity(); // Reset The Current Modelview Matrix + glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0 + glBegin(GL_TRIANGLES); // Drawing Using Triangles + glVertex3f( 0.0f, 1.0f, 0.0f); // Top + glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left + glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right + glEnd(); // Finished Drawing The Triangle + glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units + glBegin(GL_QUADS); // Draw A Quad + glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left + glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right + glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right + glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left + glEnd(); + + + glutSwapBuffers ( ); + // Swap The Buffers To Not Be Left With A Clear Screen +} + +void reshape ( int w, int h ) // Create The Reshape Function (the viewport) +{ + glViewport ( 0, 0, w, h ); + glMatrixMode ( GL_PROJECTION ); // Select The Projection Matrix + glLoadIdentity ( ); // Reset The Projection Matrix + if ( h==0 ) // Calculate The Aspect Ratio Of The Window + gluPerspective ( 80, ( float ) w, 1.0, 5000.0 ); + else + gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 ); + glMatrixMode ( GL_MODELVIEW ); // Select The Model View Matrix + glLoadIdentity ( ); // Reset The Model View Matrix +} + +void keyboard ( unsigned char key, int x, int y ) // Create Keyboard Function +{ + switch ( key ) { + case 27: // When Escape Is Pressed... + exit ( 0 ); // Exit The Program + break; // Ready For Next Case + default: // Now Wrap It Up + break; + } +} + +void arrow_keys ( int a_keys, int x, int y ) // Create Special Function (required for arrow keys) +{ + switch ( a_keys ) { + case GLUT_KEY_UP: // When Up Arrow Is Pressed... + glutFullScreen ( ); // Go Into Full Screen Mode + break; + case GLUT_KEY_DOWN: // When Down Arrow Is Pressed... + glutReshapeWindow ( 500, 500 ); // Go Into A 500 By 500 Window + break; + default: + break; + } +} + +void main ( int argc, char** argv ) // Create Main Function For Bringing It All Together +{ + glutInit ( &argc, argv ); // Erm Just Write It =) + init (); + glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode + glutInitWindowSize ( 500, 500 ); // If glutFullScreen wasn't called this is the window size + glutCreateWindow ( "NeHe's OpenGL Framework" ); // Window Title (argv[0] for current directory as title) + glutFullScreen ( ); // Put Into Full Screen + glutDisplayFunc ( display ); // Matching Earlier Functions To Their Counterparts + glutReshapeFunc ( reshape ); + glutKeyboardFunc ( keyboard ); + glutSpecialFunc ( arrow_keys ); + glutMainLoop ( ); // Initialize The Main Loop +} +