Wednesday, 19 September 2012

LAB 2B


BIT20203: Graphics Programming

LAB SHEET 3

Title                          : Graphics Programming using OpenGL
Objectives           : At the end of the session, students are able to:
                                          i.         Create a bare bone program.
                                         ii.         Create a keyboard event callback
                                       iii.         Customizing the window
                                        iv.         Draw a 2D triangle

Duration               : 2 Hours
Tasks                       :
 
1.     Write the following code. Compile and run.

      i.         Creating a bare bone program

/* ex1.c */
#include <GL/glut.h>
void display (void) {
/* Called when OpenGL needs to update the display */
glClear (GL_COLOR_BUFFER_BIT); /* Clear the window */
glFlush(); /* Force update of screen */
}

int main (int argc, char **argv) {
glutInit (&argc, argv); /* Initialise OpenGL */
glutCreateWindow ("ex1"); /* Create the window */
glutDisplayFunc (display); /* Register the "display" function */
glutMainLoop (); /* Enter the OpenGL main loop */
return 0;
}
/* end of ex1.c */

     ii.         Keyboard event callback

/* ex2.c */
#include <stdio.h>
#include <GL/glut.h>

void display (void) {
/* Called when OpenGL needs to update the display */
glClear (GL_COLOR_BUFFER_BIT); /* Clear the window */
glFlush(); /* Force update of screen */
}

void keyboard (unsigned char key, int x, int y) {
/* Called when a key is pressed */
if (key == 27) exit (0); /* 27 is the Escape key */
else printf ("You pressed %c\n", key);
}




int main(int argc, char **argv) {
glutInit (&argc, argv); /* Initialise OpenGL */
glutCreateWindow ("ex2"); /* Create the window */
glutDisplayFunc (display); /* Register the "display" function */
glutKeyboardFunc (keyboard); /* Register the "keyboard" function */
glutMainLoop (); /* Enter the OpenGL main loop */
return 0;
}
/*end of ex2.c */

   iii.         Customizing the windows

/* ex3.c */
#include <GL/glut.h>

void display (void) {
/* Called when OpenGL needs to update the display */
glClearColor (1.0,1.0,1.0,0.0);
glClear (GL_COLOR_BUFFER_BIT); /* Clear the window */
glFlush(); /* Force update of screen */
}

void keyboard (unsigned char key, int x, int y) {
/* Called when a key is pressed */
if (key == 27) exit (0); /* 27 is the Escape key */
}

int main(int argc, char **argv) {
glutInit (&argc, argv); /* Initialise OpenGL */
glutInitWindowSize (500, 500); /* Set the window size */
glutInitWindowPosition (100, 100); /* Set the window position */
glutCreateWindow ("ex3"); /* Create the window */
glutDisplayFunc (display); /* Register the "display" function */
glutKeyboardFunc (keyboard); /* Register the "keyboard" function */
glutMainLoop (); /* Enter the OpenGL main loop */
return 0;
}
/* end of ex3.c */

    iv.         Draw a 2D triangle

ex4.c draws a triangle, using the coordinates shown in the figure below




Here’s the code:
/* ex4.c */
#include <GL/glut.h>
void display (void) {
/* Called when OpenGL needs to update the display */
glClear (GL_COLOR_BUFFER_BIT); /* Clear the window */
glLoadIdentity ();
gluLookAt (0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glBegin (GL_LINE_LOOP); /* Draw a triangle */
glVertex3f(-0.3, -0.3, 0.0);
glVertex3f(0.0, 0.3, 0.0);
glVertex3f(0.3, -0.3, 0.0);
glEnd();
glFlush(); /* Force update of screen */
}

void keyboard (unsigned char key, int x, int y) {
/* Called when a key is pressed */
if (key == 27) exit (0); /* 27 is the Escape key */
}

void reshape (int width, int height)
{ /* Called when the window is created, moved or resized */
glViewport (0, 0, (GLsizei) width, (GLsizei) height);
glMatrixMode (GL_PROJECTION); /* Select the projection matrix */
glLoadIdentity (); /* Initialise it */
glOrtho(-1.0,1.0, -1.0,1.0, -1.0,1.0); /* The unit cube */
glMatrixMode (GL_MODELVIEW); /* Select the modelview matrix */
}

int main(int argc, char **argv) {
glutInit (&argc, argv); /* Initialise OpenGL */
glutInitWindowSize (500, 500); /* Set the window size */
glutInitWindowPosition (100, 100); /* Set the window position */
glutCreateWindow ("ex4"); /* Create the window */
glutDisplayFunc (display); /* Register the "display" function */
glutReshapeFunc (reshape); /* Register the "reshape" function */
glutKeyboardFunc (keyboard); /* Register the "keyboard" function */
glutMainLoop (); /* Enter the OpenGL main loop */
return 0;
}
/* end of ex4.c */

2.     Write program with the following specification:
      i.         Windows size : 800 x 600
     ii.         Windows title: My OpenGL Program
   iii.         Draw a star
    iv.         Submit your code 1 week  after your lab session.

~ end ~

No comments:

Post a Comment