PDA

View Full Version : How do I place a texture over the entire screen



crispy
03-27-2004, 01:50 PM
Hi, I'm trying to right a graphical display for conway's game of life, as a project to learn openGL. Eventually I might do some cool stuff with 3D shapes etc but for now I thought I would start simply and do it in 2D by filling the screen entirely with a texture generated from the game of life array.

Which matrix should I be putting the texture on, and which calls to OpenGL should I make to do it?

Also can anyone recommend a really good beginners openGL tutorial for those who don't really know much about graphics and graphics terminology?

Thanks.

Lurking
03-27-2004, 02:04 PM
You use the GL_PROJECTION matrix. There are loads of tutorials that will help you out at http://nehe.gamedev.net

crispy
03-27-2004, 04:20 PM
thanks for the tip, i'm now attempting to fill the projection matrix with a square and then texture it. I would expect the following code (in my render method) to display a white window as the square should fill the window, but i don't see anything. Where am I going wrong?

Thanks.


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glColor4f(1,1,1,1);

glBegin(GL_QUADS);
glTexCoord2f(0.800f, 0.800f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.200f, 0.800f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.200f, 0.200f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(0.800f, 0.200f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();

glOrtho(0,WIDTH,0,HEIGHT,-1,1);

chowe6685
03-27-2004, 04:30 PM
why do you have the glOrtho call after the drawing code?

crispy
03-27-2004, 05:05 PM
I thought I needed it, but removing the call to glOrtho still doesn't give me a white square.

chowe6685
03-27-2004, 06:31 PM
I meant why is it after the drawing code? If you want it to have any effect it needs to be before you draw anything the best choice would be after the glLoadIdentity()

Bob
03-28-2004, 02:01 AM
Given the vertex coordinates, he actually needs the projection matrix to be the identity matrix, so placing the glOrtho call after was actually more correct that placing it before.

What I don't see anything about is the modelview matrix. Is that matrix set to identity also? It should be.

edit: Well, looking even closer at the vertex coordinates, I see you're drawing a square in the XZ-plane, is that really what you want? With the identity modelview matrix, it won't be visible becuse you're looking right at the side of an infinitely thin plane. If your modelview matrix is set to rotate the plane 90 degrees, then everything should be fine.

crispy
03-28-2004, 03:18 AM
firstly thanks for your advice, I've now got the code below, I am also printing out a string "hello world" at the center of the screen, and this does display correctly. I still don't get my white square displaying tho.. Can anyone see why this still isn't working?

Thanks.


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(1,1,1,1);

glBegin(GL_QUADS);
glTexCoord2f(0.800f, 0.800f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(0.200f, 0.800f); glVertex2f( 1.0f, -1.0f);
glTexCoord2f(0.200f, 0.200f); glVertex2f( 1.0f, -1.0f);
glTexCoord2f(0.800f, 0.200f); glVertex2f(-1.0f, -1.0f);
glEnd();

char hellow[] = "hello world";
glRasterPos2i(0,0);
printStr(GLUT_BITMAP_HELVETICA_12,hellow);
glOrtho(0,WIDTH,0,HEIGHT,-1,1);

03-28-2004, 03:50 AM
As others have said, that glOrtho call should not be there. Move it either to the top of your display code, or into a context-setup block. It's technically legal to put the call there, but the results will likely not be what you're expecting.

Your vertices are wrong - you're now drawing a 2D quad with all vertices in one axis. Change your drawing mode to GL_POINTS and call glPointSizef(4.0f) before your glBegin line to see where your vertices are ending up. When you fix the vertices, make sure you get the winding order right.

You're trying to call a load of drawing commands on the PROJECTION matrix.

From your Ortho call, you're setting up world coordinates to match screen coordinates. Unfortunately you're specifying vertices with coordinates < 0, so these will be off-screen and invisible.

I suggest you would benefit from a revision of OpenGL basics, as described in the Red Book. There are a few concepts you don't seem to have grasped properly. If you don't have a paper copy, a digital copy of the 1.1 version is freely available, and that should suffice:

http://google.com/search?q=OpenGL+redbook.pdf

eror
03-28-2004, 03:55 AM
Originally posted by crispy:
firstly thanks for your advice, I've now got the code below, I am also printing out a string "hello world" at the center of the screen, and this does display correctly. I still don't get my white square displaying tho.. Can anyone see why this still isn't working?

Thanks.


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(1,1,1,1);

glBegin(GL_QUADS);
glTexCoord2f(0.800f, 0.800f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(0.200f, 0.800f); glVertex2f( 1.0f, -1.0f);
glTexCoord2f(0.200f, 0.200f); glVertex2f( 1.0f, -1.0f);
glTexCoord2f(0.800f, 0.200f); glVertex2f(-1.0f, -1.0f);
glEnd();

char hellow[] = "hello world";
glRasterPos2i(0,0);
printStr(GLUT_BITMAP_HELVETICA_12,hellow);
glOrtho(0,WIDTH,0,HEIGHT,-1,1);! glOrtho should be caled BEFORE you draw anything. It starts 2D drawing-mode. And I'm not sure if you are able to draw anything in Projection mode. I thought that ModelView is for drawing and Projection for setting a perspective.

crispy
03-28-2004, 04:21 AM
I've fiddled with it and I can now see the points. Anyway I'll go and give this red book a read.

Thanks for all the advice.

4TheKing
03-28-2004, 04:36 PM
glOrtho should be caled BEFORE you draw anything. It starts 2D drawing-mode. And I'm not sure if you are able to draw anything in Projection mode. I thought that ModelView is for drawing and Projection for setting a perspective. Yah, that's what I thot...

Bob
03-29-2004, 01:02 AM
Originally posted by eror:
And I'm not sure if you are able to draw anything in Projection mode. I thought that ModelView is for drawing and Projection for setting a perspective.It doesn't mattet which matrix mode you're in when drawing. glMatrixMode only tells OpenGL which matrix stack is affected by calls to the matrix functions. There are no other side effectes.

dvm
03-29-2004, 04:37 AM
Hey that code is not right in my opinion.
The projection matrix is used to set up the view frustum. No vertices should be passed on to it. Yes, as said above technically it's ok, but you might have unexpected results from calculating light, fog, etc. All vertices should be passed when we use the modelview matrix.
In my opinion the proper code would be:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(...); (or glFrustum or gluPerspective or gluOrtho2D)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Now set up your models
glBegin(GL_whatever);
glVertex..
glVertex...
glEnd();
//Sorry for the sloppy codeJust to give an example, just because technically you can fall over a skyscrapper doesn't mean you do it. (The analogy might be a bit too much but you get the point, I hope :-)
Furthermore the part about the projection matrix really belongs to a reshape callback function (add a pinch of glViewport(0, 0, w, h); on top), not to the display function. In the end it's just matrices that they are multiplied to your vertices but you should know to take care of them!

Bob
03-29-2004, 06:01 AM
As I said, the currect matrix mode ONLY affect which matrix stack is modified by the matrix functions. There are no other side effects.

It doesn't matter which matris stack is active when you render your objects. Every vertex will be passed first through the modelview matrix, then through the projection matrix. Lighting and fogging will always be performed after the modelview matrix, so there won't be any problems with which matrix stack is active. You don't choose which matrix stack to pass your vertices through, they always gets passed through both of them, in the correct order.

When you mention that fogging and lighting may screw up, I think you're mixing things up. It is perfectly valid, in theory and in practice, to render objects when the projection stack is active. However, you should not add your viewpoint and object transforms to the projection matrix unless you know what you're doing. That is a different thing and have nothing to do with rendering objects when the projection stack is active.