Sie sind auf Seite 1von 5

CG Lab Manual CG Lab Manual CG Lab Manual

PART A }
void myReshape(int w, int h)
1) Program to recursively subdivide a tetrahedron to from 3D Sierpinski gasket. The number {
of recursive steps is to be specified by the user. glViewport(0, 0, w, h);
#include <stdlib.h> glMatrixMode(GL_PROJECTION);
#include <stdio.h> glLoadIdentity();
#include <GL/glut.h> if (w <= h)
typedef float point[3]; glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
point v[]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333}, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
{-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}}; else
static GLfloat theta[] = {0.0,0.0,0.0}; glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
int n; 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
void triangle( point a, point b, point c) glMatrixMode(GL_MODELVIEW);
{ glBegin(GL_POLYGON); glutPostRedisplay();
glNormal3fv(a); }
glVertex3fv(a); int main(int argc, char **argv)
glVertex3fv(b); {
glVertex3fv(c); //n=atoi(argv[1]);
glEnd(); printf(" No. of Divisions ? ");
} scanf("%d",&n);
void divide_triangle(point a, point b, point c, int m) glutInit(&argc, argv);
{ point v1, v2, v3; glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
int j; glutInitWindowSize(500, 500);
if(m>0) glutCreateWindow("3D Gasket");
{ for(j=0; j<3; j++) v1[j]=(a[j]+b[j])/2; glutReshapeFunc(myReshape);
for(j=0; j<3; j++) v2[j]=(a[j]+c[j])/2; glutDisplayFunc(display);
for(j=0; j<3; j++) v3[j]=(b[j]+c[j])/2; glEnable(GL_DEPTH_TEST);
divide_triangle(a, v1, v2, m-1); glClearColor (1.0, 1.0, 1.0, 1.0);
divide_triangle(c, v2, v3, m-1); glutMainLoop();
divide_triangle(b, v3, v1, m-1); }
}
else(triangle(a,b,c));
}
void tetrahedron( int m)
{
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();

2
1 3

CG Lab Manual CG Lab Manual CG Lab Manual

2) Program to draw a color cube and spin it using openGL transformation matrices. void spinCube()
#include <stdlib.h> { theta[axis] += 1.0;
#include <GL/glut.h> if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, /* display(); */
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, glutPostRedisplay();
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; }
void mouse(int btn, int state, int x, int y)
GLfloat normals[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}
GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, void myReshape(int w, int h)
{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
void polygon(int a, int b, int c , int d) glLoadIdentity();
{ glBegin(GL_POLYGON); if (w <= h)
glColor3fv(colors[a]); glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
glNormal3fv(normals[a]); 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
glVertex3fv(vertices[a]); else
glColor3fv(colors[b]); glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
glNormal3fv(normals[b]); 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
glVertex3fv(vertices[b]); glMatrixMode(GL_MODELVIEW);
glColor3fv(colors[c]); }
glNormal3fv(normals[c]); Void main(int argc, char **argv)
glVertex3fv(vertices[c]); {
glColor3fv(colors[d]); glutInit(&argc, argv);
glNormal3fv(normals[d]); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glVertex3fv(vertices[d]); glutInitWindowSize(500, 500);
glEnd(); glutCreateWindow("Rotating a Color Cube");
void colorcube(void) glutReshapeFunc(myReshape);
{ polygon(0,3,2,1); glutDisplayFunc(display);
polygon(2,3,7,6); glutIdleFunc(spinCube);
polygon(0,4,7,3); glutMouseFunc(mouse);
polygon(1,2,6,5); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
polygon(4,5,6,7); glutMainLoop();
polygon(0,1,5,4); }
}
static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;
void display(void)
{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); Output:
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();
glutSwapBuffers();
}

4 6
5
CG Lab Manual CG Lab Manual CG Lab Manual

3) Program to draw a color cube and allow the user to move the camera suitably to experiment glRotatef(theta[1], 0.0, 1.0, 0.0);
with perspective viewing. Use openGL functions. glRotatef(theta[2], 0.0, 0.0, 1.0);
#include <stdlib.h> colorcube();
#include <GL/glut.h> glutSwapBuffers();
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}, void mouse(int btn, int state, int x, int y)
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; {
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
GLfloat normals[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; theta[axis] += 2.0;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, display();
{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, }
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; void keys(unsigned char key, int x, int y)
{
void polygon(int a, int b, int c , int d) if(key == 'x') viewer[0]-= 1.0;
{ if(key == 'X') viewer[0]+= 1.0;
glBegin(GL_POLYGON); if(key == 'y') viewer[1]-= 1.0;
glColor3fv(colors[a]); if(key == 'Y') viewer[1]+= 1.0;
glNormal3fv(normals[a]); if(key == 'z') viewer[2]-= 1.0;
glVertex3fv(vertices[a]); if(key == 'Z') viewer[2]+= 1.0;
glColor3fv(colors[b]); display();
glNormal3fv(normals[b]); }
glVertex3fv(vertices[b]); void myReshape(int w, int h)
glColor3fv(colors[c]); {
glNormal3fv(normals[c]); glViewport(0, 0, w, h);
glVertex3fv(vertices[c]); glMatrixMode(GL_PROJECTION);
glColor3fv(colors[d]); glLoadIdentity();
glNormal3fv(normals[d]); if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
glVertex3fv(vertices[d]); 2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);
glEnd(); else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
void colorcube() 2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);
{ glMatrixMode(GL_MODELVIEW);
polygon(0,3,2,1); }
polygon(2,3,7,6); void main(int argc, char **argv)
polygon(0,4,7,3); {
polygon(1,2,6,5); glutInit(&argc, argv);
polygon(4,5,6,7); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
polygon(0,1,5,4); glutInitWindowSize(500, 500);
} glutCreateWindow("Colorcube Viewer");
static GLfloat theta[] = {0.0,0.0,0.0}; glutReshapeFunc(myReshape);
static GLint axis = 2; glutDisplayFunc(display);
static GLdouble viewer[]= {0.0, 0.0, 5.0}; /* initial viewer location */ glutMouseFunc(mouse);
void display(void) glutKeyboardFunc(keys);
{ glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutMainLoop();
glLoadIdentity(); }
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(theta[0], 1.0, 0.0, 0.0);
8
7 9

CG Lab Manual CG Lab Manual CG Lab Manual

4) Program to implement the Cohen-Sutherland line clipping algorithm. Make provision to y = y0 + (y1 - y0) * (xmin - x0)/(x1 - x0);
specify the input line, window for clipping and viewport for displaying the clipped image. x = xmin;
}
Output: #include <stdio.h> if (outcodeOut == outcode0)
#include <GL/glut.h> {
#define outcode int x0 = x;
double xmin=50,ymin=50, xmax=100,ymax=100; // Window boundaries y0 = y;
double xvmin=200,yvmin=200,xvmax=300,yvmax=300; // Viewport boundaries outcode0 = ComputeOutCode (x0, y0);
//bit codes for the right, left, top, & bottom }
const int RIGHT = 8; else
const int LEFT = 2; {
const int TOP = 4; x1 = x;
const int BOTTOM = 1; y1 = y;
outcode1 = ComputeOutCode (x1, y1);
outcode ComputeOutCode (double x, double y); }
void CohenSutherlandLineClipAndDraw (double x0, double y0,double x1, double y1) }
{ outcode outcode0, outcode1, outcodeOut; }while (!done);
bool accept = false, done = false;
outcode0 = ComputeOutCode (x0, y0); if (accept)
outcode1 = ComputeOutCode (x1, y1); { // Window to viewport mappings
double sx=(xvmax-xvmin)/(xmax-xmin); // Scale parameters
do{ double sy=(yvmax-yvmin)/(ymax-ymin);
if (!(outcode0 | outcode1)) //logical or is 0 Trivially accept & exit double vx0=xvmin+(x0-xmin)*sx;
{ double vy0=yvmin+(y0-ymin)*sy;
accept = true; double vx1=xvmin+(x1-xmin)*sx;
done = true; double vy1=yvmin+(y1-ymin)*sy;
} //draw a red colored viewport
else if (outcode0 & outcode1) //logical and is not 0. Trivially reject and exit glColor3f(1.0, 0.0, 0.0);
done = true; glBegin(GL_LINE_LOOP);
else glVertex2f(xvmin, yvmin);
{ glVertex2f(xvmax, yvmin);
double x, y; glVertex2f(xvmax, yvmax);
outcodeOut = outcode0? outcode0: outcode1; glVertex2f(xvmin, yvmax);
if (outcodeOut & TOP) //point is above the clip rectangle glEnd();
{ glColor3f(0.0,0.0,1.0); // draw blue colored clipped line
x = x0 + (x1 - x0) * (ymax - y0)/(y1 - y0); glBegin(GL_LINES);
y = ymax; glVertex2d (vx0, vy0);
} glVertex2d (vx1, vy1);
else if (outcodeOut & BOTTOM) //point is below the clip rectangle glEnd();
{ }
x = x0 + (x1 - x0) * (ymin - y0)/(y1 - y0); }
y = ymin; outcode ComputeOutCode (double x, double y)
} {
else if (outcodeOut & RIGHT) //point is to the right of clip rectangle outcode code = 0;
{ if (y > ymax) //above the clip window
y = y0 + (y1 - y0) * (xmax - x0)/(x1 - x0); code |= TOP;
x = xmax; else if (y < ymin) //below the clip window
} code |= BOTTOM;
else //point is to the left of clip rectangle if (x > xmax) //to the right of clip window
{ code |= RIGHT;
10 12
11
CG Lab Manual CG Lab Manual CG Lab Manual

else if (x < xmin) //to the left of clip window Output: 5) Program, using openGL functions, to draw a simple shaded scene consisting of a teapot on a
code |= LEFT; table. Define suitably the position and properties of the light source along with the properties of
return code; the surfaces of the solid object used in the scene.
}
#include <GL/glut.h>
void display() void wall (double thickness)
{ {
double x0=120,y0=10,x1=40,y1=130; //draw thin wall with top = xz-plane, corner at origin
glClear(GL_COLOR_BUFFER_BIT); glPushMatrix();
glColor3f(1.0,0.0,0.0); glTranslated (0.5, 0.5 * thickness, 0.5);
glBegin(GL_LINES); glScaled (1.0, thickness, 1.0);
glVertex2d (x0, y0); glutSolidCube (1.0);
glVertex2d (x1, y1); glPopMatrix();
glVertex2d (60,20); }
glVertex2d (80,120); void tableLeg (double thick, double len)
glEnd(); {
glColor3f(0.0, 0.0, 1.0); glPushMatrix();
glBegin(GL_LINE_LOOP); glTranslated (0, len/2, 0);
glVertex2f(xmin, ymin); glScaled (thick, len, thick);
glVertex2f(xmax, ymin); glutSolidCube (1.0);
glVertex2f(xmax, ymax); glPopMatrix();
glVertex2f(xmin, ymax); }
glEnd();
CohenSutherlandLineClipAndDraw(x0,y0,x1,y1); void table (double topWid, double topThick, double legThick, double legLen)
CohenSutherlandLineClipAndDraw(60,20,80,120); {
glFlush(); glPushMatrix();
} glTranslated (0, legLen, 0);
void myinit() glScaled(topWid, topThick, topWid);
{ glutSolidCube (1.0);
glClearColor(1.0,1.0,1.0,1.0); glPopMatrix();
glColor3f(1.0,0.0,0.0); double dist = 0.95 * topWid/2.0 - legThick/2.0;
glPointSize(1.0); glPushMatrix();
glMatrixMode(GL_PROJECTION); glTranslated (dist, 0, dist);
glLoadIdentity(); tableLeg (legThick, legLen);
gluOrtho2D(0.0,499.0,0.0,499.0); glTranslated (0.0, 0.0, -2 * dist);
} tableLeg (legThick, legLen);
int main(int argc, char** argv) glTranslated (-2*dist, 0, 2 *dist);
{ glutInit(&argc,argv); tableLeg (legThick, legLen);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glTranslated(0, 0, -2*dist);
glutInitWindowSize(500,500); tableLeg (legThick, legLen);
glutInitWindowPosition(0,0); glPopMatrix();
glutCreateWindow("Cohen Suderland Line Clipping Algorithm"); }
glutDisplayFunc(display); void displaySolid (void)
myinit(); { GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f}; // gray
glutMainLoop(); GLfloat mat_diffuse[] = {.5f, .5f, .5f, 1.0f};
} GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat mat_shininess[] = {50.0f};
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular);
14
13 15

CG Lab Manual CG Lab Manual CG Lab Manual

glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess); glEnable (GL_LIGHT0); 6) Program to fill any given polygon using scanline area filling algorithm.
GLfloat lightIntensity[] = {0.7f, 0.7f, 0.7f, 1.0f}; glShadeModel (GL_SMOOTH);
GLfloat light_position[] = {2.0f, 6.0f, 3.0f, 0.0f}; glEnable (GL_DEPTH_TEST); #define BLACK 0
glLightfv (GL_LIGHT0, GL_POSITION, light_position); glEnable (GL_NORMALIZE); #include <stdlib.h>
glLightfv (GL_LIGHT0, GL_DIFFUSE, lightIntensity); glClearColor (0.1, 0.1, 0.1, 0.0); #include <stdio.h>
glMatrixMode (GL_PROJECTION); glViewport (0, 0, 640, 480); #include <GL/glut.h>
glLoadIdentity(); glutMainLoop(); float x1,x2,x3,x4,y1,y2,y3,y4;
double winHt = 1.0; //half-height of window } void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)
glOrtho (-winHt * 64/48.0, winHt*64/48.0, -winHt, winHt, 0.1, 100.0); {
glMatrixMode (GL_MODELVIEW); float mx,x,temp;
glLoadIdentity(); int i;
gluLookAt (2.3, 1.3, 2.0, 0.0, 0.25, 0.0, 0.0, 1.0, 0.0); if((y2-y1)<0)
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); {
glPushMatrix(); temp=y1;y1=y2;y2=temp;
glTranslated (0.4, 0.4, 0.6); temp=x1;x1=x2;x2=temp;
glRotated (45, 0, 0, 1); }
glScaled (0.08, 0.08, 0.08); if((y2-y1)!=0)
glPopMatrix(); mx=(x2-x1)/(y2-y1);
glPushMatrix(); Output: else
glTranslated (0.6, 0.38, 0.5); mx=x2-x1;
glRotated (30, 0, 1, 0); x=x1;
glutSolidTeapot (0.08); for(i=y1;i<=y2;i++)
glPopMatrix (); {
glPushMatrix(); if(x<(float)le[i])
glTranslated (0.25, 0.42, 0.35); le[i]=(int)x;
glPopMatrix(); if(x>(float)re[i])
glPushMatrix(); re[i]=(int)x;
glTranslated (0.4, 0, 0.4); x+=mx;
table (0.6, 0.02, 0.02, 0.3); }
glPopMatrix(); }
wall (0.02); void draw_pixel(int x,int y,int value)
glPushMatrix(); {
glRotated (90.0, 0.0, 0.0, 1.0); glColor3f(1.0,1.0,0.0);
wall (0.02); glBegin(GL_POINTS);
glPopMatrix(); glVertex2i(x,y);
glPushMatrix(); glEnd();
glRotated (-90.0, 1.0, 0.0, 0.0); }
wall (0.02); void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)
glPopMatrix(); {
glFlush(); int le[500],re[500];
} int i,y;
int main (int argc, char ** argv) for(i=0;i<500;i++)
{ {
glutInit (&argc, argv); le[i]=500;
glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); re[i]=0;
glutInitWindowSize (640, 480); }
glutInitWindowPosition (100, 100); edgedetect(x1,y1,x2,y2,le,re);
glutCreateWindow ("simple shaded scene consisting of a tea pot on a table"); edgedetect(x2,y2,x3,y3,le,re);
glutDisplayFunc (displaySolid); edgedetect(x3,y3,x4,y4,le,re);
glEnable (GL_LIGHTING); edgedetect(x4,y4,x1,y1,le,re);
16 18
17
CG Lab Manual CG Lab Manual CG Lab Manual

for(y=0;y<500;y++) 7. Create and rotate a triangle about the origin and a fixed point.
{ #include <GL/glut.h>
if(le[y]<=re[y]) #include<math.h>
for(i=(int)le[y];i<(int)re[y];i++) #include<stdio.h>
draw_pixel(i,y,BLACK); #define PI 3.1416
float angle=10;
} float theta;
typedef struct Point
} {
void display() float x, y, z;
{ };
x1=200.0;y1=200.0;x2=100.0;y2=300.0;x3=200.0;y3=400.0;x4=300.0;y4=300.0; void rotate_x(float);
void rotate_y(float);
glClear(GL_COLOR_BUFFER_BIT); void init();
glColor3f(0.0, 0.0, 1.0); Point p[3]={ {3.0, 0, -0.50}, {3.0, 0, -1.50}, {3.0, 1, -1.0}};
glBegin(GL_LINE_LOOP); void drawTriangle(Point p[3])
glVertex2f(x1,y1); {
glVertex2f(x2,y2); glColor3f(0.3, 0.6, 0.9);
glVertex2f(x3,y3); glLineWidth(2.0);
glVertex2f(x4,y4); glBegin(GL_LINES);
glEnd(); glVertex3f(p[0].x, p[0].y, p[0].z);
scanfill(x1,y1,x2,y2,x3,y3,x4,y4); glVertex3f(p[1].x, p[1].y, p[1].z);
glEnd();
glFlush(); glColor3f(0.6, 0.9, 0.3);
} glBegin(GL_LINES);
glVertex3f(p[1].x, p[1].y, p[1].z);
void myinit() glVertex3f(p[2].x, p[2].y, p[2].z);
{ glEnd();
glClearColor(1.0,1.0,1.0,1.0); glColor3f(0.9, 0.3, 0.6);
glColor3f(1.0,0.0,0.0); glBegin(GL_LINES);
glPointSize(1.0); glVertex3f(p[0].x, p[0].y, p[0].z);
glMatrixMode(GL_PROJECTION); glVertex3f(p[2].x, p[2].y, p[2].z);
glLoadIdentity(); glEnd();
gluOrtho2D(0.0,499.0,0.0,499.0); glFlush();
} }
void display()
int main(int argc, char** argv) {
{ init();
glutInit(&argc,argv); int opt;
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glClear(GL_COLOR_BUFFER_BIT);
glutInitWindowSize(500,500); glColor3f(1.0,1.0,1.0);
glutInitWindowPosition(0,0); glMatrixMode(GL_MODELVIEW);
glutCreateWindow("Filling a Polygon using Scan-line Algorithm"); glBegin(GL_LINES);
glutDisplayFunc(display); glColor3f(1,0,0);
myinit(); glVertex3f(0,0,0);
glutMainLoop(); glVertex3f(7, 0, 0);
} glColor3f(0,1,0);
glVertex3f(0,0,0);
Output: glVertex3f(0,3,0);
glColor3f(0,0,1);
20
19 21

CG Lab Manual CG Lab Manual CG Lab Manual

glVertex3f(0,0,0); p[i].x * sin(theta*PI/180.0); 8. Implement Brenham’s line drawing algorithm for all types of slope.
glVertex3f(0,0,3); }
glEnd(); drawTriangle(new_p); #include<GL/glut.h>
glRasterPos3f(7, 0, 0); } #include <stdlib.h>
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'X'); void init(void) #include<math.h>
glRasterPos3f(0, 3, 0); {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'Y'); glMatrixMode(GL_MODELVIEW); //set projection parameters
glRasterPos3f(0, 0, 3); glLoadIdentity(); void init(void)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'Z'); gluLookAt(10,1,1, 0,0, 0, 0, 1, 0); {
glFinish(); glMatrixMode(GL_PROJECTION); glClearColor(1.0,1.0,1.0,0.0);
drawTriangle(p); glLoadIdentity(); //reset to identity matrix glMatrixMode(GL_PROJECTION);
printf("************ Traingle Rotation ***************"); gluPerspective(45, 1, 1, 100); gluOrtho2D(0.0,300.0,0.0,300.0);
printf("\n1. Rotate around x-axis \n 2. Rotate around y-axis \n"); } }
printf("Enter your option:"); void main(int argc, char**argv) void setPixel(GLint xCoordinate, GLint yCoordinate)
scanf("%d", & opt); { {
printf("\n Enter value for theta: "); glutInit(&argc,argv); glBegin(GL_POINTS);
scanf("%f", &theta); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glVertex2i(xCoordinate,yCoordinate);
switch(opt) glutInitWindowSize(400,400); glEnd();
{ glutInitWindowPosition(500,100); glFlush(); //executes all OpenGL functions as quickly as possible
case 1: rotate_x(theta); glutCreateWindow(" Triangle Rotation"); }
break; init(); void lineBres(GLint x0, GLint y0, GLint xEnd, GLint yEnd)
case 2: rotate_y(theta); glutDisplayFunc(display); {
break; glutMainLoop(); GLint dx = fabs(xEnd - x0);
} } GLint dy = fabs(yEnd - y0);
glFlush(); GLint p = 2 * dy - dx;
} GLint twoDy = 2 * dy;
void rotate_x(float theta) GLint twoDyMinusDx = 2 * (dy-dx);
{ GLint x,y;
int i; if (x0 > xEnd){
Point new_p[3]; x = xEnd;
for(i=0;i<3;i++) y = yEnd;
{ xEnd = x;
new_p[i].x= p[i].x; }else{
new_p[i].y = p[i].y * cos(theta * PI/180.0) – x = x0;
p[i].z * sin(theta*PI/180.0); y = y0;
new_p[i].z = p[i].y * sin(theta * PI/180.0) + }
p[i].z * cos(theta*PI/180.0); setPixel(x,y);
} while(x<xEnd){
drawTriangle(new_p); x++;
} if(p<0)
void rotate_y(float theta) p += twoDy;
{ else{
int i; y++;
Point new_p[3]; p += twoDyMinusDx;
for(i=0;i<3;i++) }
{ setPixel(x,y);
new_p[i].x = p[i].z * sin(theta * PI/180.0) + }
p[i].x * cos(theta*PI/180.0); }
new_p[i].y= p[i].y; void drawMyLine(void)
new_p[i].z = p[i].z * cos(theta * PI/180.0) – {
22 24
23
CG Lab Manual CG Lab Manual CG Lab Manual

glClear(GL_COLOR_BUFFER_BIT); 9. Develop a menu driven program to animate a flag using Bezier Curve algorithm u=GLfloat(k)/GLfloat(nBezCurvePts);
glColor3f(1.0,0.0,0.0); computeBezPt(u, &bezCurvePt, nCtrlPts, ctrlPts, C);
glPointSize(4.0); glVertex2f(bezCurvePt.x, bezCurvePt.y);
GLint x0 = 100; #include<GL/glut.h> }
GLint y0 = 150; #include<stdio.h> glEnd();
GLint xEnd = 200; #include<math.h> delete[]C;
GLint yEnd = 200; #define PI 3.1416 }
lineBres(x0,y0,xEnd,yEnd); GLsizei winWidth = 600, winHeight = 600; void displayFcn()
} GLfloat xwcMin = 0.0, xwcMax = 130.0; {
void main(int argc, char**argv) GLfloat ywcMin = 0.0, ywcMax = 130.0; GLint nCtrlPts = 4, nBezCurvePts =20;
{ typedef struct wcPt3D static float theta = 0;
glutInit(&argc,argv); { wcPt3D ctrlPts[4] = {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); GLfloat x, y, z; {20, 100, 0},
glutInitWindowSize(400,400); }; {30, 110, 0},
glutInitWindowPosition(0,0); void bino(GLint n, GLint *C) {50, 90, 0},
glutCreateWindow("Digital Differential Analyzer Algorithm: Programmed by Salha"); { {60, 100, 0}};
init(); GLint k, j; ctrlPts[1].x +=10*sin(theta * PI/180.0);
glutDisplayFunc(drawMyLine); for(k=0;k<=n;k++) ctrlPts[1].y +=5*sin(theta * PI/180.0);
glutMainLoop(); { ctrlPts[2].x -= 10*sin((theta+30) * PI/180.0);
} C[k]=1; ctrlPts[2].y -= 10*sin((theta+30) * PI/180.0);
for(j=n;j>=k+1; j--) ctrlPts[3].x-= 4*sin((theta) * PI/180.0);
C[k]*=j; ctrlPts[3].y += sin((theta-30) * PI/180.0);
for(j=n-k;j>=2;j--) theta+=0.1;
C[k]/=j; glClear(GL_COLOR_BUFFER_BIT);
} glColor3f(1.0, 1.0, 1.0);
} glPointSize(5);
void computeBezPt(GLfloat u, wcPt3D *bezPt, GLint nCtrlPts, wcPt3D *ctrlPts, GLint glPushMatrix();
*C) glLineWidth(5);
{ glColor3f(255/255, 153/255.0, 51/255.0); //Indian flag: Orange color code
GLint k, n=nCtrlPts-1; for(int i=0;i<8;i++)
GLfloat bezBlendFcn; {
bezPt ->x =bezPt ->y = bezPt->z=0.0; glTranslatef(0, -0.8, 0);
for(k=0; k< nCtrlPts; k++) bezier(ctrlPts, nCtrlPts, nBezCurvePts);
{ }
bezBlendFcn = C[k] * pow(u, k) * pow( 1-u, n-k); glColor3f(1, 1, 1); //Indian flag: white color code
bezPt ->x += ctrlPts[k].x * bezBlendFcn; for(int i=0;i<8;i++)
bezPt ->y += ctrlPts[k].y * bezBlendFcn; {
bezPt ->z += ctrlPts[k].z * bezBlendFcn; glTranslatef(0, -0.8, 0);
} bezier(ctrlPts, nCtrlPts, nBezCurvePts);
} }
void bezier(wcPt3D *ctrlPts, GLint nCtrlPts, GLint nBezCurvePts) glColor3f(19/255.0, 136/255.0, 8/255.0); //Indian flag: green color code
{ for(int i=0;i<8;i++)
wcPt3D bezCurvePt; {
GLfloat u; glTranslatef(0, -0.8, 0);
GLint *C, k; bezier(ctrlPts, nCtrlPts, nBezCurvePts);
C= new GLint[nCtrlPts]; }
bino(nCtrlPts-1, C); glPopMatrix();
glBegin(GL_LINE_STRIP); glColor3f(0.7, 0.5,0.3);
for(k=0; k<=nBezCurvePts; k++) glLineWidth(5);
{ glBegin(GL_LINES);
26
25 27

CG Lab Manual CG Lab Manual

glVertex2f(20,100);
glVertex2f(20,40);
glEnd();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void winReshapeFun(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xwcMin, xwcMax, ywcMin, ywcMax);
glClear(GL_COLOR_BUFFER_BIT);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Bezier Curve");
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFun);
glutMainLoop();
}

28
29

Das könnte Ihnen auch gefallen