Wednesday, November 28, 2007

Practical 7







Today in class we were asked to do some lighting on the objects created.







Here are some pictures






Another one on the cylinder


And one on my robot

Here is the code needed to make it work

void init(void)
{
GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
glClearColor(0.0, 0.1, 0.1, 0.0);
//Enable Depth test
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
In mydisplay()

GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat low_shininess[] = { 5.0 };
//Clear buffer.
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
In main()
init();


Practical 6

This week we were asked to create an object and code it in such a way that when a certain key is pressed, the object will move in that specific direction

Here's a screenshot of it:

Here is the code used to make this thing move:
In void main
glutKeyboardFunc(Keyboard);
Declared as global are:
int x= 0;
int y=0;
In the KeyboardFunc(Keyboard)

void Keyboard(unsigned char key, int x, int y)
{
switch(key) //check which key is pressed
{
case 'd':
::x++; //add 1 to global x.
break;
case 'a':
::x--; //minus 1 to global x.
break;
case 'w':
::y++;
break;
case 's':
::y--;
break;
}
}
In my display()
glTranslatef(::x,::y,0);

Wednesday, November 21, 2007

Practical 5

On the 5th week of my lab session:



We were asked to do a robot and animate it.

However currently I have only been able to rotate it.

Either way here's a picture of it

Here's a sample code of the creation of the robot

void drawBody()
{
glBegin(GL_QUADS);
glVertex3f(15,5,5);
glVertex3f(15,5,-5);
glVertex3f(15,-20,-5);
glVertex3f(15,-20,5);
glVertex3f(-15,-20,5);
glVertex3f(-15,-20,-5);
glVertex3f(-15,5,-5);
glVertex3f(-15,5,5);
glVertex3f(-15,5,5);
glVertex3f(-15,-20,5);
glVertex3f(15,-20,5);
glVertex3f(15,5,5);
glVertex3f(-15,5,-5);
glVertex3f(-15,-20,-5);
glVertex3f(15,-20,-5);
glVertex3f(15,5,-5);
glVertex3f(-15,5,-5);
glVertex3f(-15,5,5);
glVertex3f(15,5,5);
glVertex3f(15,5,-5);
glVertex3f(-15,-20,-5);
glVertex3f(-15,-20,5);
glVertex3f(15,-20,5);
glVertex3f(15,-20,-5);
glEnd();
}

void head()
{
glutSolidSphere(9,34,23);
}
void drawLimb()
{
glBegin(GL_QUADS);
glVertex3f(5,5,5);
glVertex3f(5,5,-5);
glVertex3f(5,-15,-5);
glVertex3f(5,-15,5);
glVertex3f(5,-15,5);
glVertex3f(5,-15,-5);
glVertex3f(-5,-15,-5);
glVertex3f(-5,-15,5);
glVertex3f(-5,-15,5);
glVertex3f(-5,-15,-5);
glVertex3f(-5,5,-5);
glVertex3f(-5,5,5);
glVertex3f(-5,5,-5);
glVertex3f(-5,5,5);
glVertex3f(5,5,5);
glVertex3f(5,5,-5);
glVertex3f(-5,5,5);
glVertex3f(5,5,5);
glVertex3f(5,-15,5);
glVertex3f(-5,-15,5);
glVertex3f(5,5,5);
glVertex3f(-5,5,5);
glVertex3f(-5,-15,5);
glVertex3f(5,-15,5);
glEnd();
}
void drawRobot()
{
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(-20, 0.0, 0.0);
glRotatef(40.0, 1.0, 0.0, 0.0);
drawLimb(); //draw upper right arm
glPopMatrix();
glPushMatrix();
glColor3f(0,1,0);
glTranslatef(-20, -15.0, -2.0);
glRotatef(-30.0, 1.0, 0.0, 0.0);
drawLimb(); //lower right limb
glPopMatrix();
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(-10, -45.0, 0.0);
glRotatef(0.0, 0.0, 0.0, 0.0);
drawLimb(); // bot left leg
glPopMatrix();
glPushMatrix();
glColor3f(0,1,1);
glTranslatef(10, -45.0, 0.0);
glRotatef(60.0, 1.0, 0.0, 0.0);
drawLimb(); // bot right leg
glPopMatrix();
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(10, -25.0, 0.0);
glRotatef(0.0, 0.0, 0.0, 0.0);
drawLimb(); //top right leg
glPopMatrix();
glPushMatrix();
glColor3f(0,1,1);
glTranslatef(-10, -25.0, 0.0);
glRotatef(0.0, 0.0, 0.0, 0.0);
drawLimb(); //top left leg
glPopMatrix();
glPushMatrix();
//glTranslatef(0, 0.0, 2.5);
glColor3f(0,0,1);
drawBody();
glPopMatrix();
glPushMatrix();
glColor3f(1,0,1);
glTranslatef(0, 12.0, 0.0);
//glTranslatef(20,30.0,20.0);
head();
glPopMatrix();
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(20, 0.0, 0.0);
glRotatef(-40.0, 1.0, 0.0, 0.0);
drawLimb(); //draw upper left arm
glPopMatrix();
glPushMatrix();
glTranslatef(20, -12.5, 7.5);
glRotatef(-90.0, 1.0, 0.0, 0.0);
drawLimb(); //lower left limb
glPopMatrix();
}
And here is code for rotation
In the Display function:
glPushMatrix();
glRotatef(::rot,xx,yy,zz);
drawRobot();
glPopMatrix();

In the KeyboardFunc:

switch(key)
{
case 'x':
if(pressed==0)
{
xx=1;
pressed=1;
}
else if(pressed==1)
{
xx=0;
pressed=0;
}
break;
case 'y':
if(pressed==0)
{
yy=1;
pressed=1;
}
else if(pressed==1)
{
yy=0;
pressed=0;
}
break;
case 'z':
if(pressed==0)
{
zz=1;
pressed=1;
}
else if(pressed==1)
{
zz=0;
pressed=0;
}
break;
}

In my DoMainLoop:

void DoMainLoop()
{
if(::rot>=-90&&::rot<=90&&::set==0) { ::rot++; if(::rot==90) { ::set=1; } } else if(::rot>=-90&&::rot>=-90&&::set==1)
{
::rot--;
if(::rot==-90)
{
::set=0;
}
}
}
Erm i made a mistake on the rotation, somehow i thought it was to rotate the whole body and this code shows that, i will do the rotating of arms later