Calculating Normals from obj files

Started by m0ep, January 14, 2011, 09:17:47

Previous topic - Next topic

m0ep

Hi,

i have some obj files from teddy (http://www-ui.is.s.u-tokyo.ac.jp/~takeo/teddy/teddy.htm) witout normals. I know that there is a way to calculate them with the crossproduct but something don't work verry well.

Here is the methode:
        public static float[] crossProduct( float[] v0, float[] v1, float[] v2 ) {
            float[] tmpV1 = new float[3];
            float[] tmpV2 = new float[3];
            float[] result = new float[3];

            for ( int i = 0; i < v0.length; ++i ) {
                tmpV1[i] = v1[i] - v0[i];
                tmpV2[i] = v2[i] - v0[i];
            }

            float[] a = new float[3*2];
            float[] b = new float[3*2];

            for (int i = 0; i < v0.length; ++i) {
                a[i] = tmpV1[i];
                b[i] = tmpV2[i];


                if (i == tmpV2.length - 1) {
                    for (int j = i + 1; j < tmpV2.length * 2; ++j) {
                        a[j] = tmpV1[j - i - 1];
                        b[j] = tmpV2[j - i - 1];

                    }
                }
            }
            for (int i = 1; i < b.length - 2; ++i) {
                result[i - 1] = a[i] * b[i + 1] - a[i + 1] * b[i];
            }
            return result;
        }


The result is normalized with the OpenGL methode glEnable(GL_NORMALIZE)..glDisable(GL_NORMALIZE).

I hope you guys find some errors :)

m0ep

lwjuggler

Hi mOep,

  I use this method, hope it will be useful :

Point p1; // = value from your OBJ file
Point p2; // = value from your OBJ file
Point p3; // = value from your OBJ file

Vector3d normal = new Vector3d(p1,p2).crossProd(new Vector3d(p1,p3));


assuming :

public class Point {
	public float x;
	public float y;
	public float z;

}

public class Vector3d {
	public float x;
	public float y;
	public float z;

	public Vector3d(Point a, Point b) {
		x = b.x - a.x;
		y = b.y - a.y;
		z = b.z - a.z;
	}

	public Vector3d crossProd(Vector3d v) {
		return new Vector3d(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
	}

}

m0ep

Hi lwjuggler,

Your method don't work for me, the model is completly dark gray. I think that the normales are inverted, so i tried to exchange p1 with p2 and so on, but it's still dark.

Maybe you have another idea. ;)

avm1979

Is your lighting set up correctly?  Try manually setting up a cube to make sure it's lit as you would expect.

lwjuggler

Yeah  avm1979 is right.

This is a lighting issue, don't forget assigning materials to your surfaces.

m0ep

I think the lightng should be correct, because i have already 3 cubes and they are getting correct lighten.
lwjuggler, with materials you mean textures?

I have a test texture randomly set on each polygon, because I have no texture coordinates in the obj file, but when I activate the lighting it is still dark grey. Thats the reason why I think that the lighting should be correct.

lwjuggler

maybe you could send your whole code, then i can test it and correct it