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;
}
}

No comments: