Welcome to the new Friends-of-FPC!

Here you can find all kinds of information about the FreePascal Compiler. We have many tutorials and howtos as well as a selection of tools to help you with your programming. We also have some example codes for you. And if you want to contribute some information/ sources/ tools yourself you can do so.
Also we have finally relaunched the FoFPC forum. It's your chance for some Q&A about everything FreePascal.

Friends-of-FPC

Tutorials: Learn how to code with FreePascal.

Source Codes: A collection of examples, miscellaneous source codes and open source stuff.

Tools and Help Files: Intro- duction of some tools that might help you with FPC.

Community

Forum: Ask or answer questions about the FreePascal Compiler, programming or just babble about coding.

Contribute! Contribute your own Tutorial, Source Codes or Tools and send them to us!

Website

About: Information about Friends-of-FPC.org.

OpenGL/ FPC - Chapter 3 - by Delax

So we have our OpenGL Window. All we have to do now is to display something. But the fun isn't starting yet. We have to initialize OpenGL first.

"But why init it again?" you may ask. Well, all we did was creating an OpenGL Context on the Windows API side. Now we have to do the initialization of the OpenGL parameters like "how big is our matrix" or "what kind of coordinate system we use". That kind of thing.

First thing in OpenGL is to initialize the basic drawing canvas. Then you activate or deactivate features (like lighting, blending etc) and then you're off putting stuff to screen. First, the initialization.

procedure OpenGL_Init;
begin

  glClearColor( 0.0, 0.0, 0.0, 0.0 );

  glViewport( 0, 0, width, height );
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();

  glOrtho( -2, 2, -2*height/width, 2*height/width, -2, 2);

  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();

end;

The first line should be familiar. Following is the line glViewport(). It defines the size of our canvas. Think of the canvas as the visible range of our window. The first two parameters usually are 0,0 - starting in the top left window. The max values are set to width and height, representing our window width and height. This way, we set the complete window as visible Surface.

Another way of using this is to set the Viewport only to a part of the window and then drawing Windows API stuff in the other parts (or whatever).

glMatrixMode() is setting the kind of matrix operations we are going to do. This is a bit more complex. Let's take a closer look.

There are 3 kinds of matrix operations. The first one is GL_MODELVIEW. It is used to manipulate the view of the matrix. The second one is GL_PROJECTION. It's used to manipulate the projection of the matrix. The third one is not important right now ;)

But aside from the theory, it's more important what we can do with this stuff. But first the antidote to manipulations: glLoadIdentity();. This is resetting your matrix.

Let's start with our projection. There are two kinds of projection in OpenGL: orthographic and perspective projection. Perspective projection is what we are used to in normal life: objects are getting smaller the more distance is between you and them, etc. But with orthographic projection it's different. Objects remain the same size with distance. It's the 2D view of a 3D world. Close one eye to see what I mean (actually not, because your brain is compensating).

Anyway, we start with orthographic projection. We define glOrtho( -2, 2, -2*height/width, 2*height/width, -2, 2);. The first two parameters are the min and max of the X Axis. The third and fourth are min and max of the Y Axis and the last are min and max of the Z Axis. Whatever we draw inside this 3D system is getting to the screen. Anything outside this system is getting clipped.

"Why this division stuff?" you may ask. Well, normally OpenGL drawing windows are not rectangular. Meaning the width is usually greater than the height of the window. If we define everything from -2 to 2 it would seem that a rectangle is longer than high. So we need to add the aspect of the window to our definition. And you calculate the aspect with height/ width. Then you multiply it with the normal lenght and there you go. A normal aspect is 4:3 or around 1.3.

And thats it for now. We have time to play with many many other initializations here later. Next, we make ourself a little proc called "Draw_OpenGL".

procedure OpenGL_Draw;
begin

  glClear( GL_COLOR_BUFFER_BIT );

  glColor3f( 1.0, 0.0, 0.0 );

  glBegin(GL_TRIANGLES);

    glVertex3f(0.0, 1.0, 0.0);
    glVertex3f(-1.0, -1.0, 0.0);
    glVertex3f(1.0, -1.0, 0.0);

  glEnd();  

end;

The first thing we do is clearing the screen. To do this, we use glClear(). As parameter we have to inform OpenGL what exactly we want to clear. Right now, only our buffer needs to be cleared with our background color.

With glColor3f(); we set the color of our OpenGL objects. The color is active until another color is set.

And now for the juicy stuff. With glBegin(); we start drawing OpenGL stuff. Right now we want to draw some triangles so we set the parameter to GL_TRIANGLES. As a triangle has 3 edges, we need 3 vertices to define one.

A vertex is set with glVertex3f();. 3f stands for "3 float", meaning we need 3 floating point parameters for the X/ Y/ Z values.

Please note HOW we set the vertices to create the triangle.

If we create a triangle, the front is always drawn counter clockwise. The back of the triangle would be drawn clockwise.

And thats our drawing procedure for today.

Here we are. A simple and quite red triangle. Download the complete source here. Compile it, play with it, add some more triangles and so on. If you're done, on to the next chapter.

Delax/ Sundancer Inc.
[delax@sundancerinc.de]

Back to previous page

Useful Links









Link to us