Yeditepe University

Computer Graphics Course

OPEN GL

(Handout and Project Report)

Click for codes


Written by: Sadi Evren SEKER Home Page

Instructor: Ali Vahit Sahiner


1. Installation Issues

1.1. Installation guide for Linux

Mesa is one of the most powerful open GL libraries under Linux. It is included in installation files for RedHat 7.0 and Caldera. They are in rpm (Redhat Packet Management) format and can be installed by using "rpm -i mesa.rpm" command string (if not installed).

Installed version can be queried by using "rpm -q mesa".

There are 2 different packages mesa-devel for development and library parts.


Mesa package, for examples and applications.

Default directory for installation is /usr/X11R6/lib/mesa (which contains examples, demos, and applications on book)

Library files are uncompressed to /usr/X11R6/lib folder.

By the way GL and GLUT files are installed to Linux system


1.2. Installation guide for Windows

Glut can be downloaded from www.opengl.org.

Unfortunately opengl can only run over visual studio. To complete installation glut.dll has to be compiled. There is a readme for steps of installation. There is a dip file for visual studio workspace. It should be loaded from visual studio and in project menu all should be selected to make all.


2. Compiling and Executing

2.1. Compile under Linux

To compile a code (e.g. sadi3.c) the following command should be entered from command line. This command line will be output a binary executable file called sadi3.

gcc sadi3.c -o sadi3 -lglut -lGL -lGLU -L/usr/X11R6/lib -lXmu –lm


2.2. Compile under windows

To compile a code (e.g. sadi3.c) under windows, file should be opened by visual studio (visual c). Compilation is done when build option is selected from build menu. sadi3.exe file will be created under debug directory from current location of file.


2.3. Execution under Linux

To execute a compiled open GL file it is necessary to initialize an X environment. It is not important to run an environment manager (like KDE or GNOME) but X server should be executed.


By the way on the graphical user interface by only typing the executable file name out put will be created on the screen. So you should go to location of file you want to execute and type it name. (e.g. to run a file under /mesa/odevler I will type cd /mesa/odevler and give the execute command ./sadi3 to run file)






2.4. Execution under windows

To execute a compiled file you should go to the location of file and than the subdirectory debug to see filename.exe. Rest is simple to execute only a double click over the executable file is enough. If you want to run under visual studio environment. You should select the run option under debug menu.


3. Model programs

Both windows and Linux environments has example codes after installation (for Linux you should install mesa packet to see examples).


#include <GL/glut.h> /*include file for GL functions*/


//global declarations and functions


Void

display(void)

{

//display functions

}


void

init(void)

{

//initialization operations for variables and functions

}


void

mouse(int button, int state, int x, int y)

{

if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {

//left button event

}

if(button==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {

//right button event

}

}

int

main(int argc, char **argv)

{

a simple main function for main operations

}





5. Project

5.1. Statement of Purpose

I have wanted to code a program to rotate, translate and scale a cube.

5.2. Analyze of project

For this project there has to be a matrix form of cube to implement functions over the cube. So I have used polygon implementation in open GL libraries. By the way cube is displayed as a smooth object else it would be displayed as a wired cube.


Next analyze in the project is the light, view port and color of the object. So there has to be static view-port and a light source to see the object.


After the drawing of object scaling translating and rotating should be designed.


5.3. Design of project

Design of cube is simple; there is an array, which keeps the coordinates of point of cube. There are 8 points for a cube and each point has 3 float number to keep coordinates (x,y,z axises) so I have a 3 by 8 array.


Design of color and light is again very simple I have implemented them inside an array. Color has 3 parameters Red, Green, Blue. For implementation of the color there has to be a 4th parameter for the diffuse of color. Design of light is done by using an array again, 3 parameters for coordinates of light (x,y,z axises) and the range of light is inserted in 4th parameter of light array.


There are 2 different rotating algorithms; 1st algorithm rotates on a scalar origin and object rotates around this origin, 2nd algorithm rotates the object around itself. For the case rotating around itself is more complex, so I have implemented 2nd algorithm.


Translating and scaling is implemented by using scalar addition and scalar subtraction. For example zoom in will add some scalar values to each coordinates of cube or zoom out will subtract some scalar values form coordinates of points which builds cube. Same algorithms used for translating, for example for moving cube in z direction all z values of points should be increased (or decreased, relative to the direction of move).


5.4. Implementation of project

Implementation of cube is done by using the following array.


v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;

v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;

v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;

v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;

v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;

v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;



So this v array is 8 by 3 array (as discussed in design phase) and initial values of each coordinates are implemented related to origin. So center of my cube is in origin.


Implementation of color and light is done by the following statements in the program.


GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */

GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */



In array named light_diffuse you can see only first parameter is 1.0 this means only red value is on (remember the order of this values are red, green, blue).


In array named light_position x, y and z values are 1 so our light source is located in 1,1,1 point and range of light is infinite (which is declared on last parameter by the value of 0.0)


float tetax=0;

float tetay=0;

float tetaz=0;

int tx=0;

int ty=0;

int tz=0;


The variables above are the teta degree of rotation. So I use 3 teta angle for 3 dimension so there may be 3 mutually exclusive rotation. I have used 2 different teta values for a simple rotation that’s why I have to convert the teta degree to teta radian.


Implementation of given functions over this array is done by mathematical operations.


      1. Rotation


if (key=='j'){

if(ty>360)

ty=0;

tetay=ty*M_PI/180;

for(i=0;i<8;i++)

for(j=0;j<3;j++){

v[i][j]=k[i][j];

}

for(i=0;i<8;i++){

j=v[i][0];

v[i][0]=v[i][0]*cos(tetay)-v[i][1]*sin(tetay);

v[i][1]=sin(tetay)*j+cos(tetay)*v[i][1];

}


ty++;

printf("teta:%f\tt:%d\tcos:%f\tsin:%f\n",tetay,ty,cos(tetay),sin(tetay));

for(i=0;i<8;i++)

printf("%f\t %f\t %f\n",v[i][0],v[i][1],v[i][2]);

glutPostRedisplay();

}


So the above code is used to rotate cube on x axis. That’s implemented by using the formula:


x2=x1*cos(teta)-y1*sin(teta);

y2=x1*sin(teta)+y1*cos(teta);


After the calculation operation we have to increase the value of teta (for this example we keep degree value of teta in ty variable) for continuous rotation. The rest of the code is printing out the value of matrix for debug.


      1. Translation


For translation operation we have wanted to move the object on any given axis.


for(i=0;i<8;i++){

v[i][0]=v[i][0]+0.01;

printf("%f\t %f\t %f\n",v[i][0],v[i][1],v[i][2]);

}


That’s the code I have used for both outputting the value of matrix and increasing the value of all matrices by 0.01 (This value is selected for slowly translation, increasing or decreasing this value will yield different changes on the object).


      1. Scaling


For scaling operations we have wanted to make the cube smaller or greater. So I have used the coefficients 1.1 for increasing the scale of cube or 0.9 for decreasing the scale of cube.


for(i=0;i<8;i++){

for(j=0;j<3;j++)

v[i][j]=v[i][j]*1.1;

printf("%f\t %f\t %f\n",v[i][0],v[i][1],v[i][2]);

}


This code is implemented to increase the scale of matrix (in reference point it is decreased so you see cube greater) and output the new value of matrix to screen.






6. Conclusion


During this project and all applications written for open GL I have learned how to implement mathematical formulas over geometric shapes by using the language C.


The whole background of computer graphics depends on mathematical operations. And these operations should be transferred to computer world by using a language. We have selected C for this mission and used the libraries of open GL. So this makes easier to output shapes to screen by using C.


I think this implementation of graphics can be easily used in any design tool.