Sign in to follow this  
Bonzaii

OpenGL [SOLVED] OpenGL, nothing showing

Recommended Posts

Hello everyone, I have been beginning OpenGL and I can't see anything but a white background. I have no idea how to solve it and have tried a lot of things. I'm running Windows XP, using Visual Studio 2005. I'm using SDL to help set up the window for my OpenGL. Here is the code, pretty short and simple:
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
int fpsTimer;
bool quit = false;
Uint8* keys;
float angle = 1.0;
bool InitSDL()
{
	if(SDL_Init(SDL_INIT_VIDEO) != 0)
		return false;
	if(SDL_SetVideoMode(640, 480, 32, SDL_OPENGL) == NULL)
		return false;
	SDL_WM_SetCaption("OpenGL Test", NULL);
	return true;
}
bool InitGL()
{
	glViewport(0, 0, 640, 480);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f, 640 / 480, 1.0f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
	glShadeModel(GL_SMOOTH);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_LINE_SMOOTH);
	glEnable(GL_POINT_SMOOTH);
	glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
	glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
	glHint(GL_FOG_HINT, GL_NICEST);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	if(glGetError() != GL_NO_ERROR)
		return false;
	return true;
}
void DrawScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glRotatef(angle, 0.0f, 1.0f, 0.0f);
	glBegin(GL_POLYGON);
		glColor3f(  1.0f, 0.0f, 0.0f);
		glVertex3f(-1.0f,-1.0f, 0.0f);
		glColor3f(  0.0f, 1.0f, 0.0f);
		glVertex3f( 0.0f, 1.0f, 1.0f);
		glColor3f(  0.0f, 0.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, 0.0f);
	glEnd();
	SDL_GL_SwapBuffers();
}
float Timer_GetTicks()
{
	return SDL_GetTicks() - fpsTimer;
}
int main(int argc, char *argv[])
{
	if(InitSDL() != true)
	{
		printf("Unable to init SDL: %s\n", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);
	if(InitGL() != true)
	{
		printf("Unable to init OpenGL: %s\n", glGetError());
		exit(1);
	}
	SDL_Event event;
	while(quit == false)
	{
		fpsTimer = SDL_GetTicks();
		keys = SDL_GetKeyState(NULL);
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
				quit = true;
			if(event.type == SDL_KEYDOWN)
			{
				if(event.key.keysym.sym == SDLK_ESCAPE)
					quit = true;
			}
		}
		DrawScene();
//		if(Timer_GetTicks() < 1000 / 30)
//			SDL_Delay((float)(1000 / 30)-(float)Timer_GetTicks());
	}
	return 0;
}




Thanks in advance, Bonzaii! P.S. When I take out the gluPerspective() line, I see the spinning, colored triangle. [Edited by - Bonzaii on May 22, 2007 4:48:09 PM]

Share this post


Link to post
Share on other sites
I don't think so...because that line is in every example I see. I would like to know why it works for them and not for me.

Share this post


Link to post
Share on other sites
A quick [google] showed me this:

GLvoid InitGL(GLvoid)
{
objectID = 0; // this isn't OpenGL related per se, but it needs to be initialized

glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading

glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix

// LOOK HERE
// LOOK HERE
// LOOK HERE [READ COMMENT BELOW]
gluPerspective(45.0f, (GLfloat) kWindowWidth / (GLfloat) kWindowHeight, 0.1f, 100.0f);
// it seems you didn't make the width and height floats


// Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);


Share this post


Link to post
Share on other sites
It could be that you are not providing any kind of camera translation, so your scene sits right there at the origin, since your Z components of the triangle are zero, it gets drawn too closely to the camera, and your 1.0 near plane clips it.

Try translating it back a few (play with the z component), or add this line after the GL_MODEL_VIEW and glLoadIdentity():

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0, 0, -2, 0, 0, 0, 0, 1, 0 );

That -2 may have to be a 2 depending on the winding order of the triangle and your current culling mode.

edit: Looking more closely at your code, the axis through which you rotate should show similar results for looking at it from either side. Good luck to you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this