1 / 14

Simple Basic Examples and Skeleton Programs for OpenGL

Simple Basic Examples and Skeleton Programs for OpenGL. OpenGL Programming. OpenGL handles 2D and 3D rendering The native windowing system handles the management of windows. These operations consist of Opening, closing, and displaying User interface components and widgets Event Handling

kanoa
Download Presentation

Simple Basic Examples and Skeleton Programs for OpenGL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Basic Examples and Skeleton Programs for OpenGL

  2. OpenGL Programming • OpenGL handles 2D and 3D rendering • The native windowing system handles the management of windows. These operations consist of • Opening, closing, and displaying • User interface components and widgets • Event Handling • The GLUT library provides a portable mechanism for window management with OpenGL programs • Non-portable mechanisms include GLX for X Windows implementations and WGL for Windows NT.

  3. Window Management I • glutInit(int *argc, char **argv) • initializes GLUT and processes any command line arguments • glutInit() should be called before any other GLUT routine. • glutInitDisplayMode(unsigned intmode) • Specifies whether to use an RGBA or color-index color model. • You can also specify whether you want a single- or double-buffered window. (If you’re working in color-index mode, you’ll want to load certain colors into the color map; use glutSetColor() to do this.) • Finally, you can use this routine to indicate that you want the window to have an associated depth, stencil, and/or accumulation buffer. • For example, if you want a window with double buffering, the RGBA color model, and a depth buffer, you might call glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH).

  4. Window Management II • glutInitWindowPosition(int x, int y) Specifies the screen location for the upper-left corner of your window. • glutInitWindowSize(intwidth, intsize) Specifies the size, in pixels, of your window. • intglutCreateWindow(char *string) • creates a window with an OpenGL context. • It returns a unique identifier for the new window. • Be warned: Until glutMainLoop() is called (see next section), the window is not yet displayed.

  5. Window Management III • glutDisplayFunc(void (* func)(void)) • Is the first and most important event callback function you will see. • Whenever GLUT determines the contents of the window need to be redisplayed, the callback function registered by glutDisplayFunc() is executed.Therefore, • You should put all the routines you need to redraw the scene in the display callback function. • If your program changes the contents of the window, sometimes you will have to call glutPostRedisplay(void), which gives glutMainLoop() a nudge to call the registered display callback at its next opportunity.

  6. Window Management IV • void glutMainLoop(GLvoid) – • Places the program in the GLUT event processing loop from which the program can invoke callback functions. • This must be the last function in the mainline program preceding the return(). • intglutGet( GLenum state ) • Retrieves state information about the GLUT library. • For our example, we are interested in obtaining the size of the display • glutGet(GLUT_SCREEN_WIDTH) and • glutGetGLUT_SCREEN_HEIGHT)

  7. OpenGL Programming #ifdef __FLAT__ #include <windows.h> #endif #include <gl/glut.h> // The initialization function that comes after glutInit() void init(void) { // initialization code goes here // examples of initialization include: setting window size, display modes // color mode, shade model etc. }

  8. OpenGL Programming // The display callback function void display(void) { // display callback called by the OpenGL event handler goes here. // This callback is called every time the main window requires redrawing } // The main function int main(int argc, char** argv) { glutInit(&argc, argv) ; // initialize the glut library init(); // perform all other initialization glutDisplayFunc(display) ; // register the display callback function glutMainLoop() ; // enter the GLUT event processing loop return 0 ; }

  9. Initialization Functions • void glutInitDisplay Mode(GLenum mode) • Configure a windows display mode. Display modes define the type of buffer utilized. Examples are • GLUT_RGBA Request RGBA window (default) • GLUT_RGB An alias for GLUT-RGBA • GLUT_INDEX Request color-index window • GLUT_SINGLE Request signle-buffered window (default) • GLUT_DOUBLE Request double-buffered window • GLUT_DEPTH Request accompanying depth buffer (deafult) • GLUT_STENCEL Request accompanying stencil buffer • GLUT_ACCUM Request accompanying accumulation buffer • GLUT_ALPHA Request accompanying alpha destination buffer • GLUT_MULTISAMPLE Request a window with multi-sampling support • GLUT_STEREO Request a window with stereo support • GLUT_LUMINANCE Request a window with luminance color model

  10. Initialization Functions • GLvoid glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) • Sets the current clear color which is used to clear the collor buffer when glClear() is called with an argument of GL_COLOR_BUFFER_BIT • void glShadeModel(eGLenum mode) • Sets the current shading model to either GL_SMOOTH or GL_FLAT. GL_FLAT has only a single color per polygon used. GL_SMOOTH the color of a polygon is interpolated among the colors of its vertices. • GLvoid glClear(GL_bitfield mask) • Called whenever the window is to be cleared. The mask is usually set to GL_COLOR_BUFFER_BIT. This function will paint the entire window with the current clear color.

  11. First Complete OpenGL Program • Creates a graphics window whose height and width are a third of the total display screen's height and width, places the upper left-hand corner of the window at the very upper left hand corner of the display, requests a double-buffered, RGBA color mode window, sets the clear color to magenta and the shade model to flat. • Displays a wire cube in the window.

  12. First Complete OpenGL Program #ifdef __FLAT__ #include <windows.h> #endif #include <gl/glut.h> // The initialization function void init(void) { glutInitWindowSize( glutGet( GLUT_SCREEN_WIDTH)/3, glutGet( GLUT_SCREEN_HEIGHT)/3 ); glutInitWindowPosition( 0, 0 ); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("Second Chapter - Opening an OpenGL Window") ; glClearColor(1.0, 0.0, 1.0, 0.0) ; glShadeModel(GL_FLAT) ; }

  13. First Complete OpenGL Program // The display callback function void display(void) { glClear(GL_COLOR_BUFFER_BIT) ; glutWireCube(1.0); glutSwapBuffers(); } // The main function int main(int argc, char** argv) { glutInit(&argc, argv) ;   init(); glutDisplayFunc(display) ; glutMainLoop() ;   return 0 ; }

  14. First Complete OpenGL Program

More Related