glNormal, glAbNormal, or glNeverNormal?
Ive read up and understand normals, they point in the direction towards the ''eye'' to place textures correctly, shading effects, etc. A few of the tutorials explain how to do them with simple cubes, quaint and effective....problem. What happens however, when you want to ''load'' an object, but have no normals defined? The lighting gets strange, the shading gets off, the textures look funky, etc. Is there any mystical magical way to auto-generate the normals based on, for instance, the 3 vertexes that make up a triangle patch? In my mind, the normal can only face 2 directions, out of the patch, or into the patch.
I''m hoping I''m barking up the right tree. I''m loading in a 3D object sphere from disk (faces/vertexes only in file), and it looks fine until I begin rotating, in which the side views and back view lose light and become darker as I go. Is this a problem with normals, or are my lights just going funky on me?
*Moving Forward Along the Backward Walkway of Technology*
*Moving Forward Along the Backward Walkway of Technology*
Nevermind
Found the code hiding elsewhere in the site. If anyone else has had this problem, this code snippet does the trick...
// CalculateNormal()
// desc: given 3 points, calculates the normal to the points
void CalculateNormal( float *p1, float *p2, float *p3 )
{
float a[3], b[3], result[3];
float length;
a[0] = p1[0] - p2[0];
a[1] = p1[1] - p2[1];
a[2] = p1[2] - p2[2];
b[0] = p1[0] - p3[0];
b[1] = p1[1] - p3[1];
b[2] = p1[2] - p3[2];
result[0] = a[1] * b[2] - b[1] * a[2];
result[1] = b[0] * a[2] - a[0] * b[2];
result[2] = a[0] * b[1] - b[0] * a[1];
// calculate the length of the normal
length = (float)sqrt(result[0]*result[0] + result[1]*result[1] + result[2]*result[2]);
// normalize and specify the normal
glNormal3f(result[0]/length, result[1]/length, result[2]/length);
}
By the way, anyone willing to explain how to get the code to showup in the scroll boxes rather than in text when posting? Thanks.
*Moving Forward Along the Backward Walkway of Technology*

// CalculateNormal()
// desc: given 3 points, calculates the normal to the points
void CalculateNormal( float *p1, float *p2, float *p3 )
{
float a[3], b[3], result[3];
float length;
a[0] = p1[0] - p2[0];
a[1] = p1[1] - p2[1];
a[2] = p1[2] - p2[2];
b[0] = p1[0] - p3[0];
b[1] = p1[1] - p3[1];
b[2] = p1[2] - p3[2];
result[0] = a[1] * b[2] - b[1] * a[2];
result[1] = b[0] * a[2] - a[0] * b[2];
result[2] = a[0] * b[1] - b[0] * a[1];
// calculate the length of the normal
length = (float)sqrt(result[0]*result[0] + result[1]*result[1] + result[2]*result[2]);
// normalize and specify the normal
glNormal3f(result[0]/length, result[1]/length, result[2]/length);
}
By the way, anyone willing to explain how to get the code to showup in the scroll boxes rather than in text when posting? Thanks.
*Moving Forward Along the Backward Walkway of Technology*
*Moving Forward Along the Backward Walkway of Technology*
if you are loading an object, it is a (relatively) easy process to calculate the normal for each of the triangles you create. You must ensure that all your triangles are defined in the same rotation i.e. clockwise or anti-clockwise.
The normal is only created once at object load, as the ''normal'' will be rotated along with your 3d object.
So, yes you are "barking up the right tree"
To help you out, here is my way of getting the normal to a polygon.
also make sure you enable the normalise option in your program using glEnable(GL_NORMALIZE). you should then find the glNormal commands works a treat.
The normal is only created once at object load, as the ''normal'' will be rotated along with your 3d object.
So, yes you are "barking up the right tree"
To help you out, here is my way of getting the normal to a polygon.
typedef struct tagVector{ float x, y, x;}vector;vector normalize(vector vVector1) { float d; d = (float) sqrt((vVector1.x * vVector1.x) + (vVector1.y * vVector1.y) + (vVector1.z * vVector1.z)); if (d == 0.0) return; vVector1.x /= d; vVector1.y /= d; vVector1.z /= d; return vVector1;}vector cross_prod(vector vVector1, vector vVector2) { vector out; out.x = (vVector1.y * vVector2.z) - (vVector1.z * vVector2.y); out.y = (vVector1.z * vVector2.x) - (vVector1.x * vVector2.z); out.z = (vVector1.x * vVector2.y) - (vVector1.y * vVector2.x); out = normalize(out); }// in your code for each triangle vector d1, d2, normal; d1.x = point0.x - point1.x; d1.x = point2.x - point1.x; d1.y = point0.y - point1.y; d1.y = point2.y - point1.y; d1.z = point0.z - point1.z; d1.z = point2.z - point1.z; // returns normal.x normal.y normal.z normal = cross_product(d1, d2);
also make sure you enable the normalise option in your program using glEnable(GL_NORMALIZE). you should then find the glNormal commands works a treat.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement