Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Luebke5/16/2015 Administrivia l Back on track: canceling OpenGL lecture 2 l Assignment 1 –Greg Yukl found an alternate XForms site:

Similar presentations


Presentation on theme: "David Luebke5/16/2015 Administrivia l Back on track: canceling OpenGL lecture 2 l Assignment 1 –Greg Yukl found an alternate XForms site:"— Presentation transcript:

1 David Luebke5/16/2015 Administrivia l Back on track: canceling OpenGL lecture 2 l Assignment 1 –Greg Yukl found an alternate XForms site: http://world.std.com/~xforms/ –Updating display Call canvas_expose() directly (can ignore most args) n Figure out how to post a redisplay –Questions?

2 David Luebke5/16/2015 Introducing OpenGL l Recall the rendering pipeline: –Transform geometry (object  world, world  eye) –Apply perspective projection (eye  screen) –Clip to the view frustum –Perform visible-surface processing (Z-buffer) –Calculate surface lighting l Implementing all this is a lot of work (as you’ll find) l OpenGL provides a standard implementation –So why study the basics?

3 David Luebke5/16/2015 OpenGL Design Goals l SGI’s design goals for OpenGL: –High-performance (hardware-accelerated) graphics APIsome hardware independence –Natural, terse API with some built-in extensibility l OpenGL has become a standard because: –It doesn’t try to do too much n Only renders the image, doesn’t manage windows, etc. n No high-level animation, modeling, sound (!), etc. –It does enough n Useful rendering effects + high performance –It is promoted by SGI (& Microsoft, half-heartedly)

4 David Luebke5/16/2015 OpenGL: Conventions Functions in OpenGL start with gl –Most functions just gl (e.g., glColor() ) –Functions starting with glu are utility functions (e.g., gluLookAt() ) –Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)

5 David Luebke5/16/2015 OpenGL: Conventions l Function names indicate argument type and number –Functions ending with f take floats –Functions ending with i take ints –Functions ending with b take bytes –Functions ending with ub take unsigned bytes –Functions that end with v take an array. l Examples –glColor3f() takes 3 floats –glColor4fv() takes an array of 4 floats

6 David Luebke5/16/2015 OpenGL: Simple Use l Open a window and attach OpenGL to it l Set projection parameters (e.g., field of view) l Setup lighting, if any l Main rendering loop –Set camera pose with gluLookAt() n Camera position specified in world coordinates –Render polygons of model n Simplest case: vertices of polygons in world coordinates

7 David Luebke5/16/2015 OpenGL: Simple Use l Open a window and attach OpenGL to it –XForms glcanvas widget + code in gfx.c l Set projection parameters (e.g., field of view) l Setup lighting, if any l Main rendering loop –Set camera pose with gluLookAt() n Camera position specified in world coordinates –Render polygons of model n Simplest case: vertices of polygons in world coordinates

8 David Luebke5/16/2015 OpenGL: Simple Use l Open a window and attach OpenGL to it l Set projection parameters (e.g., field of view) l Setup lighting, if any l Main rendering loop –Set camera pose with gluLookAt() n Camera position specified in world coordinates –Render polygons of model n Simplest case: vertices of polygons in world coordinates

9 David Luebke5/16/2015 OpenGL: Perspective Projection l Typically, we use a perspective projection –Distant objects appear smaller than near objects –Vanishing point at center of screen –Defined by a view frustum (draw it) l Other projections: orthographic, isometric

10 David Luebke5/16/2015 OpenGL: Perspective Projection l In OpenGL: –Projections implemented by projection matrix –gluPerspective() creates a perspective projection matrix: glSetMatrix(GL_PROJECTION); glLoadIdentity(); //load an identity matrix gluPerspective(vfov, aspect, near, far); Parameters to gluPerspective() : –vfov : vertical field of view –aspect : window width/height –near, far : distance to near & far clipping planes

11 David Luebke5/16/2015 OpenGL: Simple Use l Open a window and attach OpenGL to it l Set projection parameters (e.g., field of view) l Setup lighting, if any l Main rendering loop –Set camera pose with gluLookAt() n Camera position specified in world coordinates –Render polygons of model n Simplest case: vertices of polygons in world coordinates

12 David Luebke5/16/2015 OpenGL: Lighting l Simplest option: change the current color between polygons or vertices –glColor() sets the current color l Or OpenGL provides a simple lighting model: –Set parameters for light(s) n Intensity, position, direction & falloff (if applicable) –Set material parameters to describe how light reflects from the surface l Won’t go into details now; check the red book if interested

13 David Luebke5/16/2015 OpenGL: Simple Use l Open a window and attach OpenGL to it l Set projection parameters (e.g., field of view) l Setup lighting, if any l Main rendering loop –Set camera pose with gluLookAt() n Camera position specified in world coordinates –Render polygons of model n Simplest case: vertices of polygons in world coordinates

14 David Luebke5/16/2015 OpenGL: Specifying Viewpoint l glMatrixMode(GL_MODELVIEW); l glLoadIdentity(); gluLookAt(eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); –eye[XYZ ]: camera position in world coordinates –look[XYZ] : a point centered in camera’s view –up[XYZ] : a vector defining the camera’s vertical l Creates a matrix that transforms points in world coordinates to camera coordinates –Camera at origin –Looking down -Z axis (Why?) –Up vector aligned with Y axis (actually Y-Z plane)

15 David Luebke5/16/2015 OpenGL: Specifying Geometry Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() –A simple example: telling GL to render a triangle glBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glEnd(); –Usage: glBegin( geomtype ) where geomtype is: n Points, lines, polygons, triangles, quadrilaterals, etc...

16 David Luebke5/16/2015 OpenGL: More Examples l Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); –This type of operation is called immediate-mode rendering; each command happens immediately –Why do you suppose OpenGL uses a series of glVertex() calls instead of one polygon function that takes all its vertices as arguments?

17 David Luebke5/16/2015 OpenGL: Front/Back Rendering l Each polygon has two sides, front and back l OpenGL can render the two differently l The ordering of vertices in the list determines which is the front side: –When looking at the front side, the vertices go counterclockwise n This is basically the right-hand rule n Note that this still holds after perspective projection

18 David Luebke5/16/2015 OpenGL: Drawing Triangles You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];... glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); l Each set of 3 vertices forms a triangle –What do the triangles drawn above look like? –How much redundant computation is happening?

19 David Luebke5/16/2015 OpenGL: Triangle Strips l An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v4); glVertex3fv(v5); glEnd(); –triangle 0 is v0, v1, v2 –triangle 1 is v2, v1, v3 (why not v1, v2, v3?) –triangle 2 is v2, v3, v4 –triangle 3 is v4, v3, v5 (again, not v3, v4, v5) v0v0 v2v2 v1v1 v3v3 v4v4 v5v5

20 David Luebke5/16/2015 OpenGL: Specifying Color l Can specify other properties such as color –To produce a single aqua-colored triangle: glColor3f(0.1, 0.5, 1.0); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); –To produce a Gouraud-shaded triangle: glColor3f(1, 0, 0); glVertex3fv(v0); glColor3f(0, 1, 0); glVertex3fv(v1); glColor3f(0, 0, 1); glVertex3fv(v2); –In OpenGL, colors can also have a fourth component  (opacity) n Generally want  = 1.0 (opaque);

21 David Luebke5/16/2015 OpenGL: Specifying Normals Calling glColor() sets the color for vertices following, until the next call to glColor() Calling glNormal() sets the normal vector for the following vertices, till next glNormal() l So flat-shaded lighting requires: glNormal3f(Nx, Ny, Nz); glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2); –While smooth shading requires: glNormal3f(N0x, N0y, N0z); glVertex3fv(v0); glNormal3f(N1x, N1y, N1z); glVertex3fv(v1); glNormal3f(N2x, N2y, N2z); glVertex3fv(v2); –(Of course, lighting requires additional setup…)


Download ppt "David Luebke5/16/2015 Administrivia l Back on track: canceling OpenGL lecture 2 l Assignment 1 –Greg Yukl found an alternate XForms site:"

Similar presentations


Ads by Google