├── CMakeLists.txt ├── README.md ├── cmake └── FindGLEW.cmake ├── demo ├── CMakeLists.txt ├── Scene.cpp ├── Scene.h ├── main.cpp └── torus.obj ├── freeglut ├── CMakeLists.txt ├── COPYING.txt ├── GL │ ├── freeglut.h │ ├── freeglut_ext.h │ ├── freeglut_std.h │ └── glut.h ├── VERSION.txt ├── freeglut_callbacks.c ├── freeglut_cursor.c ├── freeglut_display.c ├── freeglut_ext.c ├── freeglut_font.c ├── freeglut_font_data.c ├── freeglut_gamemode.c ├── freeglut_geometry.c ├── freeglut_glutfont_definitions.c ├── freeglut_init.c ├── freeglut_input_devices.c ├── freeglut_internal.h ├── freeglut_joystick.c ├── freeglut_main.c ├── freeglut_menu.c ├── freeglut_misc.c ├── freeglut_overlay.c ├── freeglut_spaceball.c ├── freeglut_state.c ├── freeglut_stroke_mono_roman.c ├── freeglut_stroke_roman.c ├── freeglut_structure.c ├── freeglut_teapot.c ├── freeglut_teapot_data.h ├── freeglut_videoresize.c ├── freeglut_window.c ├── freeglut_xinput.c └── glut.h └── src ├── Camera.cpp ├── Camera.h ├── FrameBufferObject.cpp ├── FrameBufferObject.h ├── GlutViewer.cpp ├── GlutViewer.h ├── Light.cpp ├── Light.h ├── Mesh.cpp ├── Mesh.h ├── MeshReaderWriter.cpp ├── MeshReaderWriter.h ├── Object3D.cpp ├── Object3D.h ├── Shader.cpp ├── Shader.h ├── Texture2D.cpp ├── Texture2D.h ├── TextureReaderWriter.cpp ├── TextureReaderWriter.h ├── definitions.h ├── maths ├── Color.h ├── Matrix3.h ├── Matrix4.h ├── Vector2.h ├── Vector3.h └── Vector4.h ├── openglframework.h └── shaders ├── depth.frag ├── depth.vert ├── phong.frag └── phong.vert /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Minimum cmake version required 2 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 3 | 4 | # Project configuration 5 | PROJECT(OPENGLFRAMEWORK) 6 | 7 | # Where to build the library 8 | SET(LIBRARY_OUTPUT_PATH lib/) 9 | 10 | # Where to find the module to find special packages/libraries 11 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") 12 | 13 | # Options 14 | OPTION(COMPILE_DEMO "Select this if you want to build the demo executable" OFF) 15 | 16 | # Find OpenGL 17 | FIND_PACKAGE(OpenGL REQUIRED) 18 | if(OPENGL_FOUND) 19 | MESSAGE("OpenGL found") 20 | else() 21 | MESSAGE("OpenGL not found") 22 | endif() 23 | 24 | # Find the GLEW library 25 | FIND_PACKAGE(GLEW REQUIRED) 26 | if(GLEW_FOUND) 27 | MESSAGE("GLEW found") 28 | else() 29 | MESSAGE("GLEW not found") 30 | endif() 31 | 32 | # Find the LIBJPEG library 33 | FIND_PACKAGE(JPEG REQUIRED) 34 | if(JPEG_FOUND) 35 | MESSAGE("LIBJPEG found") 36 | else() 37 | MESSAGE("LIBJPEG not found") 38 | endif() 39 | 40 | # Freeglut 41 | add_subdirectory(freeglut) 42 | 43 | # Headers 44 | INCLUDE_DIRECTORIES(src freeglut ${JPEG_INCLUDE_DIR}) 45 | 46 | # Library configuration 47 | file ( 48 | GLOB_RECURSE 49 | OPENGLFRAMEWORK_SOURCES_FILES 50 | src/* 51 | ) 52 | 53 | 54 | # Require the opengl-framework code to be compiled in a static library 55 | ADD_LIBRARY ( 56 | openglframework 57 | STATIC 58 | ${OPENGLFRAMEWORK_SOURCES_FILES} 59 | ) 60 | 61 | TARGET_LINK_LIBRARIES(openglframework ${GLEW_LIBRARIES} ${OPENGL_LIBRARY} freeglut_static) 62 | 63 | # If we need to compile the examples 64 | IF (COMPILE_DEMO) 65 | add_subdirectory(demo/) 66 | ENDIF (COMPILE_DEMO) 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DanielChappuis/opengl-framework/4bf1e6dcd4f5877477b51bc08d064a9bdecfb9a7/README.md -------------------------------------------------------------------------------- /cmake/FindGLEW.cmake: -------------------------------------------------------------------------------- 1 | # 2 | # Try to find GLEW library and include path. 3 | # Once done this will define 4 | # 5 | # GLEW_FOUND 6 | # GLEW_INCLUDE_PATH 7 | # GLEW_LIBRARY 8 | # 9 | 10 | IF (WIN32) 11 | FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h 12 | $ENV{PROGRAMFILES}/GLEW/include 13 | ${GLEW_ROOT_DIR}/include 14 | DOC "The directory where GL/glew.h resides") 15 | 16 | IF (NV_SYSTEM_PROCESSOR STREQUAL "AMD64") 17 | FIND_LIBRARY( GLEW_LIBRARY 18 | NAMES glew64 glew64s 19 | PATHS 20 | $ENV{PROGRAMFILES}/GLEW/lib 21 | ${PROJECT_SOURCE_DIR}/src/nvgl/glew/bin 22 | ${PROJECT_SOURCE_DIR}/src/nvgl/glew/lib 23 | DOC "The GLEW library (64-bit)" 24 | ) 25 | ELSE(NV_SYSTEM_PROCESSOR STREQUAL "AMD64") 26 | FIND_LIBRARY( GLEW_LIBRARY 27 | NAMES glew GLEW glew32 glew32s 28 | PATHS 29 | $ENV{PROGRAMFILES}/GLEW/lib 30 | ${PROJECT_SOURCE_DIR}/src/nvgl/glew/bin 31 | ${PROJECT_SOURCE_DIR}/src/nvgl/glew/lib 32 | DOC "The GLEW library" 33 | ) 34 | ENDIF(NV_SYSTEM_PROCESSOR STREQUAL "AMD64") 35 | ELSE (WIN32) 36 | FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h 37 | /usr/include 38 | /usr/local/include 39 | /sw/include 40 | /opt/local/include 41 | ${GLEW_ROOT_DIR}/include 42 | DOC "The directory where GL/glew.h resides") 43 | 44 | FIND_LIBRARY( GLEW_LIBRARY 45 | NAMES GLEW glew 46 | PATHS 47 | /usr/lib64 48 | /usr/lib 49 | /usr/local/lib64 50 | /usr/local/lib 51 | /sw/lib 52 | /opt/local/lib 53 | ${GLEW_ROOT_DIR}/lib 54 | DOC "The GLEW library") 55 | ENDIF (WIN32) 56 | 57 | SET(GLEW_FOUND "NO") 58 | IF (GLEW_INCLUDE_PATH AND GLEW_LIBRARY) 59 | SET(GLEW_LIBRARIES ${GLEW_LIBRARY}) 60 | SET(GLEW_FOUND "YES") 61 | ENDIF (GLEW_INCLUDE_PATH AND GLEW_LIBRARY) 62 | 63 | 64 | include(FindPackageHandleStandardArgs) 65 | find_package_handle_standard_args(GLEW DEFAULT_MSG GLEW_LIBRARY GLEW_INCLUDE_PATH) -------------------------------------------------------------------------------- /demo/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Minimum cmake version required 2 | cmake_minimum_required(VERSION 2.6) 3 | 4 | # Project configuration 5 | PROJECT(OPENGLFRAMEWORKDEMO) 6 | 7 | # Headers 8 | INCLUDE_DIRECTORIES(${OPENGLFRAMEWORKDEMO_SOURCE_DIR}) 9 | 10 | # Copy the torus.obj file used for the demo into the build directory 11 | FILE(COPY "${CMAKE_SOURCE_DIR}/demo/torus.obj" DESTINATION "${CMAKE_BINARY_DIR}/demo/") 12 | 13 | # Copy the shaders used for the demo into the build directory 14 | FILE(COPY "${CMAKE_SOURCE_DIR}/src/shaders/" DESTINATION "${CMAKE_BINARY_DIR}/demo/shaders/") 15 | 16 | # Create the demo executable 17 | ADD_EXECUTABLE(demo main.cpp Scene.h Scene.cpp) 18 | 19 | TARGET_LINK_LIBRARIES(demo openglframework) 20 | -------------------------------------------------------------------------------- /demo/Scene.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "Scene.h" 28 | 29 | // Namespaces 30 | using namespace openglframework; 31 | 32 | // Constructor 33 | Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0), 34 | mPhongShader("shaders/phong.vert", 35 | "shaders/phong.frag"){ 36 | 37 | // Move the light 0 38 | mLight0.translateWorld(Vector3(15, 15, 15)); 39 | 40 | // Load the mesh from a file 41 | MeshReaderWriter::loadMeshFromFile("torus.obj", mMesh); 42 | 43 | // Calculate the normals of the mesh 44 | mMesh.calculateNormals(); 45 | 46 | // Calculate the bounding box of the mesh 47 | Vector3 min, max; 48 | mMesh.calculateBoundingBox(min, max); 49 | 50 | // Compute the radius and the center of the scene 51 | float radius = 0.5f * (min - max).length(); 52 | Vector3 center = 0.5f * (min + max); 53 | 54 | // Set the center of the scene 55 | mViewer->setScenePosition(center, radius); 56 | } 57 | 58 | // Destructor 59 | Scene::~Scene() { 60 | mMesh.destroy(); 61 | mPhongShader.destroy(); 62 | } 63 | 64 | // Display the scene 65 | void Scene::display() { 66 | 67 | // Render the mesh 68 | renderMesh(); 69 | 70 | // Display a cube 71 | //glutSolidCube(5.0); 72 | } 73 | 74 | // Render the mesh 75 | void Scene::renderMesh() { 76 | 77 | glEnable(GL_DEPTH_TEST); 78 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 79 | glDisable(GL_CULL_FACE); 80 | 81 | // Bind the shader 82 | mPhongShader.bind(); 83 | 84 | // Set the variables of the shader 85 | const Camera& camera = mViewer->getCamera(); 86 | Matrix4 matrixIdentity; 87 | matrixIdentity.setToIdentity(); 88 | mPhongShader.setVector3Uniform("cameraWorldPosition", mViewer->getCamera().getOrigin()); 89 | mPhongShader.setMatrix4x4Uniform("modelToWorldMatrix", mMesh.getTransformMatrix()); 90 | mPhongShader.setMatrix4x4Uniform("worldToCameraMatrix", camera.getTransformMatrix().getInverse()); 91 | mPhongShader.setMatrix4x4Uniform("projectionMatrix", camera.getProjectionMatrix()); 92 | mPhongShader.setVector3Uniform("lightWorldPosition", mLight0.getOrigin()); 93 | mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f)); 94 | Color& diffCol = mLight0.getDiffuseColor(); 95 | Color& specCol = mLight0.getSpecularColor(); 96 | mPhongShader.setVector3Uniform("lightDiffuseColor", Vector3(diffCol.r, diffCol.g, diffCol.b)); 97 | mPhongShader.setVector3Uniform("lightSpecularColor", Vector3(specCol.r, specCol.g, specCol.b)); 98 | mPhongShader.setFloatUniform("shininess", 60.0f); 99 | 100 | glEnableClientState(GL_VERTEX_ARRAY); 101 | glEnableClientState(GL_NORMAL_ARRAY); 102 | if (mMesh.hasTexture()) { 103 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); 104 | } 105 | 106 | glVertexPointer(3, GL_FLOAT, 0, mMesh.getVerticesPointer()); 107 | glNormalPointer(GL_FLOAT, 0, mMesh.getNormalsPointer()); 108 | if(mMesh.hasTexture()) { 109 | glTexCoordPointer(2, GL_FLOAT, 0, mMesh.getUVTextureCoordinatesPointer()); 110 | } 111 | 112 | // For each part of the mesh 113 | for (uint i=0; i 31 | 32 | // Class Scene 33 | class Scene { 34 | 35 | private : 36 | 37 | // -------------------- Attributes -------------------- // 38 | 39 | // Pointer to the viewer 40 | openglframework::GlutViewer* mViewer; 41 | 42 | // Light 0 43 | openglframework::Light mLight0; 44 | 45 | // Mesh 46 | openglframework::Mesh mMesh; 47 | 48 | // Phong shader 49 | openglframework::Shader mPhongShader; 50 | 51 | // -------------------- Methods -------------------- // 52 | 53 | // Render the mesh 54 | void renderMesh(); 55 | 56 | public: 57 | 58 | // -------------------- Methods -------------------- // 59 | 60 | // Constructor 61 | Scene(openglframework::GlutViewer* viewer); 62 | 63 | // Destructor 64 | ~Scene(); 65 | 66 | // Display the scene 67 | void display(); 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /demo/main.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "Scene.h" 28 | 29 | // Declarations 30 | void simulate(); 31 | void display(); 32 | void reshape(int width, int height); 33 | void mouseButton(int button, int state, int x, int y); 34 | void mouseMotion(int x, int y); 35 | void init(); 36 | 37 | // Namespaces 38 | using namespace openglframework; 39 | 40 | // Global variables 41 | GlutViewer* viewer; 42 | Scene* scene; 43 | 44 | // Main function 45 | int main(int argc, char** argv) { 46 | 47 | // Create and initialize the Viewer 48 | viewer = new GlutViewer(); 49 | Vector2 windowsSize = Vector2(600, 400); 50 | Vector2 windowsPosition = Vector2(100, 100); 51 | bool initOK = viewer->init(argc, argv, "OpenGL Framework Demo", windowsSize, windowsPosition); 52 | if (!initOK) return 1; 53 | 54 | // Create the scene 55 | scene = new Scene(viewer); 56 | 57 | init(); 58 | 59 | // Glut Idle function that is continuously called 60 | glutIdleFunc(simulate); 61 | glutDisplayFunc(display); 62 | glutReshapeFunc(reshape); 63 | glutMouseFunc(mouseButton); 64 | glutMotionFunc(mouseMotion); 65 | 66 | // Glut main looop 67 | glutMainLoop(); 68 | 69 | delete viewer; 70 | delete scene; 71 | 72 | return 0; 73 | } 74 | 75 | // Simulate function 76 | void simulate() { 77 | 78 | // Display the scene 79 | display(); 80 | } 81 | 82 | // Initialization 83 | void init() { 84 | 85 | // Define the background color (black) 86 | glClearColor(0.0, 0.0, 0.0, 1.0); 87 | } 88 | 89 | // Reshape function 90 | void reshape(int width, int height) { 91 | viewer->reshape(width, height); 92 | } 93 | 94 | // Called when a mouse button event occurs 95 | void mouseButton(int button, int state, int x, int y) { 96 | viewer->mouseButtonEvent(button, state, x, y); 97 | } 98 | 99 | // Called when a mouse motion event occurs 100 | void mouseMotion(int x, int y) { 101 | viewer->mouseMotionEvent(x, y); 102 | } 103 | 104 | // Display the scene 105 | void display() { 106 | 107 | glEnable(GL_DEPTH_TEST); 108 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 109 | 110 | scene->display(); 111 | 112 | glutSwapBuffers(); 113 | 114 | // Check the OpenGL errors 115 | GlutViewer::checkOpenGLErrors(); 116 | } 117 | -------------------------------------------------------------------------------- /freeglut/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_definitions( -DFREEGLUT_EXPORTS -DFREEGLUT_STATIC -D_CRT_SECURE_NO_WARNINGS ) 2 | 3 | if(APPLE) 4 | include_directories( /usr/X11/include ) 5 | endif(APPLE) 6 | 7 | if(UNIX) 8 | add_definitions( -D__unix__ -DHAVE_FCNTL_H -DHAVE_GETTIMEOFDAY ) 9 | endif(UNIX) 10 | 11 | file ( 12 | GLOB_RECURSE 13 | FREEGLUT_SOURCES 14 | ./* 15 | ) 16 | 17 | include_directories ( 18 | ${OPENGL_INCLUDE_DIR} 19 | . 20 | ) 21 | 22 | add_library(freeglut_static 23 | ${FREEGLUT_SOURCES} 24 | ) 25 | -------------------------------------------------------------------------------- /freeglut/COPYING.txt: -------------------------------------------------------------------------------- 1 | 2 | Freeglut Copyright 3 | ------------------ 4 | 5 | Freeglut code without an explicit copyright is covered by the following 6 | copyright: 7 | 8 | Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies or substantial portions of the Software. 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 | PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 22 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | Except as contained in this notice, the name of Pawel W. Olszta shall not be 26 | used in advertising or otherwise to promote the sale, use or other dealings 27 | in this Software without prior written authorization from Pawel W. Olszta. 28 | -------------------------------------------------------------------------------- /freeglut/GL/freeglut.h: -------------------------------------------------------------------------------- 1 | #ifndef __FREEGLUT_H__ 2 | #define __FREEGLUT_H__ 3 | 4 | /* 5 | * freeglut.h 6 | * 7 | * The freeglut library include file 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 10 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 11 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 12 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | */ 16 | 17 | #include "freeglut_std.h" 18 | #include "freeglut_ext.h" 19 | 20 | /*** END OF FILE ***/ 21 | 22 | #endif /* __FREEGLUT_H__ */ 23 | -------------------------------------------------------------------------------- /freeglut/GL/freeglut_ext.h: -------------------------------------------------------------------------------- 1 | #ifndef __FREEGLUT_EXT_H__ 2 | #define __FREEGLUT_EXT_H__ 3 | 4 | /* 5 | * freeglut_ext.h 6 | * 7 | * The non-GLUT-compatible extensions to the freeglut library include file 8 | * 9 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 10 | * Written by Pawel W. Olszta, 11 | * Creation date: Thu Dec 2 1999 12 | * 13 | * Permission is hereby granted, free of charge, to any person obtaining a 14 | * copy of this software and associated documentation files (the "Software"), 15 | * to deal in the Software without restriction, including without limitation 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 17 | * and/or sell copies of the Software, and to permit persons to whom the 18 | * Software is furnished to do so, subject to the following conditions: 19 | * 20 | * The above copyright notice and this permission notice shall be included 21 | * in all copies or substantial portions of the Software. 22 | * 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 24 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 26 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 27 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 28 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | */ 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | /* 36 | * Additional GLUT Key definitions for the Special key function 37 | */ 38 | #define GLUT_KEY_NUM_LOCK 0x006D 39 | #define GLUT_KEY_BEGIN 0x006E 40 | #define GLUT_KEY_DELETE 0x006F 41 | #define GLUT_KEY_SHIFT_L 0x0070 42 | #define GLUT_KEY_SHIFT_R 0x0071 43 | #define GLUT_KEY_CTRL_L 0x0072 44 | #define GLUT_KEY_CTRL_R 0x0073 45 | #define GLUT_KEY_ALT_L 0x0074 46 | #define GLUT_KEY_ALT_R 0x0075 47 | 48 | /* 49 | * GLUT API Extension macro definitions -- behaviour when the user clicks on an "x" to close a window 50 | */ 51 | #define GLUT_ACTION_EXIT 0 52 | #define GLUT_ACTION_GLUTMAINLOOP_RETURNS 1 53 | #define GLUT_ACTION_CONTINUE_EXECUTION 2 54 | 55 | /* 56 | * Create a new rendering context when the user opens a new window? 57 | */ 58 | #define GLUT_CREATE_NEW_CONTEXT 0 59 | #define GLUT_USE_CURRENT_CONTEXT 1 60 | 61 | /* 62 | * Direct/Indirect rendering context options (has meaning only in Unix/X11) 63 | */ 64 | #define GLUT_FORCE_INDIRECT_CONTEXT 0 65 | #define GLUT_ALLOW_DIRECT_CONTEXT 1 66 | #define GLUT_TRY_DIRECT_CONTEXT 2 67 | #define GLUT_FORCE_DIRECT_CONTEXT 3 68 | 69 | /* 70 | * GLUT API Extension macro definitions -- the glutGet parameters 71 | */ 72 | #define GLUT_INIT_STATE 0x007C 73 | 74 | #define GLUT_ACTION_ON_WINDOW_CLOSE 0x01F9 75 | 76 | #define GLUT_WINDOW_BORDER_WIDTH 0x01FA 77 | #define GLUT_WINDOW_HEADER_HEIGHT 0x01FB 78 | 79 | #define GLUT_VERSION 0x01FC 80 | 81 | #define GLUT_RENDERING_CONTEXT 0x01FD 82 | #define GLUT_DIRECT_RENDERING 0x01FE 83 | 84 | #define GLUT_FULL_SCREEN 0x01FF 85 | 86 | /* 87 | * New tokens for glutInitDisplayMode. 88 | * Only one GLUT_AUXn bit may be used at a time. 89 | * Value 0x0400 is defined in OpenGLUT. 90 | */ 91 | #define GLUT_AUX 0x1000 92 | 93 | #define GLUT_AUX1 0x1000 94 | #define GLUT_AUX2 0x2000 95 | #define GLUT_AUX3 0x4000 96 | #define GLUT_AUX4 0x8000 97 | 98 | /* 99 | * Context-related flags, see freeglut_state.c 100 | */ 101 | #define GLUT_INIT_MAJOR_VERSION 0x0200 102 | #define GLUT_INIT_MINOR_VERSION 0x0201 103 | #define GLUT_INIT_FLAGS 0x0202 104 | #define GLUT_INIT_PROFILE 0x0203 105 | 106 | /* 107 | * Flags for glutInitContextFlags, see freeglut_init.c 108 | */ 109 | #define GLUT_DEBUG 0x0001 110 | #define GLUT_FORWARD_COMPATIBLE 0x0002 111 | 112 | 113 | /* 114 | * Flags for glutInitContextProfile, see freeglut_init.c 115 | */ 116 | #define GLUT_CORE_PROFILE 0x0001 117 | #define GLUT_COMPATIBILITY_PROFILE 0x0002 118 | 119 | /* 120 | * Process loop function, see freeglut_main.c 121 | */ 122 | FGAPI void FGAPIENTRY glutMainLoopEvent( void ); 123 | FGAPI void FGAPIENTRY glutLeaveMainLoop( void ); 124 | FGAPI void FGAPIENTRY glutExit ( void ); 125 | 126 | /* 127 | * Window management functions, see freeglut_window.c 128 | */ 129 | FGAPI void FGAPIENTRY glutFullScreenToggle( void ); 130 | FGAPI void FGAPIENTRY glutLeaveFullScreen( void ); 131 | 132 | /* 133 | * Window-specific callback functions, see freeglut_callbacks.c 134 | */ 135 | FGAPI void FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) ); 136 | FGAPI void FGAPIENTRY glutCloseFunc( void (* callback)( void ) ); 137 | FGAPI void FGAPIENTRY glutWMCloseFunc( void (* callback)( void ) ); 138 | /* A. Donev: Also a destruction callback for menus */ 139 | FGAPI void FGAPIENTRY glutMenuDestroyFunc( void (* callback)( void ) ); 140 | 141 | /* 142 | * State setting and retrieval functions, see freeglut_state.c 143 | */ 144 | FGAPI void FGAPIENTRY glutSetOption ( GLenum option_flag, int value ); 145 | FGAPI int * FGAPIENTRY glutGetModeValues(GLenum mode, int * size); 146 | /* A.Donev: User-data manipulation */ 147 | FGAPI void* FGAPIENTRY glutGetWindowData( void ); 148 | FGAPI void FGAPIENTRY glutSetWindowData(void* data); 149 | FGAPI void* FGAPIENTRY glutGetMenuData( void ); 150 | FGAPI void FGAPIENTRY glutSetMenuData(void* data); 151 | 152 | /* 153 | * Font stuff, see freeglut_font.c 154 | */ 155 | FGAPI int FGAPIENTRY glutBitmapHeight( void* font ); 156 | FGAPI GLfloat FGAPIENTRY glutStrokeHeight( void* font ); 157 | FGAPI void FGAPIENTRY glutBitmapString( void* font, const unsigned char *string ); 158 | FGAPI void FGAPIENTRY glutStrokeString( void* font, const unsigned char *string ); 159 | 160 | /* 161 | * Geometry functions, see freeglut_geometry.c 162 | */ 163 | FGAPI void FGAPIENTRY glutWireRhombicDodecahedron( void ); 164 | FGAPI void FGAPIENTRY glutSolidRhombicDodecahedron( void ); 165 | FGAPI void FGAPIENTRY glutWireSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale ); 166 | FGAPI void FGAPIENTRY glutSolidSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale ); 167 | FGAPI void FGAPIENTRY glutWireCylinder( GLdouble radius, GLdouble height, GLint slices, GLint stacks); 168 | FGAPI void FGAPIENTRY glutSolidCylinder( GLdouble radius, GLdouble height, GLint slices, GLint stacks); 169 | 170 | /* 171 | * Extension functions, see freeglut_ext.c 172 | */ 173 | typedef void (*GLUTproc)(); 174 | FGAPI GLUTproc FGAPIENTRY glutGetProcAddress( const char *procName ); 175 | 176 | /* 177 | * Multi-touch/multi-pointer extensions 178 | */ 179 | 180 | #define GLUT_HAS_MULTI 1 181 | 182 | FGAPI void FGAPIENTRY glutMultiEntryFunc( void (* callback)( int, int ) ); 183 | FGAPI void FGAPIENTRY glutMultiButtonFunc( void (* callback)( int, int, int, int, int ) ); 184 | FGAPI void FGAPIENTRY glutMultiMotionFunc( void (* callback)( int, int, int ) ); 185 | FGAPI void FGAPIENTRY glutMultiPassiveFunc( void (* callback)( int, int, int ) ); 186 | 187 | /* 188 | * Joystick functions, see freeglut_joystick.c 189 | */ 190 | /* USE OF THESE FUNCTIONS IS DEPRECATED !!!!! */ 191 | /* If you have a serious need for these functions in your application, please either 192 | * contact the "freeglut" developer community at freeglut-developer@lists.sourceforge.net, 193 | * switch to the OpenGLUT library, or else port your joystick functionality over to PLIB's 194 | * "js" library. 195 | */ 196 | int glutJoystickGetNumAxes( int ident ); 197 | int glutJoystickGetNumButtons( int ident ); 198 | int glutJoystickNotWorking( int ident ); 199 | float glutJoystickGetDeadBand( int ident, int axis ); 200 | void glutJoystickSetDeadBand( int ident, int axis, float db ); 201 | float glutJoystickGetSaturation( int ident, int axis ); 202 | void glutJoystickSetSaturation( int ident, int axis, float st ); 203 | void glutJoystickSetMinRange( int ident, float *axes ); 204 | void glutJoystickSetMaxRange( int ident, float *axes ); 205 | void glutJoystickSetCenter( int ident, float *axes ); 206 | void glutJoystickGetMinRange( int ident, float *axes ); 207 | void glutJoystickGetMaxRange( int ident, float *axes ); 208 | void glutJoystickGetCenter( int ident, float *axes ); 209 | 210 | /* 211 | * Initialization functions, see freeglut_init.c 212 | */ 213 | FGAPI void FGAPIENTRY glutInitContextVersion( int majorVersion, int minorVersion ); 214 | FGAPI void FGAPIENTRY glutInitContextFlags( int flags ); 215 | FGAPI void FGAPIENTRY glutInitContextProfile( int profile ); 216 | 217 | /* to get the typedef for va_list */ 218 | #include 219 | 220 | FGAPI void FGAPIENTRY glutInitErrorFunc( void (* vError)( const char *fmt, va_list ap ) ); 221 | FGAPI void FGAPIENTRY glutInitWarningFunc( void (* vWarning)( const char *fmt, va_list ap ) ); 222 | 223 | /* 224 | * GLUT API macro definitions -- the display mode definitions 225 | */ 226 | #define GLUT_CAPTIONLESS 0x0400 227 | #define GLUT_BORDERLESS 0x0800 228 | #define GLUT_SRGB 0x1000 229 | 230 | #ifdef __cplusplus 231 | } 232 | #endif 233 | 234 | /*** END OF FILE ***/ 235 | 236 | #endif /* __FREEGLUT_EXT_H__ */ 237 | -------------------------------------------------------------------------------- /freeglut/GL/glut.h: -------------------------------------------------------------------------------- 1 | #ifndef __GLUT_H__ 2 | #define __GLUT_H__ 3 | 4 | /* 5 | * glut.h 6 | * 7 | * The freeglut library include file 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 10 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 11 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 12 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | */ 16 | 17 | #include "freeglut_std.h" 18 | 19 | /*** END OF FILE ***/ 20 | 21 | #endif /* __GLUT_H__ */ 22 | -------------------------------------------------------------------------------- /freeglut/VERSION.txt: -------------------------------------------------------------------------------- 1 | 2.8.0 -------------------------------------------------------------------------------- /freeglut/freeglut_cursor.c: -------------------------------------------------------------------------------- 1 | /* 2 | * freeglut_cursor.c 3 | * 4 | * The mouse cursor related stuff. 5 | * 6 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 7 | * Written by Pawel W. Olszta, 8 | * Creation date: Thu Dec 16 1999 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included 18 | * in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 24 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #include 29 | #include "freeglut_internal.h" 30 | 31 | /* 32 | * TODO BEFORE THE STABLE RELEASE: 33 | * glutSetCursor() -- Win32 mappings are incomplete. 34 | * 35 | * It would be good to use custom mouse cursor shapes, and introduce 36 | * an option to display them using glBitmap() and/or texture mapping, 37 | * apart from the windowing system version. 38 | */ 39 | 40 | /* -- PRIVATE FUNCTIONS --------------------------------------------------- */ 41 | 42 | #if TARGET_HOST_POSIX_X11 || TARGET_HOST_MAC_OSX || TARGET_HOST_SOLARIS 43 | #include 44 | 45 | /* 46 | * A factory method for an empty cursor 47 | */ 48 | static Cursor getEmptyCursor( void ) 49 | { 50 | static Cursor cursorNone = None; 51 | if( cursorNone == None ) { 52 | char cursorNoneBits[ 32 ]; 53 | XColor dontCare; 54 | Pixmap cursorNonePixmap; 55 | memset( cursorNoneBits, 0, sizeof( cursorNoneBits ) ); 56 | memset( &dontCare, 0, sizeof( dontCare ) ); 57 | cursorNonePixmap = XCreateBitmapFromData ( fgDisplay.Display, 58 | fgDisplay.RootWindow, 59 | cursorNoneBits, 16, 16 ); 60 | if( cursorNonePixmap != None ) { 61 | cursorNone = XCreatePixmapCursor( fgDisplay.Display, 62 | cursorNonePixmap, cursorNonePixmap, 63 | &dontCare, &dontCare, 0, 0 ); 64 | XFreePixmap( fgDisplay.Display, cursorNonePixmap ); 65 | } 66 | } 67 | return cursorNone; 68 | } 69 | 70 | typedef struct tag_cursorCacheEntry cursorCacheEntry; 71 | struct tag_cursorCacheEntry { 72 | unsigned int cursorShape; /* an XC_foo value */ 73 | Cursor cachedCursor; /* None if the corresponding cursor has 74 | not been created yet */ 75 | }; 76 | 77 | /* 78 | * Note: The arrangement of the table below depends on the fact that 79 | * the "normal" GLUT_CURSOR_* values start a 0 and are consecutive. 80 | */ 81 | static cursorCacheEntry cursorCache[] = { 82 | { XC_arrow, None }, /* GLUT_CURSOR_RIGHT_ARROW */ 83 | { XC_top_left_arrow, None }, /* GLUT_CURSOR_LEFT_ARROW */ 84 | { XC_hand1, None }, /* GLUT_CURSOR_INFO */ 85 | { XC_pirate, None }, /* GLUT_CURSOR_DESTROY */ 86 | { XC_question_arrow, None }, /* GLUT_CURSOR_HELP */ 87 | { XC_exchange, None }, /* GLUT_CURSOR_CYCLE */ 88 | { XC_spraycan, None }, /* GLUT_CURSOR_SPRAY */ 89 | { XC_watch, None }, /* GLUT_CURSOR_WAIT */ 90 | { XC_xterm, None }, /* GLUT_CURSOR_TEXT */ 91 | { XC_crosshair, None }, /* GLUT_CURSOR_CROSSHAIR */ 92 | { XC_sb_v_double_arrow, None }, /* GLUT_CURSOR_UP_DOWN */ 93 | { XC_sb_h_double_arrow, None }, /* GLUT_CURSOR_LEFT_RIGHT */ 94 | { XC_top_side, None }, /* GLUT_CURSOR_TOP_SIDE */ 95 | { XC_bottom_side, None }, /* GLUT_CURSOR_BOTTOM_SIDE */ 96 | { XC_left_side, None }, /* GLUT_CURSOR_LEFT_SIDE */ 97 | { XC_right_side, None }, /* GLUT_CURSOR_RIGHT_SIDE */ 98 | { XC_top_left_corner, None }, /* GLUT_CURSOR_TOP_LEFT_CORNER */ 99 | { XC_top_right_corner, None }, /* GLUT_CURSOR_TOP_RIGHT_CORNER */ 100 | { XC_bottom_right_corner, None }, /* GLUT_CURSOR_BOTTOM_RIGHT_CORNER */ 101 | { XC_bottom_left_corner, None } /* GLUT_CURSOR_BOTTOM_LEFT_CORNER */ 102 | }; 103 | 104 | static void fghSetCursor ( SFG_Window *window, int cursorID ) 105 | { 106 | Cursor cursor; 107 | /* 108 | * XXX FULL_CROSSHAIR demotes to plain CROSSHAIR. Old GLUT allows 109 | * for this, but if there is a system that easily supports a full- 110 | * window (or full-screen) crosshair, we might consider it. 111 | */ 112 | int cursorIDToUse = 113 | ( cursorID == GLUT_CURSOR_FULL_CROSSHAIR ) ? GLUT_CURSOR_CROSSHAIR : cursorID; 114 | 115 | if( ( cursorIDToUse >= 0 ) && 116 | ( cursorIDToUse < sizeof( cursorCache ) / sizeof( cursorCache[0] ) ) ) { 117 | cursorCacheEntry *entry = &cursorCache[ cursorIDToUse ]; 118 | if( entry->cachedCursor == None ) { 119 | entry->cachedCursor = 120 | XCreateFontCursor( fgDisplay.Display, entry->cursorShape ); 121 | } 122 | cursor = entry->cachedCursor; 123 | } else { 124 | switch( cursorIDToUse ) 125 | { 126 | case GLUT_CURSOR_NONE: 127 | cursor = getEmptyCursor( ); 128 | break; 129 | 130 | case GLUT_CURSOR_INHERIT: 131 | cursor = None; 132 | break; 133 | 134 | default: 135 | fgError( "Unknown cursor type: %d", cursorIDToUse ); 136 | return; 137 | } 138 | } 139 | 140 | if ( cursorIDToUse == GLUT_CURSOR_INHERIT ) { 141 | XUndefineCursor( fgDisplay.Display, window->Window.Handle ); 142 | } else if ( cursor != None ) { 143 | XDefineCursor( fgDisplay.Display, window->Window.Handle, cursor ); 144 | } else if ( cursorIDToUse != GLUT_CURSOR_NONE ) { 145 | fgError( "Failed to create cursor" ); 146 | } 147 | } 148 | 149 | 150 | static void fghWarpPointer ( int x, int y ) 151 | { 152 | XWarpPointer( 153 | fgDisplay.Display, 154 | None, 155 | fgStructure.CurrentWindow->Window.Handle, 156 | 0, 0, 0, 0, 157 | x, y 158 | ); 159 | /* Make the warp visible immediately. */ 160 | XFlush( fgDisplay.Display ); 161 | } 162 | #endif 163 | 164 | 165 | #if TARGET_HOST_MS_WINDOWS 166 | static void fghSetCursor ( SFG_Window *window, int cursorID ) 167 | { 168 | /* 169 | * Joe Krahn is re-writing the following code. 170 | */ 171 | /* Set the cursor AND change it for this window class. */ 172 | #if !defined(__MINGW64__) && _MSC_VER <= 1200 173 | # define MAP_CURSOR(a,b) \ 174 | case a: \ 175 | SetCursor( LoadCursor( NULL, b ) ); \ 176 | SetClassLong( window->Window.Handle, \ 177 | GCL_HCURSOR, \ 178 | ( LONG )LoadCursor( NULL, b ) ); \ 179 | break; 180 | /* Nuke the cursor AND change it for this window class. */ 181 | # define ZAP_CURSOR(a,b) \ 182 | case a: \ 183 | SetCursor( NULL ); \ 184 | SetClassLong( window->Window.Handle, \ 185 | GCL_HCURSOR, ( LONG )NULL ); \ 186 | break; 187 | #else 188 | # define MAP_CURSOR(a,b) \ 189 | case a: \ 190 | SetCursor( LoadCursor( NULL, b ) ); \ 191 | SetClassLongPtr( window->Window.Handle, \ 192 | GCLP_HCURSOR, \ 193 | ( LONG )( LONG_PTR )LoadCursor( NULL, b ) ); \ 194 | break; 195 | /* Nuke the cursor AND change it for this window class. */ 196 | # define ZAP_CURSOR(a,b) \ 197 | case a: \ 198 | SetCursor( NULL ); \ 199 | SetClassLongPtr( window->Window.Handle, \ 200 | GCLP_HCURSOR, ( LONG )( LONG_PTR )NULL ); \ 201 | break; 202 | #endif 203 | 204 | switch( cursorID ) 205 | { 206 | MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW ); 207 | MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW ); 208 | MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP ); 209 | MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS ); 210 | MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP ); 211 | MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL ); 212 | MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS ); 213 | MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT ); 214 | MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_IBEAM ); 215 | MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS ); 216 | MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_SIZENS ); 217 | MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT, IDC_SIZEWE ); 218 | MAP_CURSOR( GLUT_CURSOR_TOP_SIDE, IDC_ARROW ); /* XXX ToDo */ 219 | MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, IDC_ARROW ); /* XXX ToDo */ 220 | MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE, IDC_ARROW ); /* XXX ToDo */ 221 | MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE, IDC_ARROW ); /* XXX ToDo */ 222 | MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER, IDC_SIZENWSE ); 223 | MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER, IDC_SIZENESW ); 224 | MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER, IDC_SIZENWSE ); 225 | MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, IDC_SIZENESW ); 226 | MAP_CURSOR( GLUT_CURSOR_INHERIT, IDC_ARROW ); /* XXX ToDo */ 227 | ZAP_CURSOR( GLUT_CURSOR_NONE, NULL ); 228 | MAP_CURSOR( GLUT_CURSOR_FULL_CROSSHAIR, IDC_CROSS ); /* XXX ToDo */ 229 | 230 | default: 231 | fgError( "Unknown cursor type: %d", cursorID ); 232 | break; 233 | } 234 | } 235 | 236 | 237 | static void fghWarpPointer ( int x, int y ) 238 | { 239 | POINT coords; 240 | coords.x = x; 241 | coords.y = y; 242 | 243 | /* ClientToScreen() translates {coords} for us. */ 244 | ClientToScreen( fgStructure.CurrentWindow->Window.Handle, &coords ); 245 | SetCursorPos( coords.x, coords.y ); 246 | } 247 | #endif 248 | 249 | 250 | /* -- INTERNAL FUNCTIONS ---------------------------------------------------- */ 251 | void fgSetCursor ( SFG_Window *window, int cursorID ) 252 | { 253 | fghSetCursor ( window, cursorID ); 254 | } 255 | 256 | 257 | /* -- INTERFACE FUNCTIONS -------------------------------------------------- */ 258 | 259 | /* 260 | * Set the cursor image to be used for the current window 261 | */ 262 | void FGAPIENTRY glutSetCursor( int cursorID ) 263 | { 264 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetCursor" ); 265 | FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetCursor" ); 266 | 267 | fghSetCursor ( fgStructure.CurrentWindow, cursorID ); 268 | fgStructure.CurrentWindow->State.Cursor = cursorID; 269 | } 270 | 271 | /* 272 | * Moves the mouse pointer to given window coordinates 273 | */ 274 | void FGAPIENTRY glutWarpPointer( int x, int y ) 275 | { 276 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWarpPointer" ); 277 | FREEGLUT_EXIT_IF_NO_WINDOW ( "glutWarpPointer" ); 278 | 279 | fghWarpPointer ( x, y ); 280 | } 281 | 282 | /*** END OF FILE ***/ 283 | -------------------------------------------------------------------------------- /freeglut/freeglut_display.c: -------------------------------------------------------------------------------- 1 | /* 2 | * freeglut_display.c 3 | * 4 | * Display message posting, context buffer swapping. 5 | * 6 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 7 | * Written by Pawel W. Olszta, 8 | * Creation date: Fri Dec 3 1999 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included 18 | * in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 24 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #include 29 | #include "freeglut_internal.h" 30 | 31 | /* -- INTERFACE FUNCTIONS -------------------------------------------------- */ 32 | 33 | /* 34 | * Marks the current window to have the redisplay performed when possible... 35 | */ 36 | void FGAPIENTRY glutPostRedisplay( void ) 37 | { 38 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPostRedisplay" ); 39 | if ( ! fgStructure.CurrentWindow ) 40 | { 41 | fgError ( " ERROR: Function <%s> called" 42 | " with no current window defined.", "glutPostRedisplay" ) ; 43 | } 44 | 45 | fgStructure.CurrentWindow->State.Redisplay = GL_TRUE; 46 | } 47 | 48 | /* 49 | * Swaps the buffers for the current window (if any) 50 | */ 51 | void FGAPIENTRY glutSwapBuffers( void ) 52 | { 53 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSwapBuffers" ); 54 | FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSwapBuffers" ); 55 | 56 | /* 57 | * "glXSwapBuffers" already performs an implicit call to "glFlush". What 58 | * about "SwapBuffers"? 59 | */ 60 | glFlush( ); 61 | if( ! fgStructure.CurrentWindow->Window.DoubleBuffered ) 62 | return; 63 | 64 | #if TARGET_HOST_POSIX_X11 65 | glXSwapBuffers( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle ); 66 | #elif TARGET_HOST_MS_WINDOWS 67 | SwapBuffers( fgStructure.CurrentWindow->Window.Device ); 68 | #endif 69 | 70 | /* GLUT_FPS env var support */ 71 | if( fgState.FPSInterval ) 72 | { 73 | GLint t = glutGet( GLUT_ELAPSED_TIME ); 74 | fgState.SwapCount++; 75 | if( fgState.SwapTime == 0 ) 76 | fgState.SwapTime = t; 77 | else if( t - fgState.SwapTime > fgState.FPSInterval ) 78 | { 79 | float time = 0.001f * ( t - fgState.SwapTime ); 80 | float fps = ( float )fgState.SwapCount / time; 81 | fprintf( stderr, 82 | "freeglut: %d frames in %.2f seconds = %.2f FPS\n", 83 | fgState.SwapCount, time, fps ); 84 | fgState.SwapTime = t; 85 | fgState.SwapCount = 0; 86 | } 87 | } 88 | } 89 | 90 | /* 91 | * Mark appropriate window to be displayed 92 | */ 93 | void FGAPIENTRY glutPostWindowRedisplay( int windowID ) 94 | { 95 | SFG_Window* window; 96 | 97 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPostWindowRedisplay" ); 98 | window = fgWindowByID( windowID ); 99 | freeglut_return_if_fail( window ); 100 | window->State.Redisplay = GL_TRUE; 101 | } 102 | 103 | /*** END OF FILE ***/ 104 | -------------------------------------------------------------------------------- /freeglut/freeglut_ext.c: -------------------------------------------------------------------------------- 1 | /* 2 | * freeglut_ext.c 3 | * 4 | * Functions related to OpenGL extensions. 5 | * 6 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 7 | * Written by Pawel W. Olszta, 8 | * Creation date: Thu Dec 9 1999 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included 18 | * in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 24 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #define GLX_GLXEXT_PROTOTYPES 29 | #include 30 | #include "freeglut_internal.h" 31 | 32 | static GLUTproc fghGetGLUTProcAddress( const char* procName ) 33 | { 34 | /* optimization: quick initial check */ 35 | if( strncmp( procName, "glut", 4 ) != 0 ) 36 | return NULL; 37 | 38 | #define CHECK_NAME(x) if( strcmp( procName, #x ) == 0) return (GLUTproc)x; 39 | CHECK_NAME(glutInit); 40 | CHECK_NAME(glutInitDisplayMode); 41 | CHECK_NAME(glutInitDisplayString); 42 | CHECK_NAME(glutInitWindowPosition); 43 | CHECK_NAME(glutInitWindowSize); 44 | CHECK_NAME(glutMainLoop); 45 | CHECK_NAME(glutExit); 46 | CHECK_NAME(glutCreateWindow); 47 | CHECK_NAME(glutCreateSubWindow); 48 | CHECK_NAME(glutDestroyWindow); 49 | CHECK_NAME(glutPostRedisplay); 50 | CHECK_NAME(glutPostWindowRedisplay); 51 | CHECK_NAME(glutSwapBuffers); 52 | CHECK_NAME(glutGetWindow); 53 | CHECK_NAME(glutSetWindow); 54 | CHECK_NAME(glutSetWindowTitle); 55 | CHECK_NAME(glutSetIconTitle); 56 | CHECK_NAME(glutPositionWindow); 57 | CHECK_NAME(glutReshapeWindow); 58 | CHECK_NAME(glutPopWindow); 59 | CHECK_NAME(glutPushWindow); 60 | CHECK_NAME(glutIconifyWindow); 61 | CHECK_NAME(glutShowWindow); 62 | CHECK_NAME(glutHideWindow); 63 | CHECK_NAME(glutFullScreen); 64 | CHECK_NAME(glutSetCursor); 65 | CHECK_NAME(glutWarpPointer); 66 | CHECK_NAME(glutEstablishOverlay); 67 | CHECK_NAME(glutRemoveOverlay); 68 | CHECK_NAME(glutUseLayer); 69 | CHECK_NAME(glutPostOverlayRedisplay); 70 | CHECK_NAME(glutPostWindowOverlayRedisplay); 71 | CHECK_NAME(glutShowOverlay); 72 | CHECK_NAME(glutHideOverlay); 73 | CHECK_NAME(glutCreateMenu); 74 | CHECK_NAME(glutDestroyMenu); 75 | CHECK_NAME(glutGetMenu); 76 | CHECK_NAME(glutSetMenu); 77 | CHECK_NAME(glutAddMenuEntry); 78 | CHECK_NAME(glutAddSubMenu); 79 | CHECK_NAME(glutChangeToMenuEntry); 80 | CHECK_NAME(glutChangeToSubMenu); 81 | CHECK_NAME(glutRemoveMenuItem); 82 | CHECK_NAME(glutAttachMenu); 83 | CHECK_NAME(glutDetachMenu); 84 | CHECK_NAME(glutDisplayFunc); 85 | CHECK_NAME(glutReshapeFunc); 86 | CHECK_NAME(glutKeyboardFunc); 87 | CHECK_NAME(glutMouseFunc); 88 | CHECK_NAME(glutMultiEntryFunc); 89 | CHECK_NAME(glutMultiMotionFunc); 90 | CHECK_NAME(glutMultiButtonFunc); 91 | CHECK_NAME(glutMultiPassiveFunc); 92 | CHECK_NAME(glutMotionFunc); 93 | CHECK_NAME(glutPassiveMotionFunc); 94 | CHECK_NAME(glutEntryFunc); 95 | CHECK_NAME(glutVisibilityFunc); 96 | CHECK_NAME(glutIdleFunc); 97 | CHECK_NAME(glutTimerFunc); 98 | CHECK_NAME(glutMenuStateFunc); 99 | CHECK_NAME(glutSpecialFunc); 100 | CHECK_NAME(glutSpaceballMotionFunc); 101 | CHECK_NAME(glutSpaceballRotateFunc); 102 | CHECK_NAME(glutSpaceballButtonFunc); 103 | CHECK_NAME(glutButtonBoxFunc); 104 | CHECK_NAME(glutDialsFunc); 105 | CHECK_NAME(glutTabletMotionFunc); 106 | CHECK_NAME(glutTabletButtonFunc); 107 | CHECK_NAME(glutMenuStatusFunc); 108 | CHECK_NAME(glutOverlayDisplayFunc); 109 | CHECK_NAME(glutWindowStatusFunc); 110 | CHECK_NAME(glutKeyboardUpFunc); 111 | CHECK_NAME(glutSpecialUpFunc); 112 | #if !defined(_WIN32_WCE) 113 | CHECK_NAME(glutJoystickFunc); 114 | #endif /* !defined(_WIN32_WCE) */ 115 | CHECK_NAME(glutSetColor); 116 | CHECK_NAME(glutGetColor); 117 | CHECK_NAME(glutCopyColormap); 118 | CHECK_NAME(glutGet); 119 | CHECK_NAME(glutDeviceGet); 120 | CHECK_NAME(glutExtensionSupported); 121 | CHECK_NAME(glutGetModifiers); 122 | CHECK_NAME(glutLayerGet); 123 | CHECK_NAME(glutBitmapCharacter); 124 | CHECK_NAME(glutBitmapWidth); 125 | CHECK_NAME(glutStrokeCharacter); 126 | CHECK_NAME(glutStrokeWidth); 127 | CHECK_NAME(glutBitmapLength); 128 | CHECK_NAME(glutStrokeLength); 129 | CHECK_NAME(glutWireSphere); 130 | CHECK_NAME(glutSolidSphere); 131 | CHECK_NAME(glutWireCone); 132 | CHECK_NAME(glutSolidCone); 133 | CHECK_NAME(glutWireCube); 134 | CHECK_NAME(glutSolidCube); 135 | CHECK_NAME(glutWireTorus); 136 | CHECK_NAME(glutSolidTorus); 137 | CHECK_NAME(glutWireDodecahedron); 138 | CHECK_NAME(glutSolidDodecahedron); 139 | CHECK_NAME(glutWireTeapot); 140 | CHECK_NAME(glutSolidTeapot); 141 | CHECK_NAME(glutWireOctahedron); 142 | CHECK_NAME(glutSolidOctahedron); 143 | CHECK_NAME(glutWireTetrahedron); 144 | CHECK_NAME(glutSolidTetrahedron); 145 | CHECK_NAME(glutWireIcosahedron); 146 | CHECK_NAME(glutSolidIcosahedron); 147 | CHECK_NAME(glutVideoResizeGet); 148 | CHECK_NAME(glutSetupVideoResizing); 149 | CHECK_NAME(glutStopVideoResizing); 150 | CHECK_NAME(glutVideoResize); 151 | CHECK_NAME(glutVideoPan); 152 | CHECK_NAME(glutReportErrors); 153 | CHECK_NAME(glutIgnoreKeyRepeat); 154 | CHECK_NAME(glutSetKeyRepeat); 155 | #if !defined(_WIN32_WCE) 156 | CHECK_NAME(glutForceJoystickFunc); 157 | CHECK_NAME(glutGameModeString); 158 | CHECK_NAME(glutEnterGameMode); 159 | CHECK_NAME(glutLeaveGameMode); 160 | CHECK_NAME(glutGameModeGet); 161 | #endif /* !defined(_WIN32_WCE) */ 162 | /* freeglut extensions */ 163 | CHECK_NAME(glutMainLoopEvent); 164 | CHECK_NAME(glutLeaveMainLoop); 165 | CHECK_NAME(glutCloseFunc); 166 | CHECK_NAME(glutWMCloseFunc); 167 | CHECK_NAME(glutMenuDestroyFunc); 168 | CHECK_NAME(glutFullScreenToggle); 169 | CHECK_NAME(glutLeaveFullScreen); 170 | CHECK_NAME(glutSetOption); 171 | CHECK_NAME(glutGetModeValues); 172 | CHECK_NAME(glutSetWindowData); 173 | CHECK_NAME(glutGetWindowData); 174 | CHECK_NAME(glutSetMenuData); 175 | CHECK_NAME(glutGetMenuData); 176 | CHECK_NAME(glutBitmapHeight); 177 | CHECK_NAME(glutStrokeHeight); 178 | CHECK_NAME(glutBitmapString); 179 | CHECK_NAME(glutStrokeString); 180 | CHECK_NAME(glutWireRhombicDodecahedron); 181 | CHECK_NAME(glutSolidRhombicDodecahedron); 182 | CHECK_NAME(glutWireSierpinskiSponge); 183 | CHECK_NAME(glutSolidSierpinskiSponge); 184 | CHECK_NAME(glutWireCylinder); 185 | CHECK_NAME(glutSolidCylinder); 186 | CHECK_NAME(glutGetProcAddress); 187 | CHECK_NAME(glutMouseWheelFunc); 188 | CHECK_NAME(glutJoystickGetNumAxes); 189 | CHECK_NAME(glutJoystickGetNumButtons); 190 | CHECK_NAME(glutJoystickNotWorking); 191 | CHECK_NAME(glutJoystickGetDeadBand); 192 | CHECK_NAME(glutJoystickSetDeadBand); 193 | CHECK_NAME(glutJoystickGetSaturation); 194 | CHECK_NAME(glutJoystickSetSaturation); 195 | CHECK_NAME(glutJoystickSetMinRange); 196 | CHECK_NAME(glutJoystickSetMaxRange); 197 | CHECK_NAME(glutJoystickSetCenter); 198 | CHECK_NAME(glutJoystickGetMinRange); 199 | CHECK_NAME(glutJoystickGetMaxRange); 200 | CHECK_NAME(glutJoystickGetCenter); 201 | CHECK_NAME(glutInitContextVersion); 202 | CHECK_NAME(glutInitContextFlags); 203 | CHECK_NAME(glutInitContextProfile); 204 | CHECK_NAME(glutInitErrorFunc); 205 | CHECK_NAME(glutInitWarningFunc); 206 | #undef CHECK_NAME 207 | 208 | return NULL; 209 | } 210 | 211 | 212 | SFG_Proc fghGetProcAddress( const char *procName ) 213 | { 214 | #if TARGET_HOST_MS_WINDOWS 215 | return (SFG_Proc)wglGetProcAddress( ( LPCSTR )procName ); 216 | #elif TARGET_HOST_POSIX_X11 && defined( GLX_ARB_get_proc_address ) 217 | return (SFG_Proc)glXGetProcAddressARB( ( const GLubyte * )procName ); 218 | #else 219 | return NULL; 220 | #endif 221 | } 222 | 223 | 224 | GLUTproc FGAPIENTRY 225 | glutGetProcAddress( const char *procName ) 226 | { 227 | GLUTproc p; 228 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetProcAddress" ); 229 | 230 | /* Try GLUT functions first, then core GL functions */ 231 | p = fghGetGLUTProcAddress( procName ); 232 | return ( p != NULL ) ? p : fghGetProcAddress( procName ); 233 | } 234 | -------------------------------------------------------------------------------- /freeglut/freeglut_glutfont_definitions.c: -------------------------------------------------------------------------------- 1 | /* 2 | * freeglut_glutfont_definitions.c 3 | * 4 | * Bitmap and stroke fonts displaying. 5 | * 6 | * Copyright (c) 2003 Stephen J. Baker (whether he wants it or not). 7 | * All Rights Reserved. 8 | * Written by John F. Fay , who releases the 9 | * copyright over to the "freeglut" project lead. 10 | * Creation date: Mon July 21 2003 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a 13 | * copy of this software and associated documentation files (the "Software"), 14 | * to deal in the Software without restriction, including without limitation 15 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 16 | * and/or sell copies of the Software, and to permit persons to whom the 17 | * Software is furnished to do so, subject to the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be included 20 | * in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 23 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 26 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 27 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | */ 29 | 30 | /* 31 | * This file is necessary for the *nix version of "freeglut" because the 32 | * original GLUT defined its font variables in rather an unusual way. 33 | * Publicly, in "glut.h", they were defined as "void *". Privately, 34 | * in one of the source code files, they were defined as pointers to a 35 | * structure. Most compilers and linkers are satisfied with the "void *" 36 | * and don't go any farther, but some of them balked. In particular, 37 | * when compiling with "freeglut" and then trying to run using the GLUT 38 | * ".so" library, some of them would give an error. So we are having to 39 | * create this file to define the variables as pointers to an unusual 40 | * structure to match GLUT. 41 | */ 42 | 43 | /* 44 | * freeglut_internal.h uses some GL types, but including the GL header portably 45 | * is a bit tricky, so we include freeglut_std.h here, which contains the 46 | * necessary machinery. But this poses another problem, caused by the ugly 47 | * original defintion of the font constants in "classic" GLUT: They are defined 48 | * as void* externally, so we move them temporarily out of the way by AN EXTREME 49 | * CPP HACK. 50 | */ 51 | 52 | #define glutStrokeRoman glutStrokeRomanIGNOREME 53 | #define glutStrokeMonoRoman glutStrokeMonoRomanIGNOREME 54 | #define glutBitmap9By15 glutBitmap9By15IGNOREME 55 | #define glutBitmap8By13 glutBitmap8By13IGNOREME 56 | #define glutBitmapTimesRoman10 glutBitmapTimesRoman10IGNOREME 57 | #define glutBitmapTimesRoman24 glutBitmapTimesRoman24IGNOREME 58 | #define glutBitmapHelvetica10 glutBitmapHelvetica10IGNOREME 59 | #define glutBitmapHelvetica12 glutBitmapHelvetica12IGNOREME 60 | #define glutBitmapHelvetica18 glutBitmapHelvetica18IGNOREME 61 | 62 | #include 63 | 64 | #undef glutStrokeRoman 65 | #undef glutStrokeMonoRoman 66 | #undef glutBitmap9By15 67 | #undef glutBitmap8By13 68 | #undef glutBitmapTimesRoman10 69 | #undef glutBitmapTimesRoman24 70 | #undef glutBitmapHelvetica10 71 | #undef glutBitmapHelvetica12 72 | #undef glutBitmapHelvetica18 73 | 74 | #include "freeglut_internal.h" 75 | 76 | #if TARGET_HOST_POSIX_X11 77 | 78 | struct freeglutStrokeFont 79 | { 80 | const char *name ; 81 | int num_chars ; 82 | void *ch ; 83 | float top ; 84 | float bottom ; 85 | }; 86 | 87 | struct freeglutBitmapFont 88 | { 89 | const char *name ; 90 | const int num_chars ; 91 | const int first ; 92 | const void *ch ; 93 | }; 94 | 95 | 96 | struct freeglutStrokeFont glutStrokeRoman ; 97 | struct freeglutStrokeFont glutStrokeMonoRoman ; 98 | 99 | struct freeglutBitmapFont glutBitmap9By15 ; 100 | struct freeglutBitmapFont glutBitmap8By13 ; 101 | struct freeglutBitmapFont glutBitmapTimesRoman10 ; 102 | struct freeglutBitmapFont glutBitmapTimesRoman24 ; 103 | struct freeglutBitmapFont glutBitmapHelvetica10 ; 104 | struct freeglutBitmapFont glutBitmapHelvetica12 ; 105 | struct freeglutBitmapFont glutBitmapHelvetica18 ; 106 | 107 | #endif 108 | 109 | -------------------------------------------------------------------------------- /freeglut/freeglut_misc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * freeglut_misc.c 3 | * 4 | * Functions that didn't fit anywhere else... 5 | * 6 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 7 | * Written by Pawel W. Olszta, 8 | * Creation date: Thu Dec 9 1999 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included 18 | * in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 24 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #include 29 | #include "freeglut_internal.h" 30 | 31 | /* 32 | * TODO BEFORE THE STABLE RELEASE: 33 | * 34 | * glutSetColor() -- 35 | * glutGetColor() -- 36 | * glutCopyColormap() -- 37 | * glutSetKeyRepeat() -- this is evil and should be removed from API 38 | */ 39 | 40 | /* -- INTERFACE FUNCTIONS -------------------------------------------------- */ 41 | 42 | /* 43 | * This functions checks if an OpenGL extension is supported or not 44 | * 45 | * XXX Wouldn't this be simpler and clearer if we used strtok()? 46 | */ 47 | int FGAPIENTRY glutExtensionSupported( const char* extension ) 48 | { 49 | const char *extensions, *start; 50 | const size_t len = strlen( extension ); 51 | 52 | /* Make sure there is a current window, and thus a current context available */ 53 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutExtensionSupported" ); 54 | freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 ); 55 | 56 | if (strchr(extension, ' ')) 57 | return 0; 58 | start = extensions = (const char *) glGetString(GL_EXTENSIONS); 59 | 60 | /* XXX consider printing a warning to stderr that there's no current 61 | * rendering context. 62 | */ 63 | freeglut_return_val_if_fail( extensions != NULL, 0 ); 64 | 65 | while (1) { 66 | const char *p = strstr(extensions, extension); 67 | if (!p) 68 | return 0; /* not found */ 69 | /* check that the match isn't a super string */ 70 | if ((p == start || p[-1] == ' ') && (p[len] == ' ' || p[len] == 0)) 71 | return 1; 72 | /* skip the false match and continue */ 73 | extensions = p + len; 74 | } 75 | 76 | return 0 ; 77 | } 78 | 79 | #ifndef GL_INVALID_FRAMEBUFFER_OPERATION 80 | #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT 81 | #define GL_INVALID_FRAMEBUFFER_OPERATION GL_INVALID_FRAMEBUFFER_OPERATION_EXT 82 | #else 83 | #define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506 84 | #endif 85 | #endif 86 | 87 | #ifndef GL_TABLE_TOO_LARGE 88 | #ifdef GL_TABLE_TOO_LARGE_EXT 89 | #define GL_TABLE_TOO_LARGE GL_TABLE_TOO_LARGE_EXT 90 | #else 91 | #define GL_TABLE_TOO_LARGE 0x8031 92 | #endif 93 | #endif 94 | 95 | #ifndef GL_TEXTURE_TOO_LARGE 96 | #ifdef GL_TEXTURE_TOO_LARGE_EXT 97 | #define GL_TEXTURE_TOO_LARGE GL_TEXTURE_TOO_LARGE_EXT 98 | #else 99 | #define GL_TEXTURE_TOO_LARGE 0x8065 100 | #endif 101 | #endif 102 | 103 | /* 104 | * A cut-down local version of gluErrorString to avoid depending on GLU. 105 | */ 106 | static const char* fghErrorString( GLenum error ) 107 | { 108 | switch ( error ) { 109 | case GL_INVALID_ENUM: return "invalid enumerant"; 110 | case GL_INVALID_VALUE: return "invalid value"; 111 | case GL_INVALID_OPERATION: return "invalid operation"; 112 | case GL_STACK_OVERFLOW: return "stack overflow"; 113 | case GL_STACK_UNDERFLOW: return "stack underflow"; 114 | case GL_OUT_OF_MEMORY: return "out of memory"; 115 | case GL_TABLE_TOO_LARGE: return "table too large"; 116 | case GL_INVALID_FRAMEBUFFER_OPERATION: return "invalid framebuffer operation"; 117 | case GL_TEXTURE_TOO_LARGE: return "texture too large"; 118 | default: return "unknown GL error"; 119 | } 120 | } 121 | 122 | /* 123 | * This function reports all the OpenGL errors that happened till now 124 | */ 125 | void FGAPIENTRY glutReportErrors( void ) 126 | { 127 | GLenum error; 128 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReportErrors" ); 129 | while( ( error = glGetError() ) != GL_NO_ERROR ) 130 | fgWarning( "GL error: %s", fghErrorString( error ) ); 131 | } 132 | 133 | /* 134 | * Control the auto-repeat of keystrokes to the current window 135 | */ 136 | void FGAPIENTRY glutIgnoreKeyRepeat( int ignore ) 137 | { 138 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIgnoreKeyRepeat" ); 139 | FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIgnoreKeyRepeat" ); 140 | 141 | fgStructure.CurrentWindow->State.IgnoreKeyRepeat = ignore ? GL_TRUE : GL_FALSE; 142 | } 143 | 144 | /* 145 | * Set global auto-repeat of keystrokes 146 | * 147 | * RepeatMode should be either: 148 | * GLUT_KEY_REPEAT_OFF 149 | * GLUT_KEY_REPEAT_ON 150 | * GLUT_KEY_REPEAT_DEFAULT 151 | */ 152 | void FGAPIENTRY glutSetKeyRepeat( int repeatMode ) 153 | { 154 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetKeyRepeat" ); 155 | 156 | switch( repeatMode ) 157 | { 158 | case GLUT_KEY_REPEAT_OFF: 159 | case GLUT_KEY_REPEAT_ON: 160 | fgState.KeyRepeat = repeatMode; 161 | break; 162 | 163 | case GLUT_KEY_REPEAT_DEFAULT: 164 | fgState.KeyRepeat = GLUT_KEY_REPEAT_ON; 165 | break; 166 | 167 | default: 168 | fgError ("Invalid glutSetKeyRepeat mode: %d", repeatMode); 169 | break; 170 | } 171 | } 172 | 173 | /* 174 | * Forces the joystick callback to be executed 175 | */ 176 | void FGAPIENTRY glutForceJoystickFunc( void ) 177 | { 178 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutForceJoystickFunc" ); 179 | #if !defined(_WIN32_WCE) 180 | freeglut_return_if_fail( fgStructure.CurrentWindow != NULL ); 181 | freeglut_return_if_fail( FETCH_WCB( *( fgStructure.CurrentWindow ), Joystick ) ); 182 | fgJoystickPollWindow( fgStructure.CurrentWindow ); 183 | #endif /* !defined(_WIN32_WCE) */ 184 | } 185 | 186 | /* 187 | * 188 | */ 189 | void FGAPIENTRY glutSetColor( int nColor, GLfloat red, GLfloat green, GLfloat blue ) 190 | { 191 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetColor" ); 192 | /* We really need to do something here. */ 193 | } 194 | 195 | /* 196 | * 197 | */ 198 | GLfloat FGAPIENTRY glutGetColor( int color, int component ) 199 | { 200 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetColor" ); 201 | /* We really need to do something here. */ 202 | return( 0.0f ); 203 | } 204 | 205 | /* 206 | * 207 | */ 208 | void FGAPIENTRY glutCopyColormap( int window ) 209 | { 210 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCopyColormap" ); 211 | /* We really need to do something here. */ 212 | } 213 | 214 | /*** END OF FILE ***/ 215 | -------------------------------------------------------------------------------- /freeglut/freeglut_overlay.c: -------------------------------------------------------------------------------- 1 | /* 2 | * freeglut_overlay.c 3 | * 4 | * Overlay management functions (as defined by GLUT API) 5 | * 6 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 7 | * Written by Pawel W. Olszta, 8 | * Creation date: Thu Dec 16 1999 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included 18 | * in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 24 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #include 29 | #include "freeglut_internal.h" 30 | 31 | /* 32 | * NOTE: functions declared in this file probably will not be implemented. 33 | */ 34 | 35 | /* -- INTERFACE FUNCTIONS -------------------------------------------------- */ 36 | 37 | void FGAPIENTRY glutEstablishOverlay( void ) { /* Not implemented */ } 38 | void FGAPIENTRY glutRemoveOverlay( void ) { /* Not implemented */ } 39 | void FGAPIENTRY glutUseLayer( GLenum layer ) { /* Not implemented */ } 40 | void FGAPIENTRY glutPostOverlayRedisplay( void ) { /* Not implemented */ } 41 | void FGAPIENTRY glutPostWindowOverlayRedisplay( int ID ) { /* Not implemented */ } 42 | void FGAPIENTRY glutShowOverlay( void ) { /* Not implemented */ } 43 | void FGAPIENTRY glutHideOverlay( void ) { /* Not implemented */ } 44 | 45 | /*** END OF FILE ***/ 46 | -------------------------------------------------------------------------------- /freeglut/freeglut_teapot.c: -------------------------------------------------------------------------------- 1 | /* 2 | * freeglut_teapot.c 3 | * 4 | * Teapot(tm) rendering code. 5 | * 6 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 7 | * Written by Pawel W. Olszta, 8 | * Creation date: Fri Dec 24 1999 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included 18 | * in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 24 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | /* 29 | * Original teapot code copyright follows: 30 | */ 31 | 32 | /* 33 | * (c) Copyright 1993, Silicon Graphics, Inc. 34 | * 35 | * ALL RIGHTS RESERVED 36 | * 37 | * Permission to use, copy, modify, and distribute this software 38 | * for any purpose and without fee is hereby granted, provided 39 | * that the above copyright notice appear in all copies and that 40 | * both the copyright notice and this permission notice appear in 41 | * supporting documentation, and that the name of Silicon 42 | * Graphics, Inc. not be used in advertising or publicity 43 | * pertaining to distribution of the software without specific, 44 | * written prior permission. 45 | * 46 | * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU 47 | * "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR 48 | * OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF 49 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO 50 | * EVENT SHALL SILICON GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE 51 | * ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR 52 | * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER, 53 | * INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE, 54 | * SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR 55 | * NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF THE POSSIBILITY 56 | * OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 57 | * ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR 58 | * PERFORMANCE OF THIS SOFTWARE. 59 | * 60 | * US Government Users Restricted Rights 61 | * 62 | * Use, duplication, or disclosure by the Government is subject to 63 | * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph 64 | * (c)(1)(ii) of the Rights in Technical Data and Computer 65 | * Software clause at DFARS 252.227-7013 and/or in similar or 66 | * successor clauses in the FAR or the DOD or NASA FAR 67 | * Supplement. Unpublished-- rights reserved under the copyright 68 | * laws of the United States. Contractor/manufacturer is Silicon 69 | * Graphics, Inc., 2011 N. Shoreline Blvd., Mountain View, CA 70 | * 94039-7311. 71 | * 72 | * OpenGL(TM) is a trademark of Silicon Graphics, Inc. 73 | */ 74 | 75 | #include 76 | #include "freeglut_internal.h" 77 | #include "freeglut_teapot_data.h" 78 | 79 | /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */ 80 | 81 | 82 | static void fghTeapot( GLint grid, GLdouble scale, GLenum type ) 83 | { 84 | #if defined(_WIN32_WCE) 85 | int i, numV=sizeof(strip_vertices)/4, numI=sizeof(strip_normals)/4; 86 | #else 87 | double p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3]; 88 | long i, j, k, l; 89 | #endif 90 | 91 | glPushAttrib( GL_ENABLE_BIT | GL_EVAL_BIT ); 92 | glEnable( GL_AUTO_NORMAL ); 93 | glEnable( GL_NORMALIZE ); 94 | glEnable( GL_MAP2_VERTEX_3 ); 95 | glEnable( GL_MAP2_TEXTURE_COORD_2 ); 96 | 97 | glPushMatrix(); 98 | glRotated( 270.0, 1.0, 0.0, 0.0 ); 99 | glScaled( 0.5 * scale, 0.5 * scale, 0.5 * scale ); 100 | glTranslated( 0.0, 0.0, -1.5 ); 101 | 102 | #if defined(_WIN32_WCE) 103 | glRotated( 90.0, 1.0, 0.0, 0.0 ); 104 | glBegin( GL_TRIANGLE_STRIP ); 105 | 106 | for( i = 0; i < numV-1; i++ ) 107 | { 108 | int vidx = strip_vertices[i], 109 | nidx = strip_normals[i]; 110 | 111 | if( vidx != -1 ) 112 | { 113 | glNormal3fv( normals[nidx] ); 114 | glVertex3fv( vertices[vidx] ); 115 | } 116 | else 117 | { 118 | glEnd(); 119 | glBegin( GL_TRIANGLE_STRIP ); 120 | } 121 | } 122 | 123 | glEnd(); 124 | #else 125 | for (i = 0; i < 10; i++) { 126 | for (j = 0; j < 4; j++) { 127 | for (k = 0; k < 4; k++) { 128 | for (l = 0; l < 3; l++) { 129 | p[j][k][l] = cpdata[patchdata[i][j * 4 + k]][l]; 130 | q[j][k][l] = cpdata[patchdata[i][j * 4 + (3 - k)]][l]; 131 | if (l == 1) 132 | q[j][k][l] *= -1.0; 133 | if (i < 6) { 134 | r[j][k][l] = 135 | cpdata[patchdata[i][j * 4 + (3 - k)]][l]; 136 | if (l == 0) 137 | r[j][k][l] *= -1.0; 138 | s[j][k][l] = cpdata[patchdata[i][j * 4 + k]][l]; 139 | if (l == 0) 140 | s[j][k][l] *= -1.0; 141 | if (l == 1) 142 | s[j][k][l] *= -1.0; 143 | } 144 | } 145 | } 146 | } 147 | 148 | glMap2d(GL_MAP2_TEXTURE_COORD_2, 0.0, 1.0, 2, 2, 0.0, 1.0, 4, 2, 149 | &tex[0][0][0]); 150 | glMap2d(GL_MAP2_VERTEX_3, 0.0, 1.0, 3, 4, 0.0, 1.0, 12, 4, 151 | &p[0][0][0]); 152 | glMapGrid2d(grid, 0.0, 1.0, grid, 0.0, 1.0); 153 | glEvalMesh2(type, 0, grid, 0, grid); 154 | glMap2d(GL_MAP2_VERTEX_3, 0.0, 1.0, 3, 4, 0.0, 1.0, 12, 4, 155 | &q[0][0][0]); 156 | glEvalMesh2(type, 0, grid, 0, grid); 157 | if (i < 6) { 158 | glMap2d(GL_MAP2_VERTEX_3, 0.0, 1.0, 3, 4, 0.0, 1.0, 12, 4, 159 | &r[0][0][0]); 160 | glEvalMesh2(type, 0, grid, 0, grid); 161 | glMap2d(GL_MAP2_VERTEX_3, 0.0, 1.0, 3, 4, 0.0, 1.0, 12, 4, 162 | &s[0][0][0]); 163 | glEvalMesh2(type, 0, grid, 0, grid); 164 | } 165 | } 166 | #endif /* defined(_WIN32_WCE) */ 167 | 168 | glPopMatrix(); 169 | glPopAttrib(); 170 | } 171 | 172 | 173 | /* -- INTERFACE FUNCTIONS -------------------------------------------------- */ 174 | 175 | /* 176 | * Renders a beautiful wired teapot... 177 | */ 178 | void FGAPIENTRY glutWireTeapot( GLdouble size ) 179 | { 180 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireTeapot" ); 181 | /* We will use the general teapot rendering code */ 182 | fghTeapot( 10, size, GL_LINE ); 183 | } 184 | 185 | /* 186 | * Renders a beautiful filled teapot... 187 | */ 188 | void FGAPIENTRY glutSolidTeapot( GLdouble size ) 189 | { 190 | FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidTeapot" ); 191 | /* We will use the general teapot rendering code */ 192 | fghTeapot( 7, size, GL_FILL ); 193 | } 194 | 195 | /*** END OF FILE ***/ 196 | 197 | 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /freeglut/freeglut_videoresize.c: -------------------------------------------------------------------------------- 1 | /* 2 | * freeglut_videoresize.c 3 | * 4 | * Video resize functions (as defined by GLUT API) 5 | * 6 | * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved. 7 | * Written by Pawel W. Olszta, 8 | * Creation date: Thu Dec 16 1999 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included 18 | * in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 24 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | #include 29 | #include "freeglut_internal.h" 30 | 31 | /* 32 | * NOTE: functions declared in this file probably will not be implemented. 33 | */ 34 | 35 | /* -- INTERFACE FUNCTIONS -------------------------------------------------- */ 36 | 37 | int FGAPIENTRY glutVideoResizeGet( GLenum eWhat ) { return( 0x00 ); } 38 | void FGAPIENTRY glutSetupVideoResizing( void ) { /* Not implemented */ } 39 | void FGAPIENTRY glutStopVideoResizing( void ) { /* Not implemented */ } 40 | void FGAPIENTRY glutVideoResize( int x, int y, int w, int h ) { /* Not implemented */ } 41 | void FGAPIENTRY glutVideoPan( int x, int y, int w, int h ) { /* Not implemented */ } 42 | 43 | /*** END OF FILE ***/ 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /freeglut/freeglut_xinput.c: -------------------------------------------------------------------------------- 1 | /* Written for XI1 by Nikolas Doerfler (c) 2008 * 2 | * Rewritten for XI2 by Florian Echtler (c) 2009 */ 3 | 4 | #include 5 | 6 | #include "freeglut_internal.h" 7 | 8 | #if TARGET_HOST_POSIX_X11 && HAVE_X11_EXTENSIONS_XINPUT2_H 9 | 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | /* import function from freeglut_main.c */ 17 | int fghGetXModifiers( int state ); 18 | 19 | /* extension opcode for XInput */ 20 | int xi_opcode = -1; 21 | 22 | /** 23 | * \brief Sets window up for XI2 events. 24 | */ 25 | void fgRegisterDevices( Display* dpy, Window* win ) { 26 | 27 | XIEventMask mask; 28 | unsigned char flags[2] = { 0, 0 }; 29 | int event, error; 30 | 31 | /*Display* dpy = fgDisplay.Display; 32 | Window* win = glutGetXWindow();*/ 33 | 34 | /* get XInput extension opcode */ 35 | if (!XQueryExtension( dpy, "XInputExtension", &xi_opcode, &event, &error )) { xi_opcode = -1; } 36 | 37 | /* Select for motion events */ 38 | mask.deviceid = XIAllMasterDevices; 39 | mask.mask_len = 2; 40 | mask.mask = flags; 41 | 42 | XISetMask(mask.mask, XI_Enter); 43 | XISetMask(mask.mask, XI_Motion); 44 | XISetMask(mask.mask, XI_ButtonPress); 45 | XISetMask(mask.mask, XI_ButtonRelease); 46 | XISetMask(mask.mask, XI_Leave); 47 | /*XISetMask(mask.mask, XI_KeyPress); 48 | XISetMask(mask.mask, XI_KeyRelease); 49 | XISetMask(mask.mask, XI_DeviceChanged); 50 | XISetMask(mask.mask, XI_RawEvent); 51 | XISetMask(mask.mask, XI_FocusIn); 52 | XISetMask(mask.mask, XI_FocusOut); 53 | XISetMask(mask.mask, XI_HierarchyChanged);*/ 54 | 55 | XISelectEvents( dpy, *win, &mask, 1 ); 56 | } 57 | 58 | 59 | void fgPrintXILeaveEvent(XILeaveEvent* event) 60 | { 61 | char* mode = ""; 62 | char* detail = ""; 63 | int i; 64 | 65 | printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n", 66 | event->root, event->event, event->child); 67 | switch(event->mode) 68 | { 69 | case NotifyNormal: mode = "NotifyNormal"; break; 70 | case NotifyGrab: mode = "NotifyGrab"; break; 71 | case NotifyUngrab: mode = "NotifyUngrab"; break; 72 | case NotifyWhileGrabbed: mode = "NotifyWhileGrabbed"; break; 73 | } 74 | switch (event->detail) 75 | { 76 | case NotifyAncestor: detail = "NotifyAncestor"; break; 77 | case NotifyVirtual: detail = "NotifyVirtual"; break; 78 | case NotifyInferior: detail = "NotifyInferior"; break; 79 | case NotifyNonlinear: detail = "NotifyNonlinear"; break; 80 | case NotifyNonlinearVirtual: detail = "NotifyNonlinearVirtual"; break; 81 | case NotifyPointer: detail = "NotifyPointer"; break; 82 | case NotifyPointerRoot: detail = "NotifyPointerRoot"; break; 83 | case NotifyDetailNone: detail = "NotifyDetailNone"; break; 84 | } 85 | printf(" mode: %s (detail %s)\n", mode, detail); 86 | printf(" flags: %s %s\n", event->focus ? "[focus]" : "", 87 | event->same_screen ? "[same screen]" : ""); 88 | printf(" buttons:"); 89 | for (i = 0; i < event->buttons.mask_len * 8; i++) 90 | if (XIMaskIsSet(event->buttons.mask, i)) 91 | printf(" %d", i); 92 | printf("\n"); 93 | 94 | printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n", 95 | event->mods.locked, event->mods.latched, 96 | event->mods.base); 97 | printf(" group: locked 0x%x latched 0x%x base 0x%x\n", 98 | event->group.locked, event->group.latched, 99 | event->group.base); 100 | 101 | printf(" root x/y: %.2f / %.2f\n", event->root_x, event->root_y); 102 | printf(" event x/y: %.2f / %.2f\n", event->event_x, event->event_y); 103 | 104 | } 105 | 106 | 107 | void fgPrintXIDeviceEvent(XIDeviceEvent* event) 108 | { 109 | double *val; 110 | int i; 111 | 112 | printf(" device: %d (%d)\n", event->deviceid, event->sourceid); 113 | printf(" detail: %d\n", event->detail); 114 | printf(" buttons:"); 115 | for (i = 0; i < event->buttons.mask_len * 8; i++) 116 | if (XIMaskIsSet(event->buttons.mask, i)) 117 | printf(" %d", i); 118 | printf("\n"); 119 | 120 | printf(" modifiers: locked 0x%x latched 0x%x base 0x%x\n", 121 | event->mods.locked, event->mods.latched, 122 | event->mods.base); 123 | printf(" group: locked 0x%x latched 0x%x base 0x%x\n", 124 | event->group.locked, event->group.latched, 125 | event->group.base); 126 | printf(" valuators:"); 127 | 128 | val = event->valuators.values; 129 | for (i = 0; i < event->valuators.mask_len * 8; i++) 130 | if (XIMaskIsSet(event->valuators.mask, i)) 131 | printf(" %d: %.2f", i, *val++); 132 | printf("\n"); 133 | 134 | printf(" windows: root 0x%lx event 0x%lx child 0x%ld\n", 135 | event->root, event->event, event->child); 136 | printf(" root x/y: %.2f / %.2f\n", event->root_x, event->root_y); 137 | printf(" event x/y: %.2f / %.2f\n", event->event_x, event->event_y); 138 | 139 | } 140 | 141 | 142 | /** 143 | * \brief This function is called when an Extension Event is received 144 | * and calls the corresponding callback functions for these events. 145 | */ 146 | void fgHandleExtensionEvents( XEvent* base_ev ) { 147 | 148 | int i, button = 0; 149 | XGenericEventCookie* cookie = (XGenericEventCookie*)&(base_ev->xcookie); 150 | 151 | if ( XGetEventData( fgDisplay.Display, cookie ) && (cookie->type == GenericEvent) && (cookie->extension == xi_opcode) ) { 152 | 153 | XIDeviceEvent* event = (XIDeviceEvent*)(cookie->data); 154 | /*printf("XI2 event type: %d - %d\n", cookie->evtype, event->type );*/ 155 | 156 | SFG_Window* window = fgWindowByHandle( event->event ); 157 | if (!window) return; 158 | 159 | switch (cookie->evtype) { 160 | 161 | case XI_Enter: 162 | case XI_Leave: 163 | fgState.Modifiers = fghGetXModifiers( ((XIEnterEvent*)event)->mods.base ); 164 | INVOKE_WCB( *window, MultiEntry, ( 165 | event->deviceid, 166 | (event->evtype == XI_Enter ? GLUT_ENTERED : GLUT_LEFT) 167 | )); 168 | #if _DEBUG 169 | fgPrintXILeaveEvent((XILeaveEvent*)event); 170 | #endif 171 | break; 172 | 173 | case XI_ButtonPress: 174 | case XI_ButtonRelease: 175 | fgState.Modifiers = fghGetXModifiers( event->mods.base ); 176 | INVOKE_WCB( *window, MultiButton, ( 177 | event->deviceid, 178 | event->event_x, 179 | event->event_y, 180 | (event->detail)-1, 181 | (event->evtype == XI_ButtonPress ? GLUT_DOWN : GLUT_UP) 182 | )); 183 | INVOKE_WCB( *window, Mouse, ( 184 | (event->detail)-1, 185 | (event->evtype == XI_ButtonPress ? GLUT_DOWN : GLUT_UP), 186 | event->event_x, 187 | event->event_y 188 | )); 189 | break; 190 | 191 | case XI_Motion: 192 | fgState.Modifiers = fghGetXModifiers( event->mods.base ); 193 | for (i = 0; i < event->buttons.mask_len; i++) if (event->buttons.mask[i]) button = 1; 194 | if (button) { 195 | INVOKE_WCB( *window, MultiMotion, ( event->deviceid, event->event_x, event->event_y ) ); 196 | INVOKE_WCB( *window, Motion, ( event->event_x, event->event_y ) ); 197 | } else { 198 | INVOKE_WCB( *window, MultiPassive, ( event->deviceid, event->event_x, event->event_y ) ); 199 | INVOKE_WCB( *window, Passive, ( event->event_x, event->event_y ) ); 200 | } 201 | #if _DEBUG 202 | fgPrintXIDeviceEvent(event); 203 | #endif 204 | break; 205 | 206 | default: 207 | #if _DEBUG 208 | fgWarning( "Unknown XI2 device event:" ); 209 | fgPrintXIDeviceEvent( event ); 210 | #endif 211 | break; 212 | } 213 | fgState.Modifiers = INVALID_MODIFIERS; 214 | } 215 | XFreeEventData( fgDisplay.Display, cookie ); 216 | } 217 | 218 | #endif 219 | 220 | -------------------------------------------------------------------------------- /freeglut/glut.h: -------------------------------------------------------------------------------- 1 | #ifndef __GLUT_H__ 2 | #define __GLUT_H__ 3 | 4 | /* 5 | * glut.h 6 | * 7 | * The freeglut library include file 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 10 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 11 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 12 | * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | */ 16 | 17 | #include "freeglut_std.h" 18 | 19 | /*** END OF FILE ***/ 20 | 21 | #endif /* __GLUT_H__ */ 22 | -------------------------------------------------------------------------------- /src/Camera.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | 27 | // Libraries 28 | #include "Camera.h" 29 | #include "definitions.h" 30 | #include 31 | 32 | // Namespaces 33 | using namespace openglframework; 34 | 35 | // Constructor 36 | Camera::Camera() : Object3D() { 37 | 38 | // Set default values 39 | mFieldOfView = 45.0f; 40 | mSceneRadius = 1.0f; 41 | mNearPlane = 0.1f; 42 | mFarPlane = 10.0f; 43 | mWidth = 1; 44 | mHeight = 1; 45 | 46 | // Update the projection matrix 47 | updateProjectionMatrix(); 48 | } 49 | 50 | // Destructor 51 | Camera::~Camera() { 52 | 53 | } 54 | 55 | // Update the projection matrix 56 | void Camera::updateProjectionMatrix() { 57 | 58 | // Compute the aspect ratio 59 | float aspect = float(mWidth) / float(mHeight); 60 | 61 | float top = mNearPlane * tan((mFieldOfView / 2.0f) * (float(PI) / 180.0f)); 62 | float bottom = -top; 63 | float left = bottom * aspect; 64 | float right = top * aspect; 65 | 66 | float fx = 2.0f * mNearPlane / (right - left); 67 | float fy = 2.0f * mNearPlane / (top - bottom); 68 | float fz = -(mFarPlane + mNearPlane) / (mFarPlane - mNearPlane); 69 | float fw = -2.0f * mFarPlane * mNearPlane / (mFarPlane - mNearPlane); 70 | 71 | // Recompute the projection matrix 72 | mProjectionMatrix = Matrix4(fx, 0, 0, 0, 73 | 0, fy, 0, 0, 74 | 0, 0, fz, fw, 75 | 0, 0, -1, 0); 76 | } 77 | 78 | // Translate the camera go a given point using the dx, dy fraction 79 | void Camera::translateCamera(float dx, float dy, const Vector3& worldPoint) { 80 | 81 | // Transform the world point into camera coordinates 82 | Vector3 pointCamera = mTransformMatrix.getInverse() * worldPoint; 83 | 84 | // Get the depth 85 | float z = -pointCamera.z; 86 | 87 | // Find the scaling of dx and dy from windows coordinates to near plane coordinates 88 | // and from there to camera coordinates at the object's depth 89 | float aspect = float(mWidth) / float(mHeight); 90 | float top = mNearPlane * tan(mFieldOfView * PI / 360.0f); 91 | float right = top * aspect; 92 | 93 | // Translate the camera 94 | translateLocal(Vector3(2.0f * dx * right / mNearPlane * z, 95 | -2.0f * dy * top / mNearPlane * z, 96 | 0.0f)); 97 | } 98 | -------------------------------------------------------------------------------- /src/Camera.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef CAMERA_H 27 | #define CAMERA_H 28 | 29 | // Libraries 30 | #include "Object3D.h" 31 | #include "definitions.h" 32 | 33 | namespace openglframework { 34 | 35 | // Class Camera 36 | class Camera : public Object3D { 37 | 38 | protected : 39 | 40 | // ------------------- Attributes ------------------- // 41 | 42 | // Field of view 43 | float mFieldOfView; 44 | 45 | // Radius of the scene 46 | float mSceneRadius; 47 | 48 | // Near plane 49 | float mNearPlane; 50 | 51 | // Far plane 52 | float mFarPlane; 53 | 54 | // Width of the camera 55 | uint mWidth; 56 | 57 | // Height of the camera 58 | uint mHeight; 59 | 60 | // Projection matrix 61 | Matrix4 mProjectionMatrix; 62 | 63 | // ------------------- Methods ------------------- // 64 | 65 | // Update the projection matrix 66 | void updateProjectionMatrix(); 67 | 68 | public: 69 | 70 | // ------------------- Methods ------------------- // 71 | 72 | // Constructor 73 | Camera(); 74 | 75 | // Destructor 76 | ~Camera(); 77 | 78 | // Get the projection matrix 79 | const Matrix4& getProjectionMatrix() const; 80 | 81 | // Set the dimensions of the camera 82 | void setDimensions(uint width, uint height); 83 | 84 | // Get the radius of the scene the camera should capture 85 | float getSceneRadius() const; 86 | 87 | // Set the radius of the scene the camera should capture 88 | // This will update the clipping planes accordingly 89 | void setSceneRadius(float radius); 90 | 91 | // Set the clipping planes 92 | void setClippingPlanes(float near, float far); 93 | 94 | // Set the field of view 95 | void setFieldOfView(float fov); 96 | 97 | // Set the zoom of the camera (a fraction between 0 and 1) 98 | void setZoom(float fraction); 99 | 100 | // Translate the camera go a given point using the dx, dy fraction 101 | void translateCamera(float dx, float dy, const Vector3& worldPoint); 102 | 103 | // Get the near clipping plane 104 | float getNearClippingPlane() const; 105 | 106 | // Get the far clipping plane 107 | float getFarClippingPlane() const; 108 | 109 | // Get the width 110 | uint getWidth() const; 111 | 112 | // Get the height 113 | uint getHeight() const; 114 | }; 115 | 116 | // Get the projection matrix 117 | inline const Matrix4& Camera::getProjectionMatrix() const { 118 | return mProjectionMatrix; 119 | } 120 | 121 | // Set the dimensions of the camera 122 | inline void Camera::setDimensions(uint width, uint height) { 123 | mWidth = width; 124 | mHeight = height; 125 | updateProjectionMatrix(); 126 | } 127 | 128 | // Get the radius of the scene the camera should capture 129 | inline float Camera::getSceneRadius() const { 130 | return mSceneRadius; 131 | } 132 | 133 | // Set the radius of the scene the camera should capture 134 | // This will update the clipping planes accordingly 135 | inline void Camera::setSceneRadius(float radius) { 136 | mSceneRadius = radius; 137 | setClippingPlanes(0.01f * radius, 10.0f * radius); 138 | } 139 | 140 | // Set the clipping planes 141 | inline void Camera::setClippingPlanes(float near, float far) { 142 | mNearPlane = near; 143 | mFarPlane = far; 144 | updateProjectionMatrix(); 145 | } 146 | 147 | // Set the field of view 148 | inline void Camera::setFieldOfView(float fov) { 149 | mFieldOfView = fov; 150 | updateProjectionMatrix(); 151 | } 152 | 153 | // Set the zoom of the camera (a fraction between 0 and 1) 154 | inline void Camera::setZoom(float fraction) { 155 | Vector3 zoomVector(0, 0, mSceneRadius * fraction * 3.0f); 156 | translateLocal(zoomVector); 157 | } 158 | 159 | // Get the near clipping plane 160 | inline float Camera::getNearClippingPlane() const { 161 | return mNearPlane; 162 | } 163 | 164 | // Get the far clipping plane 165 | inline float Camera::getFarClippingPlane() const { 166 | return mFarPlane; 167 | } 168 | 169 | // Get the width 170 | inline uint Camera::getWidth() const { 171 | return mWidth; 172 | } 173 | 174 | // Get the height 175 | inline uint Camera::getHeight() const { 176 | return mHeight; 177 | } 178 | 179 | } 180 | 181 | #endif 182 | -------------------------------------------------------------------------------- /src/FrameBufferObject.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "FrameBufferObject.h" 28 | #include 29 | 30 | using namespace openglframework; 31 | using namespace std; 32 | 33 | // Constructor 34 | FrameBufferObject::FrameBufferObject() : mFrameBufferID(0), mRenderBufferID (0) { 35 | 36 | } 37 | 38 | // Destructor 39 | FrameBufferObject::~FrameBufferObject() { 40 | 41 | } 42 | 43 | // Create the frame buffer object 44 | bool FrameBufferObject::create(uint width, uint height, bool needRenderBuffer) { 45 | 46 | // Destroy the current FBO 47 | destroy(); 48 | 49 | // Check that the needed OpenGL extensions are available 50 | bool isExtensionOK = checkOpenGLExtensions(); 51 | if (!isExtensionOK) { 52 | std::cerr << "Error : Impossible to use Framebuffer Object on this platform" << std::endl; 53 | assert(false); 54 | return false; 55 | } 56 | 57 | // Generate a new FBO 58 | glGenFramebuffersEXT(1, &mFrameBufferID); 59 | assert(mFrameBufferID != 0); 60 | 61 | // If we also need to create a render buffer 62 | if (needRenderBuffer) { 63 | 64 | // Generate the render buffer 65 | glGenRenderbuffers(1, &mRenderBufferID); 66 | assert(mRenderBufferID != 0); 67 | 68 | glBindRenderbuffer(GL_RENDERBUFFER, mRenderBufferID); 69 | glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, width, height); 70 | glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferID); 71 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 72 | mRenderBufferID); 73 | glBindRenderbuffer(GL_RENDERBUFFER, 0); 74 | glBindFramebuffer(GL_FRAMEBUFFER, 0); 75 | } 76 | 77 | // Check the FBO status 78 | GLenum statusFBO = glCheckFramebufferStatus(GL_FRAMEBUFFER); 79 | if(statusFBO != GL_FRAMEBUFFER_COMPLETE) { 80 | cerr << "Error : An error occured while creating the Framebuffer Object !" << endl; 81 | return false; 82 | } 83 | 84 | return true; 85 | } 86 | 87 | // Destroy the FBO 88 | void FrameBufferObject::destroy() { 89 | 90 | // Delete the frame buffer object 91 | if (mFrameBufferID) { 92 | glDeleteFramebuffers(1, &mFrameBufferID); 93 | mFrameBufferID = 0; 94 | } 95 | 96 | // Delete the render buffer 97 | if (mRenderBufferID) { 98 | glDeleteRenderbuffers(1, &mRenderBufferID); 99 | } 100 | } 101 | 102 | // Attach a texture to the frame buffer object 103 | void FrameBufferObject::attachTexture(uint position, uint textureID) { 104 | assert(mFrameBufferID); 105 | 106 | // Bind the current FBO 107 | glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferID); 108 | 109 | // Bind the texture 110 | glFramebufferTexture2D(GL_FRAMEBUFFER, position, GL_TEXTURE_2D, textureID, 0); 111 | 112 | // Unbind the current FBO 113 | glBindFramebuffer(GL_FRAMEBUFFER, 0); 114 | } 115 | -------------------------------------------------------------------------------- /src/FrameBufferObject.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef FRAME_BUFFER_OBJECT_H 27 | #define FRAME_BUFFER_OBJECT_H 28 | 29 | // Libraries 30 | #include "definitions.h" 31 | #include 32 | #include 33 | #include 34 | 35 | namespace openglframework { 36 | 37 | 38 | // Class FrameBufferObject 39 | class FrameBufferObject { 40 | 41 | private: 42 | 43 | // -------------------- Attributes -------------------- // 44 | 45 | // Frame buffer ID 46 | uint mFrameBufferID; 47 | 48 | // Render buffer ID 49 | uint mRenderBufferID; 50 | 51 | public: 52 | 53 | // -------------------- Methods -------------------- // 54 | 55 | // Constructor 56 | FrameBufferObject(); 57 | 58 | // Destructor 59 | ~FrameBufferObject(); 60 | 61 | // Create the frame buffer object 62 | bool create(uint width, uint height, bool needRenderBuffer = true); 63 | 64 | // Attach a texture to the frame buffer object 65 | void attachTexture(uint position, uint textureID); 66 | 67 | // Bind the FBO 68 | void bind(uint position) const; 69 | 70 | // Unbind the FBO 71 | void unbind() const; 72 | 73 | // Return true if the needed OpenGL extensions are available for FBO 74 | static bool checkOpenGLExtensions(); 75 | 76 | // Destroy the FBO 77 | void destroy(); 78 | }; 79 | 80 | // Bind the FBO 81 | inline void FrameBufferObject::bind(uint position) const { 82 | assert(mFrameBufferID != 0); 83 | glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferID); 84 | glDrawBuffer(position); 85 | glReadBuffer(position); 86 | } 87 | 88 | // Unbind the FBO 89 | inline void FrameBufferObject::unbind() const { 90 | assert(mFrameBufferID != 0); 91 | glDrawBuffer(GL_NONE); 92 | glReadBuffer(GL_NONE); 93 | glBindFramebuffer(GL_FRAMEBUFFER, 0); 94 | } 95 | 96 | // Return true if the needed OpenGL extensions are available for FBO 97 | inline bool FrameBufferObject::checkOpenGLExtensions() { 98 | 99 | // Check that OpenGL version is at least 3.0 or there the framebuffer object extension exists 100 | return (GLEW_VERSION_3_0 || GLEW_ARB_framebuffer_object); 101 | } 102 | 103 | } 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /src/GlutViewer.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "GlutViewer.h" 28 | #include 29 | 30 | // Namespaces 31 | using namespace openglframework; 32 | using namespace std; 33 | 34 | // Constructor 35 | GlutViewer::GlutViewer() { 36 | 37 | // Initialize the state of the mouse buttons 38 | for (int i=0; i<10; i++) { 39 | mIsButtonDown[i] = false; 40 | } 41 | } 42 | 43 | // Destructor 44 | GlutViewer::~GlutViewer() { 45 | 46 | } 47 | 48 | // Initialize the viewer 49 | bool GlutViewer::init(int argc, char** argv, const string& windowsTitle, 50 | const Vector2& windowsSize, const Vector2& windowsPosition, 51 | bool isMultisamplingActive) { 52 | 53 | // Initialize the GLUT library 54 | bool outputValue = initGLUT(argc, argv, windowsTitle, windowsSize, 55 | windowsPosition, isMultisamplingActive); 56 | 57 | // Active the multi-sampling by default 58 | if (isMultisamplingActive) { 59 | activateMultiSampling(true); 60 | } 61 | 62 | return outputValue; 63 | } 64 | 65 | // Initialize the GLUT library 66 | bool GlutViewer::initGLUT(int argc, char** argv, const string& windowsTitle, 67 | const Vector2& windowsSize, const Vector2& windowsPosition, 68 | bool isMultisamplingActive) { 69 | 70 | // Initialize GLUT 71 | glutInit(&argc, argv); 72 | uint modeWithoutMultiSampling = GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH; 73 | uint modeWithMultiSampling = GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GL_MULTISAMPLE; 74 | uint displayMode = isMultisamplingActive ? modeWithMultiSampling : modeWithoutMultiSampling; 75 | glutInitDisplayMode(displayMode); 76 | 77 | // Initialize the size of the GLUT windows 78 | glutInitWindowSize(static_cast(windowsSize.x), 79 | static_cast(windowsSize.y)); 80 | 81 | // Initialize the position of the GLUT windows 82 | glutInitWindowPosition(static_cast(windowsPosition.x), 83 | static_cast(windowsPosition.y)); 84 | 85 | // Create the GLUT windows 86 | glutCreateWindow(windowsTitle.c_str()); 87 | 88 | // Initialize the GLEW library 89 | GLenum error = glewInit(); 90 | if (error != GLEW_OK) { 91 | 92 | // Problem: glewInit failed, something is wrong 93 | cerr << "GLEW Error : " << glewGetErrorString(error) << std::endl; 94 | assert(false); 95 | return false; 96 | } 97 | 98 | return true; 99 | } 100 | 101 | // Set the camera so that we can view the whole scene 102 | void GlutViewer::resetCameraToViewAll() { 103 | 104 | // Move the camera to the origin of the scene 105 | mCamera.translateWorld(-mCamera.getOrigin()); 106 | 107 | // Move the camera to the center of the scene 108 | mCamera.translateWorld(mCenterScene); 109 | 110 | // Set the zoom of the camera so that the scene center is 111 | // in negative view direction of the camera 112 | mCamera.setZoom(1.0); 113 | } 114 | 115 | // Called when a GLUT mouse button event occurs 116 | void GlutViewer::mouseButtonEvent(int button, int state, int x, int y) { 117 | 118 | // If the mouse button is pressed 119 | if (state == GLUT_DOWN) { 120 | mLastMouseX = x; 121 | mLastMouseY = y; 122 | mIsLastPointOnSphereValid = mapMouseCoordinatesToSphere(x, y, mLastPointOnSphere); 123 | mIsButtonDown[button] = true; 124 | } 125 | else { // If the mouse button is released 126 | mIsLastPointOnSphereValid = false; 127 | mIsButtonDown[button] = false; 128 | 129 | // If it is a mouse wheel click event 130 | if (button == 3) { 131 | zoom(0, (int) (y - 0.05f * mCamera.getWidth())); 132 | } 133 | else if (button == 4) { 134 | zoom(0, (int) (y + 0.05f * mCamera.getHeight())); 135 | } 136 | } 137 | 138 | mModifiers = glutGetModifiers(); 139 | 140 | // Notify GLUT to redisplay 141 | glutPostRedisplay(); 142 | } 143 | 144 | // Called when a GLUT mouse motion event occurs 145 | void GlutViewer::mouseMotionEvent(int xMouse, int yMouse) { 146 | 147 | // Zoom 148 | if ((mIsButtonDown[GLUT_LEFT_BUTTON] && mIsButtonDown[GLUT_MIDDLE_BUTTON]) || 149 | (mIsButtonDown[GLUT_LEFT_BUTTON] && mModifiers == GLUT_ACTIVE_ALT)) { 150 | zoom(xMouse, yMouse); 151 | } 152 | // Translation 153 | else if (mIsButtonDown[GLUT_MIDDLE_BUTTON] || mIsButtonDown[GLUT_RIGHT_BUTTON] || 154 | (mIsButtonDown[GLUT_LEFT_BUTTON] && (mModifiers == GLUT_ACTIVE_ALT))) { 155 | translate(xMouse, yMouse); 156 | } 157 | // Rotation 158 | else if (mIsButtonDown[GLUT_LEFT_BUTTON]) { 159 | rotate(xMouse, yMouse); 160 | } 161 | 162 | // Remember the mouse position 163 | mLastMouseX = xMouse; 164 | mLastMouseY = yMouse; 165 | mIsLastPointOnSphereValid = mapMouseCoordinatesToSphere(xMouse, yMouse, mLastPointOnSphere); 166 | 167 | // Notify GLUT to redisplay 168 | glutPostRedisplay(); 169 | } 170 | 171 | // Map the mouse x,y coordinates to a point on a sphere 172 | bool GlutViewer::mapMouseCoordinatesToSphere(int xMouse, int yMouse, Vector3& spherePoint) const { 173 | 174 | int width = mCamera.getWidth(); 175 | int height = mCamera.getHeight(); 176 | 177 | if ((xMouse >= 0) && (xMouse <= width) && (yMouse >= 0) && (yMouse <= height)) { 178 | float x = float(xMouse - 0.5f * width) / float(width); 179 | float y = float(0.5f * height - yMouse) / float(height); 180 | float sinx = sin(PI * x * 0.5f); 181 | float siny = sin(PI * y * 0.5f); 182 | float sinx2siny2 = sinx * sinx + siny * siny; 183 | 184 | // Compute the point on the sphere 185 | spherePoint.x = sinx; 186 | spherePoint.y = siny; 187 | spherePoint.z = (sinx2siny2 < 1.0) ? sqrt(1.0f - sinx2siny2) : 0.0f; 188 | 189 | return true; 190 | } 191 | 192 | return false; 193 | } 194 | 195 | // Zoom the camera 196 | void GlutViewer::zoom(int xMouse, int yMouse) { 197 | float dy = static_cast(yMouse - mLastMouseY); 198 | float h = static_cast(mCamera.getHeight()); 199 | 200 | // Zoom the camera 201 | mCamera.setZoom(-dy / h); 202 | } 203 | 204 | // Translate the camera 205 | void GlutViewer::translate(int xMouse, int yMouse) { 206 | float dx = static_cast(xMouse - mLastMouseX); 207 | float dy = static_cast(yMouse - mLastMouseY); 208 | 209 | // Translate the camera 210 | mCamera.translateCamera(-dx / float(mCamera.getWidth()), 211 | -dy / float(mCamera.getHeight()), mCenterScene); 212 | } 213 | 214 | // Rotate the camera 215 | void GlutViewer::rotate(int xMouse, int yMouse) { 216 | 217 | if (mIsLastPointOnSphereValid) { 218 | 219 | Vector3 newPoint3D; 220 | bool isNewPointOK = mapMouseCoordinatesToSphere(xMouse, yMouse, newPoint3D); 221 | 222 | if (isNewPointOK) { 223 | Vector3 axis = mLastPointOnSphere.cross(newPoint3D); 224 | float cosAngle = mLastPointOnSphere.dot(newPoint3D); 225 | 226 | float epsilon = std::numeric_limits::epsilon(); 227 | if (fabs(cosAngle) < 1.0f && axis.length() > epsilon) { 228 | axis.normalize(); 229 | float angle = 2.0f * acos(cosAngle); 230 | 231 | // Rotate the camera around the center of the scene 232 | mCamera.rotateAroundLocalPoint(axis, -angle, mCenterScene); 233 | } 234 | } 235 | } 236 | } 237 | 238 | // Check the OpenGL errors 239 | void GlutViewer::checkOpenGLErrors() { 240 | GLenum glError; 241 | 242 | // Get the OpenGL errors 243 | glError = glGetError(); 244 | 245 | // While there are errors 246 | while (glError != GL_NO_ERROR) { 247 | 248 | // Get the error string 249 | const GLubyte* stringError = gluErrorString(glError); 250 | 251 | // Display the error 252 | if (stringError) 253 | cerr << "OpenGL Error #" << glError << "(" << gluErrorString(glError) << endl; 254 | else 255 | cerr << "OpenGL Error #" << glError << " (no message available)" << endl; 256 | 257 | // Get the next error 258 | glError = glGetError(); 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /src/GlutViewer.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef GLUT_VIEWER_H 27 | #define GLUT_VIEWER_H 28 | 29 | // Libraries 30 | #include "Shader.h" 31 | #include "Camera.h" 32 | #include "maths/Vector2.h" 33 | #include 34 | #include 35 | #include "GL/freeglut.h" 36 | 37 | namespace openglframework { 38 | 39 | // Class Renderer 40 | class GlutViewer { 41 | 42 | private: 43 | 44 | // -------------------- Attributes -------------------- // 45 | 46 | // Camera 47 | Camera mCamera; 48 | 49 | // Center of the scene 50 | Vector3 mCenterScene; 51 | 52 | // Last mouse coordinates on the windows 53 | int mLastMouseX, mLastMouseY; 54 | 55 | // Last point computed on a sphere (for camera rotation) 56 | Vector3 mLastPointOnSphere; 57 | 58 | // True if the last point computed on a sphere (for camera rotation) is valid 59 | bool mIsLastPointOnSphereValid; 60 | 61 | // State of the mouse buttons 62 | bool mIsButtonDown[10]; 63 | 64 | // GLUT keyboard modifiers 65 | int mModifiers; 66 | 67 | // -------------------- Methods -------------------- // 68 | 69 | // Initialize the GLUT library 70 | bool initGLUT(int argc, char** argv, const std::string& windowsTitle, 71 | const Vector2& windowsSize, const Vector2& windowsPosition, 72 | bool isMultisamplingActive); 73 | 74 | bool mapMouseCoordinatesToSphere(int xMouse, int yMouse, Vector3& spherePoint) const; 75 | 76 | public: 77 | 78 | // -------------------- Methods -------------------- // 79 | 80 | // Constructor 81 | GlutViewer(); 82 | 83 | // Destructor 84 | ~GlutViewer(); 85 | 86 | // Initialize the viewer 87 | bool init(int argc, char** argv, const std::string& windowsTitle, 88 | const Vector2& windowsSize, const Vector2& windowsPosition, 89 | bool isMultisamplingActive = false); 90 | 91 | // Called when the windows is reshaped 92 | void reshape(int width, int height); 93 | 94 | // Set the scene position (where the camera needs to look at) 95 | void setScenePosition(const Vector3& position, float sceneRadius); 96 | 97 | // Set the camera so that we can view the whole scene 98 | void resetCameraToViewAll(); 99 | 100 | // Enable/Disable the multi-sampling for anti-aliasing 101 | void activateMultiSampling(bool isActive) const; 102 | 103 | // Zoom the camera 104 | void zoom(int xMouse, int yMouse); 105 | 106 | // Translate the camera 107 | void translate(int xMouse, int yMouse); 108 | 109 | // Rotate the camera 110 | void rotate(int xMouse, int yMouse); 111 | 112 | // Get the camera 113 | Camera& getCamera(); 114 | 115 | void motion(int x, int y); 116 | 117 | // Called when a GLUT mouse button event occurs 118 | void mouseButtonEvent(int button, int state, int x, int y); 119 | 120 | // Called when a GLUT mouse motion event occurs 121 | void mouseMotionEvent(int xMouse, int yMouse); 122 | 123 | void keyboard(int key, int x, int y); 124 | void special(int key, int x, int y); 125 | 126 | // Check the OpenGL errors 127 | static void checkOpenGLErrors(); 128 | }; 129 | 130 | // Set the dimension of the camera viewport 131 | inline void GlutViewer::reshape(int width, int height) { 132 | mCamera.setDimensions(width, height); 133 | glViewport(0, 0, width, height); 134 | glutPostRedisplay(); 135 | } 136 | 137 | // Set the scene position (where the camera needs to look at) 138 | inline void GlutViewer::setScenePosition(const Vector3& position, float sceneRadius) { 139 | 140 | // Set the position and radius of the scene 141 | mCenterScene = position; 142 | mCamera.setSceneRadius(sceneRadius); 143 | 144 | // Reset the camera position and zoom in order to view all the scene 145 | resetCameraToViewAll(); 146 | } 147 | 148 | // Get the camera 149 | inline Camera& GlutViewer::getCamera() { 150 | return mCamera; 151 | } 152 | 153 | // Enable/Disable the multi-sampling for anti-aliasing 154 | inline void GlutViewer::activateMultiSampling(bool isActive) const { 155 | (isActive) ? glEnable(GL_MULTISAMPLE) : glDisable(GL_MULTISAMPLE); 156 | } 157 | 158 | } 159 | 160 | #endif 161 | -------------------------------------------------------------------------------- /src/Light.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "Light.h" 28 | 29 | // Namespaces 30 | using namespace openglframework; 31 | 32 | // Constructor 33 | Light::Light(GLuint id) 34 | : mLightID(id), mDiffuseColor(Color::white()), 35 | mSpecularColor(Color::white()), mIsActive(false) { 36 | 37 | } 38 | 39 | // Constructor 40 | Light::Light(GLuint id, Color diffuseColor, Color specularColor) 41 | : mLightID(id), mDiffuseColor(diffuseColor), 42 | mSpecularColor(specularColor), mIsActive(false) { 43 | 44 | } 45 | 46 | // Destructor 47 | Light::~Light() { 48 | 49 | } 50 | 51 | // Initialize the light 52 | void Light::init() { 53 | 54 | // Enable the light 55 | enable(); 56 | 57 | // Set the diffuse and specular color 58 | GLfloat diffuseColor[] = {mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, mDiffuseColor.a}; 59 | GLfloat specularColor[] = {mSpecularColor.r,mSpecularColor.g,mSpecularColor.b,mSpecularColor.a}; 60 | glLightfv(mLightID, GL_DIFFUSE, diffuseColor); 61 | glLightfv(mLightID, GL_SPECULAR, specularColor); 62 | } 63 | 64 | // Create a shadow map associated with this light 65 | bool Light::createShadowMap(uint width, uint height) { 66 | 67 | // Destroy the current shadow map 68 | destroyShadowMap(); 69 | 70 | // Create the Framebuffer object to render the shadow map 71 | bool isFBOCreated = mFBOShadowMap.create(width, height, false); 72 | if (!isFBOCreated) { 73 | std::cerr << "Error : Cannot create the Shadow Map !" << std::endl; 74 | destroyShadowMap(); 75 | return false; 76 | } 77 | 78 | // Bind the Framebuffer object 79 | mFBOShadowMap.bind(GL_NONE); 80 | 81 | // Create the shadow map depth texture 82 | mShadowMap.create(width, height, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE); 83 | 84 | // Attache the shadow map texture to the Framebuffer object 85 | mFBOShadowMap.attachTexture(GL_DEPTH_ATTACHMENT_EXT, mShadowMap.getID()); 86 | 87 | // Unbind the Framebuffer object 88 | mFBOShadowMap.unbind(); 89 | 90 | // TODO : Change the path of the shader here so that it does not depend on the build folder 91 | bool isShaderCreated = mDepthShader.create("../../opengl-framework/src/shaders/depth.vert", 92 | "../../opengl-framework/src/shaders/depth.vert"); 93 | if (!isShaderCreated) { 94 | std::cerr << "Error : Cannot create the Shadow Map !" << std::endl; 95 | destroyShadowMap(); 96 | return false; 97 | } 98 | 99 | return true; 100 | } 101 | -------------------------------------------------------------------------------- /src/Light.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef LIGHT_H 27 | #define LIGHT_H 28 | 29 | // Libraries 30 | #include "maths/Color.h" 31 | #include "maths/Vector3.h" 32 | #include "Object3D.h" 33 | #include "Texture2D.h" 34 | #include "FrameBufferObject.h" 35 | #include "Shader.h" 36 | #include 37 | 38 | namespace openglframework { 39 | 40 | // Class Light 41 | class Light : public Object3D { 42 | 43 | private: 44 | 45 | // -------------------- Attributes -------------------- // 46 | 47 | // OpenGL light ID 48 | GLuint mLightID; 49 | 50 | // Diffuse color of the light 51 | Color mDiffuseColor; 52 | 53 | // Specular color of the light 54 | Color mSpecularColor; 55 | 56 | // True if the light is active 57 | bool mIsActive; 58 | 59 | // Shadow map associated with this light 60 | Texture2D mShadowMap; 61 | 62 | // Framebuffer object to render the shadow map 63 | FrameBufferObject mFBOShadowMap; 64 | 65 | // Shader to render the depth of the scene into a texture 66 | Shader mDepthShader; 67 | 68 | public: 69 | 70 | // -------------------- Methods -------------------- // 71 | 72 | // Constructor 73 | Light(GLuint id); 74 | 75 | // Constructor 76 | Light(GLuint id, Color diffuseColor, Color specularColor); 77 | 78 | // Destructor 79 | virtual ~Light(); 80 | 81 | // Return the diffuse color 82 | Color getDiffuseColor() const; 83 | 84 | // Set the diffuse color 85 | void setDiffuseColor(const Color& color); 86 | 87 | // Return the specular color 88 | Color getSpecularColor() const; 89 | 90 | // Set the specular color 91 | void setSpecularColor(const Color& color); 92 | 93 | // Return true if the light is active 94 | bool getIsActive() const; 95 | 96 | // Initialize the light 97 | void init(); 98 | 99 | // Enable the light 100 | void enable(); 101 | 102 | // Disable the light 103 | void disable(); 104 | 105 | // Create a shadow map associated with this light 106 | bool createShadowMap(uint width, uint height); 107 | 108 | // Call this method before rendering the scene for the shadow map computation 109 | void startRenderingShadowMap(); 110 | 111 | // Call this method after having rendered the scene for the shadow map computation 112 | void stopRenderingShadowMap(); 113 | 114 | // Destroy the shadow map associated with this light 115 | void destroyShadowMap(); 116 | }; 117 | 118 | // Return the diffuse color 119 | inline Color Light::getDiffuseColor() const { 120 | return mDiffuseColor; 121 | } 122 | 123 | // Set the diffuse color 124 | inline void Light::setDiffuseColor(const Color& color) { 125 | mDiffuseColor = color; 126 | } 127 | 128 | // Return the specular color 129 | inline Color Light::getSpecularColor() const { 130 | return mSpecularColor; 131 | } 132 | 133 | // Set the specular color 134 | inline void Light::setSpecularColor(const Color& color) { 135 | mSpecularColor = color; 136 | } 137 | 138 | // Return true if the light is active 139 | inline bool Light::getIsActive() const { 140 | return mIsActive; 141 | } 142 | 143 | // Enable the light 144 | inline void Light::enable() { 145 | 146 | mIsActive = true; 147 | 148 | // Enable the light 149 | glEnable(GL_LIGHTING); 150 | glEnable(GL_LIGHT0 + mLightID); 151 | } 152 | 153 | // Disable the light 154 | inline void Light::disable() { 155 | 156 | mIsActive = false; 157 | 158 | // Disable the light 159 | glDisable(GL_LIGHT0 + mLightID); 160 | } 161 | 162 | // Destroy the shadow map associated with this light 163 | inline void Light::destroyShadowMap() { 164 | mShadowMap.destroy(); 165 | mFBOShadowMap.destroy(); 166 | mDepthShader.destroy(); 167 | } 168 | 169 | // Call this method before rendering the scene for the shadow map computation 170 | inline void Light::startRenderingShadowMap() { 171 | assert(mShadowMap.getID()); 172 | } 173 | 174 | // Call this method after having rendered the scene for the shadow map computation 175 | inline void Light::stopRenderingShadowMap() { 176 | assert(mShadowMap.getID()); 177 | } 178 | 179 | } 180 | 181 | #endif 182 | -------------------------------------------------------------------------------- /src/Mesh.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "Mesh.h" 28 | 29 | // Namespaces 30 | using namespace openglframework; 31 | using namespace std; 32 | 33 | // Constructor 34 | Mesh::Mesh() { 35 | 36 | } 37 | 38 | // Destructor 39 | Mesh::~Mesh() { 40 | 41 | } 42 | 43 | // Destroy the mesh 44 | void Mesh::destroy() { 45 | 46 | mVertices.clear(); 47 | mNormals.clear(); 48 | mTangents.clear(); 49 | mIndices.clear(); 50 | mColors.clear(); 51 | mUVs.clear(); 52 | mTextures.clear(); 53 | } 54 | 55 | // Compute the normals of the mesh 56 | void Mesh::calculateNormals() { 57 | 58 | mNormals = vector(getNbVertices(), Vector3(0, 0, 0)); 59 | 60 | // For each triangular face 61 | for (uint i=0; i 0); 88 | mNormals[i] = mNormals[i].normalize(); 89 | } 90 | } 91 | 92 | // Compute the tangents of the mesh 93 | void Mesh::calculateTangents() { 94 | 95 | mTangents = std::vector(getNbVertices(), Vector3(0, 0, 0)); 96 | 97 | // For each face 98 | for (uint i=0; i::const_iterator it(mVertices.begin()); 149 | 150 | // For each vertex of the mesh 151 | for (; it != mVertices.end(); ++it) { 152 | 153 | if( (*it).x < min.x ) min.x = (*it).x; 154 | else if ( (*it).x > max.x ) max.x = (*it).x; 155 | 156 | if( (*it).y < min.y ) min.y = (*it).y; 157 | else if ( (*it).y > max.y ) max.y = (*it).y; 158 | 159 | if( (*it).z < min.z ) min.z = (*it).z; 160 | else if ( (*it).z > max.z ) max.z = (*it).z; 161 | } 162 | } 163 | else { 164 | std::cerr << "Error : Impossible to calculate the bounding box of the mesh because there" << 165 | "are no vertices !" << std::endl; 166 | assert(false); 167 | } 168 | } 169 | 170 | // Scale of vertices of the mesh using a given factor 171 | void Mesh::scaleVertices(float factor) { 172 | 173 | // For each vertex 174 | for (uint i=0; i 31 | #include 32 | #include "Mesh.h" 33 | 34 | namespace openglframework { 35 | 36 | // Class MeshReaderWriter 37 | // This class is used to read or write any mesh file in order to 38 | // create the corresponding Mesh object. Currently, this class 39 | // is able to read meshes of the current formats : .obj 40 | class MeshReaderWriter { 41 | 42 | private : 43 | 44 | // -------------------- Methods -------------------- // 45 | 46 | // Constructor (private because we do not want instances of this class) 47 | MeshReaderWriter(); 48 | 49 | // Load an OBJ file with a triangular or quad mesh 50 | static void loadOBJFile(const std::string& filename, Mesh& meshToCreate); 51 | 52 | // Store a mesh into a OBJ file 53 | static void writeOBJFile(const std::string& filename, const Mesh &meshToWrite); 54 | 55 | public : 56 | 57 | // -------------------- Methods -------------------- // 58 | 59 | // Read a mesh from a file 60 | static void loadMeshFromFile(const std::string& filename, 61 | Mesh& meshToCreate) 62 | throw(std::invalid_argument, std::runtime_error); 63 | 64 | // Write a mesh to a file 65 | static void writeMeshToFile(const std::string& filename, 66 | const Mesh& meshToWrite) 67 | throw(std::invalid_argument, std::runtime_error); 68 | }; 69 | 70 | // Class VertexMergingData 71 | // This class is used in the method to read a mesh 72 | class VertexMergingData { 73 | 74 | public: 75 | VertexMergingData() : indexPosition(0), indexNormal(0), indexUV(0) {} 76 | unsigned int indexPosition; 77 | unsigned int indexNormal; 78 | unsigned int indexUV; 79 | }; 80 | 81 | // Class VertexMergingDataComparison 82 | // This class is used in the method to read a mesh 83 | class VertexMergingDataComparison { 84 | 85 | public: 86 | bool operator()(const VertexMergingData& x, const VertexMergingData& y) const { 87 | if(x.indexPosition < y.indexPosition) 88 | return true; 89 | if(x.indexPosition == y.indexPosition && x.indexNormal < y.indexNormal) 90 | return true; 91 | if(x.indexPosition == y.indexPosition && x.indexNormal == 92 | y.indexNormal && x.indexUV < y.indexUV) 93 | return true; 94 | return false; 95 | } 96 | }; 97 | 98 | } 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /src/Object3D.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "Object3D.h" 28 | 29 | // Namespaces 30 | using namespace openglframework; 31 | 32 | // Constructor 33 | Object3D::Object3D() { 34 | // Set the transformation matrix to the identity 35 | setToIdentity(); 36 | } 37 | 38 | // Destructor 39 | Object3D::~Object3D() { 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/Object3D.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef OBJECT3D_H 27 | #define OBJECT3D_H 28 | 29 | // Libraries 30 | #include "maths/Vector3.h" 31 | #include "maths/Matrix4.h" 32 | 33 | namespace openglframework { 34 | 35 | // Class Object3D 36 | // This class represent a generic 3D object on the scene. 37 | class Object3D { 38 | 39 | protected: 40 | 41 | // -------------------- Attributes -------------------- // 42 | 43 | // Transformation matrix that convert local-space 44 | // coordinates to world-space coordinates 45 | Matrix4 mTransformMatrix; 46 | 47 | public: 48 | 49 | // -------------------- Methods -------------------- // 50 | 51 | // Constructor 52 | Object3D(); 53 | 54 | // Destructor 55 | virtual ~Object3D(); 56 | 57 | // Return the transform matrix 58 | const Matrix4& getTransformMatrix() const; 59 | 60 | // Set to the identity transform 61 | void setToIdentity(); 62 | 63 | // Return the origin of object in world-space 64 | Vector3 getOrigin() const; 65 | 66 | // Translate the object in world-space 67 | void translateWorld(const Vector3& v); 68 | 69 | // Translate the object in local-space 70 | void translateLocal(const Vector3& v); 71 | 72 | // Rotate the object in world-space 73 | void rotateWorld(const Vector3& axis, float angle); 74 | 75 | // Rotate the object in local-space 76 | void rotateLocal(const Vector3& axis, float angle); 77 | 78 | // Rotate around a world-space point 79 | void rotateAroundWorldPoint(const Vector3& axis, float angle, const Vector3& point); 80 | 81 | // Rotate around a local-space point 82 | void rotateAroundLocalPoint(const Vector3& axis, float angle, const Vector3& worldPoint); 83 | }; 84 | 85 | // Return the transform matrix 86 | inline const Matrix4& Object3D::getTransformMatrix() const { 87 | return mTransformMatrix; 88 | } 89 | 90 | // Set to the identity transform 91 | inline void Object3D::setToIdentity() { 92 | mTransformMatrix.setToIdentity(); 93 | } 94 | 95 | // Return the origin of object in world-space 96 | inline Vector3 Object3D::getOrigin() const { 97 | return mTransformMatrix * Vector3(0.0, 0.0, 0.0); 98 | } 99 | 100 | // Translate the object in world-space 101 | inline void Object3D::translateWorld(const Vector3& v) { 102 | mTransformMatrix = Matrix4::translationMatrix(v) * mTransformMatrix; 103 | } 104 | 105 | // Translate the object in local-space 106 | inline void Object3D::translateLocal(const Vector3& v) { 107 | mTransformMatrix = mTransformMatrix * Matrix4::translationMatrix(v); 108 | } 109 | 110 | // Rotate the object in world-space 111 | inline void Object3D::rotateWorld(const Vector3& axis, float angle) { 112 | mTransformMatrix = Matrix4::rotationMatrix(axis, angle) * mTransformMatrix; 113 | } 114 | 115 | // Rotate the object in local-space 116 | inline void Object3D::rotateLocal(const Vector3& axis, float angle) { 117 | mTransformMatrix = mTransformMatrix * Matrix4::rotationMatrix(axis, angle); 118 | } 119 | 120 | // Rotate the object around a world-space point 121 | inline void Object3D::rotateAroundWorldPoint(const Vector3& axis, float angle, 122 | const Vector3& worldPoint) { 123 | mTransformMatrix = Matrix4::translationMatrix(worldPoint) * Matrix4::rotationMatrix(axis, angle) 124 | * Matrix4::translationMatrix(-worldPoint) * mTransformMatrix; 125 | } 126 | 127 | // Rotate the object around a local-space point 128 | inline void Object3D::rotateAroundLocalPoint(const Vector3& axis, float angle, 129 | const Vector3& worldPoint) { 130 | 131 | // Convert the world point into the local coordinate system 132 | Vector3 localPoint = mTransformMatrix.getInverse() * worldPoint; 133 | 134 | mTransformMatrix = mTransformMatrix * Matrix4::translationMatrix(localPoint) 135 | * Matrix4::rotationMatrix(axis, angle) 136 | * Matrix4::translationMatrix(-localPoint); 137 | } 138 | 139 | } 140 | 141 | #endif 142 | -------------------------------------------------------------------------------- /src/Shader.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "Shader.h" 28 | #include 29 | #include 30 | #include 31 | 32 | // Namespaces 33 | using namespace openglframework; 34 | using namespace std; 35 | 36 | // Constructor 37 | Shader::Shader() : mProgramObjectID(0) { 38 | 39 | } 40 | 41 | // Constructor with arguments 42 | Shader::Shader(const std::string vertexShaderFilename, const std::string fragmentShaderFilename) 43 | : mProgramObjectID(0) { 44 | 45 | // Create the shader 46 | create(vertexShaderFilename, fragmentShaderFilename); 47 | } 48 | 49 | // Destructor 50 | Shader::~Shader() { 51 | 52 | } 53 | 54 | // Create the shader 55 | bool Shader::create(const std::string vertexShaderFilename, 56 | const std::string fragmentShaderFilename) { 57 | 58 | // Set the shader filenames 59 | mFilenameVertexShader = vertexShaderFilename; 60 | mFilenameFragmentShader = fragmentShaderFilename; 61 | 62 | // Check that the needed OpenGL extensions are available 63 | bool isExtensionOK = checkOpenGLExtensions(); 64 | if (!isExtensionOK) { 65 | cerr << "Error : Impossible to use GLSL vertex or fragment shaders on this platform" << endl; 66 | assert(false); 67 | return false; 68 | } 69 | 70 | // Delete the current shader 71 | destroy(); 72 | 73 | assert(!vertexShaderFilename.empty() && !fragmentShaderFilename.empty()); 74 | 75 | // ------------------- Load the vertex shader ------------------- // 76 | GLuint vertexShaderID; 77 | std::ifstream fileVertexShader; 78 | fileVertexShader.open(vertexShaderFilename.c_str(), std::ios::binary); 79 | assert(fileVertexShader.is_open()); 80 | 81 | if (fileVertexShader.is_open()) { 82 | 83 | // Get the size of the file 84 | fileVertexShader.seekg(0, std::ios::end); 85 | uint fileSize = (uint) (fileVertexShader.tellg()); 86 | assert(fileSize != 0); 87 | 88 | // Read the file 89 | fileVertexShader.seekg(std::ios::beg); 90 | char* bufferVertexShader = new char[fileSize + 1]; 91 | fileVertexShader.read(bufferVertexShader, fileSize); 92 | fileVertexShader.close(); 93 | bufferVertexShader[fileSize] = '\0'; 94 | 95 | // Create the OpenGL vertex shader and compile it 96 | vertexShaderID = glCreateShader(GL_VERTEX_SHADER); 97 | assert(vertexShaderID != 0); 98 | glShaderSource(vertexShaderID, 1, (const char **) (&bufferVertexShader), NULL); 99 | glCompileShader(vertexShaderID); 100 | delete[] bufferVertexShader; 101 | 102 | // Get the compilation information 103 | int compiled; 104 | glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &compiled); 105 | 106 | // If the compilation failed 107 | if (compiled == 0) { 108 | 109 | // Get the log of the compilation 110 | int lengthLog; 111 | glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &lengthLog); 112 | char* str = new char[lengthLog]; 113 | glGetShaderInfoLog(vertexShaderID, lengthLog, NULL, str); 114 | 115 | // Display the log of the compilation 116 | std::cerr << "Vertex Shader Error : " << str << std::endl; 117 | delete[] str; 118 | assert(false); 119 | return false; 120 | } 121 | } 122 | else { 123 | std::cerr << "Error : Impossible to open the vertex shader file " << 124 | vertexShaderFilename << std::endl; 125 | assert(false); 126 | return false; 127 | } 128 | 129 | // ------------------- Load the fragment shader ------------------- // 130 | GLuint fragmentShaderID; 131 | std::ifstream fileFragmentShader; 132 | fileFragmentShader.open(fragmentShaderFilename.c_str(), std::ios::binary); 133 | assert(fileFragmentShader.is_open()); 134 | 135 | if (fileFragmentShader.is_open()) { 136 | 137 | // Get the size of the file 138 | fileFragmentShader.seekg(0, std::ios::end); 139 | uint fileSize = (uint) (fileFragmentShader.tellg()); 140 | assert(fileSize != 0); 141 | 142 | // Read the file 143 | fileFragmentShader.seekg(std::ios::beg); 144 | char* bufferFragmentShader = new char[fileSize + 1]; 145 | fileFragmentShader.read(bufferFragmentShader, fileSize); 146 | fileFragmentShader.close(); 147 | bufferFragmentShader[fileSize] = '\0'; 148 | 149 | // Create the OpenGL fragment shader and compile it 150 | fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); 151 | assert(fragmentShaderID != 0); 152 | glShaderSource(fragmentShaderID, 1, (const char **) (&bufferFragmentShader), NULL); 153 | glCompileShader(fragmentShaderID); 154 | delete[] bufferFragmentShader; 155 | 156 | // Get the compilation information 157 | int compiled; 158 | glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &compiled); 159 | 160 | // If the compilation failed 161 | if (compiled == 0) { 162 | 163 | // Get the log of the compilation 164 | int lengthLog; 165 | glGetShaderiv(fragmentShaderID, GL_INFO_LOG_LENGTH, &lengthLog); 166 | char* str = new char[lengthLog]; 167 | glGetShaderInfoLog(fragmentShaderID, lengthLog, NULL, str); 168 | 169 | // Display the log of the compilation 170 | std::cerr << "Fragment Shader Error : " << str << std::endl; 171 | delete[] str; 172 | assert(false); 173 | return false; 174 | } 175 | } 176 | else { 177 | std::cerr << "Error : Impossible to open the fragment shader file " << 178 | fragmentShaderFilename << std::endl; 179 | assert(false); 180 | return false; 181 | } 182 | 183 | // Create the shader program and attach the shaders 184 | mProgramObjectID = glCreateProgram(); 185 | assert(mProgramObjectID); 186 | glAttachShader(mProgramObjectID, vertexShaderID); 187 | glAttachShader(mProgramObjectID, fragmentShaderID); 188 | glDeleteShader(vertexShaderID); 189 | glDeleteShader(fragmentShaderID); 190 | 191 | // Try to link the program 192 | glLinkProgram(mProgramObjectID); 193 | int linked; 194 | glGetProgramiv(mProgramObjectID, GL_LINK_STATUS, &linked); 195 | if (!linked) { 196 | int logLength; 197 | glGetProgramiv(mProgramObjectID, GL_INFO_LOG_LENGTH, &logLength); 198 | char* strLog = new char[logLength]; 199 | glGetProgramInfoLog(mProgramObjectID, logLength, NULL, strLog); 200 | cerr << "Linker Error in vertex shader " << vertexShaderFilename << 201 | " or in fragment shader " << fragmentShaderFilename << " : " << strLog << endl; 202 | delete[] strLog; 203 | destroy(); 204 | assert(false); 205 | } 206 | 207 | return true; 208 | } 209 | -------------------------------------------------------------------------------- /src/Shader.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef SHADER_H 27 | #define SHADER_H 28 | 29 | // Libraries 30 | #include "definitions.h" 31 | #include "maths/Matrix4.h" 32 | #include "maths/Vector2.h" 33 | #include "maths/Vector3.h" 34 | #include "maths/Vector4.h" 35 | #include 36 | #include 37 | #include 38 | 39 | namespace openglframework { 40 | 41 | // Class Shader 42 | class Shader { 43 | 44 | private : 45 | 46 | // -------------------- Attributes -------------------- // 47 | 48 | // Shader object program ID 49 | GLuint mProgramObjectID; 50 | 51 | // Filenames of the vertex and fragment shaders 52 | std::string mFilenameVertexShader, mFilenameFragmentShader; 53 | 54 | public : 55 | 56 | // -------------------- Methods -------------------- // 57 | 58 | // Constructor 59 | Shader(); 60 | 61 | // Constructor with arguments 62 | Shader(const std::string vertexShaderFilename, const std::string fragmentShaderFilename); 63 | 64 | // Destructor 65 | ~Shader(); 66 | 67 | // Create the shader 68 | bool create(const std::string vertexShaderFilename, 69 | const std::string fragmentShaderFilename); 70 | 71 | // Clear the shader 72 | void destroy(); 73 | 74 | // Bind the shader 75 | void bind() const; 76 | 77 | // Unbind the shader 78 | void unbind() const; 79 | 80 | // Return the location of a uniform variable inside a shader program 81 | int getUniformLocation(const std::string& variableName) const; 82 | 83 | // Set a float uniform value to this shader (be careful if the uniform is not 84 | // used in the shader, the compiler will remove it, then when you will try 85 | // to set it, an assert will occur) 86 | void setFloatUniform(const std::string& variableName, float value) const; 87 | 88 | // Set an int uniform value to this shader (be careful if the uniform is not 89 | // used in the shader, the compiler will remove it, then when you will try 90 | // to set it, an assert will occur) 91 | void setIntUniform(const std::string& variableName, int value) const; 92 | 93 | // Set a vector 2 uniform value to this shader (be careful if the uniform is not 94 | // used in the shader, the compiler will remove it, then when you will try 95 | // to set it, an assert will occur) 96 | void setVector2Uniform(const std::string& variableName, const Vector2& v) const; 97 | 98 | // Set a vector 3 uniform value to this shader (be careful if the uniform is not 99 | // used in the shader, the compiler will remove it, then when you will try 100 | // to set it, an assert will occur) 101 | void setVector3Uniform(const std::string& variableName, const Vector3& v) const; 102 | 103 | // Set a vector 4 uniform value to this shader (be careful if the uniform is not 104 | // used in the shader, the compiler will remove it, then when you will try 105 | // to set it, an assert will occur) 106 | void setVector4Uniform(const std::string& variableName, const Vector4 &v) const; 107 | 108 | // Set a 3x3 matrix uniform value to this shader (be careful if the uniform is not 109 | // used in the shader, the compiler will remove it, then when you will try 110 | // to set it, an assert will occur) 111 | void setMatrix3x3Uniform(const std::string& variableName, const float* matrix, 112 | bool transpose = false) const; 113 | 114 | // Set a 4x4 matrix uniform value to this shader (be careful if the uniform is not 115 | // used in the shader, the compiler will remove it, then when you will try 116 | // to set it, an assert will occur) 117 | void setMatrix4x4Uniform(const std::string& variableName, const float* matrix, 118 | bool transpose = false) const; 119 | 120 | // Set a 4x4 matrix uniform value to this shader (be careful if the uniform is not 121 | // used in the shader, the compiler will remove it, then when you will try 122 | // to set it, an assert will occur) 123 | void setMatrix4x4Uniform(const std::string& variableName, const Matrix4& matrix) const; 124 | 125 | // Return true if the needed OpenGL extensions are available 126 | static bool checkOpenGLExtensions(); 127 | }; 128 | 129 | // Bind the shader 130 | inline void Shader::bind() const { 131 | assert(mProgramObjectID != 0); 132 | glUseProgram(mProgramObjectID); 133 | } 134 | 135 | // Unbind the shader 136 | inline void Shader::unbind() const { 137 | assert(mProgramObjectID != 0); 138 | glUseProgram(0); 139 | } 140 | 141 | // Return the location of a uniform variable inside a shader program 142 | inline int Shader::getUniformLocation(const std::string& variableName) const { 143 | assert(mProgramObjectID != 0); 144 | int location = glGetUniformLocation(mProgramObjectID, variableName.c_str()); 145 | if (location == -1) { 146 | std::cerr << "Error in vertex shader " << mFilenameVertexShader << " or in fragment shader" 147 | << mFilenameFragmentShader << " : No Uniform variable : " << variableName 148 | << std::endl; 149 | } 150 | assert(location != -1); 151 | return location; 152 | } 153 | 154 | // Clear the shader 155 | inline void Shader::destroy() { 156 | if (mProgramObjectID != 0) { 157 | glDeleteShader(mProgramObjectID); 158 | mProgramObjectID = 0; 159 | } 160 | } 161 | 162 | // Set a float uniform value to this shader (be careful if the uniform is not 163 | // used in the shader, the compiler will remove it, then when you will try 164 | // to set it, an assert will occur) 165 | inline void Shader::setFloatUniform(const std::string& variableName, float value) const { 166 | assert(mProgramObjectID != 0); 167 | glUniform1f(getUniformLocation(variableName), value); 168 | } 169 | 170 | // Set an int uniform value to this shader (be careful if the uniform is not 171 | // used in the shader, the compiler will remove it, then when you will try 172 | // to set it, an assert will occur) 173 | inline void Shader::setIntUniform(const std::string& variableName, int value) const { 174 | assert(mProgramObjectID != 0); 175 | glUniform1i(getUniformLocation(variableName), value); 176 | } 177 | 178 | // Set a vector 2 uniform value to this shader (be careful if the uniform is not 179 | // used in the shader, the compiler will remove it, then when you will try 180 | // to set it, an assert will occur) 181 | inline void Shader::setVector2Uniform(const std::string& variableName, const Vector2& v) const { 182 | assert(mProgramObjectID != 0); 183 | glUniform2f(getUniformLocation(variableName), v.x, v.y); 184 | } 185 | 186 | // Set a vector 3 uniform value to this shader (be careful if the uniform is not 187 | // used in the shader, the compiler will remove it, then when you will try 188 | // to set it, an assert will occur) 189 | inline void Shader::setVector3Uniform(const std::string& variableName, const Vector3 &v) const { 190 | assert(mProgramObjectID != 0); 191 | glUniform3f(getUniformLocation(variableName), v.x, v.y, v.z); 192 | } 193 | 194 | // Set a vector 4 uniform value to this shader (be careful if the uniform is not 195 | // used in the shader, the compiler will remove it, then when you will try 196 | // to set it, an assert will occur) 197 | inline void Shader::setVector4Uniform(const std::string& variableName, const Vector4& v) const { 198 | assert(mProgramObjectID != 0); 199 | glUniform4f(getUniformLocation(variableName), v.x, v.y, v.z, v.w); 200 | } 201 | 202 | // Set a 4x4 matrix uniform value to this shader (be careful if the uniform is not 203 | // used in the shader, the compiler will remove it, then when you will try 204 | // to set it, an assert will occur) 205 | inline void Shader::setMatrix3x3Uniform(const std::string& variableName, const float* matrix, 206 | bool transpose) const { 207 | assert(mProgramObjectID != 0); 208 | glUniformMatrix3fv(getUniformLocation(variableName), 1, transpose, matrix); 209 | } 210 | 211 | // Set a 4x4 matrix uniform value to this shader (be careful if the uniform is not 212 | // used in the shader, the compiler will remove it, then when you will try 213 | // to set it, an assert will occur) 214 | inline void Shader::setMatrix4x4Uniform(const std::string& variableName, const float* matrix, 215 | bool transpose) const { 216 | assert(mProgramObjectID != 0); 217 | glUniformMatrix4fv(getUniformLocation(variableName), 1, transpose, matrix); 218 | } 219 | 220 | // Set a 4x4 matrix uniform value to this shader (be careful if the uniform is not 221 | // used in the shader, the compiler will remove it, then when you will try 222 | // to set it, an assert will occur) 223 | inline void Shader::setMatrix4x4Uniform(const std::string& variableName, const Matrix4& matrix) const { 224 | assert(mProgramObjectID != 0); 225 | GLfloat mat[16]; 226 | for (int i=0; i<4; i++) { 227 | for (int j=0; j<4; j++) { 228 | mat[i*4 + j] = matrix.m[i][j]; 229 | } 230 | } 231 | glUniformMatrix4fv(getUniformLocation(variableName), 1, true, mat); 232 | } 233 | 234 | // Return true if the needed OpenGL extensions are available for shaders 235 | inline bool Shader::checkOpenGLExtensions() { 236 | 237 | // Check that GLSL vertex and fragment shaders are available on the platform 238 | return (GLEW_VERSION_2_0 || (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)); 239 | } 240 | 241 | } 242 | 243 | #endif 244 | -------------------------------------------------------------------------------- /src/Texture2D.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Libraries 27 | #include "Texture2D.h" 28 | #include 29 | #include 30 | #include 31 | 32 | // Namespaces 33 | using namespace openglframework; 34 | 35 | // Constructor 36 | Texture2D::Texture2D() : mID(0), mLayer(0), mWidth(0), mHeight(0) { 37 | 38 | } 39 | 40 | // Constructor 41 | Texture2D::Texture2D(uint width, uint height, uint internalFormat, uint format, uint type) 42 | : mID(0), mLayer(0), mWidth(0), mHeight(0){ 43 | 44 | // Create the texture 45 | create(width, height, internalFormat, format, type); 46 | } 47 | 48 | // Destructor 49 | Texture2D::~Texture2D() { 50 | 51 | } 52 | 53 | // Create the texture 54 | void Texture2D::create(uint width, uint height, uint internalFormat, uint format, uint type, 55 | void* data) throw(std::invalid_argument) { 56 | 57 | // Destroy the current texture 58 | destroy(); 59 | 60 | mWidth = width; 61 | mHeight = height; 62 | 63 | // Create the OpenGL texture 64 | glGenTextures(1, &mID); 65 | assert(mID != 0); 66 | glBindTexture(GL_TEXTURE_2D, mID); 67 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 68 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 69 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 70 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 71 | glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0, format, type, data); 72 | glBindTexture(GL_TEXTURE_2D, 0); 73 | } 74 | 75 | // Destroy the texture 76 | void Texture2D::destroy() { 77 | if (mID != 0) { 78 | glDeleteTextures(1, &mID); 79 | mID = 0; 80 | mLayer = 0; 81 | mWidth = 0; 82 | mHeight = 0; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/Texture2D.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef TEXTURE2D_H 27 | #define TEXTURE2D_H 28 | 29 | // Libraries 30 | #include 31 | #include 32 | #include "definitions.h" 33 | #include 34 | 35 | namespace openglframework { 36 | 37 | // Class Texture2D 38 | // This class represents a 2D texture 39 | class Texture2D { 40 | 41 | private: 42 | 43 | // -------------------- Attributes -------------------- // 44 | 45 | // OpenGL texture ID 46 | GLuint mID; 47 | 48 | // Layer of the texture 49 | GLuint mLayer; 50 | 51 | // Width 52 | uint mWidth; 53 | 54 | // Height 55 | uint mHeight; 56 | 57 | public: 58 | 59 | // -------------------- Methods -------------------- // 60 | 61 | // Constructor 62 | Texture2D(); 63 | 64 | // Constructor 65 | Texture2D(uint width, uint height, uint internalFormat, uint format, uint type); 66 | 67 | // Destructor 68 | ~Texture2D(); 69 | 70 | // Create the texture 71 | void create(uint width, uint height, uint internalFormat, uint format, uint type, 72 | void* data = NULL) throw(std::invalid_argument); 73 | 74 | // Destroy the texture 75 | void destroy(); 76 | 77 | // Bind the texture 78 | void bind() const; 79 | 80 | // Unbind the texture 81 | void unbind() const; 82 | 83 | // Get the OpenGL texture ID 84 | uint getID() const; 85 | 86 | // Get the layer of the texture 87 | uint getLayer() const; 88 | 89 | // Set the layer of the texture 90 | void setLayer(uint layer); 91 | 92 | // Get the width 93 | uint getWidth() const; 94 | 95 | // Get the height 96 | uint getHeight() const; 97 | }; 98 | 99 | // Bind the texture 100 | inline void Texture2D::bind() const { 101 | assert(mID != 0); 102 | glEnable(GL_TEXTURE_2D); 103 | glActiveTexture(GL_TEXTURE0 + mLayer); 104 | glBindTexture(GL_TEXTURE_2D, mID); 105 | } 106 | 107 | // Unbind the texture 108 | inline void Texture2D::unbind() const { 109 | assert(mID != 0); 110 | glActiveTexture(GL_TEXTURE0 + mLayer); 111 | glBindTexture(GL_TEXTURE_2D, 0); 112 | glDisable(GL_TEXTURE_2D); 113 | } 114 | 115 | // Get the OpenGL texture ID 116 | inline uint Texture2D::getID() const { 117 | return mID; 118 | } 119 | 120 | // Get the layer of the texture 121 | inline uint Texture2D::getLayer() const { 122 | return mLayer; 123 | } 124 | 125 | // Set the layer of the texture 126 | inline void Texture2D::setLayer(uint layer) { 127 | mLayer = layer; 128 | } 129 | 130 | // Get the width 131 | inline uint Texture2D::getWidth() const { 132 | return mWidth; 133 | } 134 | 135 | // Get the height 136 | inline uint Texture2D::getHeight() const { 137 | return mHeight; 138 | } 139 | 140 | } 141 | 142 | #endif 143 | -------------------------------------------------------------------------------- /src/TextureReaderWriter.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef TEXTURE_READER_WRITER_H 27 | #define TEXTURE_READER_WRITER_H 28 | 29 | // Libraries 30 | #include "Texture2D.h" 31 | #include 32 | #include 33 | #include 34 | 35 | namespace openglframework { 36 | 37 | // Class TextureReaderWriter 38 | // This class is used to load or write a texture image in different picture format. 39 | // It currently allows to read and write the following formats : .tga 40 | class TextureReaderWriter { 41 | 42 | private : 43 | 44 | // ------------------- Constants ------------------- // 45 | 46 | // JPEG quality for compression (in percent) 47 | static const uint JPEG_COMPRESSION_QUALITY; 48 | 49 | // -------------------- Methods -------------------- // 50 | 51 | // Constructor (private because we do not want instances of this class) 52 | TextureReaderWriter(); 53 | 54 | // Read a TGA picture 55 | static void readTGAPicture(const std::string& filename, 56 | Texture2D& textureToCreate) throw(std::runtime_error); 57 | 58 | // Write a TGA picture 59 | static void writeTGAPicture(const std::string& filename, 60 | const Texture2D& texture) throw(std::runtime_error); 61 | 62 | // Read a JPEG picture 63 | static void readJPEGPicture(const std::string& filename, 64 | Texture2D& textureToCreate) throw(std::runtime_error); 65 | 66 | // Write a JPEG picture 67 | static void writeJPEGPicture(const std::string& filename, 68 | const Texture2D& texture) throw(std::runtime_error); 69 | 70 | public : 71 | 72 | // -------------------- Methods -------------------- // 73 | 74 | // Load a texture from a file 75 | static void loadTextureFromFile(const std::string& filename, 76 | Texture2D& textureToCreate) 77 | throw(std::runtime_error, std::invalid_argument); 78 | 79 | // Write a texture to a file 80 | static void writeTextureToFile(const std::string& filename, 81 | const Texture2D& texture) 82 | throw(std::runtime_error, std::invalid_argument); 83 | }; 84 | 85 | } 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /src/definitions.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef DEFINITIONS_H 27 | #define DEFINITIONS_H 28 | 29 | namespace openglframework { 30 | 31 | // ------------------- Type definitions ------------------- // 32 | typedef unsigned int uint; 33 | 34 | // ------------------- Constants ------------------- // 35 | const float PI = 3.141592654f; 36 | 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/maths/Color.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef COLOR_H 27 | #define COLOR_H 28 | 29 | // Structure Color 30 | // This structure represents a RGBA color. 31 | struct Color { 32 | 33 | public: 34 | 35 | // -------------------- Attributes -------------------- // 36 | 37 | // RGBA color components 38 | float r, g, b, a; 39 | 40 | // -------------------- Methods -------------------- // 41 | 42 | // Constructor 43 | Color() : r(1), g(1), b(1), a(1) {} 44 | 45 | // Constructor 46 | Color(float r, float g, float b, float a) : r(r), g(g), b(b), a(a) {} 47 | 48 | // Constructor 49 | Color(const Color& color) : r(color.r), g(color.g), b(color.b), a(color.a) {} 50 | 51 | // Destructor 52 | ~Color() {} 53 | 54 | // Return the black color 55 | static Color black() { return Color(0.0f, 0.0f, 0.0f, 1.0f);} 56 | 57 | // Return the white color 58 | static Color white() { return Color(1.0f, 1.0f, 1.0f, 1.0f);} 59 | 60 | // = operator 61 | Color& operator=(const Color& color) { 62 | if (&color != this) { 63 | r = color.r; 64 | g = color.g; 65 | b = color.b; 66 | a = color.a; 67 | } 68 | return *this; 69 | } 70 | }; 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /src/maths/Vector2.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef VECTOR2_H 27 | #define VECTOR2_H 28 | 29 | // Libraries 30 | #include 31 | #include 32 | 33 | namespace openglframework { 34 | 35 | // Class Vector2 36 | // This class represents a 2D vector. 37 | class Vector2 { 38 | 39 | public: 40 | 41 | // -------------------- Attributes -------------------- // 42 | 43 | // Components of the vector 44 | float x, y; 45 | 46 | 47 | // -------------------- Methods -------------------- // 48 | 49 | // Constructor 50 | Vector2(float x=0, float y=0) : x(x), y(y) {} 51 | 52 | // Constructor 53 | Vector2(const Vector2& vector) : x(vector.x), y(vector.y) {} 54 | 55 | // + operator 56 | Vector2 operator+(const Vector2 &v) const { 57 | return Vector2(x + v.x, y + v.y); 58 | } 59 | 60 | // += operator 61 | Vector2& operator+=(const Vector2 &v) { 62 | x += v.x; y += v.y; 63 | return *this; 64 | } 65 | 66 | // - operator 67 | Vector2 operator-(const Vector2 &v) const { 68 | return Vector2(x - v.x, y - v.y); 69 | } 70 | 71 | // -= operator 72 | Vector2& operator-=(const Vector2 &v) { 73 | x -= v.x; y -= v.y; 74 | return *this; 75 | } 76 | 77 | // = operator 78 | Vector2& operator=(const Vector2& vector) { 79 | if (&vector != this) { 80 | x = vector.x; 81 | y = vector.y; 82 | } 83 | return *this; 84 | } 85 | 86 | // == operator 87 | bool operator==(const Vector2 &v) const { 88 | return x == v.x && y == v.y; 89 | } 90 | 91 | // * operator 92 | Vector2 operator*(float f) const { 93 | return Vector2(f*x, f*y); 94 | } 95 | 96 | // *= operator 97 | Vector2 &operator*=(float f) { 98 | x *= f; y *= f; 99 | return *this; 100 | } 101 | 102 | // / operator 103 | Vector2 operator/(float f) const { 104 | assert(f!=0); 105 | float inv = 1.f / f; 106 | return Vector2(x * inv, y * inv); 107 | } 108 | 109 | // /= operator 110 | Vector2 &operator/=(float f) { 111 | assert(f!=0); 112 | float inv = 1.f / f; 113 | x *= inv; y *= inv; 114 | return *this; 115 | } 116 | 117 | // - operator 118 | Vector2 operator-() const { 119 | return Vector2(-x, -y); 120 | } 121 | 122 | // [] operator 123 | float &operator[](int i) { 124 | assert(i >= 0 && i <= 1); 125 | switch (i) { 126 | case 0: return x; 127 | case 1: return y; 128 | } 129 | return y; 130 | } 131 | 132 | // Normalize the vector and return it 133 | Vector2 normalize() { 134 | float l = length(); 135 | assert(l > 0); 136 | x /= l; 137 | y /= l; 138 | return *this; 139 | } 140 | 141 | // Clamp the vector values between 0 and 1 142 | Vector2 clamp01() { 143 | if (x>1.f) x=1.f; 144 | else if (x<0.f) x=0.f; 145 | if (y>1.f) y=1.f; 146 | else if (y<0.f) y=0.f; 147 | return *this; 148 | } 149 | 150 | // Return the squared length of the vector 151 | float lengthSquared() const { return x*x + y*y; } 152 | 153 | // Return the length of the vector 154 | float length() const { return sqrt(lengthSquared()); } 155 | }; 156 | 157 | } 158 | 159 | #endif 160 | -------------------------------------------------------------------------------- /src/maths/Vector3.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef VECTOR3_H 27 | #define VECTOR3_H 28 | 29 | // Libraries 30 | #include 31 | #include 32 | #include 33 | 34 | namespace openglframework { 35 | 36 | // Class Vector3 37 | // This class represents a 3D vector. 38 | class Vector3 { 39 | 40 | public: 41 | 42 | // -------------------- Attributes -------------------- // 43 | 44 | // Components of the vector 45 | float x, y, z; 46 | 47 | // -------------------- Methods -------------------- // 48 | 49 | // Constructor 50 | Vector3(float x=0, float y=0, float z=0) : x(x), y(y), z(z) {} 51 | 52 | // Constructor 53 | Vector3(const Vector3& vector) : x(vector.x), y(vector.y), z(vector.z) {} 54 | 55 | // Constructor 56 | ~Vector3() {} 57 | 58 | // = operator 59 | Vector3& operator=(const Vector3& vector) { 60 | if (&vector != this) { 61 | x = vector.x; 62 | y = vector.y; 63 | z = vector.z; 64 | } 65 | return *this; 66 | } 67 | 68 | // + operator 69 | Vector3 operator+(const Vector3 &v) const { 70 | return Vector3(x + v.x, y + v.y, z + v.z); 71 | } 72 | 73 | // += operator 74 | Vector3& operator+=(const Vector3 &v) { 75 | x += v.x; y += v.y; z += v.z; 76 | return *this; 77 | } 78 | 79 | // - operator 80 | Vector3 operator-(const Vector3 &v) const { 81 | return Vector3(x - v.x, y - v.y, z - v.z); 82 | } 83 | 84 | // -= operator 85 | Vector3& operator-=(const Vector3 &v) { 86 | x -= v.x; y -= v.y; z -= v.z; 87 | return *this; 88 | } 89 | 90 | // == operator 91 | bool operator==(const Vector3 &v) const { 92 | return x == v.x && y == v.y && z == v.z; 93 | } 94 | 95 | // != operator 96 | bool operator!=(const Vector3 &v) const { 97 | return !( *this == v ); 98 | } 99 | 100 | // * operator 101 | Vector3 operator*(float f) const { 102 | return Vector3(f*x, f*y, f*z); 103 | } 104 | 105 | // *= operator 106 | Vector3 &operator*=(float f) { 107 | x *= f; y *= f; z *= f; 108 | return *this; 109 | } 110 | 111 | // / operator 112 | Vector3 operator/(float f) const { 113 | assert(f > std::numeric_limits::epsilon() ); 114 | float inv = 1.f / f; 115 | return Vector3(x * inv, y * inv, z * inv); 116 | } 117 | 118 | // /= operator 119 | Vector3 &operator/=(float f) { 120 | assert(f > std::numeric_limits::epsilon()); 121 | float inv = 1.f / f; 122 | x *= inv; y *= inv; z *= inv; 123 | return *this; 124 | } 125 | 126 | // - operator 127 | Vector3 operator-() const { 128 | return Vector3(-x, -y, -z); 129 | } 130 | 131 | // [] operator 132 | float &operator[](int i) { 133 | assert(i >= 0 && i <= 2); 134 | switch (i) { 135 | case 0: return x; 136 | case 1: return y; 137 | case 2: return z; 138 | } 139 | return z; 140 | } 141 | 142 | // [] operator 143 | const float &operator[](int i) const { 144 | assert(i >= 0 && i <= 2); 145 | switch (i) { 146 | case 0: return x; 147 | case 1: return y; 148 | case 2: return z; 149 | } 150 | return z; 151 | } 152 | 153 | // Cross product operator 154 | Vector3 cross(const Vector3 &v) const{ 155 | return Vector3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); 156 | } 157 | 158 | // Dot product operator 159 | float dot(const Vector3 &v) const{ 160 | return x * v.x + y * v.y + z * v.z; 161 | } 162 | 163 | // Normalize the vector and return it 164 | Vector3 normalize() { 165 | float l = length(); 166 | if(l < std::numeric_limits::epsilon() ) { 167 | assert(false); 168 | } 169 | x /= l; 170 | y /= l; 171 | z /= l; 172 | return *this; 173 | } 174 | 175 | bool isNull() const { 176 | return( x == 0. && y == 0. && z == 0. ); 177 | } 178 | 179 | // Clamp the values between 0 and 1 180 | Vector3 clamp01() { 181 | if (x>1.f) x=1.f; 182 | else if (x<0.f) x=0.f; 183 | if (y>1.f) y=1.f; 184 | else if (y<0.f) y=0.f; 185 | if (z>1.f) z=1.f; 186 | else if (z<0.f) z=0.f; 187 | return *this; 188 | } 189 | 190 | // Return the squared length of the vector 191 | float lengthSquared() const { return x*x + y*y + z*z; } 192 | 193 | // Return the length of the vector 194 | float length() const { return sqrt(lengthSquared()); } 195 | }; 196 | 197 | inline Vector3 operator*(float f, const Vector3 & o) { 198 | return o*f; 199 | } 200 | 201 | } 202 | 203 | #endif 204 | -------------------------------------------------------------------------------- /src/maths/Vector4.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef VECTOR4_H 27 | #define VECTOR4_H 28 | 29 | // Libraries 30 | #include 31 | #include 32 | 33 | namespace openglframework { 34 | 35 | // Class Vector4 36 | // This class represents a 4D vector. 37 | class Vector4 { 38 | 39 | public: 40 | 41 | // -------------------- Attributes -------------------- // 42 | 43 | // Components of the vector 44 | float x, y, z, w; 45 | 46 | // -------------------- Methods -------------------- // 47 | 48 | // Constructor 49 | Vector4(float x=0, float y=0, float z=0, float w=0) : x(x), y(y), z(z), w(w) {} 50 | 51 | // Constructor 52 | Vector4(const Vector4& vector) : x(vector.x), y(vector.y), z(vector.z), w(vector.w) {} 53 | 54 | // + operator 55 | Vector4 operator+(const Vector4 &v) const { 56 | return Vector4(x + v.x, y + v.y, z + v.z, w + v.w); 57 | } 58 | 59 | // += operator 60 | Vector4& operator+=(const Vector4 &v) { 61 | x += v.x; y += v.y; z += v.z; w += v.w; 62 | return *this; 63 | } 64 | 65 | // - operator 66 | Vector4 operator-(const Vector4 &v) const { 67 | return Vector4(x - v.x, y - v.y, z - v.z, w - v.w); 68 | } 69 | 70 | // -= operator 71 | Vector4& operator-=(const Vector4 &v) { 72 | x -= v.x; y -= v.y; z -= v.z, w -=v.w; 73 | return *this; 74 | } 75 | 76 | // = operator 77 | Vector4& operator=(const Vector4& vector) { 78 | if (&vector != this) { 79 | x = vector.x; 80 | y = vector.y; 81 | z = vector.z; 82 | w = vector.w; 83 | } 84 | return *this; 85 | } 86 | 87 | // == operator 88 | bool operator==(const Vector4 &v) const { 89 | return x == v.x && y == v.y && z == v.z && w == v.w; 90 | } 91 | 92 | // * operator 93 | Vector4 operator*(float f) const { 94 | return Vector4(f*x, f*y, f*z, f*w); 95 | } 96 | 97 | // *= operator 98 | Vector4 &operator*=(float f) { 99 | x *= f; y *= f; z *= f; w *= f; 100 | return *this; 101 | } 102 | 103 | // / operator 104 | Vector4 operator/(float f) const { 105 | assert(f!=0); 106 | float inv = 1.f / f; 107 | return Vector4(x * inv, y * inv, z * inv, w * inv); 108 | } 109 | 110 | // /= operator 111 | Vector4 &operator/=(float f) { 112 | assert(f!=0); 113 | float inv = 1.f / f; 114 | x *= inv; y *= inv; z *= inv; w *= inv; 115 | return *this; 116 | } 117 | 118 | // - operator 119 | Vector4 operator-() const { 120 | return Vector4(-x, -y, -z, -w); 121 | } 122 | 123 | // [] operator 124 | float &operator[](int i) { 125 | assert(i >= 0 && i <= 3); 126 | switch (i) { 127 | case 0: return x; 128 | case 1: return y; 129 | case 2: return z; 130 | case 3: return w; 131 | } 132 | return w; 133 | } 134 | 135 | // Dot product operator 136 | float dot(const Vector4 &v) const { 137 | return x * v.x + y * v.y + z * v.z + w * v.w; 138 | } 139 | 140 | // Multiply two vectors by their components 141 | Vector4 componentMul(const Vector4 &v) const { 142 | return Vector4(x * v.x, y * v.y, z * v.z, w * v.w); 143 | } 144 | 145 | // Clamp the values between 0 and 1 146 | Vector4 clamp01() { 147 | if (x>1.f) x=1.f; 148 | else if (x<0.f) x=0.f; 149 | if (y>1.f) y=1.f; 150 | else if (y<0.f) y=0.f; 151 | if (z>1.f) z=1.f; 152 | else if (z<0.f) z=0.f; 153 | if (w>1.f) w=1.f; 154 | else if (w<0.f) w=0.f; 155 | return *this; 156 | } 157 | 158 | // Return the squared length of the vector 159 | float lengthSquared() const { return x * x + y * y + z * z + w * w; } 160 | 161 | // Return the length of the vector 162 | float length() const { return sqrt(lengthSquared()); } 163 | }; 164 | 165 | } 166 | 167 | #endif //_VECTOR4_H 168 | -------------------------------------------------------------------------------- /src/openglframework.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | #ifndef OPENGL_FRAMEWORK_H 27 | #define OPENGL_FRAMEWORK_H 28 | 29 | // Libraries 30 | #include "MeshReaderWriter.h" 31 | #include "TextureReaderWriter.h" 32 | #include "GlutViewer.h" 33 | #include "Camera.h" 34 | #include "Light.h" 35 | #include "Mesh.h" 36 | #include "Shader.h" 37 | #include "Texture2D.h" 38 | #include "FrameBufferObject.h" 39 | #include "Shader.h" 40 | #include "maths/Color.h" 41 | #include "maths/Vector2.h" 42 | #include "maths/Vector3.h" 43 | #include "maths/Vector4.h" 44 | #include "maths/Matrix4.h" 45 | #include "maths/Matrix3.h" 46 | #include "definitions.h" 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /src/shaders/depth.frag: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | void main(void) { 27 | 28 | // Compute the depth of the pixel 29 | float depth = 30 | 31 | gl_FragColor = vec4(depth, depth, depth, 1); 32 | } 33 | -------------------------------------------------------------------------------- /src/shaders/depth.vert: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Uniform variables 27 | uniform mat4 modelToWorldMatrix; // Model too world coordinates matrix 28 | uniform mat4 worldToCameraMatrix; // World to camera coordinates matrix 29 | uniform mat4 projectionMatrix; // Projection matrix 30 | 31 | void main(void) { 32 | 33 | // Compute the clip-space vertex coordinates 34 | gl_Position = projectionMatrix * worldToCameraMatrix * 35 | modelToWorldMatrix * gl_Vertex; 36 | } 37 | -------------------------------------------------------------------------------- /src/shaders/phong.frag: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Uniform variables 27 | uniform vec3 cameraWorldPosition; // World position of the camera 28 | uniform vec3 lightWorldPosition; // World position of the light 29 | uniform vec3 lightAmbientColor; // Lights ambient color 30 | uniform vec3 lightDiffuseColor; // Light diffuse color 31 | uniform vec3 lightSpecularColor; // Light specular color 32 | uniform float shininess; // Shininess 33 | uniform sampler2D texture; // Texture 34 | uniform bool isTexture; // True if we need to use the texture 35 | 36 | // Varying variables 37 | varying vec3 worldPosition; // World position of the vertex 38 | varying vec3 worldNormal; // World surface normalWorld 39 | varying vec2 texCoords; // Texture coordinates 40 | 41 | void main() { 42 | 43 | // Compute the ambient term 44 | vec3 ambient = lightAmbientColor; 45 | 46 | // Get the texture color 47 | 48 | vec3 textureColor = vec3(1); 49 | if (isTexture) textureColor = texture2D(texture, texCoords).rgb; 50 | 51 | // Compute the diffuse term 52 | vec3 L = normalize(lightWorldPosition - worldPosition); 53 | vec3 N = normalize(worldNormal); 54 | vec3 diffuse = lightDiffuseColor * max(dot(N, L), 0.0) * textureColor; 55 | 56 | // Compute the specular term 57 | vec3 V = normalize(cameraWorldPosition - worldPosition); 58 | vec3 H = normalize(V + L); 59 | vec3 specular = lightSpecularColor * pow(max(dot(N, H), 0), shininess); 60 | 61 | // Compute the final color 62 | gl_FragColor = vec4(ambient + diffuse + specular, 1.0); 63 | } 64 | -------------------------------------------------------------------------------- /src/shaders/phong.vert: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | * OpenGL-Framework * 3 | * Copyright (c) 2013 Daniel Chappuis * 4 | ********************************************************************************* 5 | * * 6 | * This software is provided 'as-is', without any express or implied warranty. * 7 | * In no event will the authors be held liable for any damages arising from the * 8 | * use of this software. * 9 | * * 10 | * Permission is granted to anyone to use this software for any purpose, * 11 | * including commercial applications, and to alter it and redistribute it * 12 | * freely, subject to the following restrictions: * 13 | * * 14 | * 1. The origin of this software must not be misrepresented; you must not claim * 15 | * that you wrote the original software. If you use this software in a * 16 | * product, an acknowledgment in the product documentation would be * 17 | * appreciated but is not required. * 18 | * * 19 | * 2. Altered source versions must be plainly marked as such, and must not be * 20 | * misrepresented as being the original software. * 21 | * * 22 | * 3. This notice may not be removed or altered from any source distribution. * 23 | * * 24 | ********************************************************************************/ 25 | 26 | // Uniform variables 27 | uniform mat4 modelToWorldMatrix; // Model too world coordinates matrix 28 | uniform mat4 worldToCameraMatrix; // World to camera coordinates matrix 29 | uniform mat4 projectionMatrix; // Projection matrix 30 | 31 | // Varying variables 32 | varying vec3 worldPosition; // World position of the vertex 33 | varying vec3 worldNormal; // World surface normalWorld 34 | varying vec2 texCoords; // Texture coordinates 35 | 36 | void main() { 37 | 38 | // Compute the vertex position 39 | vec4 worldPos = modelToWorldMatrix * gl_Vertex; 40 | worldPosition = worldPos.xyz; 41 | 42 | // Compute the world surface normal 43 | worldNormal = (modelToWorldMatrix * vec4(gl_Normal, 0.0)).xyz; 44 | 45 | // Get the texture coordinates 46 | texCoords = gl_MultiTexCoord0.xy; 47 | 48 | // Compute the clip-space vertex coordinates 49 | gl_Position = projectionMatrix * worldToCameraMatrix * worldPos; 50 | } 51 | --------------------------------------------------------------------------------