Professional Documents
Culture Documents
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
{
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. */
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