Page 1 of 1

Extra Vertices specified? -OpenGL problem

Posted: Wed Feb 01, 2012 11:30 am
by N64vSNES
I swear my brain isn't working lately....

Say I have this array:

Code: Select all

static float CubeFace_Top[12] = {
	-1, 1, -1,
	-1, 1, 1,
	1, 1, 1,
	1, 1, -1
};
And this code:

Code: Select all

	for ( int x = 0;x < 8;x++ )
	{
		for ( int z = 0;z < 8;z++ )
		{
			glPushMatrix();
			glTranslatef((float)x, 0, (float)-z);
			glBegin(GL_QUADS);
			glVertex3f(CubeFace_Top[0], CubeFace_Top[1], CubeFace_Top[2]);
			glVertex3f(CubeFace_Top[3], CubeFace_Top[4], CubeFace_Top[5]);
			glVertex3f(CubeFace_Top[6], CubeFace_Top[7], CubeFace_Top[8]);
			glVertex3f(CubeFace_Top[9], CubeFace_Top[10], CubeFace_Top[11]);
			glEnd();
			glPopMatrix();
		}
	}
It should give a somewhat tiled result, right?
Well, yes it works just fine.

But if I set the polygons raster mode to GL_LINE via glPolygonMode().

Then here is the result:
Image

This isn't anything I haven't encountered before, but it is usually caused by submitting a number of vertices that can't be equally divided by 4 (for quads anyway).

Anyone takers? I've tried everything I can think of, I originally thought I was setting up the vertex arrays wrong but the screenshot was rendered using the immediate mode as shown in the code.

I'm sure I'm missing something obvious, I literally sent 2 hours figuring out why depth sorting wasn't working yesterday when I hadn't enabled depth testing :nono:

Thanks!

Re: Extra Vertices specified? -OpenGL problem

Posted: Wed Feb 01, 2012 4:51 pm
by Nokurn
Put a different color on each vertex to pinpoint which glVertex3f call is causing problems. Put a breakpoint on that line and go through the entire loop once to see if some weird shit's going on with your Y coordinate.
Another thing you might want to try is dumping the view matrix after every glTranslatef call and checking each one to see if your transform is causing the fuckery.
After that, try progressively cutting back your code until you get the problem to go away. Then further investigate the piece of code that you suspect caused it. I can't see any issues here. If you can reduce the problem sufficiently that you could provide more source code, I'd be happy to take a closer look.

Re: Extra Vertices specified? -OpenGL problem

Posted: Wed Feb 01, 2012 8:07 pm
by Ginto8
Counting lines, the problem occurs on the 8th quad along the Z axis. It almost looks to me as though one of your vertices is getting clipped out by the far clipping plane, giving you the weird triangles. Have you tried moving your view around? If this problem is recurring regardless of location, then this wouldn't be the problem, but if it fixes when you move closer then it might be.

Re: Extra Vertices specified? -OpenGL problem

Posted: Thu Feb 02, 2012 9:00 am
by N64vSNES
Thanks for the replies!

It seems to only occur whenever the the vertices are close to the nearest viewpoint. (1.0). Or more specifically as Gino8 said, whenever some of the vertices are skipped from the clipping.

If they are transformed to a reasonably distance away, it works fine. I guess OpenGL clips the vertices and then joins them, instead of the other way around (which makes sense I guess).
Is there anything I can do about this though? Or better, should I do anything about it?

Re: Extra Vertices specified? -OpenGL problem

Posted: Thu Feb 02, 2012 7:46 pm
by Ginto8
You can change the far clipping plane by changing your call to gluPerspective() (or glFrustrum, whichever you use). It's also possible to use glDepthRange, but this clips normalized device coordinates instead of changing the mapping from view space to clip space, and probably isn't quite suited to your needs.