SDL w/ OpenGL. No backface culling

Started by
9 comments, last by zedz 17 years ago
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]
-------------------------- Alex--------------------------
Advertisement
Well you didn't setup your depth testing to use the correct mode


glDepthFunc(GL_LEQUAL);


Try that.
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.
-------------------------- Alex--------------------------
Did you set these parameters up then?

SDL_GL_DEPTH_SIZE

SDL_GL_STENCIL_SIZE

I set the depth size to 16. I haven't done the Stencil Buffer yet because I haven't actually used it. Is it still necessary that I do so?
-------------------------- Alex--------------------------
Just gonna throw this out there. Are you sure your polys all have the correct vertex winding? ( you have counter clock wise specified ).
==============================
A Developers Blog | Dark Rock Studios - My Site
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.
-------------------------- Alex--------------------------
Cool glad you got it atleast partially working.
==============================
A Developers Blog | Dark Rock Studios - My Site
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.
hackerkey://v4sw7+8CHS$hw6+8ln6pr8O$ck4ma4+9u5Lw7VX$m0l5Ri8ONotepad++/e3+8t3b8AORTen7+9a17s0r4g8OP
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.

This topic is closed to new replies.

Advertisement