Sign in to follow this  
adam17

Normal Rotation

Recommended Posts

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

Share this post


Link to post
Share on other sites
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[i] /= l;
}
public static final Vector3 subVec(Vector3 v1, Vector3 v2)
{
float newData[] = new float[3];
for( int i = 0; i < 3; i++ )
{
newData[i] = v1.getTriplet()[i] - v2.getTriplet()[i];
}
return new Vector3(newData);
}
public final float getLength()
{
float tmp = 0;
for( int i = 0; i < 3; i++)
tmp += data[i]* data[i];
return (float)Math.sqrt(tmp);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this