OpenGL Fixed Intro: Packages 1. OpenGL • Basic package • Platform-independent: Independent of any 2. GLU (OpenGL Utility library) • OpenGL utilities • Provides higher-level utilities • Part of OpenGL system 3. GLUT (OpenGL Utility Toolkit) • OpenGL utility toolkit • Developed independently of OpenGL API • Designed to handle windowing system • No longer maintained • Not open source – Open source descendants include FreeGLUT and OpenGLUT 4. GLEW (OpenGL Extension Wrangler) • Cross-platform open-source C/C++ extension loading library • Provides efficient run-time mechanisms for determining which OpenGL ex- tensions are supported on the target platform 5. Includes: • Windows – include – include – include – include • Unix-based systems – include – include – include – include • Mac (OS X) –

1 OpenGL Fixed Intro: Compiling 1. Compiling in MS Windows (Visual Studio v6) • Must change linker settings • Go to Project/Settings/Link • Add all of the .lib files above to those listed in Object/library modules • Compile as usual 2. Unix-based systems • gcc filename -lGL -lGLU -lglut • If math library required, include -lm 3. Mac (OS X) • gcc -framework GLUT -framework OpenGL filename

2 OpenGL Fixed Intro: Naming Conventions Functions: {#}{type}{v} where prefix is gl, glu, or glut

OpenGL Data Types:

Suffix Data type C type OGL type b 8 bit int signed char GLbyte s 16 bit int short GLshort i 32 bit int int/long GLint/GLsizei f 32 bit fp float GLfloat/GLclampf d 64 bit fp double GLdouble/GLclampd ub 8 bit unsigned int unsigned char GLubyte/GLboolean us 16 bit ” unsigned short GLushort ui 32 bit ” unsigned int/long GLuint/GLenum/GLbitfield

Constants: GL GLUT

3 OpenGL Fixed Intro: OpenGL Program Framework - Bare Bones Example

#include void init (void) { glClearColor (1.0, 1.0, 1.0, 0.0); glShadeModel (GL_FLAT); } void display (void) { glClear (GL_COLOR_BUFFER_BIT); glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);

// Draw figures here

glFlush (); }

//Possibly add’l functions; e.g. menu, interaction functions int main (int argc, char** argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB); glutInitWindowSize (450, 250); glutInitWindowPosition (0, 0); glutCreateWindow (argv[0]); init (); glutDisplayFunc (display);

//register add’l GLUT callbacks //Possible add’l setup code

glutMainLoop (); return 0; }

4 OpenGL Fixed Intro: OpenGL Program Framework - Steps 1. Initialize glut 2. Initialize windowing system (GLUT) 3. Initialize OpenGL 4. Register display function • Contains all calls required to draw the image • Is a callback function: – Function registered with windowing system, to be called when a specific event occurs • Display function called whenever image needs to be redrawn 5. Register other callback functions 6. Call GLUT event loop • Required to allow continuous display of image • Basic algorithm: while (true) { if (need to redraw) redraw else if (event occurred) handle event else call idle function }

5 OpenGL Fixed Intro: Basic GLUT Functions • For whatever reason, these calls are usually placed in main, not a separate function • Those functions with an asterisk are not part of basic GLUT - only FreeGLUT or OpenGLUT 1. GLUT initialization

void glutInit(int *argcp, char **argv);

void glutInitDisplayMode(unsigned int mode);

Mode GLUT RGB/GLUT INDEX GLUT SINGLE/GLUT DOUBLE GLUT DEPTH GLUT STENCIL GLUT ACCUM

*void glutInitContextVersion(int majorVersion, int minorVersion); • Must be called before glutCreateWindow (see below) • This - with glutInitContextProfile - set OpenGL’s context

*void glutInitContextProfile(int profile);

Profile GLUT CORE PROFILE - uses only those features associated with the specified context version GLUT COMPATIBILITY PROFILE - incorporates backwards compatibility

2. Window system setup • In addition to the following, numerous functions exist for controlling display mode, reshaping, and positioning windows programmatically

void glutInitWindowPosition(int x, int y); • (x, y) represent upper left corner in pixels • Defined in terms of screen coordinates void glutInitWindowSize(int width, int height); • In pixels

6 OpenGL Fixed Intro: Basic GLUT Functions (2)

int glutCreateWindow(const char *title);

*void glutCloseFunc ( void (*func) ( void ) ); • Function called when window closed • Used for cleanup 3. Register the display function

void glutDisplayFunc(void (*)(void)); 4. The GLUT main loop

void glutMainLoop(void); • Enters the GLUT infinite loop • Once entered, no way for program to regain control

*void glutMainLoopEvent ( void ); • Processes a single iteration of FreeGLUT’s event loop • Once entered, no way for program to regain control

*void glutLeaveMainLoop ( void ); • Exits FreeGLUT’s main loop • Behavior dependent on window action settings (see below)

7 OpenGL Fixed Intro: Basic GLUT Functions (3)

5. Setting GLUT state

*void glutSetOption ( GLenum state, int value );

State Action GLUT ACTION ON WINDOW CLOSE Control what happens when a window is closed GLUT INIT DISPLAY MODE Set display mode for new windows GLUT INIT WINDOW X Set initial horizontal position of new windows GLUT INIT WINDOW Y Set initial vertical position of new windows GLUT INIT WINDOW WIDTH Set width of new windows GLUT INIT WINDOW HEIGHT Set height of new windows GLUT RENDERING CONTEXT Set OpenGL rendering context for new windows GLUT MULTISAMPLE Set number of samples for new windows if GLUT MULTISAMPLE set GLUT GEOMETRY VISUALIZE NORMALS Set if FreeGLUR geometric objects normals rendered

State Value GLUT ACTION ON WINDOW CLOSE GLUT ACTION EXIT: exit the application (default) GLUT ACTION GLUTMAINLOOP RETURNS: return from main loop GLUT ACTION CONTINUE EXECUTION: continue execution of window GLUT INIT DISPLAY MODE see glutInitDisplayMode GLUT INIT WINDOW X int GLUT INIT WINDOW Y int GLUT INIT WINDOW WIDTH int GLUT INIT WINDOW HEIGHT int GLUT RENDERING CONTEXT GLUT CREATE NEW CONTEXT GLUT USE CURRENT CONTEXT GLUT MULTISAMPLE int GLUT GEOMETRY VISUALIZE NORMALS GL TRUE GL FALSE

8 OpenGL Fixed Intro: Basic GLUT Functions (4)

6. Querying GLUT state

*int glutGet ( GLenum state );

• Following dependent on current window • List is incomplete - other states include color and buffer info

State Value returned GLUT WINDOW X GLUT WINDOW Y GLUT WINDOW WIDTH pixels GLUT WINDOW HEIGHT pixels GLUT FULL SCREEN 1 if full screen

7. These independent of current window State Meaning Value GLUT SCREEN WIDTH pixels GLUT SCREEN HEIGHT pixels GLUT SCREEN WIDTH MM mm GLUT SCREEN HEIGHT MM mm GLUT SCREEN X GLUT SCREEN Y GLUT INIT WINDOW X Value last set by glutInitWindowPosition GLUT INIT WINDOW Y ditto GLUT INIT WINDOW WIDTH Value last set by glutInitWindowSize GLUT INIT WINDOW HEIGHT ditto GLUT INIT DISPLAY MODE Value last set by glutInitDisplayMode

9 OpenGL Fixed Intro: OpenGL Function Categories 1. Primitives 2. Attributes 3. Control 4. Input 5. Transformation 6. Viewing 7. Bitmap 8. Environmental

10