Extra Vertices specified? -OpenGL problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Extra Vertices specified? -OpenGL problem

Post 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!
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: Extra Vertices specified? -OpenGL problem

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Extra Vertices specified? -OpenGL problem

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Extra Vertices specified? -OpenGL problem

Post 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?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Extra Vertices specified? -OpenGL problem

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply