@@ -120,6 +120,7 @@ void MiniGL::drawVector(const Vector3r &a, const Vector3r &b, const float w, flo
120
120
{
121
121
float thickness = 0 .001f * w;
122
122
123
+ // Draw a thick line as a cylinder with constant color.
123
124
drawCylinder (a, b, color, thickness, 8 , false );
124
125
}
125
126
@@ -128,13 +129,19 @@ void MiniGL::drawCylinder(const Vector3r &a, const Vector3r &b, const float *col
128
129
Vector3r diffcolor (color);
129
130
Vector3r speccolor (1.0 , 1.0 , 1.0 );
130
131
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.
131
134
Vector3r ab = b - a;
132
135
Real v = ab.norm ();
133
136
Eigen::Transform<Real, 3 , Eigen::Affine> transform =
134
137
Eigen::Translation<Real, 3 >(a) *
135
138
Quaternionr::FromTwoVectors (Vector3r (0.0 , 0.0 , v), ab);
136
139
Vector3r xMid = transform * Vector3r (0.0 , 0.0 , 0.5 * v);
137
140
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.
138
145
unsigned int n = subdivisions;
139
146
VectorXr vertices ((n+1 ) * 2 * 3 );
140
147
VectorXr normals ((n+1 ) * 2 * 3 );
@@ -194,6 +201,10 @@ void MiniGL::drawSphere(const Vector3r &translation, float radius, float *color,
194
201
Vector3r diffcolor (color);
195
202
Vector3r speccolor (1.0 , 1.0 , 1.0 );
196
203
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.
197
208
unsigned int n = subDivision;
198
209
VectorXr vertices ((n+1 ) * n * 3 );
199
210
VectorXr normals ((n+1 ) * n * 3 );
@@ -288,6 +299,7 @@ void MiniGL::drawMesh(const std::vector<Vector3r> &vertices, const std::vector<u
288
299
289
300
void MiniGL::drawQuad (const Vector3r &a, const Vector3r &b, const Vector3r &c, const Vector3r &d, const Vector3r &norm, float *color)
290
301
{
302
+ // The quad is subdivided into two triangles for rendering.
291
303
drawTriangle (a, b, c, norm, color);
292
304
drawTriangle (a, c, d, norm, color);
293
305
}
@@ -389,6 +401,7 @@ void MiniGL::setViewport(float pfovy, float pznear, float pzfar, const Vector3r
389
401
znear = pznear;
390
402
zfar = pzfar;
391
403
404
+ // Compute the lookAt modelview matrix (cf. gluLookAt).
392
405
Vector3r f = (plookat - peyepoint).normalized ();
393
406
Vector3r up (0.0 , 1.0 , 0.0 );
394
407
Vector3r s = f.cross (up);
@@ -504,6 +517,7 @@ void MiniGL::init(const int width, const int height, const char *name, const boo
504
517
glGenBuffers (1 , &m_vbo_vertices);
505
518
glGenBuffers (1 , &m_vbo_normals);
506
519
glGenBuffers (1 , &m_vbo_faces);
520
+ // Set the default normal (cf. glNormal).
507
521
glVertexAttrib3f (1 , 0.0 , 0.0 , 1.0 );
508
522
509
523
m_lastTime = glfwGetTime ();
@@ -655,7 +669,8 @@ void MiniGL::char_callback(GLFWwindow* window, unsigned int codepoint)
655
669
}
656
670
657
671
void MiniGL::setProjectionMatrix (int width, int height)
658
- {
672
+ {
673
+ // Compute the perspective projection matrix (cf. gluPerspective).
659
674
Real aspect = (Real)width / (Real)height;
660
675
Real fovy_rad = fovy * M_PI / 180.0 ;
661
676
Real f = cos (0.5 * fovy_rad) / sin (0.5 * fovy_rad);
@@ -951,6 +966,7 @@ void MiniGL::unproject(const Vector3r& win, Vector3r& pos)
951
966
GLint viewport[4 ];
952
967
glGetIntegerv (GL_VIEWPORT, viewport);
953
968
969
+ // Map the specified window coordinates to object coordinates (cf. gluUnProject).
954
970
Vector4r ndc;
955
971
ndc (0 ) = static_cast <Real>(2.0 ) * (win (0 ) - static_cast <Real>(viewport[0 ])) / static_cast <Real>(viewport[2 ]) - static_cast <Real>(1.0 );
956
972
ndc (1 ) = static_cast <Real>(2.0 ) * (win (1 ) - static_cast <Real>(viewport[1 ])) / static_cast <Real>(viewport[3 ]) - static_cast <Real>(1.0 );
0 commit comments