Advertisement

Tutorial 3

Started by January 05, 2006 11:17 PM
1 comment, last by inmysights 18 years, 8 months ago
I am currently attemtping tutorial 3. When I compile it everything is fine. So for a learning experience I wanted to translate the trianlge and quad to different area of the screen. So I change the tanslate function alittle, and what happens, they appear off the screen? Took me awhile to figure out why. The translate function doesn not correlate to pixels as I thought it did. Why not? I cannot find any material that explains how to move objects on the screen using pixels can anyone explain how to do this please? I was using 100 in each parameter to move the objects 100 pixels, but they jusy dissapear. Thank you in advance for your help! glTranslatef(100.0f,100.0f,100.0f); glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only glBegin(GL_QUADS); // Start Drawing Quads glVertex3f(-1.0f, 1.0f, 0.0f); // Left And Up 1 Unit (Top Left) glVertex3f( 1.0f, 1.0f, 0.0f); // Right And Up 1 Unit (Top Right) glVertex3f( 1.0f,-1.0f, 0.0f); // Right And Down One Unit (Bottom Right) glVertex3f(-1.0f,-1.0f, 0.0f); // Left And Down One Unit (Bottom Left) glEnd();
In OpenGL, in 3D mode, which is what you are working with right now, moving 1 unit over translates to moving 1 unit over, not 'pixels', so, you have to take a few different approaches than you do in 2D. Starting out, glTranslatef(100.0f,100.0f,100.0f); will move the current draw operation 100 units in the X, Y, and Z directions. The X-axis is left and right, Y is up and down, and Z is into and out of the screen, so you end up drawing 100 units deep into the screen, moving to the right 100 units and have moved up 100 units. Now, your object is being drawn, you just can't see it! If you had a camera system in your program (a class you'd have to implement, it's not something predefined), then you could move over to (100, 100, 100) and see your object, but since you probally don't you have four choices:

1. Change your translation operations so they match the current viewport and view you have by default. So, for example, instead call glTranslatef(1.0f, 1.0f, -1.0f); or the like so the object stays on the screen.

2. Add a camera system, look at the OpenGL FAQ for tutorial sites that provide this, but you should definitly check out NeHe's site first. Once you have the camera system implemented, you can explor your '3d world' and see your quad where it's at.

3. Use the gluLookAt function, to see the object where it's at. This will be faster than a full fledged camera system, but you will eventually have to implement a camera system, so it's not reccomended.

4. Switch to 2D Mode, and use the 2D methods of drawing, so you can draw to (100, 100) or move over to there using glrasterpos2f. Of course the downside is that you are now using 2D rather than 3D - which is fine if you switch back to 3D before you next draw your 3D stuff [wink]

So, if you want to move by pixels, your best best is 2D, otherwise you will have to get used to the 3D 'unit' system, and define how things are yourself. Good luck!
Advertisement
Thank you for the reply, exactly what I was looking for!!

This topic is closed to new replies.

Advertisement