View Full Version : gluOrtho2D Problems
I'm trying to set up a simple 2D orthographic view with which I'm going to draw two quads. One quad will have my logo. The other quad, placed in front of the other one, will be colored black and fade out using the alpha channel. Here's the whole code to set it up. At the end there is a call to a SizeOpenGLScreen() function. That code works fine and sets my screen up with a perspective view for the first person view.
void DisplayLogo()
{
SDL_Event devent;
int i = 255; //alpha channel of fading box
GLuint texID;
Uint16 texWidth,texHeight;
GLushort *texImage;
if (CreateTexture2D(texID,
texWidth,
texHeight,
texImage,
"data/logo.png") != NULL)
{
cerr << "Error Loading Image" << endl;
Quit(0);
}
glViewport(0,0,1024,512);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1024,512,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while (i>0)
{
while (SDL_PollEvent(& devent)) //poll for an event
{
switch (devent.type) //compare the types of events
{
case SDL_QUIT : //if the event is the window closing
done = true; //set done to being true
break;
case SDL_KEYDOWN : //if the event is a keypress
HandleKeyPressEvent(&devent.key.keysym); //handle the keypress
break;
case SDL_KEYUP : //if the event is a key release
HandleKeyReleaseEvent(&devent.key.keysym); //handle the key release
break;
default: //if none of the above, do nothing
break;
}
}
//set our frame interval
FrameInterval = CalculateFrameRate();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the buffers
glLoadIdentity(); //reset our matrix
glEnable(GL_BLEND); //enable blending
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //set the blend function
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D); //enable texturing
glBindTexture(GL_TEXTURE_2D,texID); //bind the texture
glBegin(GL_QUADS); //draw a quad for our logo
glTexCoord2f(0,1);
glVertex2f(0,0);
glTexCoord2f(1,1);
glVertex2f(1024,0);
glTexCoord2f(1,0);
glVertex2f(1024,512);
glTexCoord2f(0,0);
glVertex2f(0,512);
glEnd();
glDisable(GL_TEXTURE_2D); //disable texturing
glBegin(GL_QUADS); //draw a black quad to fade out and make logo fade in
glColor4ub(0,0,0,i);
glVertex2f(0,0);
glVertex2f(1024,0);
glVertex2f(1024,512);
glVertex2f(0,512);
glEnd();
SDL_GL_SwapBuffers(); //swap the buffers
if (enterPressed) //if enter is pressed
{
i=0; //force the quitting of the logo displaying
}
i-=(RoundUp(15*FrameInterval)); //subtract from the alpha channel to fade front box
}
glEnable(GL_DEPTH_TEST);
SizeOpenGLScreen(SCREEN_WIDTH,SCREEN_WIDTH);
}
My problems are this:
1) No texture. I think I'm doing the texture coordinates wrong but I'm not sure what about them is wrong.
2) The box is about 40 pixels down from the top for some odd reason.
Any help is appreciated.
I found a third problem:
3) The point (1024,512) is WAY down and to the right of my screen.
sealfin
2004.09.06, 06:00 PM
Why do you specifiy glTexCoord2f(), and then use integer co-ordinates? Why not just use glTexCoord2ub?
Why do you use literal constants in the viewport/projection transformations (1024 et al), and pass to a function/method symbollic constants (SCREEN_WIDTH et al) with that meaning later on? Be consistent and you'll save yourself hassle...
Oh, and I've just seen this: SizeOpenGLScreen(SCREEN_WIDTH, SCREEN_WIDTH); :blink: Surely that second parameter should be the height?
Ok. I corrected the SizeOpenGLScreen() parameters and used the constants in place of the literal constants. When I changed to 2ub I got errors saying it was undeclared (glTexCoord2ub) so I went back to 2f.
So now my box does start in the upper left and spans perfectly to the lower right but the texture is still not there. It is a 1024 x 512 texture so it should work (plus it passed the error check).
sealfin
2004.09.06, 06:10 PM
Ok, I think you're problem is that the values you're passing to the parameters of glTexCoord2f() are not being converted properly; you should either use floating point values (glTexCoord2f( 0.0, 1.0); et al) or use an explicit cast (glTexCoord(( GLfloat )0, ( GLfloat )1 ); et al.) I've run into problems like this in the past, but Project Builder was kind enough to warn you, while Xcode seemingly just sniggers...
(I know there's an unsigned type version of glTexCoord*(), but I hardly use it so can't remember which unsigned types have a version; try glTexCoord2ui(), or check the documentation if you still need to use unsigned types.)
By the way, this isn't shown in your code, but GL_TEXTURE_2D is enabled when you load the texture?
It is in there right before I bind the texture and draw the quad. Do I need to turn it off before I draw the colored quad? I am just to be sure but I wasn't sure if it was necessary. I changed the values to float values and still no texture.
sealfin
2004.09.06, 06:14 PM
As I stated before, check that you've enabled GL_TEXTURE_2D before you call CreateTexture2D()
Ok I enable GL_TEXTURE_2D right at the start in my init code. I still enable it before drawing the textured quad and disable it afterwards (if I don't it won't draw either quad). I'm still not getting a texture. I'll try some more things.
aaronsullivan
2004.09.06, 06:18 PM
Um... there is no glTexCoord2ub. It would be nice if there was one. ;)
I'd comment out the polygon that fades thing. Just focus on getting your texture to show up. It looks okay at first glance. Sorry I don't have more time to check it out. As a major longshot, try
glDisable(GL_CULL_FACE);
before you draw the quad. :/
sealfin
2004.09.06, 06:26 PM
Erm, just realised this; I think you're drawing the textured quad using the alpha-channeled colour you're using for the fade out; call glColor4f( 1.0, 1.0, 1.0, 1.0 ); inside the glBegin( GL_QUADS ); before you render the textured quad and see what happens... if that doesn't help, I'd suggest you follow aaronsullivan's suggestion...
I commented out the fading quad, disabled GL_CULL_FACE, and set the color to full white and still no texture. I'm not sure what to try now.
arekkusu
2004.09.06, 06:50 PM
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1024,512,0);
This is fine; you can set your projection to whatever scale you want. But,
glViewport(0,0,1024,512);
the viewport scale should match your window or fullscreen scale. Looks like you've fixed that via your SizeOpenGLScreen().
the values you're passing to the parameters of glTexCoord2f() are not being converted properly; you should either use floating point values (glTexCoord2f( 0.0, 1.0); et al) or use an explicit cast (glTexCoord(( GLfloat )0, ( GLfloat )1 ); et al.)
No, glVertex2f(1024,512) is perfectly valid. The compiler will look at the prototype for glVertex2f, see that it takes floats, and cast 1024 to a float at compile time which is fine. It compiles to the exact same thing as glVertex2f(1024.0f, 512.0f).
This is not the same thing as
int x = 1024, y = 512;
glVertex2f(x, y);
since now there is run time cast.
glVertex2i() or glVertex2s() will take integer values, but they'll be converted into floats internally anyway, so just stick with glVertex2f().
glVertex2ub would only allow values in the range 0-255, so it wouldn't be very useful for anything other than NES emulators.
As I stated before, check that you've enabled GL_TEXTURE_2D before you call CreateTexture2D()
This doesn't matter. Texture application doesn't affect texture binding and upload.
One quad will have my logo. The other quad, placed in front of the other one, will be colored black and fade out using the alpha channel.
Why not just draw your one logo quad and fade it out with the alpha channel? Use glColor4ub(1,1,1,i);
I changed the code so that i counts up and the textured quad get it for an alpha channel so that it starts transparent (i=0) and goes to fully opaque (255). I'm still not seeing the texture, but I'm working on it.
After all the changes (only one quad, moved glEnable(GL_TEXTURE_2D), and one or two other things), here is the new code:
void DisplayLogo()
{
SDL_Event devent;
int i = 0; //alpha channel of fading box
GLuint texID;
Uint16 texWidth,texHeight;
GLushort *texImage;
if (CreateTexture2D(texID,
texWidth,
texHeight,
texImage,
"data/logo.png") != NULL)
{
cerr << "Error Loading Image" << endl;
Quit(0);
}
glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while (i<360)
{
while (SDL_PollEvent(& devent)) //poll for an event
{
switch (devent.type) //compare the types of events
{
case SDL_QUIT : //if the event is the window closing
done = true; //set done to being true
break;
case SDL_KEYDOWN : //if the event is a keypress
HandleKeyPressEvent(&devent.key.keysym); //handle the keypress
break;
case SDL_KEYUP : //if the event is a key release
HandleKeyReleaseEvent(&devent.key.keysym); //handle the key release
break;
default: //if none of the above, do nothing
break;
}
}
//set our frame interval
FrameInterval = CalculateFrameRate();
if (i<255)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the buffers
glLoadIdentity(); //reset our matrix
glEnable(GL_BLEND); //enable blending
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //set the blend function
glBindTexture(GL_TEXTURE_2D,texID); //bind the texture
glBegin(GL_QUADS); //draw a quad for our logo
glColor4ub(1,1,1,i);
glTexCoord2f(0.0,1.0);
glVertex2f(0,0);
glTexCoord2f(1.0,1.0);
glVertex2f(SCREEN_WIDTH,0);
glTexCoord2f(1.0,0.0);
glVertex2f(SCREEN_WIDTH,SCREEN_HEIGHT);
glTexCoord2f(0.0,0.0);
glVertex2f(0,SCREEN_HEIGHT);
glEnd();
SDL_GL_SwapBuffers(); //swap the buffers
}
if (enterPressed) //if enter is pressed
{
i=360; //force the quitting of the logo displaying
}
i+=(RoundUp(15*FrameInterval)); //subtract from the alpha channel to fade front box
}
glDisable(GL_TEXTURE_2D);
SizeOpenGLScreen(SCREEN_WIDTH,SCREEN_HEIGHT);
}
When ran, it simply displays nothing for a few seconds (so it's actually running through this stuff) then goes on to the rest of the game. I figure I'm doing something wrong after the glBegin(GL_QUAD); but I could be doing something wrong somewhere else to not have a textured quad.
arekkusu
2004.09.06, 10:41 PM
Make sure the texture has real alpha data? (you're not still throwing it away, are you?)
In the default GL_MODULATE texture mode, the texel color is multiplied by the current color, so if your alpha channel is all zeros, you'll never see anything.
Optionally, if you are just drawing the logo on top of a black background, and fading to black, then you don't need alpha or blending. You can just fade the current color from white to black.
If you're fading the logo out on top of something else, then you will need blending.
Thanks for simplifying it (removing the alpha channel). Now it looks great.
arekkusu: I just looked back into one of my other posts and was curious if you could help me understand your code a little better. I'm not quite sure how much of my code to get rid of and where to put yours in. I also don't know if there's more to it that you're assuming people know/knew. If you could post back in that one (I'm putting this here figuring you'll read it) I would really appreciate it. I'll reply in it with my code (back to it's unchanged, no mipmapping or alpha channel, state).
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.