Download presentation
Presentation is loading. Please wait.
Published byHoward Warren Modified over 9 years ago
1
Basic OpenGL Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 10, 2003
2
10 Sep 2003CS 3812 Review: Linear Interpolation [1/2] Approximating a quantity between places where the quantity is known, is called interpolation. The simplest interpolation method is linear interpolation (lirping). Linear interpolation makes the (usually false!) assumption that the graph of the quantity between the two known values is a straight line. When we lirp, we are given two values of a quantity; we determine its value somewhere between these. The simple case: When t = 0, the quantity is equal to a. When t = 1, the quantity is equal to b. Given a value of t between 0 and 1, the interpolated value of the quantity is
3
10 Sep 2003CS 3813 Review: Linear Interpolation [2/2] What if we are not given values of the quantity at 0 and 1? Say the value at s 1 is a and the value at s 2 is b, and we want to find the interpolated value at s. We set and proceed as before: Lastly, when we want to lirp quantities with several coordinates (like points or colors), lirp each coordinate separately.
4
10 Sep 2003CS 3814 Review: Introduction to OpenGL [1/2] Professional-quality 2-D & 3-D graphics API Developed by Silicon Graphics Inc. in 1992. Based on Iris GL, the SGI graphics library. Available in a number of languages. OS-independent and hardware-independent. Aimed at 2-D/3-D scenes made of polygons (and lines and points). Not as good for 2-D windows/text/GUI-style graphics.
5
10 Sep 2003CS 3815 Review: Introduction to OpenGL [2/2] OpenGL Itself The interface with the graphics hardware. Designed for efficient implementation in hardware. Particular OpenGL implementations may be partially or totally software. C/C++ header:. The OpenGL Utilities (GLU) Additional functions & types for various graphics operations. Designed to be implemented in software; calls GL. C/C++ header:. OpenGL Extensions Functionality that anyone can add to OpenGL. OpenGL specifies rules that extensions are to follow. May be system-dependent. We will not use any extensions.
6
10 Sep 2003CS 3816 Basic OpenGL: Overview We now discuss how to use OpenGL to produce 3-D graphics. We will cover: The design of OpenGL. Design philosophy and conventions. OpenGL primitives. The basic things that OpenGL can draw. Tomorrow we will look the workings of an actual OpenGL/GLUT program.
7
10 Sep 2003CS 3817 The Design of OpenGL: Introduction OpenGL is an API for rendering raster images of 2-D/3-D scenes. So OpenGL’s work ends when the completed image (or frame, in an animation context) is in the frame buffer. We deal with OpenGL via function calls (or commands). No global variables. Most OpenGL functions have few parameters. But you make lots of function calls. No complex data types. OpenGL is function-call intensive. Think: advantages/disadvantages.
8
10 Sep 2003CS 3818 The Design of OpenGL: Example Code To draw a red triangle with vertices (0,0), (1,0), (1,1): glColor3d(0.9, 0.1, 0.1); // red (setting an attribute) glBegin(GL_TRIANGLES); // starting a primitive glVertex2d(0., 0.); // vertex data glVertex2d(1., 0.); glVertex2d(1., 1.); glEnd(); // ending the primitive
9
10 Sep 2003CS 3819 The Design of OpenGL : Naming Conventions [1/2] OpenGL (C API) Functions Begin with “ gl ”, words capitalized & run together Example: glClearColor Can include type information. For example, the “ 2d ” in “ glVertex2d ” indicates two parameters of type GLdouble. Constants Begin with “ GL ”, all upper-case, “ _ ” between words Example: GL_TRIANGLE_STRIP Types Begin with “ GL ”, next word not capitalized, all words run together Example: GLdouble
10
10 Sep 2003CS 38110 The Design of OpenGL : Naming Conventions [2/2] Related packages use similar conventions. GLU Function: gluScaleImage Constant: GLU_TESS_ERROR Type: GLUtesselatorObj GLUT (to be discussed on Friday) Function: glutInitDisplayMode Constant: GLUT_MIDDLE_BUTTON
11
10 Sep 2003CS 38111 The Design of OpenGL: Types [1/2] OpenGL defines its own types, which have the same (minimum) precision on all systems. Some of these: GLint : at least 32-bit integer GLfloat : at least 32-bit floating-point GLdouble : at least 64-bit floating-point and others … So, for example, GLdouble is probably the same as double, but may not be. Converting (say) a GLdouble to a double is fine. But be careful when tossing around GLdouble * and double *. (Why?)
12
10 Sep 2003CS 38112 The Design of OpenGL: Types [2/2] Some OpenGL commands have several forms allowing for different types. For example, glVertex * can take 2, 3, or 4 parameters of many different types. Function glVertex2d takes 2 parameters of type GLdouble. Function glVertex3f takes 3 parameters of type GLfloat. Function glVertex3fv (“v” for “vector”) takes a single parameter of type GLfloat * (should be a pointer to an array of 3 GLfloat ’s). The command glTranslate * always takes three parameters, but they may vary in type. Function glTranslated takes 3 GLdouble ’s. Function glTranslatef takes 3 GLfloat ’s.
13
10 Sep 2003CS 38113 The Design of OpenGL: Attributes & Primitives OpenGL functions as a state machine. There are three kinds of functions: Those that set state. Those that return state. Those that draw. Drawing is done via primitives. States are used to set attributes of those primitives. So: all drawn objects are composed of primitives. The properties of these are attributes, which are determined by OpenGL states. (Recall the red triangle example.)
14
10 Sep 2003CS 38114 OpenGL Primitives: Overview [1/2] All rendering operations are composed of primitives. These need to be useful to the programmer and doable efficiently by the library & hardware. We will now look at those OpenGL primitives that are handled via the glBegin - glEnd mechanism. There are ten of these; they consist of ways to draw: Points. Polylines. Filled polygons. Other primitive rendering operations are handled differently in OpenGL. Specifically, those involving screen-aligned rectangles: pixmaps, bitmaps, and screen-aligned rectangular polygons. Recall: OpenGL has no circle/ellipse/curve primitives.
15
10 Sep 2003CS 38115 OpenGL Primitives: Overview [2/2] The ten glBegin -style OpenGL Primitives Points (1 primitive) GL_POINTS Polylines (3 primitives) GL_LINES GL_LINE_STRIP GL_LINE_LOOP Filled Polygons (6 primitives) Triangles GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN Quadrilaterals GL_QUADS GL_QUAD_STRIP General Polygons GL_POLYGON
16
10 Sep 2003CS 38116 A primitive is given a number of vertices (specified with glVertex… ). Now we look at what the primitives do with the vertices they are given. Numbers indicate vertex ordering. Blue objects mark what is actually rendered. Points GL_POINTS OpenGL Primitives: Points 1 5 24 3 6
17
10 Sep 2003CS 38117 Polylines GL_LINES GL_LINE_STRIP GL_LINE_LOOP OpenGL Primitives: Polylines 1 5 24 3 6 6 6 1 5 24 3 1 5 24 3
18
10 Sep 2003CS 38118 Polygons: Triangles GL_TRIANGLES Clockwise or counterclockwise does not matter (yet). GL_TRIANGLE_STRIP GL_TRIANGLE_FAN OpenGL Primitives: Polygons — Triangles 1 5 24 3 6 6 1 5 2 4 3 1 5 24 3 6
19
10 Sep 2003CS 38119 Polygons: Quadrilaterals GL_QUADS Clockwise or counterclockwise does not matter (yet). GL_QUAD_STRIP Note differences in vertex ordering! Polygons: General GL_POLYGON OpenGL Primitives: Polygons — Quad’s, General 1 5 23 4 7 6 1 5 2 4 3 2 8 6 1 5 4 3 8 7 6
20
10 Sep 2003CS 38120 OpenGL Primitives: Restrictions When drawing points, lines, and triangles, vertices can be in any positions you like. Individual quadrilaterals and general polygons must be: Planar (this is easy in 2-D). Simple (no crossings, holes). Convex (bulging outward; no concavities). Know the ten primitives! Know the associated vertex orderings!
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.