[SOLVED] Problem with light (I think)

Hey all,

I created a 3D engine for a game I’m making, but I have a little problem. I think the problem has to do with the lighting, but I’m not sure. I was wondering if someone could help me out.

An image says a thousand words… so I attached an image explaining the problem. The left barbell, the correct one, is imported from a working OBJ importer I found. The one on the right is from my own loader, based on the working one.

As I said, I think the problem has to do with the lighting, but I’m not sure… Anyone help, please?

~Matt

P.S.: I’m pretty sure it’s the light, because if I render it without light & with the correct color (although it isn’t really material than) it shows up fine, as far as it can without light. The image showing this is below the other.

At the risk of stating the obvious, it looks like your normals are messed up. Have you tried drawing the normal for each vertex so you can see if they’re pointing in the correct direction?

At a guess you’ve either got the components for your normals switched (x instead of y, etc.) or your normals are being associated with the wrong vertices.

The normals are definitely associated with the wrong vertices.

When the X/Y would be swapped, you’d still have perfectly smooth shading, but it would look like the light was coming from another angle.

Normals, could be, I’ll check if that’s the problem.

Thanks a lot guys :smiley: Will keep you informed.

~Matt

It seems, fine, looking at the code. How would I draw the normals, can you tell me please? (A)

~Matt

To render the normals:

  1. use GL_LINES
  2. draw a line from the vertex to vertex+normal

Anyway, maybe you could dump some code.

Thanks for your quick response.

Okay, I added the normals, and at a first glance, it seems completely f**ked up. :\

What I think is weird, that if I both draw the normals and the barbell itself, they only appear half (the back is completely black), if I render just the barbell, it’s the same, and if I only render the normals, they appear completely. Why’s that?

Of course, it also is possible I didn’t correctly draw the normals. :\ Code I used the draw the normals:


// This for each plane/face/whatever you call them.
gl.glBegin(GL.GL_LINES); {
                    gl.glColor3f(255,255,255);
                    for (int i = 0; i < max; ++i) {
                        point = plane.getPoint3D(i);
                        point2 = plane.getNormal(i);
                        gl.glVertex3f(point.x+point2.x,point.y+point2.y,point.z+point2.z);
                    }
                } gl.glEnd();

I rather don’t release source code, but if you can’t help me without, I’m willing to mail (or so) you the src.

~Matt

Each line has 2 points…

gl.glVertex3f(point.x,point.y,point.z);
gl.glVertex3f(point.x+point2.x,point.y+point2.y,point.z+point2.z);

Yea… I was just thinking about that. Made that after but decided to await your reaction.

Seems fine to me, although I don’t know much about this stuff.

~Matt

P.S.: Same here, if I draw with the barbell, they only appear half, and completely without. Btw, it doesn’t matter where I put a light, w/e the color is, but the back remains dark.

I’m quite sure the normal indices you pick for your vertices are off by one.

But without sourcecode… who knows!

So… you’re doing:

foreach vertex glVertex3f glNormalf glTexCoord2f

OpenGL is a statemachine, so it remembers every attribute until it is changed.

You must first specify the vertex attributes (normals, colors, texcoords) and then you ‘finalize’ it by specifying the vertex.

Currently, your texcoords and normals end up at the ‘next’ vertex, which has the same effect as picking the attributes at the next offset (off by one).

This fixes it:
foreach vertex glNormalf glTexCoord2f glVertex3f

Thanks a lot! :smiley:
It worked, really thanks. :smiley:

Btw, did the code look okay, or was it ineffective/ugly? Please let me know, so I can improve it. :slight_smile:

~Matt

It’s goofy, but you should focus on your game.

Once you run into performance issues, there are plenty of sites to help you.

For now, make something playable, that’s the hardest part.