Thursday, November 15, 2007

Computer Graphic Practical 5

Question 1 -
How did it arrive with the transformed coordinates?


Answer:

(2 3 1 1) is the transformation and it is alined vertically at the right side of the vertex.

First column:
(4 0 0 2) = (X 0 0 2)

Second column:
(0 4 0 3) = (0 Y 0 3)

Third column:
(0 0 4 1) = (0 0 Z 1)


glBegin();
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)
glEnd();

Question 2 -
What is/are the transformation(s) applied?


Answer:

Translation and scaling.

Question 3 -
OpenGL keeps track of only 1 matrix for transformation.
It will be tedious to do transformation for more than 1 mesh/
polygon/objects to different locations.
Why?


Answer:

When having more than 1 transformation, the previous transformation matrix will be overwritten by the others as OpenGL will run the transformation one at a time.

Question 4 -
(a) What is the current operation matrix after this function?

Answer:

Translation and Scaling.

(5 0 0 10)
(0 5 0 2 )
(0 0 5 4 )
(0 0 0 1 )

(b) What is the current operation matrix after this function?

Answer:

Rotation.

(5 0 0 10) * (0 0 5 1) (0 0 5 10)
(0 5 0 2 ) * (0 5 0 1 ) = (0 5 0 2)
(0 0 5 4 ) * (5 0 0 1)
(-5 0 0 4)
(0 0 0 1 ) * (0 0 0 1) (0 0 0 1)

(c) What is the current operation matrix after this function?

Answer:

translation.

(0 0 5 10) (0 0 0 2) (0 0 5 12)
(0 5 0 2) + (0 0 0 2) = (0 5 0 4)
(-5 0 0 4) (0 0 0 2) (-5 0 0 6)
(0 0 0 1) (0 0 0 1) (0 0 0 1)

(d) What is the current operation matrix after this function?

Answer:

scaling of (2, 1, 1)

(0 0 5 10) (2 0 0 1) (0 0 5 10)
(0 5 0 2) * (0 1 0 1) = (0 5 0 2)
(-5 0 0 4) (0 0 1 1) (-5 0 0 4)
(0 0 0 1) (0 0 0 1) (0 0 0 1)

(e) What is the current operation matrix after this function?

Answer:

(1 0 0 0) (0 0 0 0) (1 0 0 0)
(0 1 0 0) + (0 0 0 5) = (0 1 0 5)

(0 0 1 0) (0 0 0 0) (0 0 1 0)
(0 0 0 1) (0 0 0 1) (0 0 0 1)

(f) What is the current operation matrix after this function?

Answer:

(0 0 5 12)
(0 5 0 4 ) * =
(-5 0 0 6)
(0 0 0 1)


Exercise 3


Q5) What did you see? Screen shot it and explain why is the 2nd cube appeared at
(0,0,0)?

Answer:

Since the cube had being translated by (5, 0, 0), everything will start at that point. So when
glTranslatef(-5,0,0) is applied, it goes back to (0, 0, 0).
In order to make the 2nd cube to be at the position of (-5, 0, 0), I used the following code:

glPushMatrix();
glTranslatef(5,0,0);
DrawCube();
glPopMatrix();
glTranslatef(-5,0,0);
DrawCube();

glPushMatrix() will allow (0, 0, 0) to be saved. After the translation and a DrawCube() function been called, glPopMatrix() will go back to (0, 0, 0) and carry out another translation of (-5, 0, 0) and call another DrawCube().

I drew the following cube using a function called "drawCube()", and used 4 "glBegin(GL_POLYGON);" for individual sides, and after the translations. I am still unable to back everything starts from (0, 0, 0) and i'm looking into it.




#include "glut.h"


void DrawCube()
{
glColor3ub(255,0,0); // red, back-side
glBegin(GL_POLYGON);
glVertex3f(-2.0f, -2.0f, -2.0);
glVertex3f(2.0f, -2.0f, -2.0);
glVertex3f(2.0f, 2.0f, -2.0);
glVertex3f(-2.0f, 2.0f, -2.0);
glEnd();


glColor3ub(0,255,255); // green, back
glBegin(GL_POLYGON);
glVertex3f(-2.0f, -2.0f, 2.0);
glVertex3f(2.0f, -2.0f, 2.0);
glVertex3f(2.0f, 2.0f, 2.0);
glVertex3f(-2.0f, 2.0f, 2.0);
glEnd();


glColor3ub(0,100,100); // dark-green, top
glBegin(GL_POLYGON);
glVertex3f(-2.0f, 2.0f, 2.0);
glVertex3f(2.0f, 2.0f, 2.0);
glVertex3f(2.0f, 2.0f, -2.0);
glVertex3f(-2.0f, 2.0f, -2.0);
glEnd();


glColor3ub(255,255,0); // yellow, bottom
glBegin(GL_POLYGON);
glVertex3f(-2.0f, -2.0f, 2.0);
glVertex3f(2.0f, -2.0f, 2.0);
glVertex3f(2.0f, -2.0f, -2.0);
glVertex3f(-2.0f, -2.0f, -2.0);
glEnd();


glColor3ub(0,0,255); // blue, front
glBegin(GL_POLYGON);
glVertex3f(2.0f, 2.0f, 2.0);
glVertex3f(2.0f, 2.0f, -2.0);
glVertex3f(2.0f, -2.0f, -2.0);
glVertex3f(2.0f, -2.0f, 2.0);
glEnd();


glColor3ub(255, 100, 0); //orange, front-side
glBegin(GL_POLYGON);
glVertex3f(-2.0f, 2.0f, 2.0);
glVertex3f(2.0f, 2.0f, 2.0);
glVertex3f(2.0f, -2.0f, 2.0);
glVertex3f(-2.0f, -2.0f, 2.0);
glEnd();
}


void MyDisplay()
{
glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
glColor3ub(255,255,255);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 100.0);
glVertex3f(0.0f, 0.0f, -10.0);
glVertex3f(100.0f, 0.0f, 0.0);
glVertex3f(-10.0f, 0.0f, 0.0);
glVertex3f(0.0f, 100.0f, 0.0);
glVertex3f(0.0f, -10.0f, 0.0);
glEnd();


glPushMatrix();
glTranslatef(5,0,0);
DrawCube();
glPopMatrix();
glTranslatef(-5,0,0);
DrawCube();
glutSwapBuffers();
}


void Rescale(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
}


void SetupRC()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-64, 64, -48, 48, 1, 100);
gluLookAt(30.0, 30.0, 30.0, 0, 1.0, 0.0, 0.0, 1.0, 0.0);
}


void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE GLUT_RGB GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("My Practical 5");
glutDisplayFunc(MyDisplay);
glutReshapeFunc(Rescale);
SetupRC();
glutMainLoop();
}


Exercise 5


1) Yes.


2) Yes.


3) Yes.

Exercise 6

I am still unable to move the cubes, but I still looking into it.


#include "glut.h"


void DrawCube()
{
glColor3ub(255,0,0); // red, back-side
glBegin(GL_POLYGON);
glVertex3f(-2.0f, -2.0f, -2.0);
glVertex3f(2.0f, -2.0f, -2.0);
glVertex3f(2.0f, 2.0f, -2.0);
glVertex3f(-2.0f, 2.0f, -2.0);
glEnd();


glColor3ub(0,255,255); // green, back
glBegin(GL_POLYGON);
glVertex3f(-2.0f, -2.0f, 2.0);
glVertex3f(2.0f, -2.0f, 2.0);
glVertex3f(2.0f, 2.0f, 2.0);
glVertex3f(-2.0f, 2.0f, 2.0);
glEnd();


glColor3ub(0,100,100); // dark-green, top
glBegin(GL_POLYGON);
glVertex3f(-2.0f, 2.0f, 2.0);
glVertex3f(2.0f, 2.0f, 2.0);
glVertex3f(2.0f, 2.0f, -2.0);
glVertex3f(-2.0f, 2.0f, -2.0);
glEnd();


glColor3ub(255,255,0); // yellow, bottom
glBegin(GL_POLYGON);
glVertex3f(-2.0f, -2.0f, 2.0);
glVertex3f(2.0f, -2.0f, 2.0);
glVertex3f(2.0f, -2.0f, -2.0);
glVertex3f(-2.0f, -2.0f, -2.0);
glEnd();


glColor3ub(0,0,255); // blue, front
glBegin(GL_POLYGON);
glVertex3f(2.0f, 2.0f, 2.0);
glVertex3f(2.0f, 2.0f, -2.0);
glVertex3f(2.0f, -2.0f, -2.0);
glVertex3f(2.0f, -2.0f, 2.0);
glEnd();


glColor3ub(255, 100, 0); //orange, front-side
glBegin(GL_POLYGON);
glVertex3f(-2.0f, 2.0f, 2.0);
glVertex3f(2.0f, 2.0f, 2.0);
glVertex3f(2.0f, -2.0f, 2.0);
glVertex3f(-2.0f, -2.0f, 2.0);
glEnd();
}


void MyDisplay()
{
glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);


glColor3ub(255,255,255);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 100.0);
glVertex3f(0.0f, 0.0f, -10.0);
glVertex3f(100.0f, 0.0f, 0.0);
glVertex3f(-10.0f, 0.0f, 0.0);
glVertex3f(0.0f, 100.0f, 0.0);
glVertex3f(0.0f, -10.0f, 0.0);
glEnd();


glPushMatrix();
glTranslatef(5,0,0);
DrawCube();
glPopMatrix();
glTranslatef(-5,0,0);
DrawCube();
glutSwapBuffers();
}


void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'w':
y++;
break;
case 's':
y--;
break;
case 'a':
x--;
break;
case 'd':
x--;
break;
}


}
void Rescale(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
}


void SetupRC()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-64, 64, -48, 48, 1, 100);
gluLookAt(30.0, 30.0, 30.0, 0, 1.0, 0.0, 0.0, 1.0, 0.0);
}


void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE GLUT_RGB GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("My Practical 5");
glutDisplayFunc(MyDisplay);
glutReshapeFunc(Rescale);
glutKeyboardFunc(keyboard);
SetupRC();
glutMainLoop();
}

Sunday, November 11, 2007

Computer Graphic Practical 4

In computer graphic this week, we tried setting up 3D viewing and learn how to draw premitives using openGL.




Firstly, we drew some axes so that it will guild us to draw 3D diagram easier in XYZ directions.




glBegin(GL_LINES); glColor3ub(255,255,255);
glVertex3f(0.0f, 0.0f, 100.0);
glVertex3f(0.0f, 0.0f, -10.0);
glVertex3f(100.0f, 0.0f, 0.0);
glVertex3f(-10.0f, 0.0f, 0.0);
glVertex3f(0.0f, 100.0f, 0.0);
glVertex3f(0.0f, -10.0f, 0.0);
glEnd();


We proceeded to draw the sphere, with 1 triangle behind it as well as one triangle in front of it.




glColor3ub(255,0,0);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, -10.0);
glVertex3f(10.0, 15.0, -30.0);
glVertex3f(10.0, -15.0, -30.0);
glEnd();
glColor3ub(0,255,255);
glutWireSphere(15,50,50);
glColor3ub(0,255,0);
glBegin(GL_TRIANGLES);
glVertex3f(20.0, 0.0, 20.0);
glVertex3f(-10.0, 15.0, 15.0);
glVertex3f(-10.0, 15.0, 30.0);
glEnd();


And finally, the final exercise which require us to make 4 different quads and collapse each other.





glColor3ub(255,0,0);
glBegin(GL_QUADS);
glVertex3f(-40,20,12);
glVertex3f(-40,25,12);
glVertex3f(40,25,12);
glVertex3f(40,20,12);
glEnd( );

glColor3ub(0,255,0);
glBegin(GL_QUADS);
glVertex3f(-35,-30,11);
glVertex3f(-35,30,11);
glVertex3f(-30,30,11);
glVertex3f(-30,-30,11);
glEnd( );

glColor3ub(255,255,0);
glBegin(GL_QUADS);
glVertex3f(30,-30,13);
glVertex3f(30,30,13);
glVertex3f(35,30,13);
glVertex3f(35,-30,13);
glEnd( );

glColor3ub(0,0,255);
glBegin(GL_QUADS);
glVertex3f(-40,-10,10);
glVertex3f(-40,-10,10);
glVertex3f(40,-10,14);
glVertex3f(40,10,14);
glEnd( );

glColor3ub(255,255,255);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 100.0);
glVertex3f(0.0f, 0.0f, -10.0);
glVertex3f(100.0f, 0.0f, 0.0);
glVertex3f(-10.0f, 0.0f, 0.0);
glVertex3f(0.0f, 100.0f, 0.0);
glVertex3f(0.0f, -10.0f, 0.0);
glEnd();

PRACTICAL QUESTIONS
Exercise 1

2) Fill in the empty space in ( ).
Answer:

3) What is the possible window size?
Answer:
A possible window size varies, however 800 x 600 is the best i think, since we always use it.
Exercise 2

1) Find u, v and n.
Answer:
n = eye - look
= (4, 4, 4) - (0, 1, 0)
= (4, 3, 4)
u = up x n
= (0, 1, 0) x (4, 3, 4)
= (4, 0, -4)
v = u x n
= (4, 0, 4) x (4, 3, 4)
= (-12, 32, -12)

2) Repeat for up = (2,1,0).
Answer:
u = up x n
=(2, 1, 0) x (4, 3, 4)
= (-4, 8, -2)

v = u x n
= (-4, 8, -2) x (4, 3, 4)
= (38,-8,-44)
Exercise 3

1) OpenGL stores drawing information in the form of matrices. on the. Can
you briefly describe what does MODEL VIEW matrix and the PROJECTION
matrix does?
Answer:
Modelview matrix is a kind of transformation which include translation, rotation, and scaling of objects, 3D viewing transformation.
Projection matrix is to adjust the position of the "viewing spot" like perspective or orthographic.

2) We can reset the matrix by loading an identity matrix on the top of the
stack. What command is used to reset a matrix?
Answer:
glLoadIdentity() is used.

3) Some parameters have to be set so that our eye can see in the 3D world.
The human eye has a field of view approximately 100°. It also has an
effectively infinite viewing distance. Which command is used to setup
such parameters for the virtual eye?
Answer:

4) The eye can be moved in the world by using the gluLookAt() command.
What do the parameters of the command do?
Answer:
glutLookAt() accepts 9 parameters which have the first 3 as 'eye', another 3 as 'look', and the last 3 as 'up'. Changing the digits will transfer the "eyepoint" to different positions. 'eye' will adjust the position of camera, 'look' is how the eye look at the object, lastly the 'up' is the normal of the object.
Exercise 4

1) What happened to the red triangle?
Answer:
The red triangle did not appear anymore, maybe it is covered by other thing.

2) What do you see now?
Answer:
THE RED TRIANGLE APPEAR AGAIN!!

Thursday, November 1, 2007

Computer Graphic Practical 3

COMPUTER GRAPHIC

In today's computer graphics, we used openGL to draw lines, triangles, quads and a final product. I did not really do a good job so I intended to go home and further improve.
LINES
The belowing codes is the one I added the function(GL_LINES) to draw the lines.

glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(200.0f, 200.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-200.0f, -200.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-200.0f, 200.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(200.0f, -200.0f, 0.0f);

The product is the following:



TRIANGLES

The belowing codes is the one I added the function(GL_TRIANGLE) to draw the triangle.

glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(200.0f, -200.0f, 0.0f);
glVertex3f(200.0f, 200.0f, 0.0f);


The product is the following:



QUADS


The belowing codes is the one I added the function(GL_QUADS) to draw the quad.

glVertex3f(-0.25,0.25,0);
glVertex3f(-0.25,-0.25,0.0);
glVertex3f(0.25,-0.25,0.0);
glVertex3f(0.25,0.25,0.0);

The product is the following:




FINAL WORK


This is just something that I created without an idea, but it is still SOMETHING alright?





The only thing that people need to take note is:

1) The colour varies from 0 - 1.
2) Positions of the pixel varies from -1 to 1, where the middle is considered 0.