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 10 - by Delax

And now for something completly different. If you look at the example from the last chapter you may notice that the perspective is kind of wrong. Well, that's because we don't use perspective projection but orthographic projection as I mentioned at the start.

As most of you want to work the "perspective" way, i'll introduce perspective projection. But for that, we'll need some outside help. To calculate our perspective, we'll use the GLU-Unit. GLU stands for "OpenGL Utility Library". This unit is helping you with some mindboggling calculations like perspective, curves or curved surfaces.

USES
  windows,      // Windows API Stuff
  gl,           // OpenGL API Stuff
  glu;          // OpenGL Utility Lib

Now we can calculate the perpestive projection with one call in our OpenGL_Init.

  glClearColor( 0.0, 0.0, 0.0, 0.0 );

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

  gluPerspective(45.0,width/height,0.1,100.0);

  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();

  glClearDepth(1.0);                  // Depth Buffer Setup
  glEnable(GL_DEPTH_TEST);            // Enables Depth Testing
  glDepthFunc(GL_LEQUAL);             // The Type Of Depth Test To Do

Instead of glOrtho we now use gluPerspective. Note that every unit has it's own prefix. So any function the GLU unit offers has the prefix "glu".

gluPerspective is called with 4 parameters. The first parameter is the angle of our field of vision, measured in degrees on the Y-axis. Imagine you are standing on one side of a football field and you are looking right to the other side. If you field of vision would be visible, it would look something like this.

You are the blue spot and your field of vision is the red marked surface. The angle of this field going out from you is the first parameter.

The second parameter is the aspect of our window. We already used that with glOrtho, so no surprise here. With this parameter, OpenGL calculates your visible X-axis. Think about it - the aspect tells you what relation the width has to the height and from the first parameter we already know the width.

The third parameter sets the near clipping value. Every Z-value smaller this parameter will be clipped. This value can only be positive. It's not possible to define a negative near clipping range.

The fourth parameter the the far clipping range. Every value greater this parameter will be clipped. But note that the last two parameters are not units on the Z-axis but units originating from the viewer (you).

Here the complete parameters as a graph.

It's not that complicated, but different. One gets used to it. And note that transformations and rotations have to be a bit more clever now as 0/0/0 is the position of the viewer right now.

  glTranslatef(0.0,0.0,-5.0);
  glRotatef( rotation, 1.0, 1.0, 0.0 );                         

     glBegin( GL_TRIANGLES );

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

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

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

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

     glEnd();

     glBegin( GL_QUADS );

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

     glEnd();

The only difference is that we move the matrix 5 units into the monitor. If we would leave this out, we would build the pyramid around the viewer, so he would be inside the pyramid.

Get the new source code here.

One final note: if you want to compile the source and you get glu_sl.pp(36,6) Error: Illegal unit name: GLU you are using an old version of the OpenGL header files. Please get the new ones from the FreePascal Server. If you don't want to do this (for whatever reason) you have to fix you OpenGL headers. First, go in your C:\PP\UNITS\WIN32\ directory and delete or rename the files "GLU_SL.PPW" and "GLU_SL.OW". Now go in the C:\PP\SOURCE\PACKAGES\OPENGL\WIN32\ directory and open the file "GLU_SL.PP" in your editor. look for the "PROGRAM" line and change "GLU" into "GLU_SL". Now compile everything and copy the files GLU_SL files into C:\PP\UNITS\WIN32\. Everything should work now.

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

Back to previous page

Useful Links









Link to us