Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 91.427 Computer Graphics I, Fall 2010 1 Programming with OpenGL Part 2: Complete Programs.

Similar presentations


Presentation on theme: "1 91.427 Computer Graphics I, Fall 2010 1 Programming with OpenGL Part 2: Complete Programs."— Presentation transcript:

1 1 91.427 Computer Graphics I, Fall 2010 1 Programming with OpenGL Part 2: Complete Programs

2 2 91.427 Computer Graphics I, Fall 2010 2 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

3 3 91.427 Computer Graphics I, Fall 2010 3 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

4 4 91.427 Computer Graphics I, Fall 2010 4 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

5 5 91.427 Computer Graphics I, Fall 2010 5 main.c #include 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(); } includes gl.h define window properties set OpenGL state enter event loop display callback

6 6 91.427 Computer Graphics I, Fall 2010 6 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 OR’ed 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

7 7 91.427 Computer Graphics I, Fall 2010 7 init.c void init() { glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); } black clear color opaque window fill/draw with white viewing volume

8 8 91.427 Computer Graphics I, Fall 2010 8 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

9 9 91.427 Computer Graphics I, Fall 2010 9 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

10 10 91.427 Computer Graphics I, Fall 2010 10 Orthographic Viewing z=0 In default orthographic view, points are projected forward along z axis onto plane z = 0

11 11 91.427 Computer Graphics I, Fall 2010 11 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);

12 12 91.427 Computer Graphics I, Fall 2010 12 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

13 13 91.427 Computer Graphics I, Fall 2010 13 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(); }

14 14 91.427 Computer Graphics I, Fall 2010 14 OpenGL Primitives GL_QUAD_STRIP GL_POLYGON GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_POINTS GL_LINES GL_LINE_LOOP GL_LINE_STRIP GL_TRIANGLES

15 15 91.427 Computer Graphics I, Fall 2010 15 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

16 16 91.427 Computer Graphics I, Fall 2010 16 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

17 17 91.427 Computer Graphics I, Fall 2010 17 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

18 18 91.427 Computer Graphics I, Fall 2010 18 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

19 19 91.427 Computer Graphics I, Fall 2010 19 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

20 20 91.427 Computer Graphics I, Fall 2010 20 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

21 21 91.427 Computer Graphics I, Fall 2010 21 Viewports Don’t have to use entire window for image: glViewport(x,y,w,h) Values in pixels (screen coordinates)


Download ppt "1 91.427 Computer Graphics I, Fall 2010 1 Programming with OpenGL Part 2: Complete Programs."

Similar presentations


Ads by Google