Wednesday, November 28, 2007

Practical 7


#include "glut.h"
GLfloat angle= 0.0f;
GLfloat width= 40.0f;
GLfloat height= 30.0f;
int x=0;
int y=0;
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);
}

void 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);

GLUquadricObj *Cylinder;
GLUquadricObj *Disk;
glEnable(GL_DEPTH);
glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
Cylinder = gluNewQuadric();
Disk = gluNewQuadric();
gluQuadricDrawStyle(Cylinder, GLU_FILL);
//glLoadIdentity();
glPopMatrix();

glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex3f(0,0,0);
glVertex3f(400,0,0);
glVertex3f(0,0,0);
glVertex3f(0,400,0);
glVertex3f(0,0,0);
glVertex3f(0,0,400);
glEnd();

glPushMatrix();
glTranslatef(::x,y,0);
glColor3f(1,0,0);
gluCylinder(Cylinder,15,15,40,32,32);
glColor3f(0,1,0);
gluDisk(Disk,0.5,1.5,32,32);
glEnd();

glutSwapBuffers();
}

// Called by GLUT library when the window has changed size

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

void SetupRC()
{
GLfloat final = width / height;
gluPerspective(90, final , 0, 1000);
gluLookAt(90,90,90,0,1,0,0,1,0);
glFlush();
glPushMatrix();
}

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

No comments: