OpenGL Programming

Below are some programs that utilize OpenGL based graphics.

Click each thumbnail to see a full screen shot. Also, provided with the screen shot is the sample code used to create the program.

OpenGL source code is provided for the following platforms

 

OpenGL Balls

Source Code

Borland C++ Source Code and Executable: Balls.zip  Linux Source Code: balls-1.0.tar.gz

The OpenGL balls project incorporates the fundamentals of physics and OpenGL into a simple demo. In this program the balls move about the screen according to the laws of projectile motion, described below. The balls are texturized with an image in either the .tga (targa) format for the Windows Source code, or the .bmp (bitmap) format in the Linux code. As the texturized balls hit the edge of the screen they bounce back in the other direction by reversing the velocity vector. At this point the value of the velocity is chosen at random.

The formula that each ball obeys is derived from the physics of projectile motion. The formula is:

X = Xo + Xo*t + (1/2)*a*t^2

Meaning that the final X position is equal to the initial X position multiplied by time, plus one half of the acceleration multiplied by the square of time.

The time and the time increments were chosen as an arbitrary value that affects the smoothness of the animation.

 This program incorporates one of the many uses of Object Oriented Programming (OOP) to make it so the number of balls displayed can be easily changed. The program utilizes a BallClass in the following way:

class BallClass {
    public:
    int mult;
    float angle, angle2,inc;
    int moving, startx, starty;
    float yposo,vyo,ypos,times,ay;
    float xposo,vxo,xpos,ax;
    float v;
    float color;
        BallClass () {     // BallClass Constructor
            mult=1;
            angle, angle2,inc=.02;
            moving, startx, starty;
            yposo=2,vyo=0,ypos=2,times=0,ay=-9.8;
            xposo=-2,vxo=random(20)+1,xpos=2,ax=0;
            v=0;
        }

};

apvector <BallClass> ball(500); // create a vector that can hold up to 500 balls
int ballnum=0;   // The reference point for the number of balls.
int totalball=5;  // The actual number of balls to display

The code above may need clarified. The BallClass is created defining all of the variables needed in order to keep track of the values used in animation. The inline function of the BallClass constructor is called each time a new object of the class is created. This defines a few initial values for the balls to start off with.

The actual implementation of the ball class is presented in the last three lines of code. In order to create an arbitrary number of objects of the BallClass a vector is created that will hold up to 500 balls. Each of these balls can be accessed individually using a for loop to read or change any values of the ball. This implementation of the BallClass is very powerful and allows one to dynamically create or destroy balls while the program is running.

OpenGL Star Simulation

Borland C++ Source Code (Microsoft Platform): Stars.zip

    This star simulation uses much of the object oriented code created in the bouncing ball simulation in the previous example. The main difference is the math calculations for the x, y, and z axes. In the ball simulation the z axis was fixed. This program in much simpler than the ball simulation in that the spheres are not textured, and there is much less math involved. Each stars location had to be a pseudo-random value with an ever increasing value on the z axis.    

StarClass () {
xpos=random(5)*.2*random(20)*pow((-1),random(4));
ypos=random(5)*.2*random(20)*pow((-1),random(4));
zpos=-1*random(50);
count = 1;
color=random(10)*.2;
}

The x and y values are calculated by multiplying a random value by 0.2 and another random value. The reason for this is that the location of the screen coordinates are of type double, not int. The random function only returns integer values. To get more variety 0.2 was multiplied by the random integer value. Now, to get the coordinate locations to the left and bottom of the screen negative values must be introduced. A mathematical trick has been implemented to obtain both positive and negative numbers at random. The number -1 is raised to the power of a random value. Any time the random value is even, the -1 will turn into a 1, thus the resulting coordinate is positive. However, if the random value is odd, the base -1 remains -1 and the resulting coordinate is negative.

 

Function Simulator

 

Source Code

Borland C++ Source Code: function32.zip  Linux Source Code: function-1.0.tar.gz
Windows Screen Saver: FunctionScreenSaver.zip  
Windows Executable function32.exe  

   Function32.cpp is an OpenGL based math intensive application that uses the Win32 API for added control over the GUI and user input. The method of compiling Function32 is a little different from the previous examples. The only difference is  this is a fully featured win32 GUI program. In the project options select Win32 from the platform option, and select GUI for the Target Model. These are also the default settings. 

    Ideally, this program will draw each line of the function specified in real-time and seem to animate the function by rotating it around specified axes. On less adept computers, the program may display the finished function, wait a few seconds and display another function. First, the program will load a specified functions x and y values into and array. Then, a series of nested for loops will draw the function as it is being rotated about specified axes. After each function is drawn, the screen is cleared, the working matrix reset, and another random function is calculated. 

The following code is the basis for the entire program. 

struct FunctionData
{
    int label; // determines the type of function
    apvector <double> datax; // x values for function
    apvector <double> datay; // y values for function
    int xaxis;
    int yaxis;
    int zaxis;

    FunctionData() { // Inline constructor function

        label=0;
        xaxis=0;
        yaxis=0;
        zaxis=0;

        datax.resize(3142);
        datay.resize(3142);
        calculate();
        findaxis();
    }

    void calculate();
    void findaxis();
} function; // here 'function' is a member of the class FunctionData

    In the code above a structure is used instead of a class because none of the data members need to be declared private to the class. All of the above examples could also use a structure in place of a class. The main reason for using a structure is to add variety to the code. Also packed tightly into the last line of the structure is a declaration of an object of the structure. These two forms are legal in C++ programming and are very convenient to use. 

    The introductory lines of the structure declare variables that are used to define the function, hold the data for the function, and define the axes of rotation. Label can be assigned a value from 0-99. There are 10 different functions provided in this program. The value of the function can be obtained by taking the floor (int) of the value of label divided by 10. This means that any value of label from 0-9 will be tan (x) , any value of label from 10-19 will be x^3... etc. 

    Next the constructor function is called to initialize some of the default data. Label, xaxis, yaxis, and zaxis are all set to zero. The size of the arrays of datax and datay are then defined. The calculate function is called to load the correct values of the function into the arrays. Finally, the function findaxis is used to pick random values for the axes of rotation.

   

OpenGL Fractal Animator

Borland C++ Source Code (Microsoft Platform): Fractal.zip

Windows Executable: Fractal.exe

        

Fractal is an OpenGL based program that uses a mathematical model to create various fractal designs and animates the fractals. The code provided above is pretty accurately documented as to the details of the programming. The mathematical theory behind the fractal generation used in this program is provided in the links below.

Fractal Theory Page 1                        Fractal Theory Page 2

These pages were taken from Ellis and Gulick's Calculus With Analytic Geometry, Fifth Edition.

Fractal2 - Julia set

Borland C++ Source Code (Microsoft Platform): Fractal2.zip

Windows Executable: Fractal2.exe

 

 

 

 

 

 

The fractal above demonstrates the Julia set. The theory behind fractal generation has been well documented, therefore a few links will be provided that were useful while creating this fractal demo. The fractal tutorials used to create this program are provided in the links section of this web page - OpenGL Links

Fractal2 implements a Complex_Number class that aides in the mathematical representation of the fractals. The operations implemented in the Complex_Number class are listed below with a brief description. 

Complex_Number (double a_temp, double b_temp) Constructor function that represents a complex number of the form (a+bi)
void Get_ab( double &a_temp, double &b_temp) Copies the complex number components (a,b) into the variables a and b, modifying client's incoming parameters
void Set_ab( double a_temp, double b_temp) Sets the complex number components (a,b) of a complex number in the form (a+bi)
void Add(Complex_Number comp_num) Adds two complex numbers, the result is retained in a modified form of the object of the class(*this)
void Subtract(Complex_Number comp_num) Subtracts two complex numbers, the result is retained in a modified form of the object of the class(*this)
void Multiply(Complex_Number comp_num) Multiplies two complex numbers, the result is retained in  a modified form of the object of the class(*this)

 


Questions? Comments? email: nick@ohiofirst.com