Get blank screen in modelview, don't know why

Started by
6 comments, last by Jossos 10 years, 4 months ago

I'm not understanding why I can't get anything rendered to the screen. I have code very similar to this in another program which renders fine, but this program just gives me a blank screen.

Here's my initialization code:


void GRAPHICS::InitGraphics(SDL_Surface* screen, bool fullscreen, int width, int height)
{
	SDL_Init(SDL_INIT_EVERYTHING);

	glClearColor(0.0, 0.0, 0.0, 1.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(35.0, width / height, 0.2, 500.0);

	//SetScreenAttributes(screen, fullscreen, width, height);
	SDL_SetVideoMode(width, height, 32, SDL_OPENGL);

	glMatrixMode(GL_MODELVIEW);

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

And here's my render function:


void GAME::Render()
{
		// ** Clear the screen and translations for a new render ** //
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glMatrixMode(GL_PROJECTION);
	glOrtho(0, header.scrWidth, header.scrHeight, 0.0, -1.0, 1.0);
	glColor3f(1.0f, 1.0f, 1.0f);

	// 2D drawings here

		// ** Put all Drawing data here ** //

	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	gluPerspective(35.0, (float)header.scrWidth / (float)header.scrHeight, 0.2, 500.0);

	glColor3f(1.0f, 1.0f, 1.0f);

	glBegin(GL_TRIANGLES);
		glVertex3f(0.0f, 0.0f, 0.0f);
		glVertex3f(0.5f, 0.0f, 0.0f);
		glVertex3f(0.0f, 0.5f, 0.0f);
	glEnd();

	SDL_GL_SwapBuffers();
}

I'm sure the fault lies somewhere in one of these functions. I have other code, but for now a simple triangle on the screen would be nice, but I can't even get that. the clear color is black by the way.

Help is greatly appreciated!

Advertisement

You're using glLoadIdentity weirdly, and also you should not use gluPerspective for modelview matrix. Also, you probably want to set the triangle's z to something like -1 so you can actually see it.


// Set up projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Load identity matrix to the current matrix mode, projection
gluPerspective(...);

// Set up modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Load identity matrix to the current matrix mode, modelview

// Draw
glBegin(GL_TRIANGLES);
// ...
glEnd();

Derp

You're using glLoadIdentity weirdly, and also you should not use gluPerspective for modelview matrix. Also, you probably want to set the triangle's z to something like -1 so you can actually see it.

Thanks, but I still have a blank screen.

My render function now:


void GAME::Render()
{
		// ** Clear the screen and translations for a new render ** //
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(35.0, (float)header.scrWidth / (float)header.scrHeight, 0.2, 500.0);

		// ** Put all Drawing data here ** //

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glColor3f(1.0f, 1.0f, 1.0f);

	glBegin(GL_TRIANGLES);
		glVertex3f(0.0f, 0.0f, 0.0f);
		glVertex3f(0.5f, 0.0f, 0.0f);
		glVertex3f(0.0f, 0.5f, 0.0f);
	glEnd();

	SDL_GL_SwapBuffers();
}

Take Sponji's second sentence as an advice again. Your projection matrix has a visible depth range from Z=-0.2 to Z=-500, and you draw your triangle at Z=0, so it's not visible.

Take Sponji's second sentence as an advice again. Your projection matrix has a visible depth range from Z=-0.2 to Z=-500, and you draw your triangle at Z=0, so it's not visible.

Yeah sorry, I did that and still nothing. I set the z to -5.0 and still blank. sorry for bein such a noob

sorry for bumping, but i really cannot get this working and have no idea why. i have 2 triangles, one at z -5 and z 5, just blank screen -.-


void GAME::Render()
{
		// ** Clear the screen and translations for a new render ** //
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	gluPerspective(35.0, 1280 / 720, 0.2, 500.0);

		// ** Put all Drawing data here ** //

	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);

	glColor3f(1.0f, 1.0f, 1.0f);
	//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );

	glBegin(GL_TRIANGLES);
		glVertex3f(0.0f, 0.0f, -5.0f);
		glVertex3f(0.5f, 0.0f, -5.0f);
		glVertex3f(0.0f, 0.5f, -5.0f);

		glVertex3f(0.0f, 0.0f, 5.0f);
		glVertex3f(0.5f, 0.0f, 5.0f);
		glVertex3f(0.0f, 0.5f, 5.0f);
	glEnd();

	SDL_GL_SwapBuffers();
}

You're resetting your projection matrix with glLoadIdentity? You should change to modelview matrix first, then call glLoadIdentity and it will reset the modelview matrix.

Derp

Edit: Thanks all, working nice now.

This topic is closed to new replies.

Advertisement