Normal Rotation

Started by
4 comments, last by gorgorath 17 years, 7 months ago
im trying to load some models from 3d studio into my engine. a problem im running into is getting the normals to face the right direction. this ONLY happens on the objects that have been copied or cloned and then rotated. their normals dont face the right direction. should i just bite the bullet and build my own normal calculator? if so, are there any good tutorials on generating vertex normals? thanks
Advertisement
normals are pretty easy ( assumed the triangles are order CCW )
public static final Vector3 crossProduct(Vector3 v1, Vector3 v2)
{
float cX, cY, cZ;

cX = v1.getY() * v2.getZ() - v1.getZ() * v2.getY();
cY = v1.getZ() * v2.getX() - v1.getX() * v2.getZ();
cZ = v1.getX() * v2.getY() - v1.getY() * v2.getX();

if(Math.abs(cX) < MathConstants.VEC_ON_EPSILON)
cX = 0.0f;
if(Math.abs(cY) < MathConstants.VEC_ON_EPSILON)
cY = 0.0f;
if(Math.abs(cZ) < MathConstants.VEC_ON_EPSILON)
cZ = 0.0f;
return new Vector3(cX, cY, cZ);
}

public static final Vector3 getNormalFromPoints(Vector3 v1, Vector3 v2, Vector3 v3)
{
Vector3 tmp = Vector3.crossProduct( Vector3.subVec(v1, v2), Vector3.subVec(v1, v3));
tmp.normalize();
return tmp;
}


/**
* Make a unit length vector
*/
public final void normalize()
{
float l = getLength();
if( l > MathConstants.VEC_ON_EPSILON)
for( int i = 0; i < 3; i++)
data /= l;
}
public static final Vector3 subVec(Vector3 v1, Vector3 v2)
{
float newData[] = new float[3];
for( int i = 0; i < 3; i++ )
{
newData = v1.getTriplet() - v2.getTriplet();
}
return new Vector3(newData);
}
public final float getLength()
{
float tmp = 0;
for( int i = 0; i < 3; i++)
tmp += data* data;
return (float)Math.sqrt(tmp);
}
Cosmic Keys to my Creations & Times
When you rotate your model, are you rotating the normal vectors as well?
Quote:Original post by adam17
im trying to load some models from 3d studio into my engine. a problem im running into is getting the normals to face the right direction. this ONLY happens on the objects that have been copied or cloned and then rotated. their normals dont face the right direction.

should i just bite the bullet and build my own normal calculator? if so, are there any good tutorials on generating vertex normals?

thanks
Are you exporting the data with a MaxScript to your own format or using OBJ/3DS or something? If the former, use this:

in coordsys local getNormal obj i

You should also check if the object is mirrored or not and pivot offset etc.

If you are loading from another file format, use the code posted by gorgorath.
im actually exporting my files as an ASE file. unfortunately the face normals and vertex normals are the same as the original model. for example, if i have a model that is facing 0,0,1 and i clone it, as a copy or instance, and rotate it so its facing 0,1,0 (or any rotation) the normals do not rotate either. they stay pointing in the same direction as the original. its kinda like opengl is not multiplying it by the modelview matrix, but thats not the case. does that make it clearer?

i have the rotation matrix for the model, which is correct, but i dont know how to rotate a normal with it.
Quote:Original post by adam17
im actually exporting my files as an ASE file. unfortunately the face normals and vertex normals are the same as the original model. for example, if i have a model that is facing 0,0,1 and i clone it, as a copy or instance, and rotate it so its facing 0,1,0 (or any rotation) the normals do not rotate either. they stay pointing in the same direction as the original. its kinda like opengl is not multiplying it by the modelview matrix, but thats not the case. does that make it clearer?

i have the rotation matrix for the model, which is correct, but i dont know how to rotate a normal with it.


glEnable(GL.GL_NORMALIZE);
Cosmic Keys to my Creations & Times

This topic is closed to new replies.

Advertisement