CG & V Labmanual
CG & V Labmanual
#include<stdio.h>
#include<GL/glut.h>
typedef float point[3];
/* initial tetrahedron */
point v[]={{0.0,0.0,1.0},{0.0,0.942809,0.33333},
{-0.816497,-0.471405,0.33333},{0.816497,-0.471405,0.333333}};
int n;
void triangle(point a,point b,point c)
/* Display one triangle using a line loop for wire frame, a single normal for constant
shading, or three normals for interpolative shading */
{
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
void tetrahedron(int m)
/* Apply triangle subdivision to faces of tetrahedron */
{
glColor3f(1.0,0.0,0.0);
divide_triangle(v[0],v[1],v[2],m);
glColor3f(0.0,1.0,0.0);
divide_triangle(v[3],v[2],v[1],m);
glColor3f(0.0,0.0,1.0);
divide_triangle(v[0],v[3],v[1],m);
glColor3f(0.0,0.0,0.0);
divide_triangle(v[0],v[2],v[3],m);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
}
Out put :
Enter number of Divisions?
2
#include<GL/glut.h>
#define false 0
#define true 1
double xmin=50,ymin=50,xmax=100,ymax=100; // Window boundaries
double xvmin=200,yvmin=200,xvmax=300,yvmax=300; // Viewport boundaries
x0=x0+te*dx;
y0=y0+te*dy;
}
// Window to viewport mapings
double sx=(xvmax-xvmin)/(xmax-xmin); // Scale parameters
double sy=(yvmax-yvmin)/(ymax-ymin);
double vx0=xvmin+(x0-xmin)*sx;
double vy0=yvmin+(y0-ymin)*sy;
double vx1=xvmin+(x1-xmin)*sx;
double vy1=yvmin+(y1-ymin)*sy;
// draw a red colored viewport
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2d (x0,y0);
glVertex2d (x1,y1);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
LiangBarskyLineClipAndDraw(x0,y0,x1,y1);
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 499.0, 0.0, 499.0);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Liang Barsky Line Clipping Algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
Out put :
3. Program to draw a color cube and spin it using openGL transformation matrices.
#include<GL/glut.h>
GLfloat vertices[][3]={{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},
{-1.0,1.0,-1.0},{-1.0,-1.0,1.0},{1.0,-1.0,1.0},
{1.0,1.0,1.0},{-1.0,1.0,1.0}};
GLfloat colors[][3]={{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},
{-1.0,1.0,-1.0},{-1.0,-1.0,1.0},{1.0,-1.0,1.0},
{1.0,1.0,1.0},{-1.0,1.0,1.0}};
void polygon(int a,int b,int c,int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube(void)
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
void display(void)
/* Display callback, clear frame buffer and Z buffer, rotate cube and draw, swap buffers
*/
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
void spinCube()
/* Idle callback, spin cube 1 degrees about selected axis */
{
theta[axis]+=1.0;
if(theta[axis]>360.0)theta[axis]-=360.0;
/*Display */
glutPostRedisplay();
}
void mouse(int btn,int state,int x,int y)
/* mouse callback, selects an axis about which to rotate */
{
glutInit(&argc,argv);
/* need both double buffering and Z buffer */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("Rotating a Color Cube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
Out Put:
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
GLfloat house[3]
[9]={{100.0,100.0,175.0,250.0,250.0,150.0,150.0,200.0,200.0
},
{100.0,300.0,400.0,300.0,100.0,100.0,150.0,150.0,100.0},
{1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}
};
GLfloat rotatemat[3][3]={{0},
{0},
{0}
};
GLfloat result[3][9]={{0},
{0},
{0}
};
GLfloat arbitrary_x=100.0;
GLfloat arbitrary_y=100.0;
GLfloat rotation_angle;
void multiply()
{
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<9;j++)
{
result[i][j]=0;
for(k=0;k<3;k++)
result[i][j]=result[i][j]+rotatemat[i]
[k]*house[k][j];
}
}
void rotate()
{
GLfloat m,n;
theta= rotation_angle *3.1415/180;
m=-arbitrary_x*(cos(theta)-
1)+arbitrary_y*(sin(theta));
n=-arbitrary_y*(cos(theta)-1)-
arbitrary_x*(sin(theta));
rotatemat[0][0]=cos(theta);
rotatemat[0][1]=-sin(theta);
rotatemat[0][2]=m;
rotatemat[1][0]=sin(theta);
rotatemat[1][1]=cos(theta);
rotatemat[1][2]=n;
rotatemat[2][0]=0;
rotatemat[2][1]=0;
rotatemat[2][2]=1;
//multiply the two matrices
multiply();
}
void drawhouse()
{
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][0],house[1][0]);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][3],house[1][3]);
glVertex2f(house[0][4],house[1][4]);
glEnd();
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][5],house[1][5]);
glVertex2f(house[0][6],house[1][6]);
glVertex2f(house[0][7],house[1][7]);
glVertex2f(house[0][8],house[1][8]);
glEnd();
void drawrotatedhouse()
{
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(result[0][0],result[1][0]);
glVertex2f(result[0][1],result[1][1]);
glVertex2f(result[0][3],result[1][3]);
glVertex2f(result[0][4],result[1][4]);
glEnd();
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(result[0][5],result[1][5]);
glVertex2f(result[0][6],result[1][6]);
glVertex2f(result[0][7],result[1][7]);
glVertex2f(result[0][8],result[1][8]);
glEnd();
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2f(100.0,100.0);
glEnd();
drawhouse();
rotate();
drawrotatedhouse();
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
Output:
#include <stdio.h>
#include <GL/glut.h>
#define bool int
#define true 1
#define false 0
//compute outcodes
outcode0 = ComputeOutCode (x0, y0);
outcode1 = ComputeOutCode (x1, y1);
do{
if (!(outcode0 | outcode1)) //logical or is
0 Trivially accept & exit
{
accept = true;
done = true;
}
else if (outcode0 & outcode1) //logical and is
not 0. Trivially reject and exit
done = true;
else
{
//failed both tests, so calculate the line segment to clip
//from an outside point to an intersection with clip edge
double x, y;
}
}
}while (!done);
if (accept)
{ // Window to viewport mappings
double sx=(xvmax-xvmin)/(xmax-xmin);
// Scale parameters
double sy=(yvmax-yvmin)/(ymax-ymin);
double vx0=xvmin+(x0-xmin)*sx;
double vy0=yvmin+(y0-ymin)*sy;
double vx1=xvmin+(x1-xmin)*sx;
double vy1=yvmin+(y1-ymin)*sy;
//draw a red colored viewport
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glColor3f(0.0,0.0,1.0);
// draw blue colored clipped line
glBegin(GL_LINES);
glVertex2d (vx0, vy0);
glVertex2d (vx1, vy1);
glEnd();
}
}
//Compute the bit code for a point (x, y) using the clip
rectangle
//bounded diagonally by (xmin, ymin), and (xmax, ymax)
outcode ComputeOutCode (double x, double y)
{
outcode code = 0;
if (y > ymax) //above the clip window
code |= TOP;
else if (y < ymin) //below the clip window
code |= BOTTOM;
if (x > xmax) //to the right of clip window
code |= RIGHT;
else if (x < xmin) //to the left of clip window
code |= LEFT;
return code;
}
void display()
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT);
//draw the line with red color
glColor3f(1.0,0.0,0.0);
//bres(120,20,340,250);
glBegin(GL_LINES);
glVertex2d (x0, y0);
glVertex2d (x1, y1);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherlandLineClipAndDraw(x0,y0,x1,y1);
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
Output:
#include <math.h>
#include <GL/glut.h>
void objects(float cx,float cy,float r,float nums)
{
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
float j;
for(j=0;j<nums;j++)
{
float x,y,theta;
theta=((2.0*3.1415926)*(j))/(nums);
x=r*cos(theta);
y=r*sin(theta);
glVertex2f(x+cx,y+cy);
}
glEnd();
glColor3f(0,1,0);
glBegin(GL_LINE_LOOP); //Rectangle
glVertex2f(0.0,0.0);
glVertex2f(0.7,0.0);
glVertex2f(0.7,0.5);
glVertex2d(0.0,0.5);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0,700,700);
int i;
for(i=0;i<30;i++) //No. of extrudes
{
glTranslated(0.01,0.01,0);
objects(0.4,-0.7,0.3,56);
}
glFlush();
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitWindowSize(700,700);
glutCreateWindow("Cylinder & Paralellepiped");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
#include<gl/glut.h>
void obj(double tx,double ty,double tz,double sx,double sy,double sz)
{
glRotated(50,0,1,0);
glRotated(10,-1,0,0);
glRotated(11.7,0,0,-1);
glTranslated(tx,ty,tz);
glScaled(sx,sy,sz);
glutSolidCube(1);
glLoadIdentity();
}
void display()
{
glViewport(0,0,700,700);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
obj(0,0,0.5,1,1,0.04); // three walls
obj(0,-0.5,0,1,0.04,1);
obj(-0.5,0,0,0.04,1,1);
obj(0,-0.3,0,0.02,0.2,0.02); // four table legs
obj(0,-0.3,-0.4,0.02,0.2,0.02);
obj(0.4,-0.3,0,0.02,0.2,0.02);
obj(0.4,-0.3,-0.4,0.02,0.2,0.02);
obj(0.2,-0.18,-0.2,0.6,0.02,0.6); // table top
glRotated(50,0,1,0);
glRotated(10,-1,0,0);
glRotated(11.7,0,0,-1);
glTranslated(0.3,-0.1,-0.3);
glutSolidTeapot(0.09);
glFlush();
glLoadIdentity();
}
void main()
{
float ambient[]={1,1,1,1};
float light_pos[]={27,80,2,3};
glutInitWindowSize(700,700);
glutCreateWindow("scene");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
Output:
#include <stdlib.h>
#include <GL/glut.h>
void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Update viewer position in modelview matrix */
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0,
0.0, 0.0, 1.0, 0.0);
/* rotate cube */
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
Output:
#define BLACK 0
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
float x1,x2,x3,x4,y1,y2,y3,y4;
void edgedetect(float x1,float y1,float x2,float y2,int
*le,int *re)
{
float mx,x,temp;
int i;
if((y2-y1)<0)
{
temp=y1;y1=y2;y2=temp;
temp=x1;x1=x2;x2=temp;
}
if((y2-y1)!=0)
mx=(x2-x1)/(y2-y1);
else
mx=x2-x1;
x=x1;
for(i=y1;i<=y2;i++)
{
if(x<(float)le[i])
le[i]=(int)x;
if(x>(float)re[i])
re[i]=(int)x;
x+=mx;
}
}
void draw_pixel(int x,int y,int value)
{
glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void scanfill(float x1,float y1,float x2,float y2,float
x3,float y3,float x4,float y4)
{
int le[500],re[500];
int i,y;
for(i=0;i<500;i++)
{
le[i]=500;
re[i]=0;
}
edgedetect(x1,y1,x2,y2,le,re);
edgedetect(x2,y2,x3,y3,le,re);
edgedetect(x3,y3,x4,y4,le,re);
edgedetect(x4,y4,x1,y1,le,re);
for(y=0;y<500;y++)
{
if(le[y]<=re[y])
for(i=(int)le[y];i<(int)re[y];i++)
draw_pixel(i,y,BLACK);
}
void display()
{
x1=200.0;y1=200.0;x2=100.0;y2=300.0;x3=200.0;y3=400.0;x4=30
0.0;y4=300.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
scanfill(x1,y1,x2,y2,x3,y3,x4,y4);
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
Output:
EXTRA PROGRAMS
1.Program to plot points on the window using openGL
functions
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2f(10.0,10.0);
glVertex2f(150.0,80.0);
glVertex2f(100.0,20.0);
glVertex2f(200.0,100.0);
glEnd();
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
gluOrtho2D(0.0,550.0,0.0,550.0);
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("points");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
Output:
void display(void)
{
//gluOrtho2D(0.0,500.0,0.0,500.0);
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0); /* blue */
glVertex2i(200+a, 200+b);
glColor3f(0.0, 1.0, 0.0); /* green */
glVertex2i(300+a, 200+b);
glColor3f(1.0, 0.0, 0.0); /* red */
glVertex2i(250+a, 300+b);
glEnd();
glFlush();
}
Output:
glFrontFace(GL_CW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
/* be efficient--make teapot display list */
teapotList = glGenLists(1);
glNewList (teapotList, GL_COMPILE);
glutSolidTeapot(1.0);
glEndList ();
}
/*
* Move object into position. Use 3rd through 12th
* parameters to specify the material property. Draw a
teapot.
*/
void renderTeapot(GLfloat x, GLfloat y,
GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb, GLfloat
shine)
{
GLfloat mat[4];
glPushMatrix();
glTranslatef(x, y, 0.0);
mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] =
1.0;
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
mat[0] = difr; mat[1] = difg; mat[2] = difb;
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
mat[0] = specr; mat[1] = specg; mat[2] = specb;
glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0);
glCallList(teapotList);
glPopMatrix();
}
/**
* First column: emerald, jade, obsidian, pearl, ruby,
turquoise
* 2nd column: brass, bronze, chrome, copper, gold,
silver
* 3rd column: black, cyan, green, red, white, yellow
plastic
* 4th column: black, cyan, green, red, white, yellow
rubber
*/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderTeapot(2.0, 17.0, 0.0215, 0.1745, 0.0215,0.07568,
0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
renderTeapot(2.0, 14.0, 0.135, 0.2225, 0.1575,
0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1);
renderTeapot(2.0, 11.0, 0.05375, 0.05, 0.06625,
0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435,
0.3);
renderTeapot(2.0, 8.0, 0.25, 0.20725, 0.20725,
1, 0.829, 0.829, 0.296648, 0.296648, 0.296648,0.088);
renderTeapot(2.0, 5.0, 0.1745, 0.01175, 0.01175,
0.61424, 0.04136, 0.04136, 0.727811, 0.626959,
0.626959, 0.6);
renderTeapot(2.0, 2.0, 0.1, 0.18725, 0.1745,0.396,
0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1);
renderTeapot(6.0, 17.0, 0.329412, 0.223529, 0.027451,
0.780392, 0.568627, 0.113725, 0.992157, 0.941176,
0.807843,0.21794872);
renderTeapot(6.0, 14.0, 0.2125, 0.1275, 0.054,0.714,
0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2);
renderTeapot(6.0, 11.0, 0.25, 0.25, 0.25,
0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6);
renderTeapot(6.0, 8.0, 0.19125, 0.0735, 0.0225,0.7038,
0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1);
renderTeapot(6.0, 5.0, 0.24725, 0.1995, 0.0745,0.75164,
0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
renderTeapot(6.0, 2.0, 0.19225, 0.19225, 0.19225,
0.50754, 0.50754, 0.50754, 0.508273, 0.508273,
0.508273, 0.4);
renderTeapot(10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01,
0.01,0.50, 0.50, 0.50, .25);
renderTeapot(10.0, 14.0, 0.0, 0.1, 0.06, 0.0,
0.50980392, 0.50980392,
0.50196078, 0.50196078, 0.50196078, .25);
renderTeapot(10.0, 11.0, 0.0, 0.0, 0.0,
0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
renderTeapot(10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
0.7, 0.6, 0.6, .25);
renderTeapot(10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
0.70, 0.70, 0.70, .25);
renderTeapot(10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
0.60, 0.60, 0.50, .25);
renderTeapot(14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01,
0.01,
0.4, 0.4, 0.4, .078125);
renderTeapot(14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
0.04, 0.7, 0.7, .078125);
renderTeapot(14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
0.04, 0.7, 0.04, .078125);
/*
* Main Loop
*/
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB |
GLUT_DEPTH);
glutInitWindowSize(500, 600);
glutInitWindowPosition(50,50);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
Output:
/*
* This program demonstrates double buffering for
* flicker-free animation. The left and middle mouse
* buttons start and stop the spinning motion of the
square.
*/
#include <stdlib.h>
#include <GL/glut.h>
static GLfloat spin = 0.0;
int singleb, doubleb;
void displayd(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glutSwapBuffers ();
}
void displays(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glRectf (-25.0, -25.0, 25.0, 25.0);
glFlush();
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
singleb=glutCreateWindow("single buffered");
myinit ();
glutDisplayFunc(displays);
glutReshapeFunc (myReshape);
glutIdleFunc (spinDisplay);
glutMouseFunc (mouse);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
doubleb=glutCreateWindow("double buffered");
myinit ();
glutDisplayFunc(displayd);
glutReshapeFunc (myReshape);
glutIdleFunc (spinDisplay);
glutMouseFunc (mouse);
glutMainLoop();
}
Output:
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
Output:
#include "glut.h"
static int spin = 0;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
/* Here is where the light position is reset after the
modeling
* transformation (glRotated) is called. This places the
* light at a new position in world coordinates. The cube
* represents the position of the light.
*/
void display(void)
{
GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
Out Put: