OGL cube being silly >:(

Discussion about everything. New games, 3d math, development tips...
Post Reply
ErUs
Posts: 165
Joined: Thu Oct 07, 2004 6:13 pm

OGL cube being silly >:(

Post by ErUs »

im bored at collegeso im trying todo opengl cube spinning. but the back faces are being drawn ontop of the front ones :/

how to make front drawn first???

Code: Select all

int DrawGLScene(GLvoid)								// Here's Where We Do All The Drawing
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer
	glLoadIdentity();							// Reset The Current Modelview Matrix

	glTranslatef( 0.0f,0.0f,-6.0f);					// Move back And Into The Screen 6.0

	glRotatef( trans, 1.0f, 5.0f, 2.5f);

	//front
	glBegin(GL_TRIANGLES);						// Drawing Using Triangles
		glColor3f(1.0f,0.0f, 0.0f);			// Font
		glVertex3f( -1.0f, 1.0f, -1.0f);				// Top left
		glVertex3f( -1.0f, -1.0f, -1.0f);				// bottomleft
		glVertex3f( 1.0f, -1.0f, -1.0f);				// bottomright

		glVertex3f( 1.0f, 1.0f, -1.0f);				// topright
		glVertex3f( -1.0f, 1.0f, -1.0f);			// topleft
		glVertex3f( 1.0f, -1.0f, -1.0f);

		//BACK

		glColor3f(0.0f,1.0f, 0.0f);			// Font
		glVertex3f( 1.0f, -1.0f, 1.0f);				// bottomright
		glVertex3f( -1.0f, -1.0f, 1.0f);				// bottomleft
		glVertex3f( -1.0f, 1.0f, 1.0f);				// Top left

		glVertex3f( 1.0f, -1.0f, 1.0f);
		glVertex3f( -1.0f, 1.0f, 1.0f);			// topleft
		glVertex3f( 1.0f, 1.0f, 1.0f);				// topright

		//TOP

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f( -1.0f, 1.0f, 1.0f);
		glVertex3f( -1.0f, 1.0f, -1.0f);
		glVertex3f( 1.0f, 1.0f, -1.0f);


	glEnd();



	trans += 0.05f;


	return TRUE;								// Everything Went OK
}
kburkhart84
Posts: 277
Joined: Thu Dec 15, 2005 6:11 pm

Post by kburkhart84 »

I used to code in OpenGL immediate mode, so maybe I can help. It looks like you have the depth buffer enabled, so that probably isn't it, rather I see you are only drawing six triangles, as in three quads. A cube needs six quads as I understand it, so you aren't covering quads rather never sending the verts for them to get drawn.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

I see five traingles?

Anyway make sure you didnt forget glflush();
I have no idea if thats the cause but you might have it outside that method.
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
kburkhart84
Posts: 277
Joined: Thu Dec 15, 2005 6:11 pm

Post by kburkhart84 »

omaremad wrote:I see five traingles?

Anyway make sure you didnt forget glflush();
I have no idea if thats the cause but you might have it outside that method.
You are right, there is only five. I don't think it is possible to draw a cube with 5 triangles.

Also, you might be doing your verts as if they were tri-stripped or quads or something like that, but with GL_TRIANGLES as the primitive, you must provide 3 verts(glVertex3f(...)) calls per triangle drawn. You can use GL_QUADS to send less verts, though supposedly some implementation split it into triangles anyway though. I'm not sure about that, but either way, you are only drawing 5 triangles with your posted code. Also, glFlush and glFinish aren't usually needed unless you need to do something with what is rendered. I don't remember exactly, but I think they do the same thing, except one return right away and the other doesn't, but both force the OpenGL pipeline to finish in "finite" time, so I don't really understand the use, except that if one doesn't return until OpenGL is finished drawing what you have sent it already, that it would be useful if you need to do something with the result, such as save the screen or something like that. I never used it in my code that I can remember and I made a couple of things with vanilla OpenGL, mostly wrapped up in a class though.
ErUs
Posts: 165
Joined: Thu Oct 07, 2004 6:13 pm

Post by ErUs »

yes 5 triangles, didn't bother doing any more yet as these ones arent working properly :O

how to enable depth testing/buffer ness?
kburkhart84
Posts: 277
Joined: Thu Dec 15, 2005 6:11 pm

Post by kburkhart84 »

I believe that happens where you describe the pfd thingy. (PixelFormatDescriptor). You have to specify as well about color depth and stencil buffer. You call a function with this thingy and it returns a pixel type, which you then send to another function to "choose" this pixel type. It is in the init code.
ErUs
Posts: 165
Joined: Thu Oct 07, 2004 6:13 pm

Post by ErUs »

Code: Select all

	static	PIXELFORMATDESCRIPTOR pfd=					// pfd Tells Windows How We Want Things To Be
	{
		sizeof(PIXELFORMATDESCRIPTOR),					// Size Of This Pixel Format Descriptor
		1,								// Version Number
		PFD_DRAW_TO_WINDOW |						// Format Must Support Window
		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,						// Must Support Double Buffering
		PFD_TYPE_RGBA,							// Request An RGBA Format
		16,								// Select Our Color Depth
		0, 0, 0, 0, 0, 0,						// Color Bits Ignored
		0,								// No Alpha Buffer
		0,								// Shift Bit Ignored
		0,								// No Accumulation Buffer
		0, 0, 0, 0,							// Accumulation Bits Ignored
		16,								// 16Bit Z-Buffer (Depth Buffer)
		0,								// No Stencil Buffer
		0,								// No Auxiliary Buffer
		PFD_MAIN_PLANE,							// Main Drawing Layer
		0,								// Reserved
		0, 0, 0								// Layer Masks Ignored
	};

	if (!(hDC=GetDC(hWnd)))
		return 0;

	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))
		return 0;

	if(!SetPixelFormat(hDC,PixelFormat,&pfd))
		return 0;

	if (!(hRC=wglCreateContext(hDC)))
		return 0;

	if(!wglMakeCurrent(hDC,hRC))
		return 0;
is this right?
kburkhart84
Posts: 277
Joined: Thu Dec 15, 2005 6:11 pm

Post by kburkhart84 »

I think I've got it figured out. If you didn't change the default, back-face culling is on. Also the default is counter-clock order for vertices. What happens looking at the first triangle is this.
1
|\
| \
2-----3
(Crappy art)
But the first triangle is on the other side since you have -1 for the Z values. +1 is towards the user, -1 away into the screen. So what happens is that you are drawing the triangle counter-clockwise like this, and the face that is actually drawing is "inside" the cube, not the outside one, because the "outside" triangle is culled due to the vertex winding. Change the order of the verts and it should work.

Also, the init looks fine, unless you are going to want to actually go farther into using OGL. Lots of games need a stencil buffer, and I could be wrong, but the most optimized depths for depth and stencil buffers is supposedly 24 and 8 respectively since it fills exactly 32 bits together. I heard something like video cards optimize operations a little bit when the setup is like this. But if you are just playing around, you probably don't even need the stencil buffer.
ErUs
Posts: 165
Joined: Thu Oct 07, 2004 6:13 pm

Post by ErUs »

kburkhart84 wrote:I think I've got it figured out. If you didn't change the default, back-face culling is on. Also the default is counter-clock order for vertices. What happens looking at the first triangle is this.
1
|\
| \
2-----3
(Crappy art)
But the first triangle is on the other side since you have -1 for the Z values. +1 is towards the user, -1 away into the screen. So what happens is that you are drawing the triangle counter-clockwise like this, and the face that is actually drawing is "inside" the cube, not the outside one, because the "outside" triangle is culled due to the vertex winding. Change the order of the verts and it should work.

Also, the init looks fine, unless you are going to want to actually go farther into using OGL. Lots of games need a stencil buffer, and I could be wrong, but the most optimized depths for depth and stencil buffers is supposedly 24 and 8 respectively since it fills exactly 32 bits together. I heard something like video cards optimize operations a little bit when the setup is like this. But if you are just playing around, you probably don't even need the stencil buffer.
Oh thanks m8. i thought -1 Z was towards the screen :O
kburkhart84
Posts: 277
Joined: Thu Dec 15, 2005 6:11 pm

Post by kburkhart84 »

ErUs wrote: Oh thanks m8. i thought -1 Z was towards the screen :O
Yeah. That would also fix it. With D3D, Z axis is backward from OpenGL. Irrlicht's OpenGL driver probably reverses the axis(or uses scale or something) so it uses the same system as DX.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You can also change the front face rotation.
Post Reply