LWJGL Forum

Programming => OpenGL => Topic started by: m0ep on January 14, 2011, 09:17:47

Title: Calculating Normals from obj files
Post by: m0ep on January 14, 2011, 09:17:47
Hi,

i have some obj files from teddy (http://www-ui.is.s.u-tokyo.ac.jp/~takeo/teddy/teddy.htm (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
Title: Re: Calculating Normals from obj files
Post by: lwjuggler on January 14, 2011, 10:19:23
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);
}

}
Title: Re: Calculating Normals from obj files
Post by: m0ep on January 14, 2011, 16:51:35
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. ;)
Title: Re: Calculating Normals from obj files
Post by: avm1979 on January 14, 2011, 20:26:01
Is your lighting set up correctly?  Try manually setting up a cube to make sure it's lit as you would expect.
Title: Re: Calculating Normals from obj files
Post by: lwjuggler on January 14, 2011, 23:34:28
Yeah  avm1979 is right.

This is a lighting issue, don't forget assigning materials to your surfaces.
Title: Re: Calculating Normals from obj files
Post by: m0ep on January 15, 2011, 08:11:27
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.
Title: Re: Calculating Normals from obj files
Post by: lwjuggler on January 15, 2011, 11:16:23
maybe you could send your whole code, then i can test it and correct it