I'm new at openGL 4.5 and I currently working with lights and it's all okay when I calculate vertex normals during the geometry drawing, but now I have to calculate face's normal for a cone and I have no idea on how to do it. I'm drawing it with GL_TRIANGLE_STRIP. We can see the current snippet below:
float coneAngle = (float) Math.atan(radius / height);
coneCos = (float) Math.cos(coneAngle);
coneSin = (float) Math.sin(coneAngle);
// circleResolution * heightResolution * 3
for(int i = 0; i <= circleResolution; i++)
{
for(int j = 0; j <= heightResolution; j++)
{
angle = 2 * ((float) Math.PI) * (i / (float) circleResolution);
cos = ((float) Math.cos(angle));
sin = ((float) Math.sin(angle));
x = radius * cos;
z = radius * sin;
// Cone Bottom
vertices.add(x);
vertices.add(0.0f);
vertices.add(z);
// Cone Bottom Normal
vertices.add(coneCos * cos);
vertices.add(coneSin);
vertices.add(coneCos * sin);
// Cone Top
vertices.add(0f);
vertices.add(height);
vertices.add(0f);
// Cone Top Normal
vertices.add(0f);
vertices.add(0f);
vertices.add(0f);
}
}
// Center of Bottom Circle - Vertices
vertices.add(0.0f);
vertices.add(0.0f);
vertices.add(0.0f);
// Center of Bottom Circle - Normal
vertices.add(0.0f);
vertices.add(-1.0f);
vertices.add(0.0f);
// CircleResolution - Bottom Circle - TRIANGLE_FAN
for (int j = 0; j <= circleResolution; j++)
{
angle = (2 * ((float) Math.PI)) * ( j / (float) circleResolution );
x = (float) Math.cos(angle);
z = (float) Math.sin(angle);
vertices.add(radius * x);
vertices.add(0.0f);
vertices.add(radius * z);
// Normal
vertices.add(0.0f);
vertices.add(-1.0f);
vertices.add(0.0f);
}
The normal vector to a line or (vetor) can be achiefed by a 90° rotation of a vector along the line.
A vector along the flank of the cone is (radius
, -height
). The normal vector is (-(-height)
, radius
).
In your case the normal vector attribute of the flank of the cone, for the bottom ("Cone Bottom Normal") and top ("Cone Top Normal") vertices is:
// lenght of the flank of the cone
float flank_len = Math.sqrt(radius*radius + height*height);
// unit vector along the flank of the cone
float cone_x = radius / flank_len;
float cone_y = -height / flank_len;
.....
// Cone Bottom Normal
vertices.add(-cone_y * cos);
vertices.add( cone_x );
vertices.add(-cone_y * sin);
.....
// Cone Top Normal
vertices.add(-cone_y * cos);
vertices.add( cone_x );
vertices.add(-cone_y * sin);