Jump to content
  • Advertisement
Sign in to follow this  
TamGarTrinKi

OpenGL Problem with OpenGL (beginners stuff)

This topic is 3966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Having small problems with one of the earlier tutorial problems. I am using Visual Studio 2005 and glut (which you can probably see) Problem #1: The 'pyramid' that I have drawn there always draws its sides in the same order, so as it rotates it appears to be see through on some sides. I could of sworn there was a matrix mode that fixed that up, but I can not seem to find it. Problem #2: Is it possible to do this without the push/pop matrix calls in the draw function? The NeHe tutorial does not have them, but the other tutorial that I got the glut stuff from does. If I just take them out now the 'pyramid' then has both rotations applied to it instead of just the one intended for it.
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>

GLfloat rotateT, rotateS;
static void draw(void);
int main(int argc, char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
	glutCreateWindow("This be my window");
	glutDisplayFunc(draw);
	glutIdleFunc(draw);
	glMatrixMode(GL_PROJECTION);
	gluPerspective(50,1,0.0,200.0);
	glMatrixMode(GL_MODELVIEW);
	glutMainLoop();            
	return 0;
}

static void draw(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glTranslatef(-1.5,0,-6);
	glRotatef(rotateT,0,1,0);
	glBegin(GL_TRIANGLES);
		glColor3f(1,0,0);
		glVertex3f(0,1,0);
		glColor3f(0,1,0);
		glVertex3f(1,-1,1);
		glColor3f(1,1,0);
		glVertex3f(-1,-1,1);
		glColor3f(1,0,0);
		glVertex3f(0,1,0);
		glColor3f(1,1,0);
		glVertex3f(-1,-1,1);
		glColor3f(0,0,1);
		glVertex3f(-1,-1,-1);
		glColor3f(1,0,0);
		glVertex3f(0,1,0);
		glColor3f(0,0,1);
		glVertex3f(-1,-1,-1);
		glColor3f(1,0,1);
		glVertex3f(1,-1,-1);
		glColor3f(1,0,0);
		glVertex3f(0,1,0);
		glColor3f(1,0,1);
		glVertex3f(1,-1,-1);
		glColor3f(0,1,0);
		glVertex3f(1,-1,1);
	glEnd();
	glLoadIdentity();
	glTranslatef(1.5f,0.0f,-6.0f);
	glRotatef(rotateS,1,0,0);
	glColor3d(255,0,0);
	glBegin(GL_QUADS);
	glVertex3f(-1,1,0);
	glVertex3f(1,1,0);
	glVertex3f(1,-1,0);
	glVertex3f(-1,-1,0);
	glEnd();
	glPopMatrix();
	rotateT += 0.02;
	rotateS -= 0.15;
	glutSwapBuffers();
}

Share this post


Link to post
Share on other sites
Advertisement
First off where is your projection setup code at? e.g. glOrtho or gluPerspective?
YOu need to set your near/far planes up to say 1.0 and 100.0

glPushMatrix and glPopMatrix are your friends. Learn them you will need the later on...

You should call them like so with the code you have below


static void draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(-1.5,0,-6);
glRotatef(rotateT,0,1,0);
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(0,1,0);
glVertex3f(1,-1,1);
glColor3f(1,1,0);
glVertex3f(-1,-1,1);
glColor3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(1,1,0);
glVertex3f(-1,-1,1);
glColor3f(0,0,1);
glVertex3f(-1,-1,-1);
glColor3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(0,0,1);
glVertex3f(-1,-1,-1);
glColor3f(1,0,1);
glVertex3f(1,-1,-1);
glColor3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(1,-1,-1);
glColor3f(0,1,0);
glVertex3f(1,-1,1);
glEnd();
glPopMatrix();

glPushMatrix();
glTranslatef(1.5f,0.0f,-6.0f);
glRotatef(rotateS,1,0,0);
glColor3d(255,0,0);
glBegin(GL_QUADS);
glVertex3f(-1,1,0);
glVertex3f(1,1,0);
glVertex3f(1,-1,0);
glVertex3f(-1,-1,0);
glEnd();
glPopMatrix();

rotateT += 0.02;
rotateS -= 0.15;
glutSwapBuffers();
}



Share this post


Link to post
Share on other sites
@#1:
You have specified all Triangles in the same order, so you can either use backfaceculling or enable the DepthTest.

for backfaceculling use: glCullFace(GL_FRONT); glEnable(GL_CULL_FACE);

for hidden surface removal use glEnable(GL_DEPTH_TEST); glClearDepth(1.0f);

both belong to the main function after glMatrixMode(...) is called.

Share this post


Link to post
Share on other sites
Mars, the gluPerspective is there in the main between the two matrixmode calls

glMatrixMode(GL_PROJECTION);
gluPerspective(50,1,0.0,200.0);
glMatrixMode(GL_MODELVIEW);

Ok, I see what the cull face did, made only one side of the plane visible (as is evident by the flipping square that dissapears). This works fine on the pyramid but only because all the planes are facing outward. (if I reverse one of the planes (by switching the order of two of its points) it gets drawn over the top again)

The depth test makes the whole thing flicker really badly, and makes all sides of the pyramid appear see through (although that could be due to flickering altering images, not sure) I did try the depth code before, after, and inbetween the matrixmode calls, made no difference.

Share this post


Link to post
Share on other sites
Your program have two major mistakes.

Firstly, the near clip plane of perspective matrix cannot be placed at zero distance. Otherwise, it will mess up the calculation. And, you should change it like this.

gluPerspective(50, 1, 0.1, 200.0);

Secondly, you must have depth test enabled for your 3D model to be drew correctly. Otherwise, any triangles draw later will overwrite the previous one.
To enable depth test, you need to insert the following lines at the initialization.

glEnable( GL_DEPTH_TEST );

Culling is a good practice to boost the framerate. But, it is usually not necessary for drawing a 3D model correctly. To take the advantage offered by culling, your 3D model should be closed and should have a consistant winding. Roughly speaking, it can double the framerate in some cases.

Share this post


Link to post
Share on other sites
Quote:
Original post by TamGarTrinKi
Ok, I see what the cull face did, made only one side of the plane visible (as is evident by the flipping square that dissapears). This works fine on the pyramid but only because all the planes are facing outward. (if I reverse one of the planes (by switching the order of two of its points) it gets drawn over the top again)

The depth test makes the whole thing flicker really badly, and makes all sides of the pyramid appear see through (although that could be due to flickering altering images, not sure) I did try the depth code before, after, and inbetween the matrixmode calls, made no difference.


Jep, that's right, the culling just works when all your triangles are specified in the same order, but you'd better do that anyway.

The GLUT_DOUBLE in conjunction with glutSwapBuffers() should eliminate the flickering effect. Don't know why it doesn't. i'm not using Depth test in my application, so probably someone else has a better answer to that.

@my_hty: true, new clipping plane set to 0 means that your imageplane is a dot instead of a plane. Maybe OpenGL changed it internally.

Share this post


Link to post
Share on other sites
Ah, that did it, once I set that near clipping plane to 0.1 instead of 0.0 the depth test no longer made it flicker and solved the drawing behind other planes.

Ok, so push/pop calls added in, culling added, depth added, near plane shifted and now on to making that cube more complete

Thanks all for help.

Fixed and the current working code.
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>

GLfloat rotateT, rotateS;
static void draw(void);
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("This be my window");
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glutDisplayFunc(draw);
glutIdleFunc(draw);
glMatrixMode(GL_PROJECTION);
gluPerspective(50,1,0.1,200.0);
glMatrixMode(GL_MODELVIEW);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}

static void draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Pyramid
glPushMatrix();
glTranslatef(-1.5,0,-6);
glRotatef(rotateT,0,1,0);
//Pointy bits
glBegin(GL_TRIANGLES);
//Face 1
glColor3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(0,1,0);
glVertex3f(1,-1,1);
glColor3f(1,1,0);
glVertex3f(-1,-1,1);
// Face 2
glColor3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(1,1,0);
glVertex3f(-1,-1,1);
glColor3f(0,0,1);
glVertex3f(-1,-1,-1);
// Face 3
glColor3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(0,0,1);
glVertex3f(-1,-1,-1);
glColor3f(1,0,1);
glVertex3f(1,-1,-1);
//Face 4
glColor3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(1,-1,-1);
glColor3f(0,1,0);
glVertex3f(1,-1,1);
glEnd();
// Base
glBegin(GL_QUADS);
glColor3f(0,1,0);
glVertex3f(1,-1,1);
glColor3f(1,0,1);
glVertex3f(1,-1,-1);
glColor3f(0,0,1);
glVertex3f(-1,-1,-1);
glColor3f(1,1,0);
glVertex3f(-1,-1,1);
glEnd();
glPopMatrix();
//To be cube
glPushMatrix();
glTranslatef(1.5f,0.0f,-6.0f);
glRotatef(rotateS,1,0,0);
glColor3d(1,0,0);
glBegin(GL_QUADS);
// Face 1
glVertex3f(-1,1,0);
glVertex3f(1,1,0);
glVertex3f(1,-1,0);
glVertex3f(-1,-1,0);
glEnd();
glPopMatrix();
rotateT += 0.02;
rotateS -= 0.15;
glutSwapBuffers();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!