SDL + OpenGL: Rendering A Cube

Started by
1 comment, last by Shonumi 13 years, 8 months ago
So I finally decided it was time to at least start fiddling around with some 3D programming after having gotten a decent amount of 2D work done, and I'm using SDL in conjunction with OpenGL for a relatively painless cross-platform experience. I've only started learning it as of today combining things from the Lazy Foo' tutorials and bits from NeHe Productions. I'm using C++, but I'm also a bit stuck on the 5th lesson from NeHe on building 3D shapes.

My problem is that I can get most of the cube there and rendered, but some parts of the cube appear transparent when viewed from one side, but the opposite viewing angle shows its solid color. It seems very weird to me, but unfortunately I'll have to post a screenshot of it later. For now, I have the source code, and I believe it's something wrong with my OpenGL rather than the SDL side of things.

/****** Initilization Header - Setup & Prepares Key Game Items ******/#include "SDL/SDL.h"#include "SDL/SDL_opengl.h"const int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;SDL_Event event;/****** Name Space for Game Variables ******/namespace var {	int frame_start_time = 0;	int frame_current_time = 0;	int frame_count = 0;}/****** Initializes Core OpenGL Features ******/bool opengl_init() {	glClearColor(0, 0, 0, 0);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0, (640.0/480.0), 0.1, 100.0);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	if(glGetError() != GL_NO_ERROR)	{		return false;	}	return true;}/****** Initializes SDL, OpenGL and Video and Window ******/bool init(){    	if(SDL_Init(SDL_INIT_EVERYTHING) < 0)	{		return false;	}	if(SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL) == NULL)	{		return false;	}	if(opengl_init() == false)	{		return false;	} 	SDL_WM_SetCaption("SDL-OpenGL Graphics : Part IV - 3D Shapes", NULL);	return true;}/* Removes objects before game closes */void clean_up(){    	SDL_Quit();    }


This is just my initialization header and it sets up an OpenGL window and an OpenGl environment.

#include "init.h"int main(int argc, char* args[]){	GLfloat rotation = 0.0f;	bool quit = false;    	if(init() == false)	{		return 1;    	}	/****** Main Game Loop ******/        	while(quit == false)	{		/****** Get Most Current Time ******/		var::frame_start_time = SDL_GetTicks();		/****** Draw Rectangle ******/		glClear(GL_COLOR_BUFFER_BIT);		glTranslatef(0.0f, 0.0f, -6.0f);		glRotatef(rotation, 1.0f, 1.0f, 0.0f);		glBegin(GL_QUADS);		/* Cube Top */		glColor4f(1.0f, 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);		glVertex3f(1.0f, 1.0f, 1.0f);		/* Cube Bottom */		glColor4f(1.0f, 0.5f, 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);		glVertex3f(1.0f, -1.0f, 1.0f);		/* Cube Front */		glColor4f(0.0f, 1.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);		glVertex3f(-1.0f, -1.0f, 1.0f);		/* Cube Back */		glColor4f(0.0f, 1.0f, 0.5f, 1.0f);		glVertex3f(-1.0f, 1.0f, -1.0f);		glVertex3f(1.0f, 1.0f, -1.0f);		glVertex3f(1.0f, -1.0f, -1.0f);		glVertex3f(-1.0f, -1.0f, -1.0f);		/* Cube Left Side */		glColor4f(0.5f, 0.5f, 0.5f, 1.0f);		glVertex3f(-1.0f, 1.0f, -1.0f);		glVertex3f(-1.0f, 1.0f, 1.0f);		glVertex3f(-1.0f, -1.0f, 1.0f);		glVertex3f(-1.0f, -1.0f, -1.0f);		/* Cube Right Side */		glColor4f(0.15f, 0.25f, 0.75f, 1.0f);		glVertex3f(1.0f, 1.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();		glLoadIdentity();		rotation -= 0.5f;		/****** Check for Key & System input ******/    		while(SDL_PollEvent(&event))		{  			/******  Application Quit Event ******/           			if(event.type == SDL_QUIT)			{  				quit = true;			}		}		/****** Update Screen And Frame Counts ******/		SDL_GL_SwapBuffers();		var::frame_count++;		var::frame_current_time = SDL_GetTicks();		/****** Frame Rate Handle ******/		if((var::frame_current_time - var::frame_start_time) < (1000/60))		{			var::frame_count = 0;			SDL_Delay((1000/60) - (var::frame_current_time - var::frame_start_time));		}	}     clean_up();        return 0;    }


And this here is the main source file. Like I have said, I just started learning OpenGL, so it's probably something small I've missed, but I'm not too sure just what it is. I've had to take bits and pieces from different tutorials mainly because the NeHe SDL examples for Linux aren't up to date (and in C).

I'm pretty sure I drew each vertex correctly; I replaced mine with one's from the NeHe tutorial and it still gave me the same results. It's not a problem with my graphics driver either. I tried this on both my desktop and laptop. I've a feeling I may have left out a few things that OpenGL needs when its initialized, but I haven't a clue what they may be in this case as I've been running fine up until now. In a while, I'll get back here with a screenshot to further clarify my problem.

EDIT:
http://img695.imageshack.us/img695/7577/problemjt.png

As one can see from the image, the cube clearly seems to have several transparent sides, but when rotated in the program, the colors appear.

[Edited by - Shonumi on August 15, 2010 11:05:21 PM]
Advertisement
The problems is that you didn't enabled the depth buffer.
Add this in the top of your opengl_init():

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

and add this to glClear:

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
Hey, it's working now! Thanks! I guess I should have examined the tutorials a bit closer, it seems so simple now.

This topic is closed to new replies.

Advertisement