applied coding conventions

This commit is contained in:
denizdiktas 2023-06-07 10:58:16 +03:00
parent 3737f3a66f
commit 6acd902d2f
2 changed files with 143 additions and 117 deletions

View File

@ -8,11 +8,10 @@
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include <string> #include <string>
using namespace std;
namespace { namespace {
// vertex shader // vertex shader
const char* vShader = R"vs( const char* vertex_shader_code = R"vs(
#version 330 #version 330
layout (location = 0) in vec3 pos; layout (location = 0) in vec3 pos;
@ -32,7 +31,7 @@ void main()
// GEOMETRY SHADER // GEOMETRY SHADER
// * I am using the geometry shader to compute the face-normals in the GPU on the fly // * I am using the geometry shader to compute the face-normals in the GPU on the fly
const char* gShader = R"gs( const char* geometry_shader_code = R"gs(
#version 330 #version 330
in vec3 vpos[]; in vec3 vpos[];
@ -60,7 +59,7 @@ void main()
// FRAGMENT SHADER // FRAGMENT SHADER
static const char* fShader = R"fs( static const char* fragment_shader_code = R"fs(
#version 330 #version 330
in vec4 vCol; in vec4 vCol;
@ -87,12 +86,12 @@ MainWidget::~MainWidget()
void MainWidget::mousePressEvent(QMouseEvent *e) void MainWidget::mousePressEvent(QMouseEvent *e)
{ {
// Save mouse press position // Save mouse press position
mousePressPosition = QVector2D(e->position()); m_mouse_press_position = QVector2D(e->position());
} }
void MainWidget::mouseReleaseEvent(QMouseEvent *e) void MainWidget::mouseReleaseEvent(QMouseEvent *e)
{ {
// Mouse release position - mouse press position // Mouse release position - mouse press position
QVector2D diff = QVector2D(e->position()) - mousePressPosition; QVector2D diff = QVector2D(e->position()) - m_mouse_press_position;
// Rotation axis is perpendicular to the mouse position difference // Rotation axis is perpendicular to the mouse position difference
// vector // vector
@ -102,22 +101,23 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *e)
qreal acc = diff.length() / 100.0; qreal acc = diff.length() / 100.0;
// Calculate new rotation axis as weighted sum // Calculate new rotation axis as weighted sum
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized(); m_rotation_axis = (m_rotation_axis * m_angular_speed + n * acc).normalized();
// Increase angular speed // Increase angular speed
angularSpeed += acc; m_angular_speed += acc;
} }
void MainWidget::timerEvent(QTimerEvent *) void MainWidget::timerEvent(QTimerEvent *)
{ {
// Decrease angular speed (friction) // Decrease angular speed (friction)
angularSpeed *= 0.99; m_angular_speed *= 0.99;
// Stop rotation when speed goes below threshold // Stop rotation when speed goes below threshold
if (angularSpeed < 0.01) { if (m_angular_speed < 0.01) {
angularSpeed = 0.0; m_angular_speed = 0.0;
} else { } else {
// Update rotation // Update rotation
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation; m_rotation = QQuaternion::fromAxisAndAngle(m_rotation_axis, m_angular_speed) *
m_rotation;
// Request an update // Request an update
update(); update();
@ -146,51 +146,53 @@ void MainWidget::initializeGL()
//glEnable(GL_CULL_FACE); //glEnable(GL_CULL_FACE);
// Use QBasicTimer because its faster than QTimer // Use QBasicTimer because its faster than QTimer
timer.start(12, this); m_timer.start(12, this);
} }
void MainWidget::addShader(GLuint theProgram, const char* shaderCode, GLenum shaderType) void MainWidget::addShader(GLuint the_program, const char* shader_code,
GLenum shader_type)
{ {
GLuint theShader = glCreateShader(shaderType); GLuint the_shader = glCreateShader(shader_type);
const GLchar* theCode[] = { shaderCode }; const GLchar* the_code[] = { shader_code };
GLint codeLength[] = { strlen(shaderCode) }; GLint code_length[] = { strlen(shader_code) };
glShaderSource(theShader, 1, theCode, codeLength); glShaderSource(the_shader, 1, the_code, code_length);
glCompileShader(theShader); glCompileShader(the_shader);
GLint result = 0; GLint result = 0;
GLchar elog[1024] = { 0 }; GLchar elog[1024] = { 0 };
glGetShaderiv(theShader, GL_COMPILE_STATUS, &result); glGetShaderiv(the_shader, GL_COMPILE_STATUS, &result);
if (!result) if (!result)
{ {
string shaderTypeName; std::string shader_type_name;
switch (shaderType) switch (shader_type)
{ {
case GL_VERTEX_SHADER: shaderTypeName = "VERTEX"; break; case GL_VERTEX_SHADER: shader_type_name = "VERTEX"; break;
case GL_GEOMETRY_SHADER: shaderTypeName = "GEOMETRY"; break; case GL_GEOMETRY_SHADER: shader_type_name = "GEOMETRY"; break;
case GL_FRAGMENT_SHADER: shaderTypeName = "FRAGMENT"; break; case GL_FRAGMENT_SHADER: shader_type_name = "FRAGMENT"; break;
} }
glGetShaderInfoLog(theShader, sizeof(elog), NULL, elog); glGetShaderInfoLog(the_shader, sizeof(elog), NULL, elog);
cout << "! error compiling the " << shaderTypeName << " shader:\n" << elog << endl; std::cout << "! error compiling the " << shader_type_name <<
" shader:\n" << elog << std::endl;
return; return;
} }
glAttachShader(theProgram, theShader); glAttachShader(the_program, the_shader);
} }
void MainWidget::initShaderProgram() void MainWidget::initShaderProgram()
{ {
shader = glCreateProgram(); shader = glCreateProgram();
if (!shader) if (!shader)
{ {
cout << "error creating shader program!\n"; std::cout << "error creating shader program!\n";
return; return;
} }
addShader(shader, vShader, GL_VERTEX_SHADER); addShader(shader, vertex_shader_code, GL_VERTEX_SHADER);
addShader(shader, gShader, GL_GEOMETRY_SHADER); addShader(shader, geometry_shader_code, GL_GEOMETRY_SHADER);
addShader(shader, fShader, GL_FRAGMENT_SHADER); addShader(shader, fragment_shader_code, GL_FRAGMENT_SHADER);
GLint result = 0; GLint result = 0;
GLchar elog[1024] = { 0 }; GLchar elog[1024] = { 0 };
@ -200,7 +202,7 @@ void MainWidget::initShaderProgram()
if (!result) if (!result)
{ {
glGetProgramInfoLog(shader, sizeof(elog), NULL, elog); glGetProgramInfoLog(shader, sizeof(elog), NULL, elog);
cout << "! error linking program:\n" << elog << endl; std::cout << "! error linking program:\n" << elog << std::endl;
return; return;
} }
@ -209,12 +211,12 @@ void MainWidget::initShaderProgram()
if (!result) if (!result)
{ {
glGetProgramInfoLog(shader, sizeof(elog), NULL, elog); glGetProgramInfoLog(shader, sizeof(elog), NULL, elog);
cout << "! error validating program:\n" << elog << endl; std::cout << "! error validating program:\n" << elog << std::endl;
return; return;
} }
uniformMVP = glGetUniformLocation(shader, "MVP"); m_uniform_mvp = glGetUniformLocation(shader, "MVP");
cout << "uniform loc = " << uniformMVP << endl; std::cout << "uniform loc = " << m_uniform_mvp << std::endl;
} }
@ -230,15 +232,15 @@ void MainWidget::initGeometry()
GLuint indices[] = { 0,1,2 }; GLuint indices[] = { 0,1,2 };
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &m_vao);
glBindVertexArray(vao); glBindVertexArray(m_vao);
{ {
glGenBuffers(1, &ibo); glGenBuffers(1, &m_ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glGenBuffers(1, &vbo); glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
{ {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
@ -253,9 +255,9 @@ void MainWidget::initGeometry()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
void MainWidget::createSphere(int numSlices, int numStacks, float r) void MainWidget::createSphere(int num_slices, int num_stacks, float r)
{ {
numStacks = std::max<int>(2, numStacks); num_stacks = std::max<int>(2, num_stacks);
std::vector<QVector3D> vertices, normals; std::vector<QVector3D> vertices, normals;
// NORTH POLE // NORTH POLE
@ -265,23 +267,23 @@ void MainWidget::createSphere(int numSlices, int numStacks, float r)
// SOUTH POLE // SOUTH POLE
vertices.push_back(QVector3D(0, 0, -r)); vertices.push_back(QVector3D(0, 0, -r));
normals.push_back(QVector3D(0, 0, -1)); normals.push_back(QVector3D(0, 0, -1));
int startingIndexOfMiddleVertices = vertices.size(); int starting_index_of_middle_vertices = vertices.size();
for (int j = 1; j < numStacks; ++j) for (int j = 1; j < num_stacks; ++j)
{ {
// Calculate the latitude (vertical angle) for the current stack // Calculate the latitude (vertical angle) for the current stack
float lat = M_PI * j / numStacks; float lat = M_PI * j / num_stacks;
float rxy = r * sin(lat); float rxy = r * std::sin(lat);
float z = r * cos(lat); float z = r * std::cos(lat);
for (int i = 0; i < numSlices; ++i) for (int i = 0; i < num_slices; ++i)
{ {
// Calculate the longitude (horizontal angle) for the current slice // Calculate the longitude (horizontal angle) for the current slice
float lon = 2 * M_PI * i / numSlices; float lon = 2 * M_PI * i / num_slices;
// Convert spherical coordinates to Cartesian coordinates // Convert spherical coordinates to Cartesian coordinates
float x = rxy * cos(lon); float x = rxy * std::cos(lon);
float y = rxy * sin(lon); float y = rxy * std::sin(lon);
auto p = QVector3D(x, y, z); auto p = QVector3D(x, y, z);
auto n = p / p.length(); auto n = p / p.length();
@ -303,13 +305,13 @@ void MainWidget::createSphere(int numSlices, int numStacks, float r)
std::vector<GLuint> indices; std::vector<GLuint> indices;
// NORTH CAP // NORTH CAP
const int northVertexIndex = 0; const int north_vertex_index = 0;
const int northCapVertexIndexStart = startingIndexOfMiddleVertices; const int north_cap_vertex_index_start = starting_index_of_middle_vertices;
for (int i = 0; i < numSlices; i++) for (int i = 0; i < num_slices; i++)
{ {
indices.push_back(northVertexIndex); indices.push_back(north_vertex_index);
indices.push_back(northCapVertexIndexStart + i); indices.push_back(north_cap_vertex_index_start + i);
indices.push_back(northCapVertexIndexStart + (i + 1) % numSlices); indices.push_back(north_cap_vertex_index_start + (i + 1) % num_slices);
} }
// 0 = NORTH VERTEX // 0 = NORTH VERTEX
@ -317,38 +319,42 @@ void MainWidget::createSphere(int numSlices, int numStacks, float r)
// [2, 2 + (numSlices-1)] = bottom vertices of the stack #1 // [2, 2 + (numSlices-1)] = bottom vertices of the stack #1
// [2+numSlices, 2 + (2*numSlices - 1)] = bottom vertices of the stack #2 // [2+numSlices, 2 + (2*numSlices - 1)] = bottom vertices of the stack #2
// ... // ...
// [2+(k-1)*numSlices, 2 + (k*numSlices -1) ] = bottom vertices of the stack #k // [2+(k-1)*numSlices, 2 + (k*numSlices -1)] = bottom vertices of the stack #k
// .. // ..
// [2+(numStacks-1)*numSlices, 2+(numStacks*numSlices-1)] = bottom vertices of the last stack (# numStacks) // [2+(numStacks-1)*numSlices, 2+(numStacks*numSlices-1)] = bottom vertices of
// the last stack (# numStacks)
// SOUTH CAP // SOUTH CAP
const int southVertexIndex = 1; const int south_vertex_index = 1;
const int southCapIndexStart = startingIndexOfMiddleVertices + (numStacks - 2) * numSlices; const int south_cap_index_start = starting_index_of_middle_vertices +
for (int i = 0; i < numSlices; i++) (num_stacks - 2) * num_slices;
for (int i = 0; i < num_slices; ++i)
{ {
const auto vi0 = southVertexIndex; const auto vi0 = south_vertex_index;
const auto vi1 = southCapIndexStart + i; const auto vi1 = south_cap_index_start + i;
const auto vi2 = southCapIndexStart + (i + 1) % numSlices; const auto vi2 = south_cap_index_start + (i + 1) % num_slices;
indices.push_back(vi2); indices.push_back(vi2);
indices.push_back(vi1); indices.push_back(vi1);
indices.push_back(vi0); indices.push_back(vi0);
} }
// MIDDLE TRIANGLES // MIDDLE TRIANGLES
for (int k = 0; k < numStacks - 2; k++) for (int k = 0; k < num_stacks - 2; ++k)
{ {
const int stackStartIndex = startingIndexOfMiddleVertices + k * numSlices; const int stack_start_index = starting_index_of_middle_vertices +
const int nextStackStartIndex = stackStartIndex + numSlices; k * num_slices;
for (int i = 0; i < numSlices; i++) const int next_stack_start_index = stack_start_index + num_slices;
for (int i = 0; i < num_slices; ++i)
{ {
// check why the following code snippet does not work (winding order?)
//int vi0 = stackStartIndex + i; //int vi0 = stackStartIndex + i;
//int vi1 = nextStackStartIndex + i; //int vi1 = nextStackStartIndex + i;
//int vi2 = nextStackStartIndex + (i + 1) % numSlices; //int vi2 = nextStackStartIndex + (i + 1) % numSlices;
//int vi3 = stackStartIndex + (i + 1) % numSlices; //int vi3 = stackStartIndex + (i + 1) % numSlices;
int vi0 = stackStartIndex + i; int vi0 = stack_start_index + i;
int vi1 = stackStartIndex + (i + 1) % numSlices; int vi1 = stack_start_index + (i + 1) % num_slices;
int vi2 = nextStackStartIndex + i; int vi2 = next_stack_start_index + i;
int vi3 = nextStackStartIndex + (i + 1) % numSlices; int vi3 = next_stack_start_index + (i + 1) % num_slices;
indices.push_back(vi0); indices.push_back(vi0);
indices.push_back(vi2); indices.push_back(vi2);
@ -359,41 +365,61 @@ void MainWidget::createSphere(int numSlices, int numStacks, float r)
indices.push_back(vi1); indices.push_back(vi1);
} }
} }
m_num_indices = indices.size();
numIndices = indices.size();
glGenVertexArrays(1, &vao); // DEFINE OPENGL BUFFERS
glBindVertexArray(vao); glGenVertexArrays(1, &m_vao);
{ glBindVertexArray(m_vao);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), reinterpret_cast<const void*>(indices.data()), GL_STATIC_DRAW);
glGenBuffers(1, &vbo); // Index buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo); glGenBuffers(1, &m_ibo);
{ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D) * vertex_data.size(), reinterpret_cast<const void*>(vertex_data.data()), GL_STATIC_DRAW); auto indices_size = sizeof(GLuint) * indices.size();
auto indices_data = reinterpret_cast<const void*>(indices.data());
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
indices_size,
indices_data,
GL_STATIC_DRAW);
// positions // Vertex Buffer
GLint positionAttribIndex = 0; glGenBuffers(1, &m_vbo);
GLsizei stride = 6 * sizeof(float); glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glVertexAttribPointer(positionAttribIndex, 3, GL_FLOAT, GL_FALSE, stride, 0); auto vertex_buffer_size = sizeof(QVector3D) * vertex_data.size();
glEnableVertexAttribArray(positionAttribIndex); auto vertex_buffer_data = reinterpret_cast<const void*>(vertex_data.data());
//normals glBufferData(GL_ARRAY_BUFFER,
GLint normalAttribIndex = 1; vertex_buffer_size,
auto* normal_offset = reinterpret_cast<const void*>(3 * sizeof(float)); vertex_buffer_data,
glVertexAttribPointer(normalAttribIndex, 3, GL_FLOAT, GL_FALSE, stride, normal_offset); GL_STATIC_DRAW);
glEnableVertexAttribArray(normalAttribIndex);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
} // Position Vertex-Attribute
GLint position_attrib_index = 0;
const void* position_offset = 0;
GLsizei stride = 6 * sizeof(float);
glVertexAttribPointer(position_attrib_index,
3,
GL_FLOAT, GL_FALSE,
stride,
position_offset);
glEnableVertexAttribArray(position_attrib_index);
// Normal Vertex-Attribute
GLint normal_attrib_index = 1;
auto* normal_offset = reinterpret_cast<const void*>(3 * sizeof(float));
glVertexAttribPointer(normal_attrib_index,
3,
GL_FLOAT,
GL_FALSE,
stride,
normal_offset);
glEnableVertexAttribArray(normal_attrib_index);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
// Note: calling this before glBindVertexArray(0) results in no output!
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
//! [3]
void MainWidget::resizeGL(int w, int h) void MainWidget::resizeGL(int w, int h)
@ -405,8 +431,8 @@ void MainWidget::resizeGL(int w, int h)
const qreal z_near = 1.0, z_far = 100.0, fov = 45.0; const qreal z_near = 1.0, z_far = 100.0, fov = 45.0;
// Reset projection // Reset projection
projection.setToIdentity(); m_projection.setToIdentity();
projection.perspective(fov, aspect, z_near, z_far); m_projection.perspective(fov, aspect, z_near, z_far);
} }
void MainWidget::paintGL() void MainWidget::paintGL()
@ -428,15 +454,15 @@ void MainWidget::paintGL()
glUseProgram(shader); glUseProgram(shader);
auto mvp = projection * view * model; auto mvp = m_projection * view * model;
glUniformMatrix4fv(uniformMVP, 1, GL_FALSE, mvp.data()); glUniformMatrix4fv(m_uniform_mvp, 1, GL_FALSE, mvp.data());
{ {
// DRAW TRIANGLE // DRAW TRIANGLE
glBindVertexArray(vao); glBindVertexArray(m_vao);
{ {
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); //glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, m_num_indices, GL_UNSIGNED_INT, 0);
} }
glBindVertexArray(0); glBindVertexArray(0);
} }

View File

@ -36,24 +36,24 @@ protected:
void paintGL() override; void paintGL() override;
GLuint vao, vbo, ibo, shader, numIndices; GLuint m_vao, m_vbo, m_ibo, shader, m_num_indices;
GLuint uniformMVP; // ModelViewProjection GLuint m_uniform_mvp; // uniform location for MVP-matrix in the shader
void addShader(GLuint theProgram, const char* shaderCode, GLenum shaderType); void addShader(GLuint program, const char* shader_code, GLenum shader_type);
void initShaderProgram(); void initShaderProgram();
void initGeometry(); void initGeometry();
void createSphere(int numSlices, int numStacks, float r); void createSphere(int num_slices, int num_stacks, float r);
private: private:
QBasicTimer timer; QBasicTimer m_timer;
QMatrix4x4 projection; QMatrix4x4 m_projection;
QVector2D mousePressPosition; QVector2D m_mouse_press_position;
QVector3D rotationAxis; QVector3D m_rotation_axis;
qreal angularSpeed = 0; qreal m_angular_speed = 0;
QQuaternion rotation; QQuaternion m_rotation;
}; };
#endif // MAINWIDGET_H #endif // MAINWIDGET_H