From 548323b36e7aaaa24c1526581a0e32513ed2098c Mon Sep 17 00:00:00 2001 From: Marcus Hudritsch Date: Sun, 3 Nov 2024 12:34:21 +0100 Subject: [PATCH] Replaced all new/delete by vectors in exercises Resolved all clangd issues --- apps/exercises/ch06_ColorCube/ColorCube.cpp | 322 ++++++------ .../ch07_DiffuseCube/DiffuseCube.cpp | 350 ++++++------- .../ch07_DiffuseSphere/DiffuseSphere.cpp | 382 +++++++------- .../BlinnPhongLighting.cpp | 495 +++++++++--------- .../ch09_TextureMapping/TextureMapping.cpp | 428 ++++++++------- .../cv02_CalderonFilter.cpp | 3 +- .../cv06_WarpTriangle/cv06_WarpTriangle.cpp | 47 +- .../cv07_MeshWarping/cv07_MeshWarping.cpp | 50 +- .../cv08_MeshMorphing/cv08_MeshMorphing.cpp | 24 +- .../cv13_FaceTracking/cv13_FaceTracking.cpp | 13 +- .../cv13_FacialLandmarkDetection.cpp | 25 +- .../cv13_HeadPoseEstimation.cpp | 8 +- .../cv13_Snapchat2D/cv13_Snapchat2D.cpp | 14 +- 13 files changed, 1108 insertions(+), 1053 deletions(-) diff --git a/apps/exercises/ch06_ColorCube/ColorCube.cpp b/apps/exercises/ch06_ColorCube/ColorCube.cpp index b05eb742..d09686d7 100644 --- a/apps/exercises/ch06_ColorCube/ColorCube.cpp +++ b/apps/exercises/ch06_ColorCube/ColorCube.cpp @@ -5,13 +5,15 @@ * \date April 2016 (FS16) * \authors Marcus Hudritsch * \copyright http://opensource.org/licenses/GPL-3.0 -*/ + */ +#include "glcorearb.h" #include // OpenGL headers #include // GLFW GUI library #include // 4x4 matrix class #include // 3D vector class #include // Basics for OpenGL shaders, buffers & textures +#include //----------------------------------------------------------------------------- //! Struct definition for vertex attribute position and color @@ -34,44 +36,44 @@ struct VertexPC }; //----------------------------------------------------------------------------- // Global application variables -static GLFWwindow* window; //!< The global GLFW window handle -static SLstring _projectRoot; //!< Directory of executable -static SLint _scrWidth; //!< Window width at start up -static SLint _scrHeight; //!< Window height at start up - -static SLMat4f _cameraMatrix; //!< 4x4 matrix for camera to world transform -static SLMat4f _viewMatrix; //!< 4x4 matrix for world to camera transform -static SLMat4f _modelMatrix; //!< 4x4 matrix for model to world transform -static SLMat4f _projectionMatrix; //!< Projection from view space to normalized device coordinates - -static GLuint _vao = 0; //!< ID of the vertex array object -static GLuint _vboV = 0; //!< ID of the VBO for vertex attributes -static GLuint _vboI = 0; //!< ID of the VBO for vertex index array -static GLuint _numV = 0; //!< NO. of vertices -static GLuint _numI = 0; //!< NO. of vertex indexes for triangles - -static float _camZ; //!< z-distance of camera -static float _rotX, _rotY; //!< rotation angles around x & y axis -static int _deltaX, _deltaY; //!< delta mouse motion -static int _startX, _startY; //!< x,y mouse start positions -static int _mouseX, _mouseY; //!< current mouse position -static bool _mouseLeftDown; //!< Flag if mouse is down -static GLuint _modifiers = 0; //!< modifier bit flags -const GLuint NONE = 0; //!< constant for no modifier -const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier -const GLuint CTRL = 0x00400000; //!< constant for control key modifier -const GLuint ALT = 0x00800000; //!< constant for alt key modifier - -static GLuint _shaderVertID = 0; //! vertex shader id -static GLuint _shaderFragID = 0; //! fragment shader id -static GLuint _shaderProgID = 0; //! shader program id +static GLFWwindow* window; //!< The global GLFW window handle +static SLstring projectRoot; //!< Directory of executable +static SLint scrWidth; //!< Window width at start up +static SLint scrHeight; //!< Window height at start up + +static SLMat4f cameraMatrix; //!< 4x4 matrix for camera to world transform +static SLMat4f viewMatrix; //!< 4x4 matrix for world to camera transform +static SLMat4f modelMatrix; //!< 4x4 matrix for model to world transform +static SLMat4f projectionMatrix; //!< Projection from view space to normalized device coordinates + +static GLuint vao = 0; //!< ID of the vertex array object +static GLuint vboV = 0; //!< ID of the VBO for vertex attributes +static GLuint vboI = 0; //!< ID of the VBO for vertex index array +static GLuint numV = 0; //!< NO. of vertices +static GLuint numI = 0; //!< NO. of vertex indexes for triangles + +static float camZ; //!< z-distance of camera +static float rotX, rotY; //!< rotation angles around x & y axis +static int deltaX, deltaY; //!< delta mouse motion +static int startX, startY; //!< x,y mouse start positions +static int mouseX, mouseY; //!< current mouse position +static bool mouseLeftDown; //!< Flag if mouse is down +static GLuint modifiers = 0; //!< modifier bit flags +const GLuint NONE = 0; //!< constant for no modifier +const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier +const GLuint CTRL = 0x00400000; //!< constant for control key modifier +const GLuint ALT = 0x00800000; //!< constant for alt key modifier + +static GLuint shaderVertID = 0; //! vertex shader id +static GLuint shaderFragID = 0; //! fragment shader id +static GLuint shaderProgID = 0; //! shader program id // Attribute & uniform variable location indexes -static GLint _pLoc; //!< attribute location for vertex position -static GLint _cLoc; //!< attribute location for vertex color -static GLint _pmLoc; //!< uniform location for projection matrix -static GLint _vmLoc; //!< uniform location for view matrix -static GLint _mmLoc; //!< uniform location for model matrix +static GLint pLoc; //!< attribute location for vertex position +static GLint cLoc; //!< attribute location for vertex color +static GLint pmLoc; //!< uniform location for projection matrix +static GLint vmLoc; //!< uniform location for view matrix +static GLint mmLoc; //!< uniform location for model matrix //----------------------------------------------------------------------------- void buildBox() @@ -79,8 +81,8 @@ void buildBox() // create C arrays on heap // Define the vertex position and colors as an array of structure // We define the colors with the same components as the cubes corners. - _numV = 8; - VertexPC* vertices = new VertexPC[_numV]; + numV = 8; + std::vector vertices(numV); vertices[0].set(1, 1, 1, 1, 1, 1); // LTN vertices[1].set(1, 0, 1, 1, 0, 1); // LBN vertices[2].set(1, 0, 0, 1, 0, 0); // LBF @@ -91,63 +93,61 @@ void buildBox() vertices[7].set(0, 1, 0, 0, 1, 0); // RTF // Define the triangle indexes of the cubes vertices - _numI = 36; - GLuint* indices = new GLuint[_numI]; - int n = 0; - indices[n++] = 0; - indices[n++] = 1; - indices[n++] = 2; - indices[n++] = 0; - indices[n++] = 2; - indices[n++] = 3; // Right - indices[n++] = 4; - indices[n++] = 5; - indices[n++] = 6; - indices[n++] = 4; - indices[n++] = 6; - indices[n++] = 7; // Left - indices[n++] = 0; - indices[n++] = 3; - indices[n++] = 7; - indices[n++] = 0; - indices[n++] = 7; - indices[n++] = 6; // Top - indices[n++] = 1; - indices[n++] = 5; - indices[n++] = 2; - indices[n++] = 2; - indices[n++] = 5; - indices[n++] = 4; // Bottom - indices[n++] = 0; - indices[n++] = 5; - indices[n++] = 1; - indices[n++] = 0; - indices[n++] = 6; - indices[n++] = 5; // Near - indices[n++] = 4; - indices[n++] = 7; - indices[n++] = 3; - indices[n++] = 3; - indices[n++] = 2; - indices[n++] = 4; // Far + numI = 36; + std::vector indices(numI); + int n = 0; + indices[n++] = 0; + indices[n++] = 1; + indices[n++] = 2; + indices[n++] = 0; + indices[n++] = 2; + indices[n++] = 3; // Right + indices[n++] = 4; + indices[n++] = 5; + indices[n++] = 6; + indices[n++] = 4; + indices[n++] = 6; + indices[n++] = 7; // Left + indices[n++] = 0; + indices[n++] = 3; + indices[n++] = 7; + indices[n++] = 0; + indices[n++] = 7; + indices[n++] = 6; // Top + indices[n++] = 1; + indices[n++] = 5; + indices[n++] = 2; + indices[n++] = 2; + indices[n++] = 5; + indices[n++] = 4; // Bottom + indices[n++] = 0; + indices[n++] = 5; + indices[n++] = 1; + indices[n++] = 0; + indices[n++] = 6; + indices[n++] = 5; // Near + indices[n++] = 4; + indices[n++] = 7; + indices[n++] = 3; + indices[n++] = 3; + indices[n++] = 2; + indices[n++] = 4; // Far // Generate the OpenGL vertex array object - glUtils::buildVAO(_vao, - _vboV, - _vboI, - vertices, - (GLint)_numV, + glUtils::buildVAO(vao, + vboV, + vboI, + vertices.data(), + (GLint)numV, sizeof(VertexPC), - indices, - (GLint)_numI, - sizeof(GL_UNSIGNED_INT), - (GLint)_shaderProgID, - _pLoc, - _cLoc); - - // delete data on heap. The VBOs are now on the GPU - delete[] vertices; - delete[] indices; + indices.data(), + (GLint)numI, + sizeof(unsigned int), + (GLint)shaderProgID, + pLoc, + cLoc); + + // The vertices and indices get deleted here by the vectors (RAII) } //----------------------------------------------------------------------------- /*! @@ -157,27 +157,27 @@ should be called after a window with a valid OpenGL context is present. void onInit() { // Position of the camera - _camZ = 3; + camZ = 3; // Mouse rotation parameters - _rotX = _rotY = 0; - _deltaX = _deltaY = 0; - _mouseLeftDown = false; + rotX = rotY = 0; + deltaX = deltaY = 0; + mouseLeftDown = false; // Load, compile & link shaders - _shaderVertID = glUtils::buildShader(_projectRoot + "/data/shaders/ch06_ColorCube.vert", GL_VERTEX_SHADER); - _shaderFragID = glUtils::buildShader(_projectRoot + "/data/shaders/ch06_ColorCube.frag", GL_FRAGMENT_SHADER); - _shaderProgID = glUtils::buildProgram(_shaderVertID, _shaderFragID); + shaderVertID = glUtils::buildShader(projectRoot + "/data/shaders/ch06_ColorCube.vert", GL_VERTEX_SHADER); + shaderFragID = glUtils::buildShader(projectRoot + "/data/shaders/ch06_ColorCube.frag", GL_FRAGMENT_SHADER); + shaderProgID = glUtils::buildProgram(shaderVertID, shaderFragID); // Activate the shader program - glUseProgram(_shaderProgID); + glUseProgram(shaderProgID); // Get the variable locations (identifiers) within the program - _pLoc = glGetAttribLocation(_shaderProgID, "a_position"); - _cLoc = glGetAttribLocation(_shaderProgID, "a_color"); - _pmLoc = glGetUniformLocation(_shaderProgID, "u_pMatrix"); - _vmLoc = glGetUniformLocation(_shaderProgID, "u_vMatrix"); - _mmLoc = glGetUniformLocation(_shaderProgID, "u_mMatrix"); + pLoc = glGetAttribLocation(shaderProgID, "a_position"); + cLoc = glGetAttribLocation(shaderProgID, "a_color"); + pmLoc = glGetUniformLocation(shaderProgID, "u_pMatrix"); + vmLoc = glGetUniformLocation(shaderProgID, "u_vMatrix"); + mmLoc = glGetUniformLocation(shaderProgID, "u_mMatrix"); buildBox(); @@ -194,14 +194,14 @@ deallocation of resources. void onClose(GLFWwindow* myWindow) { // Delete shaders & programs on GPU - glDeleteShader(_shaderVertID); - glDeleteShader(_shaderFragID); - glDeleteProgram(_shaderProgID); + glDeleteShader(shaderVertID); + glDeleteShader(shaderFragID); + glDeleteProgram(shaderProgID); // Delete arrays & buffers on GPU - glDeleteVertexArrays(1, &_vao); - glDeleteBuffers(1, &_vboV); - glDeleteBuffers(1, &_vboI); + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vboV); + glDeleteBuffers(1, &vboI); } //----------------------------------------------------------------------------- /*! @@ -215,33 +215,33 @@ bool onPaint() /* 2b) Camera transform: rotate the coordinate system increasingly * first around the y- and then around the x-axis. This type of camera * transform is called turntable animation.*/ - _cameraMatrix.identity(); - _cameraMatrix.rotate(_rotY + _deltaY, 0, 1, 0); - _cameraMatrix.rotate(_rotX + _deltaX, 1, 0, 0); + cameraMatrix.identity(); + cameraMatrix.rotate(rotY + (float)deltaY, 0, 1, 0); + cameraMatrix.rotate(rotX + (float)deltaX, 1, 0, 0); // 2a) Move the camera to its position. - _cameraMatrix.translate(0, 0, _camZ); + cameraMatrix.translate(0, 0, camZ); // 2c) View transform is world to camera (= inverse of camera matrix) - _viewMatrix = _cameraMatrix.inverted(); + viewMatrix = cameraMatrix.inverted(); // 3) Model transform: move the cube so that it rotates around its center - _modelMatrix.identity(); - _modelMatrix.translate(-0.5f, -0.5f, -0.5f); + modelMatrix.identity(); + modelMatrix.translate(-0.5f, -0.5f, -0.5f); // 4) Lights get prepared here later on // 5) Activate the shader program and pass the uniform variables to the shader - glUseProgram(_shaderProgID); - glUniformMatrix4fv(_pmLoc, 1, 0, (float*)&_projectionMatrix); - glUniformMatrix4fv(_vmLoc, 1, 0, (float*)&_viewMatrix); - glUniformMatrix4fv(_mmLoc, 1, 0, (float*)&_modelMatrix); + glUseProgram(shaderProgID); + glUniformMatrix4fv(pmLoc, 1, 0, (float*)&projectionMatrix); + glUniformMatrix4fv(vmLoc, 1, 0, (float*)&viewMatrix); + glUniformMatrix4fv(mmLoc, 1, 0, (float*)&modelMatrix); // 6) Activate the vertex array - glBindVertexArray(_vao); + glBindVertexArray(vao); // 7) Final draw call that draws the cube with triangles by indexes - glDrawElements(GL_TRIANGLES, (GLint)_numI, GL_UNSIGNED_INT, nullptr); + glDrawElements(GL_TRIANGLES, (GLint)numI, GL_UNSIGNED_INT, nullptr); // 8) Fast copy the back buffer to the front buffer. This is OS dependent. glfwSwapBuffers(window); @@ -262,7 +262,7 @@ void onResize(GLFWwindow* myWindow, int width, int height) float h = (float)height; // define the projection matrix - _projectionMatrix.perspective(50, w / h, 0.01f, 10.0f); + projectionMatrix.perspective(50, w / h, 0.01f, 10.0f); // define the viewport glViewport(0, 0, width, height); @@ -277,14 +277,14 @@ Mouse button down & release eventhandler starts and end mouse rotation */ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) { - SLint x = _mouseX; - SLint y = _mouseY; + SLint x = mouseX; + SLint y = mouseY; - _mouseLeftDown = (action == GLFW_PRESS); - if (_mouseLeftDown) + mouseLeftDown = (action == GLFW_PRESS); + if (mouseLeftDown) { - _startX = x; - _startY = y; + startX = x; + startY = y; // Renders only the lines of a polygon during mouse moves if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -292,10 +292,10 @@ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) } else { - _rotX += _deltaX; - _rotY += _deltaY; - _deltaX = 0; - _deltaY = 0; + rotX += (float)deltaX; + rotY += (float)deltaY; + deltaX = 0; + deltaY = 0; // Renders filled polygons if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -308,13 +308,13 @@ Mouse move eventhandler tracks the mouse delta since touch down (_deltaX/_deltaY */ void onMouseMove(GLFWwindow* myWindow, double x, double y) { - _mouseX = (int)x; - _mouseY = (int)y; + mouseX = (int)x; + mouseY = (int)y; - if (_mouseLeftDown) + if (mouseLeftDown) { - _deltaY = (int)(_startX - x); - _deltaX = (int)(_startY - y); + deltaY = (int)(startX - x); + deltaX = (int)(startY - y); onPaint(); } } @@ -324,9 +324,9 @@ Mouse wheel eventhandler that moves the camera forward or backwards */ void onMouseWheel(GLFWwindow* myWindow, double xscroll, double yscroll) { - if (_modifiers == NONE) + if (modifiers == NONE) { - _camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; + camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; onPaint(); } } @@ -344,24 +344,26 @@ void onKey(GLFWwindow* myWindow, int GLFWKey, int scancode, int action, int mods onClose(window); glfwSetWindowShouldClose(window, GL_TRUE); break; - case GLFW_KEY_LEFT_SHIFT: _modifiers = _modifiers | SHIFT; break; - case GLFW_KEY_RIGHT_SHIFT: _modifiers = _modifiers | SHIFT; break; - case GLFW_KEY_LEFT_CONTROL: _modifiers = _modifiers | CTRL; break; - case GLFW_KEY_RIGHT_CONTROL: _modifiers = _modifiers | CTRL; break; - case GLFW_KEY_LEFT_ALT: _modifiers = _modifiers | ALT; break; - case GLFW_KEY_RIGHT_ALT: _modifiers = _modifiers | ALT; break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: modifiers = modifiers | SHIFT; break; + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: modifiers = modifiers | CTRL; break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: modifiers = modifiers | ALT; break; + default: break; } } else if (action == GLFW_RELEASE) { switch (GLFWKey) { - case GLFW_KEY_LEFT_SHIFT: _modifiers = _modifiers ^ SHIFT; break; - case GLFW_KEY_RIGHT_SHIFT: _modifiers = _modifiers ^ SHIFT; break; - case GLFW_KEY_LEFT_CONTROL: _modifiers = _modifiers ^ CTRL; break; - case GLFW_KEY_RIGHT_CONTROL: _modifiers = _modifiers ^ CTRL; break; - case GLFW_KEY_LEFT_ALT: _modifiers = _modifiers ^ ALT; break; - case GLFW_KEY_RIGHT_ALT: _modifiers = _modifiers ^ ALT; break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: modifiers = modifiers ^ SHIFT; break; + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: modifiers = modifiers ^ CTRL; break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: modifiers = modifiers ^ ALT; break; + default: break; } } } @@ -441,13 +443,13 @@ The C main procedure running the GLFW GUI application. */ int main(int argc, char* argv[]) { - _projectRoot = SLstring(SL_PROJECT_ROOT); + projectRoot = SLstring(SL_PROJECT_ROOT); - _scrWidth = 640; - _scrHeight = 480; + scrWidth = 640; + scrHeight = 480; // Init OpenGL and the window library GLFW - initGLFW(_scrWidth, _scrHeight, "ColorCube"); + initGLFW(scrWidth, scrHeight, "ColorCube"); // Check errors before we start GETGLERROR; @@ -459,7 +461,7 @@ int main(int argc, char* argv[]) onInit(); // Call once resize to define the projection - onResize(window, _scrWidth, _scrHeight); + onResize(window, scrWidth, scrHeight); // Event loop while (!glfwWindowShouldClose(window)) diff --git a/apps/exercises/ch07_DiffuseCube/DiffuseCube.cpp b/apps/exercises/ch07_DiffuseCube/DiffuseCube.cpp index a0c7a6cf..51d0188f 100644 --- a/apps/exercises/ch07_DiffuseCube/DiffuseCube.cpp +++ b/apps/exercises/ch07_DiffuseCube/DiffuseCube.cpp @@ -5,13 +5,14 @@ * \date September 2012 (HS12) * \authors Marcus Hudritsch * \copyright http://opensource.org/licenses/GPL-3.0 -*/ + */ #include // OpenGL headers #include // GLFW GUI library #include // 4x4 matrix class #include // 3D vector class #include // Basics for OpenGL shaders, buffers & textures +#include //----------------------------------------------------------------------------- //! Struct definition for vertex attribute position and normal @@ -30,47 +31,47 @@ struct VertexPN //----------------------------------------------------------------------------- // Global application variables -static GLFWwindow* window; //!< The global GLFW window handle -static SLstring _projectRoot; //!< Directory of executable -static SLint _scrWidth; //!< Window width at start up -static SLint _scrHeight; //!< Window height at start up - -static SLMat4f _cameraMatrix; //!< 4x4 matrix for camera to world transform -static SLMat4f _viewMatrix; //!< 4x4 matrix for world to camera transform -static SLMat4f _modelMatrix; //!< 4x4 matrix for model to world transform -static SLMat4f _projectionMatrix; //!< Projection from view space to normalized device coordinates - -static GLuint _vao = 0; //!< ID of the vertex array object -static GLuint _vboV = 0; //!< ID of the VBO for vertex attributes -static GLuint _vboI = 0; //!< ID of the VBO for vertex index array -static GLuint _numV = 0; //!< NO. of vertices -static GLuint _numI = 0; //!< NO. of vertex indexes for triangles - -static float _camZ; //!< z-distance of camera -static float _rotX, _rotY; //!< rotation angles around x & y axis -static int _deltaX, _deltaY; //!< delta mouse motion -static int _startX, _startY; //!< x,y mouse start positions -static int _mouseX, _mouseY; //!< current mouse position -static bool _mouseLeftDown; //!< Flag if mouse is down -static GLuint _modifiers = 0; //!< modifier bit flags -static const GLuint NONE = 0; //!< constant for no modifier -static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier -static const GLuint CTRL = 0x00400000; //!< constant for control key modifier -static const GLuint ALT = 0x00800000; //!< constant for alt key modifier - -static GLuint _shaderVertID = 0; //! vertex shader id -static GLuint _shaderFragID = 0; //! fragment shader id -static GLuint _shaderProgID = 0; //! shader program id +static GLFWwindow* window; //!< The global GLFW window handle +static SLstring projectRoot; //!< Directory of executable +static SLint scrWidth; //!< Window width at start up +static SLint scrHeight; //!< Window height at start up + +static SLMat4f cameraMatrix; //!< 4x4 matrix for camera to world transform +static SLMat4f viewMatrix; //!< 4x4 matrix for world to camera transform +static SLMat4f modelMatrix; //!< 4x4 matrix for model to world transform +static SLMat4f projectionMatrix; //!< Projection from view space to normalized device coordinates + +static GLuint vao = 0; //!< ID of the vertex array object +static GLuint vboV = 0; //!< ID of the VBO for vertex attributes +static GLuint vboI = 0; //!< ID of the VBO for vertex index array +static GLuint numV = 0; //!< NO. of vertices +static GLuint numI = 0; //!< NO. of vertex indexes for triangles + +static float camZ; //!< z-distance of camera +static float rotX, rotY; //!< rotation angles around x & y axis +static int deltaX, deltaY; //!< delta mouse motion +static int startX, startY; //!< x,y mouse start positions +static int mouseX, mouseY; //!< current mouse position +static bool mouseLeftDown; //!< Flag if mouse is down +static GLuint modifiers = 0; //!< modifier bit flags +static const GLuint NONE = 0; //!< constant for no modifier +static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier +static const GLuint CTRL = 0x00400000; //!< constant for control key modifier +static const GLuint ALT = 0x00800000; //!< constant for alt key modifier + +static GLuint shaderVertID = 0; //! vertex shader id +static GLuint shaderFragID = 0; //! fragment shader id +static GLuint shaderProgID = 0; //! shader program id // Attribute & uniform variable location indexes -static GLint _pLoc; //!< attribute location for vertex position -static GLint _nLoc; //!< attribute location for vertex normal -static GLint _pmLoc; //!< uniform location for projection matrix -static GLint _vmLoc; //!< uniform location for view matrix -static GLint _mmLoc; //!< uniform location for model matrix -static GLint _lightSpotDirVSLoc; //!< uniform location for light direction in view space (VS) -static GLint _lightDiffuseLoc; //!< uniform location for diffuse light intensity -static GLint _matDiffuseLoc; //!< uniform location for diffuse light reflection +static GLint pLoc; //!< attribute location for vertex position +static GLint nLoc; //!< attribute location for vertex normal +static GLint pmLoc; //!< uniform location for projection matrix +static GLint vmLoc; //!< uniform location for view matrix +static GLint mmLoc; //!< uniform location for model matrix +static GLint lightSpotDirVSLoc; //!< uniform location for light direction in view space (VS) +static GLint lightDiffuseLoc; //!< uniform location for diffuse light intensity +static GLint matDiffuseLoc; //!< uniform location for diffuse light reflection //----------------------------------------------------------------------------- /*! @@ -80,8 +81,8 @@ void buildBox() { // create C arrays on heap // Define the vertex pos. and normals as an array of structure - _numV = 24; - VertexPN* vertices = new VertexPN[_numV]; + numV = 24; + std::vector vertices(numV); vertices[0].set(1, 1, 1, 1, 0, 0); vertices[1].set(1, 0, 1, 1, 0, 0); vertices[2].set(1, 0, 0, 1, 0, 0); @@ -108,64 +109,63 @@ void buildBox() vertices[23].set(0, 0, 1, 0, -1, 0); // Define the triangle indexes of the cubes vertices - _numI = 36; - GLuint* indices = new GLuint[_numI]; - int n = 0; - indices[n++] = 0; - indices[n++] = 1; - indices[n++] = 2; - indices[n++] = 0; - indices[n++] = 2; - indices[n++] = 3; - indices[n++] = 4; - indices[n++] = 5; - indices[n++] = 6; - indices[n++] = 4; - indices[n++] = 6; - indices[n++] = 7; - indices[n++] = 8; - indices[n++] = 9; - indices[n++] = 10; - indices[n++] = 8; - indices[n++] = 10; - indices[n++] = 11; - indices[n++] = 12; - indices[n++] = 13; - indices[n++] = 14; - indices[n++] = 12; - indices[n++] = 14; - indices[n++] = 15; - indices[n++] = 16; - indices[n++] = 17; - indices[n++] = 18; - indices[n++] = 16; - indices[n++] = 18; - indices[n++] = 19; - indices[n++] = 20; - indices[n++] = 21; - indices[n++] = 22; - indices[n++] = 20; - indices[n++] = 22; - indices[n++] = 23; + numI = 36; + std::vector indices(numI); + + int n = 0; + indices[n++] = 0; + indices[n++] = 1; + indices[n++] = 2; + indices[n++] = 0; + indices[n++] = 2; + indices[n++] = 3; + indices[n++] = 4; + indices[n++] = 5; + indices[n++] = 6; + indices[n++] = 4; + indices[n++] = 6; + indices[n++] = 7; + indices[n++] = 8; + indices[n++] = 9; + indices[n++] = 10; + indices[n++] = 8; + indices[n++] = 10; + indices[n++] = 11; + indices[n++] = 12; + indices[n++] = 13; + indices[n++] = 14; + indices[n++] = 12; + indices[n++] = 14; + indices[n++] = 15; + indices[n++] = 16; + indices[n++] = 17; + indices[n++] = 18; + indices[n++] = 16; + indices[n++] = 18; + indices[n++] = 19; + indices[n++] = 20; + indices[n++] = 21; + indices[n++] = 22; + indices[n++] = 20; + indices[n++] = 22; + indices[n++] = 23; // Generate the OpenGL vertex array object - glUtils::buildVAO(_vao, - _vboV, - _vboI, - vertices, - (GLint)_numV, + glUtils::buildVAO(vao, + vboV, + vboI, + vertices.data(), + (GLint)numV, sizeof(VertexPN), - indices, - (GLint)_numI, - sizeof(GL_UNSIGNED_INT), - (GLint)_shaderProgID, - _pLoc, + indices.data(), + (GLint)numI, + sizeof(unsigned int), + (GLint)shaderProgID, + pLoc, -1, - _nLoc); + nLoc); - // Delete arrays on heap. The data for rendering is now on the GPU - delete[] vertices; - delete[] indices; + // The vertices and indices get deleted here by the vectors (RAII) } //----------------------------------------------------------------------------- /*! @@ -175,30 +175,30 @@ should be called after a window with a valid OpenGL context is present. void onInit() { // Position of the camera - _camZ = 3; + camZ = 3; // Mouse rotation parameters - _rotX = _rotY = 0; - _deltaX = _deltaY = 0; - _mouseLeftDown = false; + rotX = rotY = 0; + deltaX = deltaY = 0; + mouseLeftDown = false; // Load, compile & link shaders - _shaderVertID = glUtils::buildShader(_projectRoot + "/data/shaders/ch07_DiffuseLighting.vert", GL_VERTEX_SHADER); - _shaderFragID = glUtils::buildShader(_projectRoot + "/data/shaders/ch07_DiffuseLighting.frag", GL_FRAGMENT_SHADER); - _shaderProgID = glUtils::buildProgram(_shaderVertID, _shaderFragID); + shaderVertID = glUtils::buildShader(projectRoot + "/data/shaders/ch07_DiffuseLighting.vert", GL_VERTEX_SHADER); + shaderFragID = glUtils::buildShader(projectRoot + "/data/shaders/ch07_DiffuseLighting.frag", GL_FRAGMENT_SHADER); + shaderProgID = glUtils::buildProgram(shaderVertID, shaderFragID); // Activate the shader program - glUseProgram(_shaderProgID); + glUseProgram(shaderProgID); // Get the variable locations (identifiers) within the program - _pLoc = glGetAttribLocation(_shaderProgID, "a_position"); - _nLoc = glGetAttribLocation(_shaderProgID, "a_normal"); - _pmLoc = glGetUniformLocation(_shaderProgID, "u_pMatrix"); - _vmLoc = glGetUniformLocation(_shaderProgID, "u_vMatrix"); - _mmLoc = glGetUniformLocation(_shaderProgID, "u_mMatrix"); - _lightSpotDirVSLoc = glGetUniformLocation(_shaderProgID, "u_lightSpotDir"); - _lightDiffuseLoc = glGetUniformLocation(_shaderProgID, "u_lightDiff"); - _matDiffuseLoc = glGetUniformLocation(_shaderProgID, "u_matDiff"); + pLoc = glGetAttribLocation(shaderProgID, "a_position"); + nLoc = glGetAttribLocation(shaderProgID, "a_normal"); + pmLoc = glGetUniformLocation(shaderProgID, "u_pMatrix"); + vmLoc = glGetUniformLocation(shaderProgID, "u_vMatrix"); + mmLoc = glGetUniformLocation(shaderProgID, "u_mMatrix"); + lightSpotDirVSLoc = glGetUniformLocation(shaderProgID, "u_lightSpotDir"); + lightDiffuseLoc = glGetUniformLocation(shaderProgID, "u_lightDiff"); + matDiffuseLoc = glGetUniformLocation(shaderProgID, "u_matDiff"); buildBox(); @@ -215,14 +215,14 @@ deallocation of resources. void onClose(GLFWwindow* myWindow) { // Delete shaders & programs on GPU - glDeleteShader(_shaderVertID); - glDeleteShader(_shaderFragID); - glDeleteProgram(_shaderProgID); + glDeleteShader(shaderVertID); + glDeleteShader(shaderFragID); + glDeleteProgram(shaderProgID); // Delete arrays & buffers on GPU - glDeleteVertexArrays(1, &_vao); - glDeleteBuffers(1, &_vboV); - glDeleteBuffers(1, &_vboI); + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vboV); + glDeleteBuffers(1, &vboI); } //----------------------------------------------------------------------------- /*! @@ -236,36 +236,36 @@ bool onPaint() /* 2a) Camera transform: rotate the coordinate system increasingly * first around the y- and then around the x-axis. This type of camera * transform is called turntable animation.*/ - _cameraMatrix.identity(); - _cameraMatrix.rotate(_rotY + _deltaY, 0, 1, 0); - _cameraMatrix.rotate(_rotX + _deltaX, 1, 0, 0); + cameraMatrix.identity(); + cameraMatrix.rotate(rotY + (float)deltaY, 0, 1, 0); + cameraMatrix.rotate(rotX + (float)deltaX, 1, 0, 0); // 2b) Move the camera to its position. - _cameraMatrix.translate(0, 0, _camZ); + cameraMatrix.translate(0, 0, camZ); // 2c) View transform is world to camera (= inverse of camera matrix) - _viewMatrix = _cameraMatrix.inverted(); + viewMatrix = cameraMatrix.inverted(); // 3) Model transform: move the cube so that it rotates around its center - _modelMatrix.identity(); - _modelMatrix.translate(-0.5f, -0.5f, -0.5f); + modelMatrix.identity(); + modelMatrix.translate(-0.5f, -0.5f, -0.5f); // 4) Lights get prepared here later on // 5) Activate the shader program and pass the uniform variables to the shader - glUseProgram(_shaderProgID); - glUniformMatrix4fv(_pmLoc, 1, 0, (float*)&_projectionMatrix); - glUniformMatrix4fv(_vmLoc, 1, 0, (float*)&_viewMatrix); - glUniformMatrix4fv(_mmLoc, 1, 0, (float*)&_modelMatrix); - glUniform3f(_lightSpotDirVSLoc, 0.5f, 1.0f, 1.0f); // light direction in view space - glUniform4f(_lightDiffuseLoc, 1.0f, 1.0f, 1.0f, 1.0f); // diffuse light intensity - glUniform4f(_matDiffuseLoc, 1.0f, 0.0f, 0.0f, 1.0f); // diffuse material reflection + glUseProgram(shaderProgID); + glUniformMatrix4fv(pmLoc, 1, 0, (float*)&projectionMatrix); + glUniformMatrix4fv(vmLoc, 1, 0, (float*)&viewMatrix); + glUniformMatrix4fv(mmLoc, 1, 0, (float*)&modelMatrix); + glUniform3f(lightSpotDirVSLoc, 0.5f, 1.0f, 1.0f); // light direction in view space + glUniform4f(lightDiffuseLoc, 1.0f, 1.0f, 1.0f, 1.0f); // diffuse light intensity + glUniform4f(matDiffuseLoc, 1.0f, 0.0f, 0.0f, 1.0f); // diffuse material reflection // 6) Activate the vertex array - glBindVertexArray(_vao); + glBindVertexArray(vao); // 7) Final draw call that draws the cube with triangles by indexes - glDrawElements(GL_TRIANGLES, (GLsizei)_numI, GL_UNSIGNED_INT, nullptr); + glDrawElements(GL_TRIANGLES, (GLsizei)numI, GL_UNSIGNED_INT, nullptr); // 8) Fast copy the back buffer to the front buffer. This is OS dependent. glfwSwapBuffers(window); @@ -286,7 +286,7 @@ void onResize(GLFWwindow* myWindow, int width, int height) float h = (float)height; // define the projection matrix - _projectionMatrix.perspective(50, w / h, 0.01f, 10.0f); + projectionMatrix.perspective(50, w / h, 0.01f, 10.0f); // define the viewport (OpenGL applies it in the background) glViewport(0, 0, width, height); @@ -300,14 +300,14 @@ Mouse button down & release eventhandler starts and end mouse rotation */ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) { - SLint x = _mouseX; - SLint y = _mouseY; + SLint x = mouseX; + SLint y = mouseY; - _mouseLeftDown = (action == GLFW_PRESS); - if (_mouseLeftDown) + mouseLeftDown = (action == GLFW_PRESS); + if (mouseLeftDown) { - _startX = x; - _startY = y; + startX = x; + startY = y; // Renders only the lines of a polygon during mouse moves if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -315,10 +315,10 @@ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) } else { - _rotX += _deltaX; - _rotY += _deltaY; - _deltaX = 0; - _deltaY = 0; + rotX += (float)deltaX; + rotY += (float)deltaY; + deltaX = 0; + deltaY = 0; // Renders filled polygons if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -331,13 +331,13 @@ Mouse move eventhandler tracks the mouse delta since touch down (_deltaX/_deltaY */ void onMouseMove(GLFWwindow* myWindow, double x, double y) { - _mouseX = (int)x; - _mouseY = (int)y; + mouseX = (int)x; + mouseY = (int)y; - if (_mouseLeftDown) + if (mouseLeftDown) { - _deltaY = (int)(_startX - x); - _deltaX = (int)(_startY - y); + deltaY = (int)(startX - x); + deltaX = (int)(startY - y); onPaint(); } } @@ -347,9 +347,9 @@ Mouse wheel eventhandler that moves the camera forward or backwards */ void onMouseWheel(GLFWwindow* myWindow, double xscroll, double yscroll) { - if (_modifiers == NONE) + if (modifiers == NONE) { - _camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; + camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; onPaint(); } } @@ -368,23 +368,12 @@ void onKey(GLFWwindow* myWindow, int GLFWKey, int scancode, int action, int mods glfwSetWindowShouldClose(window, GL_TRUE); break; case GLFW_KEY_LEFT_SHIFT: - _modifiers = _modifiers | SHIFT; - break; - case GLFW_KEY_RIGHT_SHIFT: - _modifiers = _modifiers | SHIFT; - break; + case GLFW_KEY_RIGHT_SHIFT: modifiers = modifiers | SHIFT; break; case GLFW_KEY_LEFT_CONTROL: - _modifiers = _modifiers | CTRL; - break; - case GLFW_KEY_RIGHT_CONTROL: - _modifiers = _modifiers | CTRL; - break; + case GLFW_KEY_RIGHT_CONTROL: modifiers = modifiers | CTRL; break; case GLFW_KEY_LEFT_ALT: - _modifiers = _modifiers | ALT; - break; - case GLFW_KEY_RIGHT_ALT: - _modifiers = _modifiers | ALT; - break; + case GLFW_KEY_RIGHT_ALT: modifiers = modifiers | ALT; break; + default: break; } } else if (action == GLFW_RELEASE) @@ -392,23 +381,12 @@ void onKey(GLFWwindow* myWindow, int GLFWKey, int scancode, int action, int mods switch (GLFWKey) { case GLFW_KEY_LEFT_SHIFT: - _modifiers = _modifiers ^ SHIFT; - break; - case GLFW_KEY_RIGHT_SHIFT: - _modifiers = _modifiers ^ SHIFT; - break; + case GLFW_KEY_RIGHT_SHIFT: modifiers = modifiers ^ SHIFT; break; case GLFW_KEY_LEFT_CONTROL: - _modifiers = _modifiers ^ CTRL; - break; - case GLFW_KEY_RIGHT_CONTROL: - _modifiers = _modifiers ^ CTRL; - break; + case GLFW_KEY_RIGHT_CONTROL: modifiers = modifiers ^ CTRL; break; case GLFW_KEY_LEFT_ALT: - _modifiers = _modifiers ^ ALT; - break; - case GLFW_KEY_RIGHT_ALT: - _modifiers = _modifiers ^ ALT; - break; + case GLFW_KEY_RIGHT_ALT: modifiers = modifiers ^ ALT; break; + default: break; } } } @@ -491,13 +469,13 @@ The C main procedure running the GLFW GUI application. */ int main(int argc, char* argv[]) { - _projectRoot = SLstring(SL_PROJECT_ROOT); + projectRoot = SLstring(SL_PROJECT_ROOT); - _scrWidth = 640; - _scrHeight = 480; + scrWidth = 640; + scrHeight = 480; // Init OpenGL and the window library GLFW - initGLFW(_scrWidth, _scrHeight, "DiffuseCube"); + initGLFW(scrWidth, scrHeight, "DiffuseCube"); // Check errors before we start GETGLERROR; @@ -509,7 +487,7 @@ int main(int argc, char* argv[]) onInit(); // Call once resize to define the projection - onResize(window, _scrWidth, _scrHeight); + onResize(window, scrWidth, scrHeight); // Event loop while (!glfwWindowShouldClose(window)) diff --git a/apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp b/apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp index 0c295ac5..d4f561ea 100644 --- a/apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp +++ b/apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp @@ -29,50 +29,50 @@ struct VertexPN }; //----------------------------------------------------------------------------- // Global application variables -GLFWwindow* window; //!< The global glfw window handle -static SLstring _projectRoot; //!< Directory of executable -SLint _scrWidth; //!< Window width at start up -SLint _scrHeight; //!< Window height at start up - -static SLMat4f _cameraMatrix; //!< 4x4 matrix for camera to world transform -static SLMat4f _viewMatrix; //!< 4x4 matrix for world to camera transform -static SLMat4f _modelMatrix; //!< 4x4 matrix for model to world transform -static SLMat4f _projectionMatrix; //!< Projection from view space to normalized device coordinates - -static GLuint _vao = 0; //!< ID of the Vertex Array Object (VAO) -static GLuint _vboV = 0; //!< ID of the VBO for vertex array -static GLuint _vboI = 0; //!< ID of the VBO for vertex index array - -static GLuint _numV = 0; //!< NO. of vertices -static GLuint _numI = 0; //!< NO. of vertex indexes for triangles -static GLint _resolution; //!< resolution of sphere stack & slices -static GLint _primitiveType; //!< Type of GL primitive to render - -static float _camZ; //!< z-distance of camera -static float _rotX, _rotY; //!< rotation angles around x & y axis -static int _deltaX, _deltaY; //!< delta mouse motion -static int _startX, _startY; //!< x,y mouse start positions -static int _mouseX, _mouseY; //!< current mouse position -static bool _mouseLeftDown; //!< Flag if mouse is down -static GLuint _modifiers = 0; //!< modifier bit flags -static const GLuint NONE = 0; //!< constant for no modifier -static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier -static const GLuint CTRL = 0x00400000; //!< constant for control key modifier -static const GLuint ALT = 0x00800000; //!< constant for alt key modifier - -static GLuint _shaderVertID = 0; //! vertex shader id -static GLuint _shaderFragID = 0; //! fragment shader id -static GLuint _shaderProgID = 0; //! shader program id +GLFWwindow* window; //!< The global glfw window handle +static SLstring projectRoot; //!< Directory of executable +SLint scrWidth; //!< Window width at start up +SLint scrHeight; //!< Window height at start up + +static SLMat4f cameraMatrix; //!< 4x4 matrix for camera to world transform +static SLMat4f viewMatrix; //!< 4x4 matrix for world to camera transform +static SLMat4f modelMatrix; //!< 4x4 matrix for model to world transform +static SLMat4f projectionMatrix; //!< Projection from view space to normalized device coordinates + +static GLuint vao = 0; //!< ID of the Vertex Array Object (VAO) +static GLuint vboV = 0; //!< ID of the VBO for vertex array +static GLuint vboI = 0; //!< ID of the VBO for vertex index array + +static GLuint numV = 0; //!< NO. of vertices +static GLuint numI = 0; //!< NO. of vertex indexes for triangles +static GLint resolution; //!< resolution of sphere stack & slices +static GLint primitiveType; //!< Type of GL primitive to render + +static float camZ; //!< z-distance of camera +static float rotX, rotY; //!< rotation angles around x & y axis +static int deltaX, deltaY; //!< delta mouse motion +static int startX, startY; //!< x,y mouse start positions +static int mouseX, mouseY; //!< current mouse position +static bool mouseLeftDown; //!< Flag if mouse is down +static GLuint modifiers = 0; //!< modifier bit flags +static const GLuint NONE = 0; //!< constant for no modifier +static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier +static const GLuint CTRL = 0x00400000; //!< constant for control key modifier +static const GLuint ALT = 0x00800000; //!< constant for alt key modifier + +static GLuint shaderVertID = 0; //! vertex shader id +static GLuint shaderFragID = 0; //! fragment shader id +static GLuint shaderProgID = 0; //! shader program id // Attribute & uniform variable location indexes -static GLint _pLoc; //!< attribute location for vertex position -static GLint _nLoc; //!< attribute location for vertex normal -static GLint _pmLoc; //!< uniform location for projection matrix -static GLint _vmLoc; //!< uniform location for view matrix -static GLint _mmLoc; //!< uniform location for model matrix -static GLint _lightSpotDirVSLoc; //!< uniform location for light direction in view space (VS) -static GLint _lightDiffuseLoc; //!< uniform location for diffuse light intensity -static GLint _matDiffuseLoc; //!< uniform location for diffuse light reflection +static GLint pLoc; //!< attribute location for vertex position +static GLint nLoc; //!< attribute location for vertex normal +static GLint pmLoc; //!< uniform location for projection matrix +static GLint vmLoc; //!< uniform location for view matrix +static GLint mmLoc; //!< uniform location for model matrix +static GLint lightSpotDirVSLoc; //!< uniform location for light direction in view space (VS) +static GLint lightDiffuseLoc; //!< uniform location for diffuse light intensity +static GLint matDiffuseLoc; //!< uniform location for diffuse light reflection static const SLfloat PI = 3.14159265358979f; @@ -84,8 +84,8 @@ void buildBox() { // create C arrays on heap // Define the vertex pos. and normals as an array of structure - _numV = 24; - VertexPN* vertices = new VertexPN[_numV]; + numV = 24; + std::vector vertices(numV); vertices[0].set(1, 1, 1, 1, 0, 0); vertices[1].set(1, 0, 1, 1, 0, 0); vertices[2].set(1, 0, 0, 1, 0, 0); @@ -112,65 +112,64 @@ void buildBox() vertices[23].set(0, 0, 1, 0, -1, 0); // Define the triangle indexes of the cubes vertices - _numI = 36; - GLuint* indices = new GLuint[_numI]; - int n = 0; - indices[n++] = 0; - indices[n++] = 1; - indices[n++] = 2; - indices[n++] = 0; - indices[n++] = 2; - indices[n++] = 3; - indices[n++] = 4; - indices[n++] = 5; - indices[n++] = 6; - indices[n++] = 4; - indices[n++] = 6; - indices[n++] = 7; - indices[n++] = 8; - indices[n++] = 9; - indices[n++] = 10; - indices[n++] = 8; - indices[n++] = 10; - indices[n++] = 11; - indices[n++] = 12; - indices[n++] = 13; - indices[n++] = 14; - indices[n++] = 12; - indices[n++] = 14; - indices[n++] = 15; - indices[n++] = 16; - indices[n++] = 17; - indices[n++] = 18; - indices[n++] = 16; - indices[n++] = 18; - indices[n++] = 19; - indices[n++] = 20; - indices[n++] = 21; - indices[n++] = 22; - indices[n++] = 20; - indices[n++] = 22; - indices[n++] = 23; - _primitiveType = GL_TRIANGLES; + numI = 36; + std::vector indices(numI); + + int n = 0; + indices[n++] = 0; + indices[n++] = 1; + indices[n++] = 2; + indices[n++] = 0; + indices[n++] = 2; + indices[n++] = 3; + indices[n++] = 4; + indices[n++] = 5; + indices[n++] = 6; + indices[n++] = 4; + indices[n++] = 6; + indices[n++] = 7; + indices[n++] = 8; + indices[n++] = 9; + indices[n++] = 10; + indices[n++] = 8; + indices[n++] = 10; + indices[n++] = 11; + indices[n++] = 12; + indices[n++] = 13; + indices[n++] = 14; + indices[n++] = 12; + indices[n++] = 14; + indices[n++] = 15; + indices[n++] = 16; + indices[n++] = 17; + indices[n++] = 18; + indices[n++] = 16; + indices[n++] = 18; + indices[n++] = 19; + indices[n++] = 20; + indices[n++] = 21; + indices[n++] = 22; + indices[n++] = 20; + indices[n++] = 22; + indices[n++] = 23; + primitiveType = GL_TRIANGLES; // Generate the OpenGL vertex array object - glUtils::buildVAO(_vao, - _vboV, - _vboI, - vertices, - (GLint)_numV, + glUtils::buildVAO(vao, + vboV, + vboI, + vertices.data(), + (GLint)numV, sizeof(VertexPN), - indices, - (GLint)_numI, - sizeof(GL_UNSIGNED_INT), - (GLint)_shaderProgID, - _pLoc, + indices.data(), + (GLint)numI, + sizeof(unsigned int), + (GLint)shaderProgID, + pLoc, -1, - _nLoc); + nLoc); - // Delete arrays on heap. The data for rendering is now on the GPU - delete[] vertices; - delete[] indices; + // The vertices and indices get deleted here by the vectors (RAII) } //----------------------------------------------------------------------------- /*! @@ -191,32 +190,28 @@ void buildSphere(float radius, int stacks, int slices, GLuint primitveType) // z = r*cos(theta); // Create vertex array - VertexPN* vertices = 0; //!< Array of vertices + std::vector vertices; // ??? // create Index array - GLuint* indices = 0; + std::vector indices; // ??? // Delete arrays on heap. The data for rendering is now on the GPU - if (vertices && indices) + if (vertices.size() && indices.size()) { - glUtils::buildVAO(_vao, - _vboV, - _vboI, - vertices, - _numV, + glUtils::buildVAO(vao, + vboV, + vboI, + vertices.data(), + (GLint)numV, sizeof(VertexPN), - indices, - _numI, - sizeof(GL_UNSIGNED_INT), - _shaderProgID, - _pLoc, - _nLoc); - - // Delete arrays on heap - delete[] vertices; - delete[] indices; + indices.data(), + (GLint)numI, + sizeof(unsigned int), + (GLint)shaderProgID, + pLoc, + nLoc); } else std::cout << "**** You have to define some vertices and indices first in buildSphere! ****" << std::endl; @@ -248,37 +243,37 @@ should be called after a window with a valid OpenGL context is present. void onInit() { // backwards movement of the camera - _camZ = 3; + camZ = 3; // Mouse rotation parameters - _rotX = _rotY = 0; - _deltaX = _deltaY = 0; - _mouseLeftDown = false; + rotX = rotY = 0; + deltaX = deltaY = 0; + mouseLeftDown = false; // Load, compile & link shaders - _shaderVertID = glUtils::buildShader(_projectRoot + "/data/shaders/ch07_DiffuseLighting.vert", GL_VERTEX_SHADER); - _shaderFragID = glUtils::buildShader(_projectRoot + "/data/shaders/ch07_DiffuseLighting.frag", GL_FRAGMENT_SHADER); - _shaderProgID = glUtils::buildProgram(_shaderVertID, _shaderFragID); + shaderVertID = glUtils::buildShader(projectRoot + "/data/shaders/ch07_DiffuseLighting.vert", GL_VERTEX_SHADER); + shaderFragID = glUtils::buildShader(projectRoot + "/data/shaders/ch07_DiffuseLighting.frag", GL_FRAGMENT_SHADER); + shaderProgID = glUtils::buildProgram(shaderVertID, shaderFragID); // Activate the shader program - glUseProgram(_shaderProgID); + glUseProgram(shaderProgID); // Get the variable locations (identifiers) within the program - _pLoc = glGetAttribLocation(_shaderProgID, "a_position"); - _nLoc = glGetAttribLocation(_shaderProgID, "a_normal"); - _pmLoc = glGetUniformLocation(_shaderProgID, "u_pMatrix"); - _vmLoc = glGetUniformLocation(_shaderProgID, "u_vMatrix"); - _mmLoc = glGetUniformLocation(_shaderProgID, "u_mMatrix"); - _lightSpotDirVSLoc = glGetUniformLocation(_shaderProgID, "u_lightSpotDir"); - _lightDiffuseLoc = glGetUniformLocation(_shaderProgID, "u_lightDiff"); - _matDiffuseLoc = glGetUniformLocation(_shaderProgID, "u_matDiff"); + pLoc = glGetAttribLocation(shaderProgID, "a_position"); + nLoc = glGetAttribLocation(shaderProgID, "a_normal"); + pmLoc = glGetUniformLocation(shaderProgID, "u_pMatrix"); + vmLoc = glGetUniformLocation(shaderProgID, "u_vMatrix"); + mmLoc = glGetUniformLocation(shaderProgID, "u_mMatrix"); + lightSpotDirVSLoc = glGetUniformLocation(shaderProgID, "u_lightSpotDir"); + lightDiffuseLoc = glGetUniformLocation(shaderProgID, "u_lightDiff"); + matDiffuseLoc = glGetUniformLocation(shaderProgID, "u_matDiff"); // Create sphere - _resolution = 16; - _primitiveType = GL_TRIANGLE_STRIP; + resolution = 16; + primitiveType = GL_TRIANGLE_STRIP; // buildBox(); - buildSphere(1.0f, _resolution, _resolution, _primitiveType); + buildSphere(1.0f, resolution, resolution, primitiveType); glClearColor(0.5f, 0.5f, 0.5f, 1); // Set the background color glEnable(GL_DEPTH_TEST); // Enables depth test @@ -292,14 +287,14 @@ deallocation of resources. void onClose(GLFWwindow* myWindow) { // Delete shaders & programs on GPU - glDeleteShader(_shaderVertID); - glDeleteShader(_shaderFragID); - glDeleteProgram(_shaderProgID); + glDeleteShader(shaderVertID); + glDeleteShader(shaderFragID); + glDeleteProgram(shaderProgID); // Delete arrays & buffers on GPU - glDeleteVertexArrays(1, &_vao); - glDeleteBuffers(1, &_vboV); - glDeleteBuffers(1, &_vboI); + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vboV); + glDeleteBuffers(1, &vboI); } //----------------------------------------------------------------------------- /*! @@ -313,34 +308,34 @@ bool onPaint() /* 2a) Camera transform: rotate the coordinate system increasingly * first around the y- and then around the x-axis. This type of camera * transform is called turntable animation.*/ - _cameraMatrix.identity(); - _cameraMatrix.rotate(_rotY + _deltaY, 0, 1, 0); - _cameraMatrix.rotate(_rotX + _deltaX, 1, 0, 0); + cameraMatrix.identity(); + cameraMatrix.rotate(rotY + (float)deltaY, 0, 1, 0); + cameraMatrix.rotate(rotX + (float)deltaX, 1, 0, 0); // 2c) Move the camera to its position. - _cameraMatrix.translate(0, 0, _camZ); + cameraMatrix.translate(0, 0, camZ); // 2c) View transform is world to camera (= inverse of camera matrix) - _viewMatrix = _cameraMatrix.inverted(); + viewMatrix = cameraMatrix.inverted(); // 3) Model transform: move the cube so that it rotates around its center - _modelMatrix.identity(); + modelMatrix.identity(); // 4) Lights get prepared here later on // 5) Pass the uniform variables to the shader - glUniformMatrix4fv(_pmLoc, 1, 0, (float*)&_projectionMatrix); - glUniformMatrix4fv(_vmLoc, 1, 0, (float*)&_viewMatrix); - glUniformMatrix4fv(_mmLoc, 1, 0, (float*)&_modelMatrix); - glUniform3f(_lightSpotDirVSLoc, 0.5f, 1.0f, 1.0f); // light direction in view space - glUniform4f(_lightDiffuseLoc, 1.0f, 1.0f, 1.0f, 1.0f); // diffuse light intensity (RGBA) - glUniform4f(_matDiffuseLoc, 1.0f, 0.0f, 0.0f, 1.0f); // diffuse material reflection (RGBA) + glUniformMatrix4fv(pmLoc, 1, 0, (float*)&projectionMatrix); + glUniformMatrix4fv(vmLoc, 1, 0, (float*)&viewMatrix); + glUniformMatrix4fv(mmLoc, 1, 0, (float*)&modelMatrix); + glUniform3f(lightSpotDirVSLoc, 0.5f, 1.0f, 1.0f); // light direction in view space + glUniform4f(lightDiffuseLoc, 1.0f, 1.0f, 1.0f, 1.0f); // diffuse light intensity (RGBA) + glUniform4f(matDiffuseLoc, 1.0f, 0.0f, 0.0f, 1.0f); // diffuse material reflection (RGBA) // 6) Activate the vertex array - glBindVertexArray(_vao); + glBindVertexArray(vao); // 7) Final draw call that draws the cube with triangles by indexes - glDrawElements(_primitiveType, _numI, GL_UNSIGNED_INT, 0); + glDrawElements(primitiveType, (GLint)numI, GL_UNSIGNED_INT, 0); // 8) Fast copy the back buffer to the front buffer. This is OS dependent. glfwSwapBuffers(window); @@ -350,12 +345,12 @@ bool onPaint() static float lastTimeSec = 0; float timeNowSec = (float)glfwGetTime(); float fps = calcFPS(timeNowSec - lastTimeSec); - string prim = _primitiveType == GL_TRIANGLES ? "GL_TRIANGLES" : "GL_TRIANGLE_STRIPS"; + string prim = primitiveType == GL_TRIANGLES ? "GL_TRIANGLES" : "GL_TRIANGLE_STRIPS"; snprintf(title, sizeof(title), "Sphere, %d x %d, fps: %4.0f, %s", - _resolution, - _resolution, + resolution, + resolution, fps, prim.c_str()); glfwSetWindowTitle(window, title); @@ -376,7 +371,7 @@ void onResize(GLFWwindow* myWindow, int width, int height) double h = (double)height; // define the projection matrix - _projectionMatrix.perspective(50.0f, (float)(w / h), 0.01f, 10.0f); + projectionMatrix.perspective(50.0f, (float)(w / h), 0.01f, 10.0f); // define the viewport glViewport(0, 0, width, height); @@ -389,14 +384,14 @@ Mouse button down & release eventhandler starts and end mouse rotation */ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) { - SLint x = _mouseX; - SLint y = _mouseY; + SLint x = mouseX; + SLint y = mouseY; - _mouseLeftDown = (action == GLFW_PRESS); - if (_mouseLeftDown) + mouseLeftDown = (action == GLFW_PRESS); + if (mouseLeftDown) { - _startX = x; - _startY = y; + startX = x; + startY = y; // Renders only the lines of a polygon during mouse moves if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -404,10 +399,10 @@ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) } else { - _rotX += _deltaX; - _rotY += _deltaY; - _deltaX = 0; - _deltaY = 0; + rotX += (float)deltaX; + rotY += (float)deltaY; + deltaX = 0; + deltaY = 0; // Renders filled polygons if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -420,13 +415,13 @@ Mouse move eventhandler tracks the mouse delta since touch down (_deltaX/_deltaY */ void onMouseMove(GLFWwindow* myWindow, double x, double y) { - _mouseX = (int)x; - _mouseY = (int)y; + mouseX = (int)x; + mouseY = (int)y; - if (_mouseLeftDown) + if (mouseLeftDown) { - _deltaY = (int)(_startX - x); - _deltaX = (int)(_startY - y); + deltaY = (int)(startX - x); + deltaX = (int)(startY - y); onPaint(); } } @@ -436,9 +431,9 @@ Mouse wheel eventhandler that moves the camera foreward or backwards */ void onMouseWheel(GLFWwindow* myWindow, double xscroll, double yscroll) { - if (_modifiers == NONE) + if (modifiers == NONE) { - _camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; + camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; onPaint(); } } @@ -457,25 +452,26 @@ void onKey(GLFWwindow* myWindow, int GLFWKey, int scancode, int action, int mods glfwSetWindowShouldClose(window, GL_TRUE); break; case GLFW_KEY_RIGHT: - _resolution = _resolution << 1; - _primitiveType = GL_TRIANGLE_STRIP; - buildSphere(1.0f, _resolution, _resolution, _primitiveType); + resolution = resolution << 1; + primitiveType = GL_TRIANGLE_STRIP; + buildSphere(1.0f, resolution, resolution, primitiveType); break; case GLFW_KEY_LEFT: - _primitiveType = GL_TRIANGLE_STRIP; - if (_resolution > 4) _resolution = _resolution >> 1; - buildSphere(1.0f, _resolution, _resolution, _primitiveType); + primitiveType = GL_TRIANGLE_STRIP; + if (resolution > 4) resolution = resolution >> 1; + buildSphere(1.0f, resolution, resolution, primitiveType); break; case GLFW_KEY_UP: - _resolution = _resolution << 1; - _primitiveType = GL_TRIANGLES; - buildSphere(1.0f, _resolution, _resolution, _primitiveType); + resolution = resolution << 1; + primitiveType = GL_TRIANGLES; + buildSphere(1.0f, resolution, resolution, primitiveType); break; case GLFW_KEY_DOWN: - _primitiveType = GL_TRIANGLES; - if (_resolution > 4) _resolution = _resolution >> 1; - buildSphere(1.0f, _resolution, _resolution, _primitiveType); + primitiveType = GL_TRIANGLES; + if (resolution > 4) resolution = resolution >> 1; + buildSphere(1.0f, resolution, resolution, primitiveType); break; + default: break; } } } @@ -559,13 +555,13 @@ The C main procedure running the GLFW GUI application. */ int main(int argc, char* argv[]) { - _projectRoot = SLstring(SL_PROJECT_ROOT); + projectRoot = SLstring(SL_PROJECT_ROOT); - _scrWidth = 640; - _scrHeight = 480; + scrWidth = 640; + scrHeight = 480; // Init OpenGL and the window library GLFW - initGLFW(_scrWidth, _scrHeight, "ColorSphere"); + initGLFW(scrWidth, scrHeight, "ColorSphere"); // Check errors before we start GETGLERROR; @@ -577,7 +573,7 @@ int main(int argc, char* argv[]) onInit(); // Call once resize to define the projection - onResize(window, _scrWidth, _scrHeight); + onResize(window, scrWidth, scrHeight); // Event loop while (!glfwWindowShouldClose(window)) diff --git a/apps/exercises/ch08_BlinnPhongLighting/BlinnPhongLighting.cpp b/apps/exercises/ch08_BlinnPhongLighting/BlinnPhongLighting.cpp index 90866ece..db6a43ab 100644 --- a/apps/exercises/ch08_BlinnPhongLighting/BlinnPhongLighting.cpp +++ b/apps/exercises/ch08_BlinnPhongLighting/BlinnPhongLighting.cpp @@ -5,7 +5,7 @@ * \date September 2012 (HS12) * \authors Marcus Hudritsch * \copyright http://opensource.org/licenses/GPL-3.0 -*/ + */ #include // OpenGL headers #include // GLFW GUI library @@ -29,74 +29,74 @@ struct VertexPN }; //----------------------------------------------------------------------------- // Global application variables -static GLFWwindow* window; //!< The global GLFW window handle -static SLstring _projectRoot; //!< Directory of executable -static SLint _scrWidth; //!< Window width at start up -static SLint _scrHeight; //!< Window height at start up - -static SLMat4f _cameraMatrix; //!< 4x4 matrix for camera to world transform -static SLMat4f _viewMatrix; //!< 4x4 matrix for world to camera transform -static SLMat4f _modelMatrix; //!< 4x4 matrix for model to world transform -static SLMat4f _lightMatrix; //!< 4x4 matrix for light to world transform -static SLMat4f _projectionMatrix; //!< Projection from view space to normalized device coordinates - -static GLuint _vao = 0; //!< ID of the vertex array object -static GLuint _vboV = 0; //!< ID of the VBO for vertex attributes -static GLuint _vboI = 0; //!< ID of the VBO for vertex index array -static GLuint _numV = 0; //!< NO. of vertices -static GLuint _numI = 0; //!< NO. of vertex indexes for triangles - -static float _camZ; //!< z-distance of camera -static float _rotX, _rotY; //!< rotation angles around x & y axis -static int _deltaX, _deltaY; //!< delta mouse motion -static int _startX, _startY; //!< x,y mouse start positions -static int _mouseX, _mouseY; //!< current mouse position -static bool _mouseLeftDown; //!< Flag if mouse is down -static GLuint _modifiers = 0; //!< modifier bit flags -static const GLuint NONE = 0; //!< constant for no modifier -static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier -static const GLuint CTRL = 0x00400000; //!< constant for control key modifier -static const GLuint ALT = 0x00800000; //!< constant for alt key modifier - -static SLVec4f _globalAmbi; //!< global ambient intensity -static SLVec4f _lightAmbi; //!< Light ambient intensity -static SLVec4f _lightDiff; //!< Light diffuse intensity -static SLVec4f _lightSpec; //!< Light specular intensity -static float _lightSpotDeg; //!< Light spot cutoff angle in degrees -static float _lightSpotExp; //!< Light spot exponent -static SLVec3f _lightAtt; //!< Light attenuation factors -static SLVec4f _matAmbi; //!< Material ambient reflection coeff. -static SLVec4f _matDiff; //!< Material diffuse reflection coeff. -static SLVec4f _matSpec; //!< Material specular reflection coeff. -static SLVec4f _matEmis; //!< Material emissive coeff. -static float _matShin; //!< Material shininess - -static GLuint _shaderVertID = 0; //! vertex shader id -static GLuint _shaderFragID = 0; //! fragment shader id -static GLuint _shaderProgID = 0; //! shader program id +static GLFWwindow* window; //!< The global GLFW window handle +static SLstring projectRoot; //!< Directory of executable +static SLint scrWidth; //!< Window width at start up +static SLint scrHeight; //!< Window height at start up + +static SLMat4f cameraMatrix; //!< 4x4 matrix for camera to world transform +static SLMat4f viewMatrix; //!< 4x4 matrix for world to camera transform +static SLMat4f modelMatrix; //!< 4x4 matrix for model to world transform +static SLMat4f lightMatrix; //!< 4x4 matrix for light to world transform +static SLMat4f projectionMatrix; //!< Projection from view space to normalized device coordinates + +static GLuint vao = 0; //!< ID of the vertex array object +static GLuint vboV = 0; //!< ID of the VBO for vertex attributes +static GLuint vboI = 0; //!< ID of the VBO for vertex index array +static GLuint numV = 0; //!< NO. of vertices +static GLuint numI = 0; //!< NO. of vertex indexes for triangles + +static float camZ; //!< z-distance of camera +static float rotX, rotY; //!< rotation angles around x & y axis +static int deltaX, deltaY; //!< delta mouse motion +static int startX, startY; //!< x,y mouse start positions +static int mouseX, mouseY; //!< current mouse position +static bool mouseLeftDown; //!< Flag if mouse is down +static GLuint modifiers = 0; //!< modifier bit flags +static const GLuint NONE = 0; //!< constant for no modifier +static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier +static const GLuint CTRL = 0x00400000; //!< constant for control key modifier +static const GLuint ALT = 0x00800000; //!< constant for alt key modifier + +static SLVec4f globalAmbi; //!< global ambient intensity +static SLVec4f lightAmbi; //!< Light ambient intensity +static SLVec4f lightDiff; //!< Light diffuse intensity +static SLVec4f lightSpec; //!< Light specular intensity +static float lightSpotDeg; //!< Light spot cutoff angle in degrees +static float lightSpotExp; //!< Light spot exponent +static SLVec3f lightAtt; //!< Light attenuation factors +static SLVec4f matAmbi; //!< Material ambient reflection coeff. +static SLVec4f matDiff; //!< Material diffuse reflection coeff. +static SLVec4f matSpec; //!< Material specular reflection coeff. +static SLVec4f matEmis; //!< Material emissive coeff. +static float matShin; //!< Material shininess + +static GLuint shaderVertID = 0; //! vertex shader id +static GLuint shaderFragID = 0; //! fragment shader id +static GLuint shaderProgID = 0; //! shader program id // Attribute & uniform variable location indexes -static GLint _pLoc; //!< attribute location for vertex position -static GLint _nLoc; //!< attribute location for vertex normal -static GLint _pmLoc; //!< uniform location for projection matrix -static GLint _vmLoc; //!< uniform location for view matrix -static GLint _mmLoc; //!< uniform location for model matrix - -static GLint _globalAmbiLoc; //!< uniform location for global ambient intensity -static GLint _lightPosVSLoc; //!< uniform location for light position in VS -static GLint _lightAmbiLoc; //!< uniform location for ambient light intensity -static GLint _lightDiffLoc; //!< uniform location for diffuse light intensity -static GLint _lightSpecLoc; //!< uniform location for specular light intensity -static GLint _lightSpotDirLoc; //!< uniform location for light direction in VS -static GLint _lightSpotDegLoc; //!< uniform location for spot cutoff angle in degrees -static GLint _lightSpotCosLoc; //!< uniform location for cosine of spot cutoff angle -static GLint _lightSpotExpLoc; //!< uniform location for cosine of spot cutoff angle -static GLint _lightAttLoc; //!< uniform location fpr light attenuation factors -static GLint _matAmbiLoc; //!< uniform location for ambient light reflection -static GLint _matDiffLoc; //!< uniform location for diffuse light reflection -static GLint _matSpecLoc; //!< uniform location for specular light reflection -static GLint _matEmisLoc; //!< uniform location for light emission -static GLint _matShinLoc; //!< uniform location for shininess +static GLint pLoc; //!< attribute location for vertex position +static GLint nLoc; //!< attribute location for vertex normal +static GLint pmLoc; //!< uniform location for projection matrix +static GLint vmLoc; //!< uniform location for view matrix +static GLint mmLoc; //!< uniform location for model matrix + +static GLint globalAmbiLoc; //!< uniform location for global ambient intensity +static GLint lightPosVSLoc; //!< uniform location for light position in VS +static GLint lightAmbiLoc; //!< uniform location for ambient light intensity +static GLint lightDiffLoc; //!< uniform location for diffuse light intensity +static GLint lightSpecLoc; //!< uniform location for specular light intensity +static GLint lightSpotDirLoc; //!< uniform location for light direction in VS +static GLint lightSpotDegLoc; //!< uniform location for spot cutoff angle in degrees +static GLint lightSpotCosLoc; //!< uniform location for cosine of spot cutoff angle +static GLint lightSpotExpLoc; //!< uniform location for cosine of spot cutoff angle +static GLint lightAttLoc; //!< uniform location fpr light attenuation factors +static GLint matAmbiLoc; //!< uniform location for ambient light reflection +static GLint matDiffLoc; //!< uniform location for diffuse light reflection +static GLint matSpecLoc; //!< uniform location for specular light reflection +static GLint matEmisLoc; //!< uniform location for light emission +static GLint matShinLoc; //!< uniform location for shininess //----------------------------------------------------------------------------- /*! @@ -106,8 +106,8 @@ void buildBox() { // create C arrays on heap // Define the vertex pos. and normals as an array of structure - _numV = 24; - VertexPN* vertices = new VertexPN[_numV]; + numV = 24; + std::vector vertices(numV); vertices[0].set(1, 1, 1, 1, 0, 0); vertices[1].set(1, 0, 1, 1, 0, 0); vertices[2].set(1, 0, 0, 1, 0, 0); @@ -134,64 +134,63 @@ void buildBox() vertices[23].set(0, 0, 1, 0, -1, 0); // Define the triangle indexes of the cubes vertices - _numI = 36; - GLuint* indices = new GLuint[_numI]; - int n = 0; - indices[n++] = 0; - indices[n++] = 1; - indices[n++] = 2; - indices[n++] = 0; - indices[n++] = 2; - indices[n++] = 3; - indices[n++] = 4; - indices[n++] = 5; - indices[n++] = 6; - indices[n++] = 4; - indices[n++] = 6; - indices[n++] = 7; - indices[n++] = 8; - indices[n++] = 9; - indices[n++] = 10; - indices[n++] = 8; - indices[n++] = 10; - indices[n++] = 11; - indices[n++] = 12; - indices[n++] = 13; - indices[n++] = 14; - indices[n++] = 12; - indices[n++] = 14; - indices[n++] = 15; - indices[n++] = 16; - indices[n++] = 17; - indices[n++] = 18; - indices[n++] = 16; - indices[n++] = 18; - indices[n++] = 19; - indices[n++] = 20; - indices[n++] = 21; - indices[n++] = 22; - indices[n++] = 20; - indices[n++] = 22; - indices[n++] = 23; + numI = 36; + std::vector indices(numI); + + int n = 0; + indices[n++] = 0; + indices[n++] = 1; + indices[n++] = 2; + indices[n++] = 0; + indices[n++] = 2; + indices[n++] = 3; + indices[n++] = 4; + indices[n++] = 5; + indices[n++] = 6; + indices[n++] = 4; + indices[n++] = 6; + indices[n++] = 7; + indices[n++] = 8; + indices[n++] = 9; + indices[n++] = 10; + indices[n++] = 8; + indices[n++] = 10; + indices[n++] = 11; + indices[n++] = 12; + indices[n++] = 13; + indices[n++] = 14; + indices[n++] = 12; + indices[n++] = 14; + indices[n++] = 15; + indices[n++] = 16; + indices[n++] = 17; + indices[n++] = 18; + indices[n++] = 16; + indices[n++] = 18; + indices[n++] = 19; + indices[n++] = 20; + indices[n++] = 21; + indices[n++] = 22; + indices[n++] = 20; + indices[n++] = 22; + indices[n++] = 23; // Generate the OpenGL vertex array object - glUtils::buildVAO(_vao, - _vboV, - _vboI, - vertices, - (GLint)_numV, + glUtils::buildVAO(vao, + vboV, + vboI, + vertices.data(), + (GLint)numV, sizeof(VertexPN), - indices, - (GLint)_numI, - sizeof(GL_UNSIGNED_INT), - (GLint)_shaderProgID, - _pLoc, + indices.data(), + (GLint)numI, + sizeof(unsigned int), + (GLint)shaderProgID, + pLoc, -1, - _nLoc); + nLoc); - // Delete arrays on heap. The data for rendering is now on the GPU - delete[] vertices; - delete[] indices; + // The vertices and indices get deleted here by the vectors (RAII) } //----------------------------------------------------------------------------- /*! @@ -201,60 +200,60 @@ should be called after a window with a valid OpenGL context is present. void onInit() { // Position of the camera - _camZ = 3; + camZ = 3; // Mouse rotation parameters - _rotX = _rotY = 0; - _deltaX = _deltaY = 0; - _mouseLeftDown = false; + rotX = rotY = 0; + deltaX = deltaY = 0; + mouseLeftDown = false; // Set light parameters - _globalAmbi.set(0.05f, 0.05f, 0.05f); - _lightAmbi.set(0.2f, 0.2f, 0.2f); - _lightDiff.set(1.0f, 1.0f, 1.0f); - _lightSpec.set(1.0f, 1.0f, 1.0f); - _lightMatrix.translate(0, 0, 3); - _lightSpotDeg = 10.0f; // 180.0f; // point light - _lightSpotExp = 1.0f; - _lightAtt = SLVec3f(1, 0, 0); // constant light attenuation = no attenuation - _matAmbi.set(1.0f, 0.0f, 0.0f); - _matDiff.set(1.0f, 0.0f, 0.0f); - _matSpec.set(1.0f, 1.0f, 1.0f); - _matEmis.set(0.0f, 0.0f, 0.0f); - _matShin = 1000.0f; + globalAmbi.set(0.05f, 0.05f, 0.05f); + lightAmbi.set(0.2f, 0.2f, 0.2f); + lightDiff.set(1.0f, 1.0f, 1.0f); + lightSpec.set(1.0f, 1.0f, 1.0f); + lightMatrix.translate(0, 0, 3); + lightSpotDeg = 10.0f; // 180.0f; // point light + lightSpotExp = 1.0f; + lightAtt = SLVec3f(1, 0, 0); // constant light attenuation = no attenuation + matAmbi.set(1.0f, 0.0f, 0.0f); + matDiff.set(1.0f, 0.0f, 0.0f); + matSpec.set(1.0f, 1.0f, 1.0f); + matEmis.set(0.0f, 0.0f, 0.0f); + matShin = 1000.0f; // Load, compile & link shaders - _shaderVertID = glUtils::buildShader(_projectRoot + "/data/shaders/ch08_BlinnPhongLighting.vert", GL_VERTEX_SHADER); - _shaderFragID = glUtils::buildShader(_projectRoot + "/data/shaders/ch08_BlinnPhongLighting.frag", GL_FRAGMENT_SHADER); - _shaderProgID = glUtils::buildProgram(_shaderVertID, _shaderFragID); + shaderVertID = glUtils::buildShader(projectRoot + "/data/shaders/ch08_BlinnPhongLighting.vert", GL_VERTEX_SHADER); + shaderFragID = glUtils::buildShader(projectRoot + "/data/shaders/ch08_BlinnPhongLighting.frag", GL_FRAGMENT_SHADER); + shaderProgID = glUtils::buildProgram(shaderVertID, shaderFragID); // Activate the shader program - glUseProgram(_shaderProgID); + glUseProgram(shaderProgID); // Get the variable locations (identifiers) within the program - _pLoc = glGetAttribLocation(_shaderProgID, "a_position"); - _nLoc = glGetAttribLocation(_shaderProgID, "a_normal"); - - _mmLoc = glGetUniformLocation(_shaderProgID, "u_mMatrix"); - _vmLoc = glGetUniformLocation(_shaderProgID, "u_vMatrix"); - _pmLoc = glGetUniformLocation(_shaderProgID, "u_pMatrix"); - - _lightPosVSLoc = glGetUniformLocation(_shaderProgID, "u_lightPosVS"); - _lightAmbiLoc = glGetUniformLocation(_shaderProgID, "u_lightAmbi"); - _lightDiffLoc = glGetUniformLocation(_shaderProgID, "u_lightDiff"); - _lightSpecLoc = glGetUniformLocation(_shaderProgID, "u_lightSpec"); - _lightSpotDirLoc = glGetUniformLocation(_shaderProgID, "u_lightSpotDir"); - _lightSpotDegLoc = glGetUniformLocation(_shaderProgID, "u_lightSpotDeg"); - _lightSpotCosLoc = glGetUniformLocation(_shaderProgID, "u_lightSpotCos"); - _lightSpotExpLoc = glGetUniformLocation(_shaderProgID, "u_lightSpotExp"); - _lightAttLoc = glGetUniformLocation(_shaderProgID, "u_lightAtt"); - _globalAmbiLoc = glGetUniformLocation(_shaderProgID, "u_globalAmbi"); - - _matAmbiLoc = glGetUniformLocation(_shaderProgID, "u_matAmbi"); - _matDiffLoc = glGetUniformLocation(_shaderProgID, "u_matDiff"); - _matSpecLoc = glGetUniformLocation(_shaderProgID, "u_matSpec"); - _matEmisLoc = glGetUniformLocation(_shaderProgID, "u_matEmis"); - _matShinLoc = glGetUniformLocation(_shaderProgID, "u_matShin"); + pLoc = glGetAttribLocation(shaderProgID, "a_position"); + nLoc = glGetAttribLocation(shaderProgID, "a_normal"); + + mmLoc = glGetUniformLocation(shaderProgID, "u_mMatrix"); + vmLoc = glGetUniformLocation(shaderProgID, "u_vMatrix"); + pmLoc = glGetUniformLocation(shaderProgID, "u_pMatrix"); + + lightPosVSLoc = glGetUniformLocation(shaderProgID, "u_lightPosVS"); + lightAmbiLoc = glGetUniformLocation(shaderProgID, "u_lightAmbi"); + lightDiffLoc = glGetUniformLocation(shaderProgID, "u_lightDiff"); + lightSpecLoc = glGetUniformLocation(shaderProgID, "u_lightSpec"); + lightSpotDirLoc = glGetUniformLocation(shaderProgID, "u_lightSpotDir"); + lightSpotDegLoc = glGetUniformLocation(shaderProgID, "u_lightSpotDeg"); + lightSpotCosLoc = glGetUniformLocation(shaderProgID, "u_lightSpotCos"); + lightSpotExpLoc = glGetUniformLocation(shaderProgID, "u_lightSpotExp"); + lightAttLoc = glGetUniformLocation(shaderProgID, "u_lightAtt"); + globalAmbiLoc = glGetUniformLocation(shaderProgID, "u_globalAmbi"); + + matAmbiLoc = glGetUniformLocation(shaderProgID, "u_matAmbi"); + matDiffLoc = glGetUniformLocation(shaderProgID, "u_matDiff"); + matSpecLoc = glGetUniformLocation(shaderProgID, "u_matSpec"); + matEmisLoc = glGetUniformLocation(shaderProgID, "u_matEmis"); + matShinLoc = glGetUniformLocation(shaderProgID, "u_matShin"); buildBox(); @@ -271,14 +270,14 @@ deallocation of resources. void onClose(GLFWwindow* myWindow) { // Delete shaders & programs on GPU - glDeleteShader(_shaderVertID); - glDeleteShader(_shaderFragID); - glDeleteProgram(_shaderProgID); + glDeleteShader(shaderVertID); + glDeleteShader(shaderFragID); + glDeleteProgram(shaderProgID); // Delete arrays & buffers on GPU - glDeleteVertexArrays(1, &_vao); - glDeleteBuffers(1, &_vboV); - glDeleteBuffers(1, &_vboI); + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vboV); + glDeleteBuffers(1, &vboI); } //----------------------------------------------------------------------------- /*! @@ -292,54 +291,54 @@ bool onPaint() /* 2b) Model transform: rotate the coordinate system increasingly * first around the y- and then around the x-axis. This type of camera * transform is called turntable animation.*/ - _cameraMatrix.identity(); - _cameraMatrix.rotate(_rotY + _deltaY, 0, 1, 0); - _cameraMatrix.rotate(_rotX + _deltaX, 1, 0, 0); + cameraMatrix.identity(); + cameraMatrix.rotate(rotY + (float)deltaY, 0, 1, 0); + cameraMatrix.rotate(rotX + (float)deltaX, 1, 0, 0); // 2a) Move the camera to its position. - _cameraMatrix.translate(0, 0, _camZ); + cameraMatrix.translate(0, 0, camZ); // 2c) View transform is world to camera (= inverse of camera matrix) - _viewMatrix = _cameraMatrix.inverted(); + viewMatrix = cameraMatrix.inverted(); // 3) Model transform: move the cube so that it rotates around its center - _modelMatrix.identity(); - _modelMatrix.translate(-0.5f, -0.5f, -0.5f); + modelMatrix.identity(); + modelMatrix.translate(-0.5f, -0.5f, -0.5f); // 4a) Transform light position into view space - SLVec4f lightPosVS = _viewMatrix * SLVec4f(_lightMatrix.translation()); + SLVec4f lightPosVS = viewMatrix * SLVec4f(lightMatrix.translation()); // 4b) The spotlight direction is down the negative z-axis of the light transform - SLVec3f lightSpotDirVS = _viewMatrix.mat3() * -_lightMatrix.axisZ(); + SLVec3f lightSpotDirVS = viewMatrix.mat3() * -lightMatrix.axisZ(); // 5) Activate the shader program and pass the uniform variables to the shader - glUseProgram(_shaderProgID); - glUniformMatrix4fv(_pmLoc, 1, 0, (float*)&_projectionMatrix); - glUniformMatrix4fv(_vmLoc, 1, 0, (float*)&_viewMatrix); - glUniformMatrix4fv(_mmLoc, 1, 0, (float*)&_modelMatrix); - - glUniform4fv(_globalAmbiLoc, 1, (float*)&_globalAmbi); - glUniform4fv(_lightPosVSLoc, 1, (float*)&lightPosVS); - glUniform4fv(_lightAmbiLoc, 1, (float*)&_lightAmbi); - glUniform4fv(_lightDiffLoc, 1, (float*)&_lightDiff); - glUniform4fv(_lightSpecLoc, 1, (float*)&_lightSpec); - glUniform3fv(_lightSpotDirLoc, 1, (float*)&lightSpotDirVS); - glUniform3fv(_lightAttLoc, 1, (float*)&_lightAtt); - glUniform1f(_lightSpotDegLoc, _lightSpotDeg); - glUniform1f(_lightSpotCosLoc, cos(_lightSpotDeg * Utils::DEG2RAD)); - glUniform1f(_lightSpotExpLoc, _lightSpotExp); - - glUniform4fv(_matAmbiLoc, 1, (float*)&_matAmbi); - glUniform4fv(_matDiffLoc, 1, (float*)&_matDiff); - glUniform4fv(_matSpecLoc, 1, (float*)&_matSpec); - glUniform4fv(_matEmisLoc, 1, (float*)&_matEmis); - glUniform1f(_matShinLoc, _matShin); + glUseProgram(shaderProgID); + glUniformMatrix4fv(pmLoc, 1, 0, (float*)&projectionMatrix); + glUniformMatrix4fv(vmLoc, 1, 0, (float*)&viewMatrix); + glUniformMatrix4fv(mmLoc, 1, 0, (float*)&modelMatrix); + + glUniform4fv(globalAmbiLoc, 1, (float*)&globalAmbi); + glUniform4fv(lightPosVSLoc, 1, (float*)&lightPosVS); + glUniform4fv(lightAmbiLoc, 1, (float*)&lightAmbi); + glUniform4fv(lightDiffLoc, 1, (float*)&lightDiff); + glUniform4fv(lightSpecLoc, 1, (float*)&lightSpec); + glUniform3fv(lightSpotDirLoc, 1, (float*)&lightSpotDirVS); + glUniform3fv(lightAttLoc, 1, (float*)&lightAtt); + glUniform1f(lightSpotDegLoc, lightSpotDeg); + glUniform1f(lightSpotCosLoc, cos(lightSpotDeg * Utils::DEG2RAD)); + glUniform1f(lightSpotExpLoc, lightSpotExp); + + glUniform4fv(matAmbiLoc, 1, (float*)&matAmbi); + glUniform4fv(matDiffLoc, 1, (float*)&matDiff); + glUniform4fv(matSpecLoc, 1, (float*)&matSpec); + glUniform4fv(matEmisLoc, 1, (float*)&matEmis); + glUniform1f(matShinLoc, matShin); // 6) Activate the vertex array - glBindVertexArray(_vao); + glBindVertexArray(vao); // 7) Final draw call that draws the cube with triangles by indexes - glDrawElements(GL_TRIANGLES, (GLsizei)_numI, GL_UNSIGNED_INT, nullptr); + glDrawElements(GL_TRIANGLES, (GLsizei)numI, GL_UNSIGNED_INT, nullptr); // 8) Fast copy the back buffer to the front buffer. This is OS dependent. glfwSwapBuffers(window); @@ -360,7 +359,7 @@ void onResize(GLFWwindow* myWindow, int width, int height) float h = (float)height; // define the projection matrix - _projectionMatrix.perspective(50, w / h, 0.01f, 10.0f); + projectionMatrix.perspective(50, w / h, 0.01f, 10.0f); // define the viewport glViewport(0, 0, width, height); @@ -375,14 +374,14 @@ Mouse button down & release eventhandler starts and end mouse rotation */ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) { - SLint x = _mouseX; - SLint y = _mouseY; + SLint x = mouseX; + SLint y = mouseY; - _mouseLeftDown = (action == GLFW_PRESS); - if (_mouseLeftDown) + mouseLeftDown = (action == GLFW_PRESS); + if (mouseLeftDown) { - _startX = x; - _startY = y; + startX = x; + startY = y; // Renders only the lines of a polygon during mouse moves if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -390,10 +389,10 @@ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) } else { - _rotX += _deltaX; - _rotY += _deltaY; - _deltaX = 0; - _deltaY = 0; + rotX += (float)deltaX; + rotY += (float)deltaY; + deltaX = 0; + deltaY = 0; // Renders filled polygons if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -406,13 +405,13 @@ Mouse move eventhandler tracks the mouse delta since touch down (_deltaX/_deltaY */ void onMouseMove(GLFWwindow* myWindow, double x, double y) { - _mouseX = (int)x; - _mouseY = (int)y; + mouseX = (int)x; + mouseY = (int)y; - if (_mouseLeftDown) + if (mouseLeftDown) { - _deltaY = (int)(_startX - x); - _deltaX = (int)(_startY - y); + deltaY = (int)(startX - x); + deltaX = (int)(startY - y); onPaint(); } } @@ -422,26 +421,26 @@ Mouse wheel eventhandler that moves the camera forward or backwards */ void onMouseWheel(GLFWwindow* myWindow, double xscroll, double yscroll) { - if (_modifiers == NONE) + if (modifiers == NONE) { - _camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; + camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; onPaint(); } - else if (_modifiers == ALT) + else if (modifiers == ALT) { - _matShin *= yscroll > 0.0 ? 1.5f : 0.75f; + matShin *= yscroll > 0.0 ? 1.5f : 0.75f; onPaint(); } - else if (_modifiers & ALT && _modifiers & CTRL) + else if (modifiers & ALT && modifiers & CTRL) { - _lightSpotExp += yscroll > 0.0 ? 10.0f : -10.0f; - _lightSpotExp = Utils::clamp(_lightSpotExp, 0.0f, 200.0f); + lightSpotExp += yscroll > 0.0 ? 10.0f : -10.0f; + lightSpotExp = Utils::clamp(lightSpotExp, 0.0f, 200.0f); onPaint(); } - else if (_modifiers == CTRL) + else if (modifiers == CTRL) { - _lightSpotDeg += yscroll > 0.0 ? 1.0f : -1.0f; - _lightSpotDeg = Utils::clamp(_lightSpotDeg, 0.0f, 180.0f); + lightSpotDeg += yscroll > 0.0 ? 1.0f : -1.0f; + lightSpotDeg = Utils::clamp(lightSpotDeg, 0.0f, 180.0f); onPaint(); } } @@ -459,24 +458,26 @@ void onKey(GLFWwindow* myWindow, int GLFWKey, int scancode, int action, int mods onClose(window); glfwSetWindowShouldClose(window, GL_TRUE); break; - case GLFW_KEY_LEFT_SHIFT: _modifiers = _modifiers | SHIFT; break; - case GLFW_KEY_RIGHT_SHIFT: _modifiers = _modifiers | SHIFT; break; - case GLFW_KEY_LEFT_CONTROL: _modifiers = _modifiers | CTRL; break; - case GLFW_KEY_RIGHT_CONTROL: _modifiers = _modifiers | CTRL; break; - case GLFW_KEY_LEFT_ALT: _modifiers = _modifiers | ALT; break; - case GLFW_KEY_RIGHT_ALT: _modifiers = _modifiers | ALT; break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: modifiers = modifiers | SHIFT; break; + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: modifiers = modifiers | CTRL; break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: modifiers = modifiers | ALT; break; + default: break; } } else if (action == GLFW_RELEASE) { switch (GLFWKey) { - case GLFW_KEY_LEFT_SHIFT: _modifiers = _modifiers ^ SHIFT; break; - case GLFW_KEY_RIGHT_SHIFT: _modifiers = _modifiers ^ SHIFT; break; - case GLFW_KEY_LEFT_CONTROL: _modifiers = _modifiers ^ CTRL; break; - case GLFW_KEY_RIGHT_CONTROL: _modifiers = _modifiers ^ CTRL; break; - case GLFW_KEY_LEFT_ALT: _modifiers = _modifiers ^ ALT; break; - case GLFW_KEY_RIGHT_ALT: _modifiers = _modifiers ^ ALT; break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: modifiers = modifiers ^ SHIFT; break; + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: modifiers = modifiers ^ CTRL; break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: modifiers = modifiers ^ ALT; break; + default: break; } } } @@ -559,13 +560,13 @@ The C main procedure running the GLFW GUI application. */ int main(int argc, char* argv[]) { - _projectRoot = SLstring(SL_PROJECT_ROOT); + projectRoot = SLstring(SL_PROJECT_ROOT); - _scrWidth = 640; - _scrHeight = 480; + scrWidth = 640; + scrHeight = 480; // Init OpenGL and the window library GLFW - initGLFW(_scrWidth, _scrHeight, "Blinn-Phong-Lighting"); + initGLFW(scrWidth, scrHeight, "Blinn-Phong-Lighting"); // Check errors before we start GETGLERROR; @@ -577,7 +578,7 @@ int main(int argc, char* argv[]) onInit(); // Call once resize to define the projection - onResize(window, _scrWidth, _scrHeight); + onResize(window, scrWidth, scrHeight); // Event loop while (!glfwWindowShouldClose(window)) diff --git a/apps/exercises/ch09_TextureMapping/TextureMapping.cpp b/apps/exercises/ch09_TextureMapping/TextureMapping.cpp index 735433cf..7561c1d3 100644 --- a/apps/exercises/ch09_TextureMapping/TextureMapping.cpp +++ b/apps/exercises/ch09_TextureMapping/TextureMapping.cpp @@ -25,75 +25,75 @@ struct VertexPNT SLVec2f t; // vertex texture coord. [s,t] }; //----------------------------------------------------------------------------- -static GLFWwindow* window; //!< The global glfw window handle -static SLstring _projectRoot; //!< Directory of executable -static SLint _scrWidth; //!< Window width at start up -static SLint _scrHeight; //!< Window height at start up +static GLFWwindow* window; //!< The global glfw window handle +static SLstring projectRoot; //!< Directory of executable +static SLint scrWidth; //!< Window width at start up +static SLint scrHeight; //!< Window height at start up // Global application variables -static SLMat4f _cameraMatrix; //!< 4x4 matrix for camera to world transform -static SLMat4f _viewMatrix; //!< 4x4 matrix for world to camera transform -static SLMat4f _modelMatrix; //!< 4x4 matrix for model to world transform -static SLMat4f _lightMatrix; //!< 4x4 matrix for light to world transform -static SLMat4f _projectionMatrix; //!< Projection from view space to normalized device coordinates - -static GLuint _vao = 0; //!< ID of the vertex array object -static GLuint _vboV = 0; //!< ID of the VBO for vertex attributes -static GLuint _vboI = 0; //!< ID of the VBO for vertex index array -static GLuint _numV = 0; //!< NO. of vertices -static GLuint _numI = 0; //!< NO. of vertex indexes for triangles - -static GLint _resolution; //!< resolution of sphere stack & slices - -static float _camZ; //!< z-distance of camera -static float _rotX, _rotY; //!< rotation angles around x & y axis -static int _deltaX, _deltaY; //!< delta mouse motion -static int _startX, _startY; //!< x,y mouse start positions -static int _mouseX, _mouseY; //!< current mouse position -static bool _mouseLeftDown; //!< Flag if mouse is down -static GLuint _modifiers = 0; //!< modifier bit flags -static const GLuint NONE = 0; //!< constant for no modifier -static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier -static const GLuint CTRL = 0x00400000; //!< constant for control key modifier -static const GLuint ALT = 0x00800000; //!< constant for alt key modifier - -static SLVec4f _globalAmbi; //!< global ambient intensity -static SLVec4f _lightAmbient; //!< Light ambient intensity -static SLVec4f _lightDiffuse; //!< Light diffuse intensity -static SLVec4f _lightSpecular; //!< Light specular intensity -static float _lightSpotDeg; //!< Light spot cutoff angle in degrees -static float _lightSpotExp; //!< Light spot exponent -static SLVec3f _lightAtt; //!< Light attenuation factors -static SLVec4f _matAmbient; //!< Material ambient reflection coeff. -static SLVec4f _matDiffuse; //!< Material diffuse reflection coeff. -static SLVec4f _matSpecular; //!< Material specular reflection coeff. -static SLVec4f _matEmissive; //!< Material emissive coeff. -static float _matShininess; //!< Material shininess exponent - -static GLuint _shaderVertID = 0; //!< vertex shader id -static GLuint _shaderFragID = 0; //!< fragment shader id -static GLuint _shaderProgID = 0; //!< shader program id -static GLuint _textureID = 0; //!< texture id - -static GLint _pLoc; //!< attribute location for vertex position -static GLint _nLoc; //!< attribute location for vertex normal -static GLint _uvLoc; //!< attribute location for vertex texcoords -static GLint _pmLoc; //!< uniform location for projection matrix -static GLint _vmLoc; //!< uniform location for view matrix -static GLint _mmLoc; //!< uniform location for model matrix -static GLint _globalAmbiLoc; //!< uniform location for global ambient intensity -static GLint _lightPosVSLoc; //!< uniform location for light position in VS -static GLint _lightSpotDirVSLoc; //!< uniform location for light direction in VS -static GLint _lightAmbientLoc; //!< uniform location for ambient light intensity -static GLint _lightDiffuseLoc; //!< uniform location for diffuse light intensity -static GLint _lightSpecularLoc; //!< uniform location for specular light intensity -static GLint _lightAttLoc; //!< uniform location fpr light attenuation factors -static GLint _matAmbientLoc; //!< uniform location for ambient light reflection -static GLint _matDiffuseLoc; //!< uniform location for diffuse light reflection -static GLint _matSpecularLoc; //!< uniform location for specular light reflection -static GLint _matEmissiveLoc; //!< uniform location for light emission -static GLint _matShininessLoc; //!< uniform location for shininess -static GLint _matTexDiffLoc; //!< uniform location for texture 0 +static SLMat4f cameraMatrix; //!< 4x4 matrix for camera to world transform +static SLMat4f viewMatrix; //!< 4x4 matrix for world to camera transform +static SLMat4f modelMatrix; //!< 4x4 matrix for model to world transform +static SLMat4f lightMatrix; //!< 4x4 matrix for light to world transform +static SLMat4f projectionMatrix; //!< Projection from view space to normalized device coordinates + +static GLuint vao = 0; //!< ID of the vertex array object +static GLuint vboV = 0; //!< ID of the VBO for vertex attributes +static GLuint vboI = 0; //!< ID of the VBO for vertex index array +static GLuint numV = 0; //!< NO. of vertices +static GLuint numI = 0; //!< NO. of vertex indexes for triangles + +static GLint resolution; //!< resolution of sphere stack & slices + +static float camZ; //!< z-distance of camera +static float rotX, rotY; //!< rotation angles around x & y axis +static int deltaX, deltaY; //!< delta mouse motion +static int startX, startY; //!< x,y mouse start positions +static int mouseX, mouseY; //!< current mouse position +static bool mouseLeftDown; //!< Flag if mouse is down +static GLuint modifiers = 0; //!< modifier bit flags +static const GLuint NONE = 0; //!< constant for no modifier +static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifier +static const GLuint CTRL = 0x00400000; //!< constant for control key modifier +static const GLuint ALT = 0x00800000; //!< constant for alt key modifier + +static SLVec4f globalAmbi; //!< global ambient intensity +static SLVec4f lightAmbient; //!< Light ambient intensity +static SLVec4f lightDiffuse; //!< Light diffuse intensity +static SLVec4f lightSpecular; //!< Light specular intensity +static float lightSpotDeg; //!< Light spot cutoff angle in degrees +static float lightSpotExp; //!< Light spot exponent +static SLVec3f lightAtt; //!< Light attenuation factors +static SLVec4f matAmbient; //!< Material ambient reflection coeff. +static SLVec4f matDiffuse; //!< Material diffuse reflection coeff. +static SLVec4f matSpecular; //!< Material specular reflection coeff. +static SLVec4f matEmissive; //!< Material emissive coeff. +static float matShininess; //!< Material shininess exponent + +static GLuint shaderVertID = 0; //!< vertex shader id +static GLuint shaderFragID = 0; //!< fragment shader id +static GLuint shaderProgID = 0; //!< shader program id +static GLuint textureID = 0; //!< texture id + +static GLint pLoc; //!< attribute location for vertex position +static GLint nLoc; //!< attribute location for vertex normal +static GLint uvLoc; //!< attribute location for vertex texcoords +static GLint pmLoc; //!< uniform location for projection matrix +static GLint vmLoc; //!< uniform location for view matrix +static GLint mmLoc; //!< uniform location for model matrix +static GLint globalAmbiLoc; //!< uniform location for global ambient intensity +static GLint lightPosVSLoc; //!< uniform location for light position in VS +static GLint lightSpotDirVSLoc; //!< uniform location for light direction in VS +static GLint lightAmbientLoc; //!< uniform location for ambient light intensity +static GLint lightDiffuseLoc; //!< uniform location for diffuse light intensity +static GLint lightSpecularLoc; //!< uniform location for specular light intensity +static GLint lightAttLoc; //!< uniform location fpr light attenuation factors +static GLint matAmbientLoc; //!< uniform location for ambient light reflection +static GLint matDiffuseLoc; //!< uniform location for diffuse light reflection +static GLint matSpecularLoc; //!< uniform location for specular light reflection +static GLint matEmissiveLoc; //!< uniform location for light emission +static GLint matShininessLoc; //!< uniform location for shininess +static GLint matTexDiffLoc; //!< uniform location for texture 0 //----------------------------------------------------------------------------- /*! @@ -114,32 +114,28 @@ void buildSphere(float radius, int stacks, int slices, GLuint primitveType) // z = r*cos(theta); // Create vertex array - VertexPNT* vertices = 0; //!< Array of vertices + std::vector vertices; // ??? // create Index array - GLuint* indices = 0; + std::vector indices; // ??? // Delete arrays on heap. The data for rendering is now on the GPU - if (vertices && indices) + if (vertices.size() && indices.size()) { - glUtils::buildVAO(_vao, - _vboV, - _vboI, - vertices, - _numV, + glUtils::buildVAO(vao, + vboV, + vboI, + vertices.data(), + (GLint)numV, sizeof(VertexPNT), - indices, - _numI, - sizeof(GL_UNSIGNED_INT), - _shaderProgID, - _pLoc, - _nLoc); - - // Delete arrays on heap - delete[] vertices; - delete[] indices; + indices.data(), + (GLint)numI, + sizeof(unsigned int), + (GLint)shaderProgID, + pLoc, + nLoc); } else std::cout << "**** You have to define some vertices and indices first in buildSphere! ****" << std::endl; @@ -153,32 +149,32 @@ void buildSquare() { // create vertex array for interleaved position, normal and texCoord // Position, Normal, texCrd, - _numV = 4; + numV = 4; // clang-format off - float vertices[] = {-1, 0,-1, 0,-1, 0, 0, 0, // Vertex 0 - 1, 0,-1, 0,-1, 0, 1, 0, // Vertex 1 - 1, 0, 1, 0,-1, 0, 1, 1, // Vertex 2 - -1, 0, 1, 0,-1, 0, 0, 1}; // Vertex 3 + float vertices[] = { -1, 0, -1, 0,-1, 0, 0, 0, // Vertex 0 + 1, 0, -1, 0,-1, 0, 1, 0, // Vertex 1 + 1, 0, 1, 0,-1, 0, 1, 1, // Vertex 2 + -1, 0, 1, 0,-1, 0, 0, 1}; // Vertex 3 // clang-format on // create index array for GL_TRIANGLES - _numI = 6; + numI = 6; GLuint indices[] = {0, 1, 2, 0, 2, 3}; // Generate the OpenGL vertex array object - glUtils::buildVAO(_vao, - _vboV, - _vboI, + glUtils::buildVAO(vao, + vboV, + vboI, vertices, - (GLint)_numV, + (GLint)numV, sizeof(VertexPNT), indices, - (GLint)_numI, - sizeof(GL_UNSIGNED_INT), - (GLint)_shaderProgID, - _pLoc, - _nLoc, - _uvLoc); + (GLint)numI, + sizeof(unsigned int), + (GLint)shaderProgID, + pLoc, + nLoc, + uvLoc); // The vertices and indices are on the stack memory and get deleted at the // end of the block. @@ -213,61 +209,61 @@ should be called after a window with a valid OpenGL context is present. void onInit() { // Set light parameters - _globalAmbi.set(0.0f, 0.0f, 0.0f); - _lightAmbient.set(0.1f, 0.1f, 0.1f); - _lightDiffuse.set(1.0f, 1.0f, 1.0f); - _lightSpecular.set(1.0f, 1.0f, 1.0f); - _lightMatrix.translate(0, 0, 3); - _lightSpotDeg = 180.0f; // point light - _lightSpotExp = 1.0f; - _lightAtt = SLVec3f(1, 0, 0); // constant light attenuation = no attenuation - _matAmbient.set(1.0f, 1.0f, 1.0f); - _matDiffuse.set(1.0f, 1.0f, 1.0f); - _matSpecular.set(1.0f, 1.0f, 1.0f); - _matEmissive.set(0.0f, 0.0f, 0.0f); - _matShininess = 500.0f; + globalAmbi.set(0.0f, 0.0f, 0.0f); + lightAmbient.set(0.1f, 0.1f, 0.1f); + lightDiffuse.set(1.0f, 1.0f, 1.0f); + lightSpecular.set(1.0f, 1.0f, 1.0f); + lightMatrix.translate(0, 0, 3); + lightSpotDeg = 180.0f; // point light + lightSpotExp = 1.0f; + lightAtt = SLVec3f(1, 0, 0); // constant light attenuation = no attenuation + matAmbient.set(1.0f, 1.0f, 1.0f); + matDiffuse.set(1.0f, 1.0f, 1.0f); + matSpecular.set(1.0f, 1.0f, 1.0f); + matEmissive.set(0.0f, 0.0f, 0.0f); + matShininess = 500.0f; // position of the camera - _camZ = 3.0f; + camZ = 3.0f; // Mouse rotation parameters - _rotX = 0; - _rotY = 0; - _deltaX = 0; - _deltaY = 0; - _mouseLeftDown = false; + rotX = 0; + rotY = 0; + deltaX = 0; + deltaY = 0; + mouseLeftDown = false; // Load textures - _textureID = glUtils::buildTexture(_projectRoot + "/data/images/textures/earth2048_C.png"); + textureID = glUtils::buildTexture(projectRoot + "/data/images/textures/earth2048_C.png"); // Load, compile & link shaders - _shaderVertID = glUtils::buildShader(_projectRoot + "/data/shaders/ch09_TextureMapping.vert", GL_VERTEX_SHADER); - _shaderFragID = glUtils::buildShader(_projectRoot + "/data/shaders/ch09_TextureMapping.frag", GL_FRAGMENT_SHADER); - _shaderProgID = glUtils::buildProgram(_shaderVertID, _shaderFragID); + shaderVertID = glUtils::buildShader(projectRoot + "/data/shaders/ch09_TextureMapping.vert", GL_VERTEX_SHADER); + shaderFragID = glUtils::buildShader(projectRoot + "/data/shaders/ch09_TextureMapping.frag", GL_FRAGMENT_SHADER); + shaderProgID = glUtils::buildProgram(shaderVertID, shaderFragID); // Activate the shader program - glUseProgram(_shaderProgID); + glUseProgram(shaderProgID); // Get the variable locations (identifiers) within the vertex & pixel shader programs - _pLoc = glGetAttribLocation(_shaderProgID, "a_position"); - _nLoc = glGetAttribLocation(_shaderProgID, "a_normal"); - _uvLoc = glGetAttribLocation(_shaderProgID, "a_uv"); - _pmLoc = glGetUniformLocation(_shaderProgID, "u_pMatrix"); - _vmLoc = glGetUniformLocation(_shaderProgID, "u_vMatrix"); - _mmLoc = glGetUniformLocation(_shaderProgID, "u_mMatrix"); - _globalAmbiLoc = glGetUniformLocation(_shaderProgID, "u_globalAmbi"); - _lightPosVSLoc = glGetUniformLocation(_shaderProgID, "u_lightPosVS"); - _lightSpotDirVSLoc = glGetUniformLocation(_shaderProgID, "u_lightSpotDir"); - _lightAmbientLoc = glGetUniformLocation(_shaderProgID, "u_lightAmbi"); - _lightDiffuseLoc = glGetUniformLocation(_shaderProgID, "u_lightDiff"); - _lightSpecularLoc = glGetUniformLocation(_shaderProgID, "u_lightSpec"); - _lightAttLoc = glGetUniformLocation(_shaderProgID, "u_lightAtt"); - _matAmbientLoc = glGetUniformLocation(_shaderProgID, "u_matAmbi"); - _matDiffuseLoc = glGetUniformLocation(_shaderProgID, "u_matDiff"); - _matSpecularLoc = glGetUniformLocation(_shaderProgID, "u_matSpec"); - _matEmissiveLoc = glGetUniformLocation(_shaderProgID, "u_matEmis"); - _matShininessLoc = glGetUniformLocation(_shaderProgID, "u_matShin"); - _matTexDiffLoc = glGetUniformLocation(_shaderProgID, "u_matTexDiff"); + pLoc = glGetAttribLocation(shaderProgID, "a_position"); + nLoc = glGetAttribLocation(shaderProgID, "a_normal"); + uvLoc = glGetAttribLocation(shaderProgID, "a_uv"); + pmLoc = glGetUniformLocation(shaderProgID, "u_pMatrix"); + vmLoc = glGetUniformLocation(shaderProgID, "u_vMatrix"); + mmLoc = glGetUniformLocation(shaderProgID, "u_mMatrix"); + globalAmbiLoc = glGetUniformLocation(shaderProgID, "u_globalAmbi"); + lightPosVSLoc = glGetUniformLocation(shaderProgID, "u_lightPosVS"); + lightSpotDirVSLoc = glGetUniformLocation(shaderProgID, "u_lightSpotDir"); + lightAmbientLoc = glGetUniformLocation(shaderProgID, "u_lightAmbi"); + lightDiffuseLoc = glGetUniformLocation(shaderProgID, "u_lightDiff"); + lightSpecularLoc = glGetUniformLocation(shaderProgID, "u_lightSpec"); + lightAttLoc = glGetUniformLocation(shaderProgID, "u_lightAtt"); + matAmbientLoc = glGetUniformLocation(shaderProgID, "u_matAmbi"); + matDiffuseLoc = glGetUniformLocation(shaderProgID, "u_matDiff"); + matSpecularLoc = glGetUniformLocation(shaderProgID, "u_matSpec"); + matEmissiveLoc = glGetUniformLocation(shaderProgID, "u_matEmis"); + matShininessLoc = glGetUniformLocation(shaderProgID, "u_matShin"); + matTexDiffLoc = glGetUniformLocation(shaderProgID, "u_matTexDiff"); // Build object buildSquare(); @@ -286,13 +282,13 @@ deallocation of resources. void onClose(GLFWwindow* myWindow) { // Delete shaders & programs on GPU - glDeleteShader(_shaderVertID); - glDeleteShader(_shaderFragID); - glDeleteProgram(_shaderProgID); + glDeleteShader(shaderVertID); + glDeleteShader(shaderFragID); + glDeleteProgram(shaderProgID); // Delete arrays & buffers on GPU - glDeleteBuffers(1, &_vboV); - glDeleteBuffers(1, &_vboI); + glDeleteBuffers(1, &vboV); + glDeleteBuffers(1, &vboI); } //----------------------------------------------------------------------------- /*! @@ -307,50 +303,50 @@ bool onPaint() /* 2a) Model transform: rotate the coordinate system increasingly * first around the y- and then around the x-axis. This type of camera * transform is called turntable animation.*/ - _cameraMatrix.identity(); - _cameraMatrix.rotate(_rotY + _deltaY, 0, 1, 0); - _cameraMatrix.rotate(_rotX + _deltaX, 1, 0, 0); + cameraMatrix.identity(); + cameraMatrix.rotate(rotY + (float)deltaY, 0, 1, 0); + cameraMatrix.rotate(rotX + (float)deltaX, 1, 0, 0); // 2b) Move the camera to its position. - _cameraMatrix.translate(0, 0, _camZ); + cameraMatrix.translate(0, 0, camZ); // 2c) View transform is world to camera (= inverse of camera matrix) - _viewMatrix = _cameraMatrix.inverted(); + viewMatrix = cameraMatrix.inverted(); // 3a) Rotate the model so that we see the square from the front side or the earth from the equator. - _modelMatrix.identity(); - _modelMatrix.rotate(90, -1, 0, 0); + modelMatrix.identity(); + modelMatrix.rotate(90, -1, 0, 0); // 4a) Transform light position into view space - SLVec4f lightPosVS = _viewMatrix * SLVec4f(_lightMatrix.translation()); + SLVec4f lightPosVS = viewMatrix * SLVec4f(lightMatrix.translation()); // 4b) The spotlight direction is down the negative z-axis of the light transform - SLVec3f lightSpotDirVS = _viewMatrix.mat3() * -_lightMatrix.axisZ(); + SLVec3f lightSpotDirVS = viewMatrix.mat3() * -lightMatrix.axisZ(); // 5) Activate the shader program and pass the uniform variables to the shader - glUseProgram(_shaderProgID); - glUniformMatrix4fv(_pmLoc, 1, 0, (float*)&_projectionMatrix); - glUniformMatrix4fv(_vmLoc, 1, 0, (float*)&_viewMatrix); - glUniformMatrix4fv(_mmLoc, 1, 0, (float*)&_modelMatrix); - glUniform4fv(_globalAmbiLoc, 1, (float*)&_globalAmbi); - glUniform3fv(_lightPosVSLoc, 1, (float*)&lightPosVS); - glUniform3fv(_lightSpotDirVSLoc, 1, (float*)&lightSpotDirVS); - glUniform4fv(_lightAmbientLoc, 1, (float*)&_lightAmbient); - glUniform4fv(_lightDiffuseLoc, 1, (float*)&_lightDiffuse); - glUniform4fv(_lightSpecularLoc, 1, (float*)&_lightSpecular); - glUniform3fv(_lightAttLoc, 1, (float*)&_lightAtt); - glUniform4fv(_matAmbientLoc, 1, (float*)&_matAmbient); - glUniform4fv(_matDiffuseLoc, 1, (float*)&_matDiffuse); - glUniform4fv(_matSpecularLoc, 1, (float*)&_matSpecular); - glUniform4fv(_matEmissiveLoc, 1, (float*)&_matEmissive); - glUniform1f(_matShininessLoc, _matShininess); - glUniform1i(_matTexDiffLoc, 0); + glUseProgram(shaderProgID); + glUniformMatrix4fv(pmLoc, 1, 0, (float*)&projectionMatrix); + glUniformMatrix4fv(vmLoc, 1, 0, (float*)&viewMatrix); + glUniformMatrix4fv(mmLoc, 1, 0, (float*)&modelMatrix); + glUniform4fv(globalAmbiLoc, 1, (float*)&globalAmbi); + glUniform3fv(lightPosVSLoc, 1, (float*)&lightPosVS); + glUniform3fv(lightSpotDirVSLoc, 1, (float*)&lightSpotDirVS); + glUniform4fv(lightAmbientLoc, 1, (float*)&lightAmbient); + glUniform4fv(lightDiffuseLoc, 1, (float*)&lightDiffuse); + glUniform4fv(lightSpecularLoc, 1, (float*)&lightSpecular); + glUniform3fv(lightAttLoc, 1, (float*)&lightAtt); + glUniform4fv(matAmbientLoc, 1, (float*)&matAmbient); + glUniform4fv(matDiffuseLoc, 1, (float*)&matDiffuse); + glUniform4fv(matSpecularLoc, 1, (float*)&matSpecular); + glUniform4fv(matEmissiveLoc, 1, (float*)&matEmissive); + glUniform1f(matShininessLoc, matShininess); + glUniform1i(matTexDiffLoc, 0); // 6) Activate the vertex array - glBindVertexArray(_vao); + glBindVertexArray(vao); // 7) Draw model triangles by indexes - glDrawElements(GL_TRIANGLES, (GLsizei)_numI, GL_UNSIGNED_INT, nullptr); + glDrawElements(GL_TRIANGLES, (GLsizei)numI, GL_UNSIGNED_INT, nullptr); // 8) Fast copy the back buffer to the front buffer. This is OS dependent. glfwSwapBuffers(window); @@ -382,10 +378,10 @@ void onResize(GLFWwindow* myWindow, int width, int height) float h = (float)height; // define the projection matrix - _projectionMatrix.perspective(45, - w / h, - 0.01f, - 10.0f); + projectionMatrix.perspective(45, + w / h, + 0.01f, + 10.0f); // define the viewport glViewport(0, 0, width, height); @@ -398,14 +394,14 @@ Mouse button down & release eventhandler starts and end mouse rotation */ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) { - SLint x = _mouseX; - SLint y = _mouseY; + SLint x = mouseX; + SLint y = mouseY; - _mouseLeftDown = (action == GLFW_PRESS); - if (_mouseLeftDown) + mouseLeftDown = (action == GLFW_PRESS); + if (mouseLeftDown) { - _startX = x; - _startY = y; + startX = x; + startY = y; // Renders only the lines of a polygon during mouse moves if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -413,10 +409,10 @@ void onMouseButton(GLFWwindow* myWindow, int button, int action, int mods) } else { - _rotX += _deltaX; - _rotY += _deltaY; - _deltaX = 0; - _deltaY = 0; + rotX += (float)deltaX; + rotY += (float)deltaY; + deltaX = 0; + deltaY = 0; // Renders filled polygons if (button == GLFW_MOUSE_BUTTON_RIGHT) @@ -429,13 +425,13 @@ Mouse move eventhandler tracks the mouse delta since touch down (_deltaX/_deltaY */ void onMouseMove(GLFWwindow* myWindow, double x, double y) { - _mouseX = (int)x; - _mouseY = (int)y; + mouseX = (int)x; + mouseY = (int)y; - if (_mouseLeftDown) + if (mouseLeftDown) { - _deltaY = (int)(_startX - x); - _deltaX = (int)(_startY - y); + deltaY = (int)(startX - x); + deltaX = (int)(startY - y); onPaint(); } } @@ -445,9 +441,9 @@ Mouse wheel eventhandler that moves the camera foreward or backwards */ void onMouseWheel(GLFWwindow* myWindow, double xscroll, double yscroll) { - if (_modifiers == NONE) + if (modifiers == NONE) { - _camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; + camZ += (SLfloat)Utils::sign(yscroll) * 0.1f; onPaint(); } } @@ -473,24 +469,26 @@ void onKey(GLFWwindow* myWindow, int GLFWKey, int scancode, int action, int mods //if (_resolution > 4) _resolution = _resolution >> 1; //buildSphere(1.0f, (GLuint)_resolution, (GLuint)_resolution); break; - case GLFW_KEY_LEFT_SHIFT: _modifiers = _modifiers | SHIFT; break; - case GLFW_KEY_RIGHT_SHIFT: _modifiers = _modifiers | SHIFT; break; - case GLFW_KEY_LEFT_CONTROL: _modifiers = _modifiers | CTRL; break; - case GLFW_KEY_RIGHT_CONTROL: _modifiers = _modifiers | CTRL; break; - case GLFW_KEY_LEFT_ALT: _modifiers = _modifiers | ALT; break; - case GLFW_KEY_RIGHT_ALT: _modifiers = _modifiers | ALT; break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: modifiers = modifiers | SHIFT; break; + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: modifiers = modifiers | CTRL; break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: modifiers = modifiers | ALT; break; + default: break; } } else if (action == GLFW_RELEASE) { switch (GLFWKey) { - case GLFW_KEY_LEFT_SHIFT: _modifiers = _modifiers ^ SHIFT; break; - case GLFW_KEY_RIGHT_SHIFT: _modifiers = _modifiers ^ SHIFT; break; - case GLFW_KEY_LEFT_CONTROL: _modifiers = _modifiers ^ CTRL; break; - case GLFW_KEY_RIGHT_CONTROL: _modifiers = _modifiers ^ CTRL; break; - case GLFW_KEY_LEFT_ALT: _modifiers = _modifiers ^ ALT; break; - case GLFW_KEY_RIGHT_ALT: _modifiers = _modifiers ^ ALT; break; + case GLFW_KEY_LEFT_SHIFT: + case GLFW_KEY_RIGHT_SHIFT: modifiers = modifiers ^ SHIFT; break; + case GLFW_KEY_LEFT_CONTROL: + case GLFW_KEY_RIGHT_CONTROL: modifiers = modifiers ^ CTRL; break; + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: modifiers = modifiers ^ ALT; break; + default: break; } } } @@ -573,13 +571,13 @@ The C main procedure running the GLFW GUI application. */ int main(int argc, char* argv[]) { - _projectRoot = SLstring(SL_PROJECT_ROOT); + projectRoot = SLstring(SL_PROJECT_ROOT); - _scrWidth = 640; - _scrHeight = 480; + scrWidth = 640; + scrHeight = 480; // Init OpenGL and the window library GLFW - initGLFW(_scrWidth, _scrHeight, "TextureMapping"); + initGLFW(scrWidth, scrHeight, "TextureMapping"); // Check errors before we start GETGLERROR; @@ -591,7 +589,7 @@ int main(int argc, char* argv[]) onInit(); // Call once resize to define the projection - onResize(window, _scrWidth, _scrHeight); + onResize(window, scrWidth, scrHeight); // Event loop while (!glfwWindowShouldClose(window)) diff --git a/apps/exercises/cv02_CalderonFilter/cv02_CalderonFilter.cpp b/apps/exercises/cv02_CalderonFilter/cv02_CalderonFilter.cpp index 78923156..94c26f87 100644 --- a/apps/exercises/cv02_CalderonFilter/cv02_CalderonFilter.cpp +++ b/apps/exercises/cv02_CalderonFilter/cv02_CalderonFilter.cpp @@ -3,9 +3,8 @@ * \details Minimal OpenCV app for the Instagram Calderon filter * \copyright Based on Satya Mallick's Tutorial at https://www.learnopencv.com * \date Spring 2018 -*/ + */ -#include #include #include diff --git a/apps/exercises/cv06_WarpTriangle/cv06_WarpTriangle.cpp b/apps/exercises/cv06_WarpTriangle/cv06_WarpTriangle.cpp index b8552cf2..c1dadde5 100644 --- a/apps/exercises/cv06_WarpTriangle/cv06_WarpTriangle.cpp +++ b/apps/exercises/cv06_WarpTriangle/cv06_WarpTriangle.cpp @@ -3,7 +3,7 @@ * \brief Minimal OpenCV application that warps a triangle into another * \copyright Based on Satya Mallick's Tutorial at https://www.learnopencv.com * \date Spring 2018 -*/ + */ #include #include @@ -27,11 +27,14 @@ void warpTriangle(Mat& img1, vector tri2CroppedInt; for (uint i = 0; i < 3; i++) { - tri1Cropped.push_back(Point2f(tri1[i].x - rect1.x, tri1[i].y - rect1.y)); - tri2Cropped.push_back(Point2f(tri2[i].x - rect2.x, tri2[i].y - rect2.y)); + tri1Cropped.push_back(Point2f(tri1[i].x - (float)rect1.x, + tri1[i].y - (float)rect1.y)); + tri2Cropped.push_back(Point2f(tri2[i].x - (float)rect2.x, + tri2[i].y - (float)rect2.y)); // fillConvexPoly needs a vector of int Point and not Point2f - tri2CroppedInt.push_back(Point((int)tri2Cropped[i].x, (int)tri2Cropped[i].y)); + tri2CroppedInt.push_back(Point((int)tri2Cropped[i].x, + (int)tri2Cropped[i].y)); } // Apply warpImage to small rectangular patches @@ -42,7 +45,9 @@ void warpTriangle(Mat& img1, Mat warpMat = getAffineTransform(tri1Cropped, tri2Cropped); // Apply the Affine Transform just found to the src image - Mat img2Cropped = Mat::zeros(rect2.height, rect2.width, img1Cropped.type()); + Mat img2Cropped = Mat::zeros(rect2.height, + rect2.width, + img1Cropped.type()); warpAffine(img1Cropped, img2Cropped, warpMat, @@ -51,14 +56,24 @@ void warpTriangle(Mat& img1, BORDER_REFLECT_101); // Create white triangle mask - Mat mask = Mat::zeros(rect2.height, rect2.width, CV_32FC3); - fillConvexPoly(mask, tri2CroppedInt, Scalar(1.0, 1.0, 1.0), LINE_AA, 0); + Mat mask = Mat::zeros(rect2.height, + rect2.width, + CV_32FC3); + fillConvexPoly(mask, + tri2CroppedInt, + Scalar(1.0, 1.0, 1.0), + LINE_AA, + 0); // Delete all outside of warped triangle - multiply(img2Cropped, mask, img2Cropped); + multiply(img2Cropped, + mask, + img2Cropped); // Delete all inside the target triangle - multiply(img2(rect2), Scalar(1.0, 1.0, 1.0) - mask, img2(rect2)); + multiply(img2(rect2), + Scalar(1.0, 1.0, 1.0) - mask, + img2(rect2)); // Add warped triangle to target image img2(rect2) = img2(rect2) + img2Cropped; @@ -119,8 +134,18 @@ int main() } // Draw triangles in input and output images - polylines(imgIn, triInInt, true, color, 1, LINE_AA); - polylines(imgOut, triOutInt, true, color, 1, LINE_AA); + polylines(imgIn, + triInInt, + true, + color, + 1, + LINE_AA); + polylines(imgOut, + triOutInt, + true, + color, + 1, + LINE_AA); string title1 = "Input"; imshow(title1, imgIn); diff --git a/apps/exercises/cv07_MeshWarping/cv07_MeshWarping.cpp b/apps/exercises/cv07_MeshWarping/cv07_MeshWarping.cpp index d02dec8c..b08bdb62 100644 --- a/apps/exercises/cv07_MeshWarping/cv07_MeshWarping.cpp +++ b/apps/exercises/cv07_MeshWarping/cv07_MeshWarping.cpp @@ -4,7 +4,7 @@ * \copyright Based on Satya Mallick's Tutorial: * https://www.learnopencv.com/warp-one-triangle-to-another-using-opencv-c-python * \date Spring 2018 -*/ + */ #include #include @@ -134,11 +134,14 @@ void warpTriangle(Mat& img1, vector tri2CroppedInt; for (uint i = 0; i < 3; i++) { - tri1Cropped.push_back(Point2f(tri1[i].x - rect1.x, tri1[i].y - rect1.y)); - tri2Cropped.push_back(Point2f(tri2[i].x - rect2.x, tri2[i].y - rect2.y)); + tri1Cropped.push_back(Point2f(tri1[i].x - (float)rect1.x, + tri1[i].y - (float)rect1.y)); + tri2Cropped.push_back(Point2f(tri2[i].x - (float)rect2.x, + tri2[i].y - (float)rect2.y)); // fillConvexPoly needs a vector of int Point and not Point2f - tri2CroppedInt.push_back(Point((int)tri2Cropped[i].x, (int)tri2Cropped[i].y)); + tri2CroppedInt.push_back(Point((int)tri2Cropped[i].x, + (int)tri2Cropped[i].y)); } // Apply warpImage to small rectangular patches @@ -149,7 +152,9 @@ void warpTriangle(Mat& img1, Mat warpMat = getAffineTransform(tri1Cropped, tri2Cropped); // Apply the Affine Transform just found to the src image - Mat img2Cropped = Mat::zeros(rect2.height, rect2.width, img1Cropped.type()); + Mat img2Cropped = Mat::zeros(rect2.height, + rect2.width, + img1Cropped.type()); warpAffine(img1Cropped, img2Cropped, warpMat, @@ -159,13 +164,19 @@ void warpTriangle(Mat& img1, // Create white triangle mask Mat mask = Mat::zeros(rect2.height, rect2.width, CV_32FC3); - fillConvexPoly(mask, tri2CroppedInt, Scalar(1.0, 1.0, 1.0), LINE_AA, 0); + fillConvexPoly(mask, + tri2CroppedInt, + Scalar(1.0, 1.0, 1.0), + LINE_AA, + 0); // Delete all outside of warped triangle multiply(img2Cropped, mask, img2Cropped); // Delete all inside the target triangle - multiply(img2(rect2), Scalar(1.0, 1.0, 1.0) - mask, img2(rect2)); + multiply(img2(rect2), + Scalar(1.0, 1.0, 1.0) - mask, + img2(rect2)); // Add warped triangle to target image img2(rect2) = img2(rect2) + img2Cropped; @@ -230,8 +241,8 @@ int main() // Keep bounding rectangle around face points Size size = img_orig.size(); Rect rectFace = boundingRect(points); - Point2f center(rectFace.x + rectFace.width * 0.5f, - rectFace.y + rectFace.height * 0.5f); + Point2f center((float)rectFace.x + (float)rectFace.width * 0.5f, + (float)rectFace.y + (float)rectFace.height * 0.5f); // Add image border points points.push_back(Point2d(0, 0)); @@ -249,12 +260,22 @@ int main() // Create and draw the Delaunay triangulation vector> triIndexes1; - createDelaunay(img1, subdiv, points, true, triIndexes1); + createDelaunay(img1, + subdiv, + points, + true, + triIndexes1); // drawDelaunay(img1, subdiv, Scalar(255, 255, 255)); // Draw all points red for (Point2f p : points) - circle(img1, p, 3, Scalar(0, 0, 255), FILLED, LINE_AA, 0); + circle(img1, + p, + 3, + Scalar(0, 0, 255), + FILLED, + LINE_AA, + 0); // Allocate space for voronoi Diagram Mat img_voronoi = Mat::zeros(img1.rows, img1.cols, CV_8UC3); @@ -285,7 +306,12 @@ int main() wPoints[i] = ((points[i] - center) * scale) + center; // Warp all triangles - warpImage(img_orig, imgW, points, wPoints, triIndexes1); + warpImage(img_orig, + imgW, + points, + wPoints, + triIndexes1); + imshow("Warped Image", imgW); // Wait for key to exit loop diff --git a/apps/exercises/cv08_MeshMorphing/cv08_MeshMorphing.cpp b/apps/exercises/cv08_MeshMorphing/cv08_MeshMorphing.cpp index 46337b12..28999103 100644 --- a/apps/exercises/cv08_MeshMorphing/cv08_MeshMorphing.cpp +++ b/apps/exercises/cv08_MeshMorphing/cv08_MeshMorphing.cpp @@ -3,7 +3,7 @@ * \brief Minimal OpenCV application that morphs two triangular meshes * \copyright Based on Satya Mallick's Tutorial at https://www.learnopencv.com * \date Spring 2018 -*/ + */ #include #include @@ -157,10 +157,10 @@ void morphTriangle(Mat& img1, vector tMRectInt; // for fillConvexPoly we need ints for (uint i = 0; i < 3; i++) { - tMRectFlt.push_back(Point2f(tM[i].x - rM.x, tM[i].y - rM.y)); - tMRectInt.push_back(Point2i((int)(tM[i].x - rM.x), (int)(tM[i].y - rM.y))); - t1RectFlt.push_back(Point2f(t1[i].x - r1.x, t1[i].y - r1.y)); - t2RectFlt.push_back(Point2f(t2[i].x - r2.x, t2[i].y - r2.y)); + tMRectFlt.push_back(Point2f(tM[i].x - (float)rM.x, tM[i].y - (float)rM.y)); + tMRectInt.push_back(Point2i((int)(tM[i].x - (float)rM.x), (int)(tM[i].y - (float)rM.y))); + t1RectFlt.push_back(Point2f(t1[i].x - (float)r1.x, t1[i].y - (float)r1.y)); + t2RectFlt.push_back(Point2f(t2[i].x - (float)r2.x, t2[i].y - (float)r2.y)); } // Create white triangle mask @@ -175,8 +175,14 @@ void morphTriangle(Mat& img1, Mat warpImage1 = Mat::zeros(rM.height, rM.width, img1Rect.type()); Mat warpImage2 = Mat::zeros(rM.height, rM.width, img2Rect.type()); - applyAffineTransform(warpImage1, img1Rect, t1RectFlt, tMRectFlt); - applyAffineTransform(warpImage2, img2Rect, t2RectFlt, tMRectFlt); + applyAffineTransform(warpImage1, + img1Rect, + t1RectFlt, + tMRectFlt); + applyAffineTransform(warpImage2, + img2Rect, + t2RectFlt, + tMRectFlt); // Alpha blend rectangular patches into new image Mat imgRect = (1.0f - alpha) * warpImage1 + alpha * warpImage2; @@ -185,7 +191,9 @@ void morphTriangle(Mat& img1, multiply(imgRect, mask, imgRect); // Delete all inside the target triangle - multiply(imgM(rM), Scalar(1.0f, 1.0f, 1.0f) - mask, imgM(rM)); + multiply(imgM(rM), + Scalar(1.0f, 1.0f, 1.0f) - mask, + imgM(rM)); // Add morphed triangle to target image imgM(rM) = imgM(rM) + imgRect; diff --git a/apps/exercises/cv13_FaceTracking/cv13_FaceTracking.cpp b/apps/exercises/cv13_FaceTracking/cv13_FaceTracking.cpp index 4bdd025e..f591e038 100644 --- a/apps/exercises/cv13_FaceTracking/cv13_FaceTracking.cpp +++ b/apps/exercises/cv13_FaceTracking/cv13_FaceTracking.cpp @@ -3,13 +3,12 @@ * \brief Minimal OpenCV application for face Tracking in video * \copyright Based on Satya Mallick's Tutorial at https://www.learnopencv.com * \date Authumn 2017 -*/ + */ #include #include #include -#include #include using namespace std; @@ -46,7 +45,10 @@ void detectFaceAndDisplay(Mat frame) for (size_t i = 0; i < faces.size(); i++) { - rectangle(frame, faces[i], Scalar(255, 0, 255), 2); + rectangle(frame, + faces[i], + Scalar(255, 0, 255), + 2); Mat faceROI = frame_gray(faces[i]); std::vector eyes; @@ -63,7 +65,10 @@ void detectFaceAndDisplay(Mat frame) { eyes[j].x += faces[i].x; eyes[j].y += faces[i].y; - rectangle(frame, eyes[j], Scalar(255, 0, 0), 2); + rectangle(frame, + eyes[j], + Scalar(255, 0, 0), + 2); } } diff --git a/apps/exercises/cv13_FacialLandmarkDetection/cv13_FacialLandmarkDetection.cpp b/apps/exercises/cv13_FacialLandmarkDetection/cv13_FacialLandmarkDetection.cpp index c88ef836..acac5b53 100644 --- a/apps/exercises/cv13_FacialLandmarkDetection/cv13_FacialLandmarkDetection.cpp +++ b/apps/exercises/cv13_FacialLandmarkDetection/cv13_FacialLandmarkDetection.cpp @@ -3,7 +3,7 @@ * \brief Minimal OpenCV app for facial landmark detection without dlib * \copyright Based on Satya Mallick's Tutorial at https://www.learnopencv.com * \date Authumn 2017 -*/ + */ #include #include @@ -44,11 +44,17 @@ int main() // Detect faces vector faces; - int min = (int)(frame.rows * 0.4f); // the bigger min the faster - int max = (int)(frame.rows * 0.8f); // the smaller max the faster + int min = (int)((float)frame.rows * 0.4f); // the bigger min the faster + int max = (int)((float)frame.rows * 0.8f); // the smaller max the faster cv::Size minSize(min, min); cv::Size maxSize(max, max); - faceDetector.detectMultiScale(gray, faces, 1.1, 3, 0, minSize, maxSize); + faceDetector.detectMultiScale(gray, + faces, + 1.1, + 3, + 0, + minSize, + maxSize); // Variable for landmarks. // Landmarks for one face is a vector of points @@ -63,9 +69,16 @@ int main() { for (uint i = 0; i < landmarks.size(); i++) { - rectangle(frame, faces[i], cv::Scalar(255, 0, 0), 2); + rectangle(frame, + faces[i], + cv::Scalar(255, 0, 0), + 2); for (auto& j : landmarks[i]) - circle(frame, j, 3, cv::Scalar(0, 0, 255), -1); + circle(frame, + j, + 3, + cv::Scalar(0, 0, 255), + -1); } } diff --git a/apps/exercises/cv13_HeadPoseEstimation/cv13_HeadPoseEstimation.cpp b/apps/exercises/cv13_HeadPoseEstimation/cv13_HeadPoseEstimation.cpp index 6daa8f3c..89f9a12d 100644 --- a/apps/exercises/cv13_HeadPoseEstimation/cv13_HeadPoseEstimation.cpp +++ b/apps/exercises/cv13_HeadPoseEstimation/cv13_HeadPoseEstimation.cpp @@ -3,7 +3,7 @@ * \brief Minimal OpenCV app for head pose estimation * \copyright Based on Satya Mallick's Tutorial at https://www.learnopencv.com * \date Authumn 2017 -*/ + */ #include @@ -84,7 +84,11 @@ int main() // Draw red dots on all image points for (auto& image_point : image_points) - circle(image, image_point, 3, Scalar(0, 0, 255), -1); + circle(image, + image_point, + 3, + Scalar(0, 0, 255), + -1); // Draw blue nose line line(image, diff --git a/apps/exercises/cv13_Snapchat2D/cv13_Snapchat2D.cpp b/apps/exercises/cv13_Snapchat2D/cv13_Snapchat2D.cpp index 74e0a1b1..e5c78c56 100644 --- a/apps/exercises/cv13_Snapchat2D/cv13_Snapchat2D.cpp +++ b/apps/exercises/cv13_Snapchat2D/cv13_Snapchat2D.cpp @@ -1,8 +1,8 @@ /** - * \file cv13_Snapchat2D.cpp + * \file cv13_Snapchat2D.cpp * \brief Minimal OpenCV app for a 2D Snapchatfilter -// Taken from Satya Mallic on: http://www.learnopencv.com - * \date Authumn 2017 + Taken from Satya Mallic on: http://www.learnopencv.com + * \date Authumn 2017 */ #include @@ -109,8 +109,8 @@ void warpTriangle(Mat& img1, vector tri2CroppedInt; for (uint i = 0; i < 3; i++) { - tri1Cropped.emplace_back(tri1[i].x - rect1.x, tri1[i].y - rect1.y); - tri2Cropped.emplace_back(tri2[i].x - rect2.x, tri2[i].y - rect2.y); + tri1Cropped.emplace_back(tri1[i].x - (float)rect1.x, tri1[i].y - (float)rect1.y); + tri2Cropped.emplace_back(tri2[i].x - (float)rect2.x, tri2[i].y - (float)rect2.y); // fillConvexPoly needs a vector of int Point and not Point2f tri2CroppedInt.emplace_back((int)tri2Cropped[i].x, (int)tri2Cropped[i].y); @@ -199,8 +199,8 @@ int main() // Detect faces vector faces; - int min = (int)(frame.rows * 0.4f); // the bigger min the faster - int max = (int)(frame.rows * 0.8f); // the smaller max the faster + int min = (int)((float)frame.rows * 0.4f); // the bigger min the faster + int max = (int)((float)frame.rows * 0.8f); // the smaller max the faster cv::Size minSize(min, min); cv::Size maxSize(max, max); faceDetector.detectMultiScale(gray, faces, 1.1, 3, 0, minSize, maxSize);