From 3e6547ec4dded0690646c35919c8c3e753799a52 Mon Sep 17 00:00:00 2001 From: Marcus Hudritsch Date: Fri, 1 Nov 2024 09:26:02 +0100 Subject: [PATCH] Added more comments to glUtils --- .../ch07_DiffuseSphere/DiffuseSphere.cpp | 51 ++-- .../ch09_TextureMapping/TextureMapping.cpp | 234 +++++++----------- apps/exercises/glUtils.cpp | 48 +++- 3 files changed, 158 insertions(+), 175 deletions(-) diff --git a/apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp b/apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp index a7120d9c..0c295ac5 100644 --- a/apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp +++ b/apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp @@ -5,7 +5,7 @@ * \date December 2015 (HS15) * \authors Marcus Hudritsch * \copyright http://opensource.org/licenses/GPL-3.0 -*/ + */ #include // OpenGL headers #include // GLFW GUI library @@ -29,24 +29,24 @@ 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 +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 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 _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 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 @@ -60,9 +60,9 @@ static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifie 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 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 @@ -201,7 +201,18 @@ void buildSphere(float radius, int stacks, int slices, GLuint primitveType) // Delete arrays on heap. The data for rendering is now on the GPU if (vertices && indices) { - glUtils::buildVAO(_vao, _vboV, _vboI, vertices, _numV, sizeof(VertexPN), indices, _numI, sizeof(GL_UNSIGNED_INT), _shaderProgID, _pLoc, _nLoc); + glUtils::buildVAO(_vao, + _vboV, + _vboI, + vertices, + _numV, + sizeof(VertexPN), + indices, + _numI, + sizeof(GL_UNSIGNED_INT), + _shaderProgID, + _pLoc, + _nLoc); // Delete arrays on heap delete[] vertices; diff --git a/apps/exercises/ch09_TextureMapping/TextureMapping.cpp b/apps/exercises/ch09_TextureMapping/TextureMapping.cpp index 87e61507..735433cf 100644 --- a/apps/exercises/ch09_TextureMapping/TextureMapping.cpp +++ b/apps/exercises/ch09_TextureMapping/TextureMapping.cpp @@ -7,7 +7,7 @@ * \copyright http://opensource.org/licenses/GPL-3.0 * \remarks Please use clangformat to format the code. See more code style on * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style -*/ + */ #include #include // OpenGL headers @@ -31,19 +31,19 @@ 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 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 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 GLint _resolution; //!< resolution of sphere stack & slices static float _camZ; //!< z-distance of camera static float _rotX, _rotY; //!< rotation angles around x & y axis @@ -57,43 +57,43 @@ static const GLuint SHIFT = 0x00200000; //!< constant for shift key modifie 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 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 //----------------------------------------------------------------------------- /*! @@ -101,105 +101,48 @@ buildSphere creates the vertex attributes for a sphere and creates the VBO at the end. The sphere is built in stacks & slices. The slices are around the z-axis. */ -void buildSphere(float radius, GLuint stacks, GLuint slices) +void buildSphere(float radius, int stacks, int slices, GLuint primitveType) { assert(stacks > 3 && slices > 3); + assert(primitveType == GL_TRIANGLES || primitveType == GL_TRIANGLE_STRIP); - // create vertex array - GLuint numV = (stacks + 1) * (slices + 1); - VertexPNT* v = new VertexPNT[numV]; - - float theta, dtheta; // angles around x-axis - float phi, dphi; // angles around z-axis - float s, t, ds, dt; // texture coords - int i, j; // loop counters - GLuint iv = 0; - - // init start values - theta = 0.0f; - dtheta = Utils::PI / stacks; - dphi = 2.0f * Utils::PI / slices; - ds = 0.0f; // ??? - dt = 0.0f; // ??? - t = 0.0f; // ??? - - // Define vertex position & normals by looping through all stacks - for (i = 0; i <= (int)stacks; ++i) - { - float sin_theta = sin(theta); - float cos_theta = cos(theta); - phi = s = 0.0f; - - // Loop through all slices - for (j = 0; j <= (int)slices; ++j) - { - if (j == (int)slices) phi = 0.0f; - - // define first the normal with length 1 - v[iv].n.x = sin_theta * cos(phi); - v[iv].n.y = sin_theta * sin(phi); - v[iv].n.z = cos_theta; - - // set the vertex position w. the scaled normal - v[iv].p.x = radius * v[iv].n.x; - v[iv].p.y = radius * v[iv].n.y; - v[iv].p.z = radius * v[iv].n.z; + // Spherical to cartesian coordinates + // dtheta = PI / stacks; + // dphi = 2 * PI / slices; + // x = r*sin(theta)*cos(phi); + // y = r*sin(theta)*sin(phi); + // z = r*cos(theta); - // set the texture coords. - v[iv].t.x = s; - v[iv].t.y = t; + // Create vertex array + VertexPNT* vertices = 0; //!< Array of vertices + // ??? - phi += dphi; - //s = ??? - iv++; - } - theta += dtheta; - //t = ??? - } + // create Index array + GLuint* indices = 0; + // ??? - // create index array for triangles - _numI = (GLuint)(slices * stacks * 2 * 3); - GLuint* indices = new GLuint[_numI]; - GLuint ii = 0, iV1, iV2; - - for (i = 0; i < (int)stacks; ++i) + // Delete arrays on heap. The data for rendering is now on the GPU + if (vertices && indices) { - // index of 1st & 2nd vertex of stack - iV1 = i * (slices + 1); - iV2 = iV1 + slices + 1; - - for (j = 0; j < (int)slices; ++j) - { - // 1st triangle ccw - indices[ii++] = iV1 + j; - indices[ii++] = iV2 + j; - indices[ii++] = iV2 + j + 1; - // 2nd triangle ccw - indices[ii++] = iV1 + j; - indices[ii++] = iV2 + j + 1; - indices[ii++] = iV1 + j + 1; - } + glUtils::buildVAO(_vao, + _vboV, + _vboI, + vertices, + _numV, + sizeof(VertexPNT), + indices, + _numI, + sizeof(GL_UNSIGNED_INT), + _shaderProgID, + _pLoc, + _nLoc); + + // Delete arrays on heap + delete[] vertices; + delete[] indices; } - - // Generate the OpenGL vertex array object - glUtils::buildVAO(_vao, - _vboV, - _vboI, - v, - (GLint)numV, - sizeof(VertexPNT), - indices, - (GLint)_numI, - sizeof(GL_UNSIGNED_INT), - (GLint)_shaderProgID, - _pLoc, - -1, - _nLoc, - _uvLoc); - - // Delete arrays on heap - delete[] v; - delete[] indices; + else + std::cout << "**** You have to define some vertices and indices first in buildSphere! ****" << std::endl; } //----------------------------------------------------------------------------- /*! @@ -275,7 +218,7 @@ void onInit() _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 + _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); @@ -327,7 +270,6 @@ void onInit() _matTexDiffLoc = glGetUniformLocation(_shaderProgID, "u_matTexDiff"); // Build object - // buildSphere(1.0f, 72, 72); buildSquare(); // Set some OpenGL states @@ -524,12 +466,12 @@ void onKey(GLFWwindow* myWindow, int GLFWKey, int scancode, int action, int mods glfwSetWindowShouldClose(window, GL_TRUE); break; case GLFW_KEY_UP: - _resolution = _resolution << 1; - buildSphere(1.0f, (GLuint)_resolution, (GLuint)_resolution); + //_resolution = _resolution << 1; + //buildSphere(1.0f, (GLuint)_resolution, (GLuint)_resolution); break; case GLFW_KEY_DOWN: - if (_resolution > 4) _resolution = _resolution >> 1; - buildSphere(1.0f, (GLuint)_resolution, (GLuint)_resolution); + //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; diff --git a/apps/exercises/glUtils.cpp b/apps/exercises/glUtils.cpp index a7390a8d..5bc1f906 100644 --- a/apps/exercises/glUtils.cpp +++ b/apps/exercises/glUtils.cpp @@ -6,7 +6,7 @@ * \copyright http://opensource.org/licenses/GPL-3.0 * \remarks Please use clangformat to format the code. See more code style on * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style -*/ + */ #include // for image loading #include @@ -253,11 +253,19 @@ GLuint glUtils::buildProgram(GLuint vertShaderID, return programHandle; } //----------------------------------------------------------------------------- -/*! Generates a Vertex Buffer Object (VBO) and copies the data into the +/** + * @brief Generates a Vertex Buffer Object (VBO) and copies the data into the buffer on the GPU. The targetTypeGL distincts between GL_ARRAY_BUFFER for attribute data and GL_ELEMENT_ARRAY_BUFFER for index data. The usageTypeGL distincts between GL_STREAM_DRAW, GL_STATIC_DRAW and GL_DYNAMIC_DRAW. -*/ + * + * @param vboID Reference to a vertex buffer object ID. If vboID is not zero the vao will be deleted first befor a new one is allocated. + * @param dataPointer Pointer to the vertex buffer data + * @param numElements No. of elements (vertices, normals, etc.) + * @param elementSizeBytes No. of bytes of one element. + * @param targetTypeGL GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER + * @param usageTypeGL GL_STREAM_DRAW, GL_STATIC_DRAW or GL_DYNAMIC_DRAW + */ void glUtils::buildVBO(GLuint& vboID, void* dataPointer, GLint numElements, @@ -265,6 +273,10 @@ void glUtils::buildVBO(GLuint& vboID, GLuint targetTypeGL, GLuint usageTypeGL) { + assert(dataPointer && "dataPointer is null"); + assert(numElements && "numElements is null"); + assert(elementSizeBytes && "elementSizeBytes is null"); + // Delete, generates and activates the VBO if (vboID) glDeleteBuffers(1, &vboID); glGenBuffers(1, &vboID); @@ -277,7 +289,8 @@ void glUtils::buildVBO(GLuint& vboID, glBufferData(targetTypeGL, bufSize, dataPointer, usageTypeGL); } //----------------------------------------------------------------------------- -/* Builds the OpenGL Vertex Array Object (VAO) with it associated vertex buffer +/** + * @brief Builds the OpenGL Vertex Array Object (VAO) with it associated vertex buffer objects. VAOs where introduces OpenGL 3.0 and reduce the overhead per draw call. All vertex attributes (e.g. position, colors, normals, texture coords, etc.) are float and are stored in one big VBO. @@ -288,7 +301,24 @@ We expect the data in following interleaved order: - 2 floats for a texture coordinate (optional) If one of the optional attributes are not in the vertex array its attribute location must be -1. -*/ +The drawing primitive is not involved in the VAO generation. It is only used in +the draw call (glDrawElements). + * + * @param vaoID Reference to a vertex array object ID. If vaoID is not zero the vao will be deleted first befor a new one is allocated. + * @param vboIDVertices Reference to a vertex buffer object ID for the vertices. If vboID is not zero the vbo will be deleted first befor a new one is allocated. + * @param vboIDIndices Reference to a vertex buffer object ID for the indices. If vboID is not zero the vbo will be deleted first befor a new one is allocated. + * @param dataPointerVertices Pointer to the interleaved data array. + * @param numVertices No. of vertices + * @param sizeVertexBytes No. of bytes for one vertex with all attributes + * @param dataPointerIndices Pointer to the index array + * @param numIndices No. of indices + * @param sizeIndexBytes No. of bytes of one index (e.g. sizeof(GL_UNSIGNED_INT)) + * @param shaderProgramID Shader program ID (not null) for which the vao will be generated + * @param attributePositionLoc Vertex position attribute location (mandatory) + * @param attributeColorLoc Vertex color attribute location (-1 if no colors are used) + * @param attributeNormalLoc Vertex normal attribute location (-1 if no colors are used) + * @param attributeTexCoordLoc Vertex texture coordinate attribute location (-1 if no colors are used) + */ void glUtils::buildVAO(GLuint& vaoID, GLuint& vboIDVertices, GLuint& vboIDIndices, @@ -436,13 +466,13 @@ GLuint glUtils::buildTexture(string textureFile, // Copy image data to the GPU. The image can be delete afterwards glTexImage2D(GL_TEXTURE_2D, // target texture type 1D, 2D or 3D - 0, // Base level for mipmapped textures - img.format(), // internal format: e.g. GL_RGBA, see spec. - (GLsizei)img.width(), // image width + 0, // Base level for mipmapped textures + img.format(), // internal format: e.g. GL_RGBA, see spec. + (GLsizei)img.width(), // image width (GLsizei)img.height(), // image height 0, // border pixels: must be 0 img.format(), // data format: e.g. GL_RGBA, see spec. - GL_UNSIGNED_BYTE, // data type + GL_UNSIGNED_BYTE, // data type (GLvoid*)img.data()); // image data pointer // generate the mipmap levels