Jump to content
  • Advertisement
Sign in to follow this  
Prodigal Squirrel

OpenGL SDL w/ OpenGL. No backface culling

This topic is 4094 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

Well, I recently wrote a program to practice using the Stencil Buffer. I used SDL for setup. Now, I thought I did everything correctly, but when it renders, it doens't use backface culling. It's almost as though it's rendering backward as the faces further back get drawn right over top of the closer faces. Here's some of my setup code:
bool init(){
	/*** Initialize SDL, Create the window, and initialize OpenGL. ***/
	if( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
		return false;
	
	if( SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL|SDL_RESIZABLE|SDL_GL_DOUBLEBUFFER) == NULL)
		return false;
	
	if( initGL() == false)
		return false;
		
	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
	resizeWindow(SCREEN_WIDTH, SCREEN_HEIGHT);
	SDL_WM_SetCaption("ETGG 202 Lab 1 - Stencil Halo", NULL);
	SDL_EnableKeyRepeat(10, 10);
	
	return true;
}

bool initGL(){
	glClearColor(0.0, 0.0, 0.0, 0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_COLOR_MATERIAL);
	glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
	
	//// Rendering 
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	
	//// Lighting
	setAmbience(1.0, 1.0, 1.0, 1.0);
	setDiffuse(1.0, 1.0, 1.0, 1.0);
	setSpecular(1.0, 1.0, 1.0, 1.0);
	setLightPosition(-1.0, -1.0, -1.0, 1.0);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glShadeModel(GL_SMOOTH);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
	
	if(glGetError() != GL_NO_ERROR)
		return false;
	
	return true;
}

void draw(){
	Vector3 pos = camera.getPosition();
	Vector3 center = pos+camera.getDirectionVector();
	Vector3 up = camera.getUpVector();
	
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	gluLookAt(pos.x(), pos.y(), pos.z(),
						center.x(), center.y(), center.z(),
						up.x(), up.y(), up.z());
	
	glLightfv(GL_LIGHT0, GL_POSITION, light);
	
	// Draws a quad as a basic surface
	glBegin(GL_QUADS);
		glColor3f(0.0, 0.8, 0.0);
		glVertex3f(-10, 0, -10);
		glVertex3f(10, 0, -10);
		glVertex3f(10, 0, 10);
		glVertex3f(-10, 0, 10);
		glColor3f(0.0, 0.0, 0.8);
		glVertex3f(-10, 10, -10);
		glVertex3f(10, 10, -10);
		glVertex3f(10, 0, -10);
		glVertex3f(-10, 0, -10);
		glColor3f(0.8, 0.0, 0.0);
		glVertex3f(-10, 10, 10);
		glVertex3f(10, 10, 10);
		glVertex3f(10, 0, 10);
		glVertex3f(-10, 0, 10);
	glEnd();
	
	glColor3f(1.0, 1.0, 1.0);
	
	/* Draw our meshes
	vector<Mesh>::iterator i = meshes.begin();
	for(i; i!=meshes.end(); i++)
		i->draw();*/
	
	SDL_GL_SwapBuffers();
}

[Edited by - Prodigal Squirrel on April 8, 2007 7:22:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
I did have that in there... I must've took it out trying to find out what was going on. Anywho, thanks for the advice, but no dice, it didn't change anything. I have even messed with the commands for culling:

glCullFace( GL_BACK );
glFrontFace( GL_CCW );
glEnable( GL_CULL_FACE );

But even still, my model gets drawn all funky. I use vertex arrays to draw the model, btw. Oh, I made it draw a cube using triangles with glBegin() and glEnd(), and it worked just fine. But every mesh I load in and draw acts funny(I can see the back of the chair modelwhen I look up at it from underneath it), and I'm using an object loader and models from a previous program, which worked 100%. The only difference in my code thus far is that I set it up with SDL instead of the Windows API.

Share this post


Link to post
Share on other sites
Just gonna throw this out there. Are you sure your polys all have the correct vertex winding? ( you have counter clock wise specified ).

Share this post


Link to post
Share on other sites
I'm glad you said something about that actually. Because I found out that the code in between my glBegin() and glEnd() were, infact, not CCW. lol

I found some code of a cube that was CCW and it worked. So I loaded in my objects, which should also be CCW, and they didn't work. What's weird, is that they've worked in a previous project I did. So... I don't know. At least I now know that it's not a problem with backface culling.

Share this post


Link to post
Share on other sites
Quote:
Original post by MARS_999
Well you didn't setup your depth testing to use the correct mode


*** Source Snippet Removed ***

Try that.


GL_LEQUAL is the default - adding that will do nothing unless he already set it to something else.

Share this post


Link to post
Share on other sites
Quote:
Original post by nemesisgeek
Quote:
Original post by MARS_999
Well you didn't setup your depth testing to use the correct mode


*** Source Snippet Removed ***

Try that.


GL_LEQUAL is the default - adding that will do nothing unless he already set it to something else.


NO its not. GL_LESS is.



PARAMETERS
func Specifies the depth comparison function. Symbolic
constants GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, and GL_ALWAYS are
accepted. The initial value is GL_LESS.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!