0

I am building a simple Wavefront Obj file loader and renderer in Java, using JOGL. However, whenever I am loading a simple bunny test mesh, it's rendering in a glitchy fashion, and I have no idea what could be causing it. I am just using geometry vertices and normals, no textures or materials.

the experienced glitch

another angle of that glitch

The following is the GL initialization code from the init() method:

gl.setSwapInterval(1);
gl.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);        
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LESS);
gl.glEnable(GL2.GL_CULL_FACE);
gl.glCullFace(GL2.GL_BACK);
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glClearDepth(1.0d);

And this is the method that I'm using to render the model (fixed pipeline, for now):

 gl.glBegin(GL2.GL_TRIANGLES);
    for(Face face : master.faces) {
        for(int i = 0; i < face.points.length; i++) {
            gl.glNormal3f(face.normals[i].x, face.normals[i].y, face.normals[i].z);             
            gl.glVertex3f(face.points[i].x, face.points[i].y, face.points[i].z);
        }
    }
    gl.glEnd();

Where master is the main "group" that contains all the faces and vertices. I've checked - everything goes into the master group, and glNormal3f and glVertex3f get called for every needed vertex in every triangle.

Can anyone tell what could be causing those glitches?

Edit 1:

Here is the code I'm using to set the projection and modelview matrices up:

float aspect = (float) width / (float) height;

gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();

glu.gluPerspective(60.0f, aspect, 0.01f, 100.0f);
glu.gluLookAt(2.0f, 1.0f, 1.5f,
        0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f);

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
4
  • First, are you sure all your faces defined in the obj file are triangles (Wavefront obj supports arbitrary face sizes)? Commented Nov 9, 2012 at 18:29
  • @pwny yes, I'm sure of that! For debugging purposes I only allow triangular faces to be defined Commented Nov 9, 2012 at 18:37
  • Where's your projection and modelview matrix setup code? Context creation? Commented Nov 9, 2012 at 19:15
  • genpfault: I've updated the question; As for the context creation, no special code is used for that; JOGL handles the basics automatically. Commented Nov 10, 2012 at 22:33

2 Answers 2

1

try removing gl.glDepthFunc(GL2.GL_LESS); gl.glClearDepth(1.0d);

Sign up to request clarification or add additional context in comments.

5 Comments

That didn't help, it's still rendering badly
gl.glCullFace(GL2.GL_FRONT);?
Well, that makes it look a bit better, but it's still only rendering half of the mesh, and some of the artifacts are still there.
may be normal vectors are broken? try to open with Blender and fix normals
i see that bunny is a standard model... but may be the source is wrong... also try to disable culling
0

Well, it seemed like this was being caused by a silly little bug in my object loader. When parsing my input vertices, it would chomp the first character of all the x coordinates of the geometry vertices. This didn't affect the positive values, since they were all normalized and 0.43 = .43, but it messed up all negative values causing them to be interpreted as positive.

It took me a while to figure that out, but I eventually found it and it just goes to show that the tiniest errors can be incredibly tricky to track down.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.