You are on page 1of 7

CGV

Practical – 2
AIM: Study to run 2D & Animated OpenGL Program in Visual
Studio.

Code:

1)
namespace gp21 {
vo d n t()

{
// Set d splay w ndow color to as glClearColor(R,G,B,Alpha)
glClearColor(0.5, 0.5, 0.5, 0.5);
// Set project on parameters.
glMatr xMode(GL_flROJECTION);
// Set 2D Transformat on as gluOrtho2D(M n W dth, Max W dth, M n
He ght,MaxHe ght)
gluOrtho2D(0.0, 800, 0.0, 600);
}
vo d home()
{
//Roof
glClear(GL_COLOR_BUFFER_BIT); // Clear d splay w ndow
// Set l ne segment color as glColor3f(R,G,B)
glColor3f(0.3, 0.5, 0.8);
glBeg n(GL_flOLYGON);
glVertex2 (200, 500);
glVertex2 (600, 500);
glVertex2 (700, 350);
glVertex2 (300, 350);
glEnd();
// Top of Front Wall
glColor3f(0.1, 0.5, 0.0);
glBeg n(GL_TRIANGLES);
glVertex2 (200, 500);
glVertex2 (100, 350);
glVertex2 (300, 350);
glEnd();
// Front Wall
glColor3f(0.7, 0.2, 0.3);
glBeg n(GL_flOLYGON);
glVertex2 (100, 350);
glVertex2 (300, 350);
glVertex2 (300, 100);
glVertex2 (100, 100);

21012012002_Jinal Ardeshna
CGV

glEnd();
// Front Door
glColor3f(0.7, 0.2, 0.9);
glBeg n(GL_flOLYGON);
glVertex2 (150, 250);
glVertex2 (250, 250);
glEnd();// Front Door Lock
glColor3f(0.3, 0.7, 0.9);
glflo ntS ze(15);
glBeg n(GL_flOINTS);
glVertex2 (170, 170);
glEnd();
//s de Wall
glColor3f(0.1, 0.2, 0.3);
glBeg n(GL_flOLYGON);
glVertex2 (300, 350);
glVertex2 (700, 350);
glVertex2 (700, 100);
glVertex2 (300, 100);
glEnd();
// w ndow one
glColor3f(0.2, 0.4, 0.3);
glBeg n(GL_flOLYGON);
glVertex2 (330, 320);
glVertex2 (450, 320);
glVertex2 (450, 230);
glVertex2 (330, 230);
glEnd();
// l ne of w ndow one
glColor3f(0.1, 0.7, 0.5);
glL neW dth(5);
glBeg n(GL_LINES);
glVertex2 (390, 3);
glVertex2 (390, 230);
glVertex2 (330, 273);
glVertex2 (450, 273);
glEnd();
// w ndow two
glColor3f(0.2, 0.4, 0.3);
glBeg n(GL_flOLYGON);
glVertex2 (530, 320);
glVertex2 (650, 320);
glVertex2 (650, 230);
glVertex2 (530, 230);
glEnd();
// l nes of w ndow two
glColor3f(0.1, 0.7, 0.5);
glL neW dth(5);
glBeg n(GL_LINES);
glVertex2 (590, 320);
glVertex2 (590, 230);
glVertex2 (530, 273);
glVertex2 (650, 273);
glEnd();
// Entrance flath
glColor3f(0.3, 0.5, 0.7);
glL neW dth(3);
glBeg n(GL_flOLYGON);

21012012002_Jinal Ardeshna
CGV

glVertex2 (150, 100);


glVertex2 (250, 100);
glVertex2 (210, 0);
glVertex2 (40, 0);
glEnd();
glColor3f(0.2, 0.4, 0.6);
glBeg n(GL_flOLYGON);
glVertex2 (400, 550);
glVertex2 (450, 550);
glVertex2 (450, 400);
glVertex2 (400, 400);
glEnd();
glColor3f(0.1, 0.5, 0.0);
glBeg n(GL_TRIANGLES);
glVertex2 (425, 590);
glVertex2 (500, 550);
glVertex2 (350, 550);
glEnd();
// flrocess all OpenGL rout nes as qu ckly as poss ble
glFlush();
}
vo d ma n( nt argc, char** argv)

{
glutIn t(&argc, argv);
// In t al ze GLUglutIn t(&argc, argv);
// Set d splay mode
glutIn tD splayMode(GLUT_SINGLE | GLUT_RGB);
// Set top - left d splay w ndow pos t on.
glutIn tW ndowflos t on(100, 100);
// Set d splay w ndow w dth and he ght
glutIn tW ndowS ze(800, 600);
// Create d splay w ndow w th the g ven t tle
glutCreateW ndow("2D House n OpenGL//21012012002_Jinal flatel ");
// Execute n t al zat on
procedure n t();
// Send graph cs to d splay w ndow
glutD splayFunc(home);
// D splay everyth ng and wa t.
glutMa nLoop();
}

namespace gp21 {
vo d ma n( nt argc, char** argv);

21012012002_Jinal Ardeshna
CGV

OUTPUT:

Code:
2)namespace gp22 {
GLfloat mat_red_d ffuse[] = { 0.7, 0.0, 0.1, 1.0 };
GLfloat mat_green_d ffuse[] = { 0.0, 0.7, 0.1, 1.0 };
GLfloat mat_blue_d ffuse[] = { 0.0, 0.1, 0.7, 1.0 };
GLfloat mat_yellow_d ffuse[] = { 0.7, 0.8, 0.1, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_sh n ness[] = { 100.0 };
GLfloat knots[8] = { 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 };
GLfloat pts1[4][4][3], pts2[4][4][3];
GLfloat pts3[4][4][3], pts4[4][4][3];
GLUnurbsObj* nurb;

21012012002_Jinal Ardeshna
CGV

int u, v;
stat c vo d d splay(vo d)

{
glClear(GL_COLOR_BUFFER_BIT | GL_DEflTH_BUFFER_BIT);
glCallL st(1);
glFlush();
}
vo d ma n( nt argc, char** argv)
{
glutIn t(&argc, argv);
glutCreateW ndow("21012012002_Jinal flatel"); glMater
alfv(GL_FRONT, GL_SflECULAR, mat_specular); glMater
alfv(GL_FRONT, GL_SHININESS, mat_sh n ness);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEflTH_TEST);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
nurb = gluNewNurbsRenderer();
gluNurbsflroperty(nurb, GLU_SAMflLING_TOLERANCE, 25.0);
gluNurbsflroperty(nurb, GLU_DISflLAY_MODE, GLU_FILL);
/* Bu ld control po nts for NURBS mole h lls.
*/ for (u = 0; u < 4; u++) {
for (v = 0; v < 4; v++) {
/* Red. */
pts1[u][v][0] = 2.0 * ((GLfloat)u);
pts1[u][v][1] = 2.0 * ((GLfloat)v);
f ((u == 1 || u == 2) && (v == 1 || v == 2))
/* Stretch up m ddle. */

pts1[u][v][2] = 6.0;
else
pts1[u][v][2] = 0.0;
/* Green. */
pts2[u][v][0] = 2.0 * ((GLfloat)u - 3.0);
pts2[u][v][1] = 2.0 * ((GLfloat)v - 3.0);
f ((u == 1 || u == 2) && (v == 1 || v == 2))
f (u == 1 && v == 1)
/* flull hard on s ngle m ddle square.
*/ pts2[u][v][2] = 15.0;
else
/* flush down on other m ddle squares. */
pts2[u][v][2] = -2.0;
else
pts2[u][v][2] = 0.0;
/* Blue. */
pts3[u][v][0] = 2.0 * ((GLfloat)u - 3.0);
pts3[u][v][1] = 2.0 * ((GLfloat)v);
f ((u == 1 || u == 2) && (v == 1 || v == 2))
f (u == 1 && v == 2)
/* flull up on s ngle m ddple square.
*/ pts3[u][v][2] = 11.0;
else
/* flull up sl ghtly on other m ddle squares*/
pts3[u][v][2] = 2.0;
else
pts3[u][v][2] = 0.0;

21012012002_Jinal Ardeshna
CGV

/* Yellow. */

pts4[u][v][0] = 2.0 * ((GLfloat)u);


pts4[u][v][1] = 2.0 * ((GLfloat)v - 3.0);
f ((u == 1 || u == 2 || u == 3) && (v == 1 || v == 2))
f (v == 1)
/* flush down front m ddle and r ght squares. */
pts4[u][v][2] = -2.0;
else
/* flull up back m ddle and r ght squares.
*/ pts4[u][v][2] = 5.0;
else
pts4[u][v][2] = 0.0;
}
}
/* Stretch up red's far r ght corner.
*/ pts1[3][3][2] = 6;
/* flull down green's near left corner a l ttle.
*/ pts2[0][0][2] = -2;
/* Turn up meet ng of four corners.
*/ pts1[0][0][2] = 1;
pts2[3][3][2] = 1;
pts3[3][0][2] = 1;
pts4[0][3][2] = 1;
glMatr xMode(GL_flROJECTION);
gluflerspect ve(55.0, 1.0, 2.0, 24.0);
glMatr xMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -15.0);
glRotatef(330.0, 1.0, 0.0, 0.0);

glNewL st(1, GL_COMflILE);


/* Render red h ll. */
glMater alfv(GL_FRONT, GL_DIFFUSE, mat_red_d ffuse);
gluBeg nSurface(nurb);
gluNurbsSurface(nurb, 8, knots, 8, knots,
4 * 3, 3, &pts1[0][0][0],
4, 4, GL_MAfl2_VERTEX_3);
gluEndSurface(nurb);
/* Render green h ll. */
glMater alfv(GL_FRONT, GL_DIFFUSE, mat_green_d ffuse);
gluBeg nSurface(nurb);
gluNurbsSurface(nurb, 8, knots, 8, knots,
4 * 3, 3, &pts2[0][0][0],
4, 4, GL_MAfl2_VERTEX_3);
gluEndSurface(nurb);
/* Render blue h ll. */
glMater alfv(GL_FRONT, GL_DIFFUSE, mat_blue_d ffuse);
gluBeg nSurface(nurb);
gluNurbsSurface(nurb, 8, knots, 8, knots,
4 * 3, 3, &pts3[0][0][0],
4, 4, GL_MAfl2_VERTEX_3);
gluEndSurface(nurb);
/* Render yellow h ll. */
glMater alfv(GL_FRONT, GL_DIFFUSE, mat_yellow_d ffuse);
gluBeg nSurface(nurb);
gluNurbsSurface(nurb, 8, knots, 8, knots,
4 * 3, 3, &pts4[0][0][0],
4, 4, GL_MAfl2_VERTEX_3);

21012012002_Jinal Ardeshna
CGV

gluEndSurface(nurb);
glEndL st();
glutD splayFunc(d splay);
glutMa nLoop();
//return 0; /* ANSI C requ res ma n to return nt. */
}
}

namespace gp22 {
vo d ma n( nt argc, char** argv);
}

OUTPUT:

21012012002_Jinal Ardeshna

You might also like