Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Universiti Tun Hussein Onn Malaysia

Faculty of Computer Science and Information Technology


BIT2023/BIT20203: Graphics Programming
LAB SHEET 2
Title
Objectives

: Graphics Programming using OpenGL


: At the end of the session, students are able to:
i.
Draw a simple 2D graphics using OpenGL.

Duration
Tasks

: 2 Hours
:

1. Write the following code. Compile and run.


#include <GL/glut.h>
void display(void)
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
/* draw unit square polygon */
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glFlush();
}
void init()
{
/* set clear color to black */
/*
glClearColor (0.0, 0.0, 0.0, 0.0); */
/* set fill color to white */
/*

glColor3f(1.0, 1.0, 1.0); */

/* set up standard orthogonal view with clipping */


/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
/* glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
mni\teaching\20092010\s1\bit2023\

*/

Universiti Tun Hussein Onn Malaysia


Faculty of Computer Science and Information Technology
int main(int argc, char** argv)
{
/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */
/* You must call glutInit before any other OpenGL/GLUT calls */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

2. Write the following code. Compile and run.


#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<GL/glut.h>

GLenum doubleBuffer;
GLint thing1, thing2;

static void Init(void)


{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearAccum(0.0, 0.0, 0.0, 0.0);
thing1 = glGenLists(1);
glNewList(thing1, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0);
glRectf(-1.0, -1.0, 1.0, 0.0);
glEndList();
thing2 = glGenLists(1);
glNewList(thing2, GL_COMPILE);
glColor3f(0.0, 1.0, 0.0);
glRectf(0.0, -1.0, 1.0, 1.0);
glEndList();
}
static void Reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
mni\teaching\20092010\s1\bit2023\

Universiti Tun Hussein Onn Malaysia


Faculty of Computer Science and Information Technology
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void Key(unsigned char key, int x, int y)
{
switch (key) {
case '1':
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
case '2':
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glutPostRedisplay();
break;
case 27:
exit(0);
}
}
static void Draw(void)
{
glPushMatrix();
glScalef(0.8, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glCallList(thing1);
glAccum(GL_LOAD, 0.5);
glClear(GL_COLOR_BUFFER_BIT);
glCallList(thing2);
glAccum(GL_ACCUM, 0.5);
glAccum(GL_RETURN, 1.0);
glPopMatrix();
if (doubleBuffer) {
glutSwapBuffers();
} else {
glFlush();
}
}
static void Args(int argc, char **argv)
{
GLint i;
doubleBuffer = GL_FALSE;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-sb") == 0) {
doubleBuffer = GL_FALSE;
} else if (strcmp(argv[i], "-db") == 0) {
doubleBuffer = GL_TRUE;
}
}
mni\teaching\20092010\s1\bit2023\

Universiti Tun Hussein Onn Malaysia


Faculty of Computer Science and Information Technology
}

int main(int argc, char **argv)


{
GLenum type;
glutInit(&argc, argv);
Args(argc, argv);
type = GLUT_RGB | GLUT_ACCUM;
type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
glutInitDisplayMode(type);
glutInitWindowSize(300, 300);
glutCreateWindow("Accum Test");
Init();
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
glutMainLoop();
}

~ end ~

mni\teaching\20092010\s1\bit2023\

You might also like