Thursday, December 20, 2007

Open GL - Assignment 1

Assignment 1

The first assignment was to model a favourite toy of ours using openGL, where not only simple polygons can be used to model the toy, but also lighting, movement enabled functions and many other features to further improve the toy.
What i modelled was an M-16 rifle.

It was pretty tough at first to model such an object, as there were many different small and sophisticated parts that i have to model.
For some of the curved parts, i tried using polygons, but since Polygons may pose quite a bit of a problems sometime, i stick to using Quads.
For the lighting, i used a specular lighting instead of a diffuse lighting, as the rifle's surface is metalic and shiny.

This is the final Product of my M-16 model. Lighting was included, including rotation functions to view different parts of the rifle, there is also a passive function where the rifle fires constanly, bullets can be seen shooting out and the empty shells dropping.
Final Model
The file/code for the model can be downloaded through the download box at the right.

Open GL - Practical 7

Practical 7

Practical 7 is for us to learn how to create quadratic ojects such as cylinders and disk and also to creat lighting.

These are the shapes i came up with
Cylinder

Cylinder with lighting

Disk
Sample code for the cylinder:

glPushMatrix();
glTranslatef(0, 0, -20);
cylinder = gluNewQuadric();
glColor3ub(255,0,0);
gluCylinder(cylinder,5,5,30,32,32);//bigger
gluQuadricDrawStyle(cylinder, GLU_FILL);

glPushMatrix();
glTranslatef(0, 0, -20);
glColor3ub(0,0,255);
gluCylinder(cylinder,8,8,20,32,32);//bigger
gluQuadricDrawStyle(cylinder, GLU_FILL);
glPopMatrix();

glPushMatrix();
glTranslatef(0, 0, 20);
glColor3ub(0,255,0);
gluCylinder(cylinder,2,2,40,32,32);//bigger
gluQuadricDrawStyle(cylinder, GLU_FILL);
glPopMatrix();

glPopMatrix();

Thursday, December 13, 2007

Open GL - Practical 5

Practical 5


For practical 5, we were ask to try out playing with Modelview Matrix and understanding how to use glPopMatrix(); and glPushMatrix();
This are the following practical exercises.

Exercise 1

1) glVertex3f(0,0,0); = (0*4+2, 0*4+3, 0*4+1) = (2, 3, 1)
glVertex3f(0,1,0); = (0*4+2, 1*4+3, 0*4+1) = (2, 7, 1)
glVertex3f(1,1,0); = (1*4+2, 1*4+3, 0*4+1) = (6, 7, 1)
glVertex3f(1,0,0); = (1*4+2, 0*4+3, 0*4+1) = (6, 3, 1)

2) The matrix is a scaling followed by a translation. Scaling is (4); translation is by (2, 3, 1).

3) The reason is because the function will multiply the current Model View matrix with whatever the function called. So if 2 or more matrix of transformation are implied, it will be over written and the results will not be accurate. That’s why the stack system is used to store the previous transformation.


Exercise 3

glBegin(GL_QUADS);
glColor3ub(255,0,0); //red
glVertex3f(10.0f, 10.0f, 10.0);
glVertex3f(10.0f, -10.0f, 10.0);
glVertex3f(-10.0f, -10.0f, 10.0);
glVertex3f(-10.0f, 10.0f, 10.0);

glColor3ub(0,0,255); //blue
glVertex3f(10.0f, 10.0f, 10.0);
glVertex3f(10.0f, 10.0f, -10.0);
glVertex3f(10.0f, -10.0f, -10.0);
glVertex3f(10.0f, -10.0f, 10.0);

glColor3ub(255,255,0); //yellow
glVertex3f(-10.0f, 10.0f, 10.0);
glVertex3f(-10.0f, 10.0f, -10.0);
glVertex3f(-10.0f, -10.0f, -10.0);
glVertex3f(-10.0f, -10.0f, 10.0);

glColor3ub(255,255/2,0); //brown/orange
glVertex3f(10.0f, 10.0f, -10.0);
glVertex3f(10.0f, -10.0f, -10.0);
glVertex3f(-10.0f, -10.0f, -10.0);
glVertex3f(-10.0f, 10.0f, -10.0);

glColor3ub(0,255,0); //green
glVertex3f(10.0f, 10.0f, 10.0);
glVertex3f(-10.0f, 10.0f, 10.0);
glVertex3f(-10.0f, 10.0f, -10.0);
glVertex3f(10.0f, 10.0f, -10.0);

glColor3ub(255,0,255); //pink
glVertex3f(10.0f, -10.0f, 10.0);
glVertex3f(-10.0f, -10.0f, 10.0);
glVertex3f(-10.0f, -10.0f, -10.0);
glVertex3f(10.0f, -10.0f, -10.0);
glEnd();


Programming Exercise - Create a solar system





Creating the solar system. First, the basic layout has to be made, so i create lots of sphere and a torus, used push, pop and translation matrix to get them in place.
Than functions had to be included to rotate some of the spheres and spin the torus. I did this using keyboard functions by declaring global variables and fucntions.

For example:

GLfloat yy = 0.0f;
GLfloat zz = 0.0f;

glRotatef(::yy,0,1,0);
glRotatef(::zz,0,0,1);

void Keyboard(unsigned char key, int x, int y)
{
switch(key) //check which key is pressed
{
case 'a':
yy++;
break;
case 'w':
zz--;
break;
case 's':
zz++;
break;
case 'd':
yy--;
break;
}
}