Skip to content

Commit 1a64674

Browse files
committed
Add comments for clarification
1 parent 0040fa3 commit 1a64674

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

GUI/OpenGL/MiniGL.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ void MiniGL::drawVector(const Vector3r &a, const Vector3r &b, const float w, flo
120120
{
121121
float thickness = 0.001f * w;
122122

123+
// Draw a thick line as a cylinder with constant color.
123124
drawCylinder(a, b, color, thickness, 8, false);
124125
}
125126

@@ -128,13 +129,19 @@ void MiniGL::drawCylinder(const Vector3r &a, const Vector3r &b, const float *col
128129
Vector3r diffcolor(color);
129130
Vector3r speccolor(1.0, 1.0, 1.0);
130131

132+
// To simplify computations, the cylinder of height v is generated
133+
// along the z axis from z=0 to z=v and then transformed to the axis ab.
131134
Vector3r ab = b - a;
132135
Real v = ab.norm();
133136
Eigen::Transform<Real, 3, Eigen::Affine> transform =
134137
Eigen::Translation<Real, 3>(a) *
135138
Quaternionr::FromTwoVectors(Vector3r(0.0, 0.0, v), ab);
136139
Vector3r xMid = transform * Vector3r(0.0, 0.0, 0.5 * v);
137140

141+
// Both the lateral surface and the base disks are subdivided into n slices (cf. gluCylinder & gluDisk).
142+
// For this purpose, the base circle is parametrized as a function of the angle theta.
143+
// The lateral slices are again subdivided into two triangles each for rendering.
144+
// Smooth normals are obtained by going outward from the midpoint.
138145
unsigned int n = subdivisions;
139146
VectorXr vertices((n+1) * 2 * 3);
140147
VectorXr normals((n+1) * 2 * 3);
@@ -194,6 +201,10 @@ void MiniGL::drawSphere(const Vector3r &translation, float radius, float *color,
194201
Vector3r diffcolor(color);
195202
Vector3r speccolor(1.0, 1.0, 1.0);
196203

204+
// The surface of the sphere is subdivided into n slices and stacks (cf. gluSphere).
205+
// For this purpose, it is parametrized as a function of the longitude theta and the colatitude phi.
206+
// The slices and stacks are again subdivided into two triangles each for rendering.
207+
// Smooth normals are obtained by going outward from the midpoint.
197208
unsigned int n = subDivision;
198209
VectorXr vertices((n+1) * n * 3);
199210
VectorXr normals((n+1) * n * 3);
@@ -288,6 +299,7 @@ void MiniGL::drawMesh(const std::vector<Vector3r> &vertices, const std::vector<u
288299

289300
void MiniGL::drawQuad(const Vector3r &a, const Vector3r &b, const Vector3r &c, const Vector3r &d, const Vector3r &norm, float *color)
290301
{
302+
// The quad is subdivided into two triangles for rendering.
291303
drawTriangle(a, b, c, norm, color);
292304
drawTriangle(a, c, d, norm, color);
293305
}
@@ -389,6 +401,7 @@ void MiniGL::setViewport(float pfovy, float pznear, float pzfar, const Vector3r
389401
znear = pznear;
390402
zfar = pzfar;
391403

404+
// Compute the lookAt modelview matrix (cf. gluLookAt).
392405
Vector3r f = (plookat - peyepoint).normalized();
393406
Vector3r up(0.0, 1.0, 0.0);
394407
Vector3r s = f.cross(up);
@@ -504,6 +517,7 @@ void MiniGL::init(const int width, const int height, const char *name, const boo
504517
glGenBuffers(1, &m_vbo_vertices);
505518
glGenBuffers(1, &m_vbo_normals);
506519
glGenBuffers(1, &m_vbo_faces);
520+
// Set the default normal (cf. glNormal).
507521
glVertexAttrib3f(1, 0.0, 0.0, 1.0);
508522

509523
m_lastTime = glfwGetTime();
@@ -655,7 +669,8 @@ void MiniGL::char_callback(GLFWwindow* window, unsigned int codepoint)
655669
}
656670

657671
void MiniGL::setProjectionMatrix (int width, int height)
658-
{
672+
{
673+
// Compute the perspective projection matrix (cf. gluPerspective).
659674
Real aspect = (Real)width / (Real)height;
660675
Real fovy_rad = fovy * M_PI / 180.0;
661676
Real f = cos(0.5 * fovy_rad) / sin(0.5 * fovy_rad);
@@ -951,6 +966,7 @@ void MiniGL::unproject(const Vector3r& win, Vector3r& pos)
951966
GLint viewport[4];
952967
glGetIntegerv(GL_VIEWPORT, viewport);
953968

969+
// Map the specified window coordinates to object coordinates (cf. gluUnProject).
954970
Vector4r ndc;
955971
ndc(0) = static_cast<Real>(2.0) * (win(0) - static_cast<Real>(viewport[0])) / static_cast<Real>(viewport[2]) - static_cast<Real>(1.0);
956972
ndc(1) = static_cast<Real>(2.0) * (win(1) - static_cast<Real>(viewport[1])) / static_cast<Real>(viewport[3]) - static_cast<Real>(1.0);

GUI/OpenGL/MiniGL.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,15 @@ namespace SPH
232232
static const GLuint getVboNormals() { return m_vbo_normals; }
233233
static const GLuint getVboFaces() { return m_vbo_faces; }
234234

235+
// Fill a VBO with vector data and map to the VAO attribute at the specified index.
235236
static void supplyVectors(GLuint index, GLuint vbo, unsigned int dim, unsigned int n, const Real* data);
237+
// Fill the dedicated VBO with 3D vertex data and map to the VAO attribute at the specified index.
236238
static void supplyVertices(GLuint index, unsigned int numVectors, const Real* data);
239+
// Fill the dedicated VBO with 3D normal data and map to the VAO attribute at the specified index.
237240
static void supplyNormals(GLuint index, unsigned int numVectors, const Real* data);
241+
// Fill a VBO with index data.
238242
static void supplyIndices(GLuint vbo, unsigned int n, const unsigned int* data);
243+
// Fill the dedicated VBO with face index data.
239244
static void supplyFaces(unsigned int numIndices, const unsigned int* data);
240245
};
241246
}

data/shaders/mini.frag

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void main()
2626
vec3 view = normalize(-IN.position);
2727
vec3 normal = normalize(IN.normal);
2828

29+
// Compute the ambient lighting.
2930
vec3 color = ambientReflectance * ambientIntensity;
3031

3132
for (int i = 0; i < NUM_LIGHTS; i++)
@@ -34,11 +35,13 @@ void main()
3435
float diffuseFactor = max(dot(normal, light), 0.0);
3536
if (diffuseFactor > 0.0)
3637
{
38+
// Compute the diffuse lighting using the Lambertian model.
3739
color += diffuseFactor * diffuseReflectance * diffuseIntensity[i];
3840

3941
vec3 halfway = normalize(light + view);
4042
float specularFactor = pow(max(dot(halfway, normal), 0.0), shininess);
4143

44+
// Compute the specular lighting using the Blinn-Phong model.
4245
color += specularFactor * specularReflectance * specularIntensity[i];
4346
}
4447
}

data/shaders/mini_screen.vert

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ layout(location = 0) in vec2 position;
77

88
void main()
99
{
10+
// Project the screen position to NDC (cf. glOrtho).
1011
gl_Position = vec4(2.0 * (position.x / width) - 1.0, -2.0 * (position.y / height) + 1.0, -1.0, 1.0);
1112
}

0 commit comments

Comments
 (0)