Programming with OpenGL Part 1: Background Early History of

Objectives • IFIPS (1973) formed two committees to come up with a standard graphics API • Development of the OpenGL API - (GKS) • OpenGL Architecture • 2D but contained good workstation model - Core - OpenGL as a state machine • Both 2D and 3D • Functions - GKS adopted as IS0 and later ANSI standard - Types (1980s) -Formats • GKS not easily extended to 3D (GKS-3D) • Simple program - Far behind hardware development

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 2

PHIGS and X SGI and GL

•Programmers Hierarchical Graphics • Silicon Graphics (SGI) revolutionized the System (PHIGS) graphics workstation by implementing the - Arose from CAD community pipeline in hardware (1982) - Database model with retained graphics (structures) • To access the system, application • programmers used a library called GL - DEC/MIT effort • With GL, it was relatively simple to - Client-server architecture with graphics program three dimensional interactive • PEX combined the two applications - Not easy to use (all the defects of each)

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 3 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 4

1 OpenGL OpenGL Evolution

The success of GL lead to OpenGL (1992), • Controlled by an Architectural Review a platform-independent API that was Board (ARB) - Easy to use - Members include SGI, Microsoft, Nvidia, HP, - Close enough to the hardware to get excellent 3DLabs, IBM,……. performance - Relatively stable (present version 2.0) - Focus on rendering • Evolution reflects new hardware capabilities – 3D texture mapping and texture objects - Omitted windowing and input to avoid window – Vertex programs system dependencies - Allows for platform specific features through extensions

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 5 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 6

OpenGL Libraries GLUT

• OpenGL core library • OpenGL Utility Toolkit (GLUT) - OpenGL32 on Windows - Provides functionality common to all window - GL on most unix/linux systems (libGL.a) systems • OpenGL Utility Library (GLU) • Open a window - Uses functions from OpenGL core to create • Get input from mouse and keyboard more complex objects • Menus • Event-driven • Links with window system - Code is portable but GLUT lacks the - GLX for X window systems functionality of a good toolkit for a specific - WGL for Windows platform - AGL for Macintosh • No slide bars

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 7 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 8

2 Software Organization OpenGL Architecture

Immediate Mode geometry application program pipeline

Per Vertex OpenGL Motif Polynomial Operations & widget or similar GLUT Evaluator Primitive Assembly GLX, AGL or WGL GLU Display Per Fragment Frame CPU Rasterization X, Win32, Mac O/S GL List Operations Buffer

software and/or hardware Texture Memory Pixel Operations Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 9 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 10

OpenGL Functions OpenGL State

• Primitives • OpenGL is a state machine - Points - Line Segments • OpenGL functions are of two types - Polygons - Primitive generating • Attributes – colors, patterns, typefaces • Can cause output if primitive is visible • How vertices are processed and appearance of primitive • Transformations are controlled by the state -Viewing - State changing - Modeling • Transformation functions • Control (GLUT) • Attribute functions • Input (GLUT) • Query

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 11 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 12

3 Lack of Object Orientation OpenGL function format

• OpenGL is not object oriented so that function name there are multiple functions for a given dimensions logical function glVertex3f(x,y,z) -glVertex3f -glVertex2i are belongs to GL library x,y,z floats -glVertex3dv • Underlying storage mode is the same • Easy to create overloaded functions in glVertex3fv(p) C++ but issue is efficiency p is a pointer to an array

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 13 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 14

OpenGL #defines A Simple Program

• Most constants are defined in the include Generate a square on a solid background files gl.h, glu.h and glut.h - Note #include should automatically include the others -Examples -glBegin(GL_POLYGON) -glClear(GL_COLOR_BUFFER_BIT) • include files also define OpenGL data types: GLfloat, GLdouble,….

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 15 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 16

4 simple.c Event Loop

#include • Note that the program defines a display void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); callback function named mydisplay glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); - Every glut program must have a display glVertex2f(-0.5, 0.5); callback glVertex2f(0.5, 0.5); - The display callback is executed whenever glVertex2f(0.5, -0.5); glEnd(); OpenGL decides the display must be refreshed, glFlush(); for example when the window is opened } int main(int argc, char** argv){ - The main function ends with the program glutCreateWindow("simple"); entering an event loop glutDisplayFunc(mydisplay); glutMainLoop(); } Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 17 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 18

Defaults Notes on compilation

•simple.c is too simple • See website and ftp for examples • Makes heavy use of state variable default • Unix/linux values for - Include files usually in …/include/GL -Viewing - Compile with –lglut –lglu –lgl loader flags - Colors - May have to add –L flag for X libraries - Window parameters - Mesa implementation included with most linux • Next version will make the defaults more distributions explicit - Check web for latest versions of Mesa and glut

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 19 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 20

5 Compilation on Windows

• Visual C++ - Get glut.h, glut32.lib and glut32.dll from web - Create a console application - Add opengl32.lib, glut32.lib, glut32.lib to project settings (under link tab) • Borland C similar • Cygwin (linux under Windows) - Can use gcc and similar makefile to linux - Use –lopengl32 –lglu32 –lglut32 flags

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 21

6