Sunday, 23 September 2012

OPENGL 101 BASICS (for reference)




Initialization
The first thing we need to do is call the glutInit() procedure. It should be called before any other GLUT routine because it initializes the GLUT library. The parameters to glutInit() should be the same as those to main(), specifically main(int argc, char** argv) and glutInit(&argc, argv), where argcp is a pointer to the program's unmodified argc variable from main. Upon return, the value pointed to by argcp will be updated, and argv is the program's unmodified argv variable from main. Like argcp, the data for argv will be updated.
The next thing we need to do is call the glutInitDisplayMode() procedure to specify the display mode for a window. You must first decide whether you want to use an RGBA (GLUT_RGBA) or color-index (GLUT_INDEX) color model. The RGBA mode stores its color buffers as red, green, blue, and alpha color components. The forth color component, alpha, corresponds to the notion of opacity. An alpha value of 1.0 implies complete opacity, and an alpha value of 0.0 complete transparancy. Color-index mode, in contrast, stores color buffers in indicies. Your decision on color mode should be based on hardware availability and what you application requires. More colors can usually be simultaneously represented with RGBA mode than with color-index mode. And for special effects, such as shading, lighting, and fog, RGBA mode provides more flexibility. In general, use RGBA mode whenever possible. RGBA mode is the default.
Another decision you need to make when setting up the display mode is whether you want to use single buffering (GLUT_SINGLE) or double buffering (GLUT_DOUBLE). Applications that use both front and back color buffers are double-buffered. Smooth animation is accomplished by rendering into only the back buffer (which isn't displayed), then causing the front and back buffers to be swapped. If you aren't using annimation, stick with single buffering, which is the default.
Finally, you must decide if you want to use a depth buffer (GLUT_DEPTH), a stencil buffer (GLUT_STENCIL) and/or an accumulation buffer (GLUT_ACCUM). The depth buffer stores a depth value for each pixel. By using a "depth test", the depth buffer can be used to display objects with a smaller depth value in front of objects with a larger depth value. The second buffer, the stencil buffer is used to restrict drawing to certain portions of the screen, just as a cardboard stencil can be used with a can of spray paint to make a printed image. Finally, the accumulation buffer is used for accumulating a series of images into a final composed image. None of these are default buffers.
We need to create the characteristics of our window. A call to glutInitWindowSize() will be used to specify the size, in pixels, of your inital window. The arguments indicate the height and width (in pixels) of the requested window. Similarly, glutInitWindowPosition() is used to specify the screen location for the upper-left corner of your initial window. The arguments, x and y, indicate the location of the window relative to the entire display.
Creating a Window
To actually create a window, the with the previously set characteristics (display mode, size, location, etc), the programmer uses the glutCreateWindow() command. The command takes a string as a parameter which may appear in the title bar if the window system you are using supports it. The window is not actually displayed until the glutMainLoop() is entered.
Display Function
The glutDisplayFunc() procedure is the first and most important event callback function you will see. A callback function is one where a programmer-specified routine can be registered to be called in response to a specific type of event. For example, the argument of glutDisplayFunc() is the function that is called whenever GLUT determines that the contents of the window needs to be redisplayed. Therefore, you should put all the routines that you need to draw a scene in this display callback function.
Reshape Function
The glutReshapeFunc() is a callback function that specifies the function that is called whenever the window is resized or moved. Typically, the function that is called when needed by the reshape function displays the window to the new size and redefines the viewing characteristics as desired. If glutReshapeFunc() is not called, a default reshape function is called which sets the view to minimize distortion and sets the display to the new height and width.
Main Loop
The very last thing you must do is call glutMainLoop(). All windows that have been created can now be shown, and rendering those windows is now effective. The program will now be able to handle events as they occur (mouse clicks, window resizing, etc). In addition, the registered display callback (from our glutDisplayFunc()) is triggered. Once this loop is entered, it is never exited!

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 ~

LAB 2A


BIT20203: Graphics Programming

LAB SHEET 2

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

Duration               : 2 Hours
Tasks                       :
 

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);  */
}
 
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.     Copy the sample code in the Page 80 of your textbook. Compile and run.




~ end ~