Archived

This topic is now archived and is closed to further replies.

Couple of Newbie Q's

This topic is 5978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Sup, I have a couple of doubts... now please don''t kill me for asking this question. What is the difference between a polygon and a Quad.. of course I know what it means in Math but in OpenGL when I construct a Quad and use the same vertices in a Polygon it doesn''t work. It took me 4 hours just to realize this. Example : void DrawCube() { glBegin(GL_QUADS); glVertex3f(-1.0f,1.0f,1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glVertex3f(1.0f, 1.0f, -1.0f); glVertex3f(1.0f, 1.0f, 1.0f); glColor3f(0.23f,0.21f,.21f); glVertex3f(1.0f, 1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(1.0f,-1.0f,1.0f); glVertex3f(1.0f,-1.0f,-1.0f); glVertex3f(1.0f,1.0f,-1.0f); glVertex3f(1.0f,1.0f,1.0f); glVertex3f(-1.0f,-1.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,1.0f,-1.0f); glVertex3f(-1.0f,1.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,1.0f); glVertex3f(1.0f,-1.0f,1.0f); glVertex3f(1.0f,-1.0f,-1.0f); glVertex3f(1.0f,-1.0f,-1.0f); glVertex3f(1.0f,1.0f,-1.0f); glVertex3f(-1.0f,1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glEnd(); } Would not have the same effect if I put in GL_POLYGONS. Why? A diagram would help if you can. Another question is what does glRotatef() actually do? Could someone please try to explain the following code : int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) { .... .... i+=1.0f; glTranslatef(0.0f,0.0f,-15.0f); DrawCube(); glRotatef(i,0.0f,-1.0f,-5.0f); glTranslatef(0.0f,6.0f,0.0f); glRotatef(i,0.0f,-1.0f,-1.0f); DrawCube(); ... ... } End result 1 cube revolving around another. I would like to know how exactly this came about with the math. I really appreciate all the help. Thanks.

Share on other sites
A Quad defines 4 (four) points.
A polygon defines an undefined number of points.
In both cases, you have to make sure that the points are coplanar (eg lie in the same plane) otherwise results for drawing are undefined.

With glBegin(GL_QUADS), OpenGL will take every group of 4 points to make quads, eg OpenGL will create 6 quads.
With glBegin(GL_POLYGONS), OpenGL will simply try to create one polygon with 26 points ! Because I bet those points are not coplanar, OpenGL may not draw or draw incorrectly the polygon.

Share on other sites
I tend to disagree with the explanation of the points having to be coplanar. The following is code for a cube created with polygons :
glBegin(GL_POLYGON);
glVertex3f(0.0f, 0.0f, 0.0f); // top face
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // front face
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // right face
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f); // left face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // bottom face
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // back face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glEnd();

Thanks newayz.

Share on other sites

for question 2 do you want the short answer or the long answer? if it is the short answer then here goes:

Do you know about matrices? well the identity matrix is this:

1, 0, 0, 0
0, 1, 0, 0
0, 0, 1, 0
0, 0, 0, 1

When you do say glRotate3f(x, 0.0f, 0.0f, 1.0f); you change it to this:

cos(x), -sin(x), 0, 0 <- (a)
sin(x), cos(x), 0, 0 <- (b)
0 , 0 , 1, 0 <- (c)
0 , 0 , 0, 1

(a) & (b) are responsible, when multiplied by your point, for changing the x and y co-ordinates to produce your rotated point

(c) makes sure that the z co-ordinate is left alone, because when rotating around the z axis, the z co-ordinate stays the same.

Phew! if you still don't understand anything, ask me.

Edited by - baldurk on February 5, 2002 2:13:43 PM

Share on other sites
  int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){........i+=1.0f;glTranslatef(0.0f,0.0f,-15.0f);DrawCube();glRotatef(i,0.0f,-1.0f,-5.0f);glTranslatef(0.0f,6.0f,0.0f);glRotatef(i,0.0f,-1.0f,-1.0f);DrawCube();......}

Let''s get into some walkthrough :

  int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){........

This is a simple header for Windows Main Procedure. This has nothing really interesting in this topic.

  i+=1.0f;

This increments i at each frame.
This value is meant to be angle, according to the following instructions (we''ll talk about them later).

  glTranslatef(0.0f,0.0f,-15.0f);DrawCube();

First of all, glTranslatef moves the origin of the world to the coordinates (x,y,z)=(0,0,-15)
It translates the world "into" the screen because in OpenGL the viewpoint looks towards the negative Z axis. So, moving the origin into the negative Z axis (at -15 in this case) causes the future scene to be visible. If Z value was positive, the scene would not be visible (or at least, less visible).
Secondly, the cube is drawn. Because the DrawCube() function draws a cube centered at (0,0,0), the previous translation causes the cube to be centered at (0,0,-15).
Note that this cube will always be displayed at the same position with the same orientation.

  glRotatef(i,0.0f,-1.0f,-5.0f);glTranslatef(0.0f,6.0f,0.0f);glRotatef(i,0.0f,-1.0f,-1.0f);

Ah, there''s your beloved glRotatef function
glRotatef rotates the world around an axis.
In glRotatef, the first argument is the angle of rotation, in degrees, and the three other arguments define the rotation axis.
For the first call glRotatef(i,0.0f,-1.0f,-5.0f) the rotation angle is i, and the rotation axis is (x,y,z)=(0,-1,-5).
Imagine an axis with a length of 1 unit down, and 5 units into the screen : glRotatef makes the world turning around this axis.
Because the value of i is incremented at each frame, the world will rotate more and more every frame.

The second call, glTranslatef, causes the world to move 6 units toward the left axis. But because the world rotated, the left axis does not define the left _screen_ axis. It defines somewhat inclined axis.

The third call, glRotatef, causes the world to rotate again around itself, but this time with an axis of (x,y,z)=(0,-1,-1).

  DrawCube();

This time we draw a second cube.
Because the world has moved ans rotated, the cube will not be draw like the first one.
Since the cube is centered on its local origin, it will be drawn on the bizarre origin which is inclined and has moved.
Note that this draw depends on previous glRotatef and glTranslatef calls, which means that the cube will move and rotate during the time because i changes at each frame.

The result is just what you expected.
I hope I was clear enough.
I did as best as I could to write some English

Share on other sites
Ooops I wrote the _long_ answer

About your cube written in one POLYGON it may not work. In fact, it can work if you''re lucky but you shouldn''t play with that since the implementation is random.

Share on other sites
Thanks Baldurk and Vincoof,
Still not completely clear vincoof. I sorta got the idea about the rotation of the world about the axis. But why exactly does only the second cube move and rotate and not the first one.Feel free to put in the math for the above Q, I'm well versed in it ;-) and what exactly is the sin, cos and tan angles in the rotation matrix specify. How would I specify, say a circle formula, as the rotation path... is there any way to do this?
If the world rotates when calling glRotate then everything on the screen should rotate right? Is there anyway to have an entity not rotating when calling the glRotate function.
Sorry bout asking so many Qs but I'm trying to understand every nut and bolt in OpenGL.
BTW, I got the cube made out of Polygons from the OpenGL game programming book. I spent 4 hours trying to construct a similar cube out of polygons but it wouldn't work. I'm wondering how the author acheived it.
Thanks again, I really appreciate it.

Edited by - mAdMaLuDaWg on February 5, 2002 2:58:08 PM

Edited by - mAdMaLuDaWg on February 5, 2002 3:00:53 PM

Edited by - mAdMaLuDaWg on February 5, 2002 3:51:47 PM

Share on other sites
As NeHe would said himself : "you''re more than welcome".
Don''t worry about the number of Qs. The board would be boring as hell if there was no Q

I won''t put any advanced math beause if I had to, it would get very complicated, and would not be very useful.

When you call glRotatef, you rotate the world.
But that does not mean that the previous world is rotated. That means that the future world will be rotated. In fact, you prepare the world you are going to draw, but what is already drawn is drawn and you won''t change it with a glRotate.

It''s about the difference between world coordinates and local coordinates.
World coordinates give a reference. Local coordinates follow the transformation matrix.

Think about a scene that you would draw in real life. Imagine that you are a human with super-powers and you can create cubes and spheres from magic.
To draw the scene is like taking a photo : you set the camera somewhere, then you organize the objects you want to see, then you *click* on the camera.
So, first of all, set your camera somewhere. At this time, you hold the camera in your hands : your location and camera''s location are the same. Now place this camera on a pedestal, so you can fix the camera somewhere while you can move around. Ok. the camera is on a fixed pedestal. The world coordinates are referenced by the camera.
Now you (you as human) move two steps forward, without moving the camera (which is now fixed on the pedestal). You have ''translated'' from 2 steps in the ''field of view'' of the camera. *You* represent the local coordinates. Everything that you will create will be created on the place where you are. Now use your magic to create a cube and set in on the ground. The cube position depends on where you are because *you* represent the local coordinates. It is centered on (0,0,0) on *you* but not relatively to the camera (relatively to the camera, the cube is 2 steps forward). Now you move 3 steps forward again. And you place a sphere. The sphere will be set on you (eg at coordinates (0,0,0) around you) but will be 5 steps forward the camera. But because *you* moved, neither the camera nor the cube, the cube is still 2 steps forward the camera ! That''s exactly what happens when you call glTranslatef : the local origin (you, in this example) moves, so new objects will be placed at this new position, but old objects do not change. You just have to imagine what the camera views to understand what is drawn.
The rotation is very simple : for instance, you have to turn 90degrees left on yourself. Note that if you place another sphere now, it will still be 5 steps forward the camera. Now you move 10 steps forward. And what happens ? You have moved 10 steps left relatively to the camera ! If you create a cube now, it will appear 5 steps sorward and 10 steps left relatively to the camera.

Phew,
I''m sorry for this bizarre example, but I hope I helped a little

Share on other sites
Thanks a billion vincoof,
That was an awesome illustration, it was real neat. How did you come up with that!!?? That cleared up a great deal of confusion for me.
Another dumb doubt if someone could answer : if I were rotating a cube placed at (0,0,0) about (0,-1,-1) would it be any different if I rotated about (0,-10,-10). I tried it and it doesn''t look like it is any different, and I don''t think it would, but I would like to hear that come out of an expert''s mouth.
Thanks again vincoof.

• 39
• 12
• 10
• 10
• 9
• Forum Statistics

• Total Topics
631365
• Total Posts
2999590
×