Programming with OpenGL Part 2: Complete Programs 91.427 Computer Graphics I, Fall 2008 1 1
Objectives Refine first program Alter default values Introduce standard program structure Simple viewing 2-D viewing as special case of 3-D viewing Fundamental OpenGL primitives Attributes 91.427 Computer Graphics I, Fall 2008 2 2
Program Structure Most OpenGL programs have similar structure Consists of following functions -main(): defines callback functions opens one or more windows with required properties enters event loop (last executable statement) -init(): sets state variables Viewing Attributes callbacks Display function Input and window functions 91.427 Computer Graphics I, Fall 2008 3 3
simple.c revisited In this version, see same output, but defined all relevant state values through function calls using default values In particular, we set Colors Viewing conditions Window properties 91.427 Computer Graphics I, Fall 2008 4 4
main.c #include <GL/glut.h> includes gl.h int main(int argc, char** argv) { glutinit(&argc,argv); glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(500,500); glutinitwindowposition(0,0); glutcreatewindow("simple"); glutdisplayfunc(mydisplay); init(); glutmainloop(); } define window properties display callback set OpenGL state enter event loop 91.427 Computer Graphics I, Fall 2008 5 5
GLUT functions glutinit allows application to get command line arguments and initializes system gluinitdisplaymode requests properties for window (rendering context) RGB color Single buffering Properties logically ORed together glutwindowsize in pixels glutwindowposition from top-left corner of display glutcreatewindow create window with title simple glutdisplayfunc display callback glutmainloop enter infinite event loop 91.427 Computer Graphics I, Fall 2008 6 6
init.c void init() { glclearcolor (0.0, 0.0, 0.0, 1.0); black clear color opaque window glcolor3f(1.0, 1.0, 1.0); fill/draw with white glmatrixmode (GL_PROJECTION); glloadidentity (); glortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); } viewing volume 91.427 Computer Graphics I, Fall 2008 7 7
Coordinate Systems Units in glvertex determined by application object or problem coordinates Viewing specifications also in object coordinates size of viewing volume determines what will appear in image Internally, OpenGL convert to camera (eye) coordinates and later to screen coordinates OpenGL also uses some internal representations usually not visible to application 91.427 Computer Graphics I, Fall 2008 8 8
OpenGL Camera OpenGL places camera at origin in object space pointing in negative z direction default viewing volume = box centered at origin with side of length 2 91.427 Computer Graphics I, Fall 2008 9 9
Orthographic Viewing In default orthographic view, points are projected forward along z axis onto plane z = 0 z=0 z=0 91.427 Computer Graphics I, Fall 2008 10 10
Transformations and Viewing In OpenGL, projection carried out by projection matrix (transformation) only one set of transformation functions ==> must set matrix mode first glmatrixmode (GL_PROJECTION) Transformation functions incremental ==> start with identity matrix alter it with projection matrix that gives view volume glloadidentity(); glortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 91.427 Computer Graphics I, Fall 2008 11 11
Two- and three-dim viewing In glortho(left, right, bottom, top, near, far) near and far distances measured from camera 2-D vertex commands place all vertices in plane z = 0 If application is in 2-D, can use function gluortho2d(left, right,bottom,top) In 2-D view or clipping volume becomes clipping window 91.427 Computer Graphics I, Fall 2008 12 12
mydisplay.c void mydisplay() { glclear(gl_color_buffer_bit); glbegin(gl_polygon); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); glend(); glflush(); } 91.427 Computer Graphics I, Fall 2008 13 13
OpenGL Primitives GL_POINTS GL_LINES GL_LINE_STRIP GL_POLYGON GL_LINE_LOOP GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN 91.427 Computer Graphics I, Fall 2008 14 14
Polygon Issues OpenGL only display correctly polygons that are Simple: edges cannot cross Convex: All points on segment also in polygon Flat: all vertices in same plane (planar) User program can check if above true OpenGL produce output if conditions violated but may not be what desired Triangles satisfy all conditions nonsimple polygon nonconvex polygon 91.427 Computer Graphics I, Fall 2008 15 15
Attributes Attributes are part of OpenGL state determine appearance of objects Color (points, lines, polygons) Size and width (points, lines) Stipple pattern (lines, polygons) Polygon mode Display as filled: solid color or stipple pattern Display edges Display vertices 91.427 Computer Graphics I, Fall 2008 16 16
RGB color Each color component stored separately in frame buffer Usually 8 bits per component in buffer Note in glcolor3f color values range from 0.0 (none) to 1.0 (all), whereas in glcolor3ub values range from 0 to 255 91.427 Computer Graphics I, Fall 2008 17 17
Indexed Color Colors = indices into tables of RGB values Requires less memory indices usually 8 bits not as important now Memory inexpensive Need more colors for shading 91.427 Computer Graphics I, Fall 2008 18 18
Color and State color as set by glcolor becomes part of state will be used until changed Colors and other attributes not part of object but assigned when object rendered can create conceptual vertex colors by code such as glcolor glvertex glcolor glvertex 91.427 Computer Graphics I, Fall 2008 19 19
Smooth Color Default is smooth shading OpenGL interpolates vertex colors across visible polygons Alternative is flat shading Color of first vertex determines fill color glshademodel (GL_SMOOTH) or GL_FLAT 91.427 Computer Graphics I, Fall 2008 20 20
Viewports Don t have to use entire window for image: glviewport(x,y,w,h) Values in pixels (screen coordinates) 91.427 Computer Graphics I, Fall 2008 21 21