Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
OpenGL II: moving and rotating 2D shapes Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Eber Irigoyen

On this article I'll show you some basic movement and rotating, I also try to 
explain how openGL works


After showing you how to setup an openGL window and draw a simple quad (OpenGL I: 
Hello World) plus doing all the message handling and correctly shutting down your 
openGL application on this article we will add some movement and rotating to our 
shapes This article is based on the first article, so if you didn't read it you may 
do so now, so it makes more sense 

After reading this article you should be able to move shapes around the screen and 
rotate them in all the different axis, what can you do with that? well, some basic 
2D games... of course moving a quad on the screen is not so exciting, but at least 
we're moving now =o) later we will see how to add textures to our shapes so they 
look way better.

Drawing in openGL is relatively easy, because when you draw, say a quad and you 
want to rotate it, you don't have to do any calculations, openGL works using a 
turtle graphics kinda of thing, where the pen that is drawing just follows 
instructions like "go forward", "turn left 90 degrees", etc... and so, as I say the 
advantage on this is that that many paths are more simply described in relative 
than in absolute terms. 
For example, it's easy to indicate the absolute coordinates of the corners of a 
square with vertical and horizontal sides, but it's not so easy to find the corners 
of an inclined square. 

A good way to describe this is if you hold a map (is the screen) and then walk 
following the directions, then you find a square that is facing you, you see it 
with vertical and horizontal sides to you (then pen), but when you first saw the 
map that same square was inclined, make sense? 

those instructions in OpenGL look like this: 

1   glTranslatef(-1.5, 0.0, PosZ);
2   //move to position 3.0, 0.0, PosZ (coming and going to/from view)
3   glRotatef(Angle, 0.0, 0.0, 1.0); // Rotate The quad On The Z axis
4   glBegin(GL_QUADS); // Draw A Quad
5   glVertex3f(-1.0, 1.0, 0.0); // Top Left
6   glVertex3f(1.0, 1.0, 0.0); // Top Right
7   glVertex3f(1.0, -1.0, 0.0); // Bottom Right
8   glVertex3f(-1.0, -1.0, 0.0); // Bottom Left
9   glEnd(); // end of the Quad

The glTranslatef is the "go -1.5 in the X axis", "go O in the Y" and "go PosZ in 
the Z axis", as you can see there's only one variable, and that's the one that is 
going to allow my shape to move (only in the Z axis for now, which is going into 
the screen depth or coming out) 

Then the glRotatef is "rotate Angle degrees in the Z axis", if I had put 1.0 in the 
second parameter is would rotate in the X axis, the third parameter in the Y axis 
and the last parameter in the Z axis.

Then I tell openGL, glBegin(GL_QUAD): "I'm going to draw a square", and then you 
specify the four points of the Quad, because you already told openGL that you're 
going to draw a square is expecting four points if you give it five or six points 
it will discard them, it will only take four by four (which make a quad) If I put 8 
points it will create 2 quads, and so on the same applies if you tell openGL that 
you are going to draw triangles, you have to give 3 by 3 points 

Ok, with no more here's the main code of our article: 

Since we are going to do some movement, we now need some variables 

10  var
11    Angle: glfloat; //angle of the shapes
12    PosZ: glfloat; //Position in the Z axis
13    DForward: Boolean; //going forward? or backwards if false
15  //That's all we need, now we initialize our variables on the InitGL part of our 
16  program: 
18  Angle := 0;
19  PosZ := -20.0;
20  DForward := True;
22  //And that's all we need, we can draw now: 
24  function DrawGLScene(): Bool; { All Rendering Done Here }
25  begin
26    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); //Clear Screen and Depth 
27  Buffer
28    glLoadIdentity(); //Reset The View (move to 0, 0, 0)
29    glColor3f(0.0, 0.0, 1.0); //set the color (1.0=totally blue)
30    glTranslatef(-1.5, 0.0, -15.0); //Draw triangle always at same position
31    glRotatef(Angle, 0.0, 1.0, 0.0); // Rotate The Triangle On The Y axis ( NEW )
32    glBegin(GL_TRIANGLES); // Drawing Using Triangles
33    glVertex3f(0.0, 1.0, 0.0); // Top
34    glVertex3f(-1.0, -1.0, 0.0); // Bottom Left
35    glVertex3f(1.0, -1.0, 0.0); // Bottom Right
36    glEnd(); // end of the triangle
38    glLoadIdentity(); //move to position 0, 0, 0
39    glColor3f(0.5, 0.0, 0.5); //set the color (0.5 red and 0.5 blue)
40    glTranslatef(-1.5, 0.0, PosZ);
41      //move to position 3.0, 0.0, PosZ (coming and going to/from view)
42    glRotatef(Angle, 0.0, 0.0, 1.0); // Rotate The quad On The Z axis
43    glBegin(GL_QUADS); // Draw A Quad
44    glVertex3f(-1.0, 1.0, 0.0); // Top Left
45    glVertex3f(1.0, 1.0, 0.0); // Top Right
46    glVertex3f(1.0, -1.0, 0.0); // Bottom Right
47    glVertex3f(-1.0, -1.0, 0.0); // Bottom Left
48    glEnd(); // end of the Quad
50    if (DForward) then
51      //control the position of the quad, it just goes forward or backward
52    begin
53      PosZ := PosZ + 0.05; //go Forward
54      if (PosZ > -10.5) then
55        //have I gone too far? go backwards now (towards screen depth)
56        DForward := False
57    end
58    else
59    begin //go backward
60      PosZ := PosZ - 0.05;
61      if (PosZ < -20.0) then //have I gone too far into depth? go forward (towards 
62  user)
63        DForward := True
64    end;
65    Angle := Angle + 0.4; //change the angle
66    DrawGLScene := True
67  end;

Simple right? once you understand how the drawing is done "internally" it's easier 
to figure out what we can do with openGL.

That's all for this article, let me know what you think about these openGL articles I try to make them as easy to understand as posible, because I know that is not that easy at the begining but let me know if you are interested in more advanced openGL articles and I will try to post some more topics soon.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC