Skip to content

Commit

Permalink
Added more comments to glUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
hsm4 committed Nov 1, 2024
1 parent 6da31fe commit 3e6547e
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 175 deletions.
51 changes: 31 additions & 20 deletions apps/exercises/ch07_DiffuseSphere/DiffuseSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* \date December 2015 (HS15)
* \authors Marcus Hudritsch
* \copyright http://opensource.org/licenses/GPL-3.0
*/
*/

#include <GL/gl3w.h> // OpenGL headers
#include <GLFW/glfw3.h> // GLFW GUI library
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
234 changes: 88 additions & 146 deletions apps/exercises/ch09_TextureMapping/TextureMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Utils.h>
#include <GL/gl3w.h> // OpenGL headers
Expand All @@ -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
Expand All @@ -57,149 +57,92 @@ 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

//-----------------------------------------------------------------------------
/*!
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;
}
//-----------------------------------------------------------------------------
/*!
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -327,7 +270,6 @@ void onInit()
_matTexDiffLoc = glGetUniformLocation(_shaderProgID, "u_matTexDiff");

// Build object
// buildSphere(1.0f, 72, 72);
buildSquare();

// Set some OpenGL states
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 3e6547e

Please sign in to comment.