Download presentation source

Download Report

Transcript Download presentation source

CS 551 / 645:
Introductory Computer Graphics
David Luebke
[email protected]
http://www.cs.virginia.edu/~cs551
David Luebke
7/27/2016
Administrivia


TA make-up office hours: 1:30-2:30 today
No class Friday
– Attending e-summit @ virginia
– Come hear millionaires pontificate, or just sleep in

Correction to Assignment 7
– Per-vertex normals (specified in .poly file)
– New versions of models available
David Luebke
7/27/2016
Introducing OpenGL

By now we know all the basics:
–
–
–
–
–


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
Implementing all this is a lot of work (surprise)
OpenGL provides a standard implementation
– So why study the basics?
David Luebke
7/27/2016
OpenGL Design Goals

SGI’s design goals for OpenGL:
– Hardware independence (in immediate mode)
without sacrificing performance
– Natural, terse API with some built-in extensibility

OpenGL has become a standard because:
– It doesn’t try to do too much


Only renders the image, doesn’t manage windows, etc.
No high-level animation, modeling, sound (!), etc.
– It does enough

Useful rendering effects + high performance
– It is promoted by SGI (& Microsoft, half-heartedly)
David Luebke
7/27/2016
OpenGL: Conventions

Functions in OpenGL start with gl
– Functions starting with glu are utility functions
(i.e., gluLookAt())
– Functions starting with glx are for interfacing with
the X Windows system (i.e., in gfx.c)

Function names indicate argument type/#
– Functions ending with f take floats
– Functions ending with i take ints, functions that
end with v take an array, with b take byte, etc.
– Ex: glColor3f() takes 3 floats, but
glColor4fv() takes an array of 4 floats
David Luebke
7/27/2016
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:

David Luebke
Points, lines, polygons, triangles, quadrilaterals, etc...
7/27/2016
OpenGL: More Examples

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?
David Luebke
7/27/2016
OpenGL: Front/Back Rendering



Each polygon has two sides, front and back
OpenGL can render the two differently
The ordering of vertices in the list determines
which is the front side:
– When looking at the front side, the vertices go
counterclockwise


David Luebke
This is basically the right-hand rule
Note that this still holds after perspective projection
7/27/2016
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();

Each set of 3 vertices forms a triangle
– What do the triangles drawn above look like?
– How much redundant computation is happening?
David Luebke
7/27/2016
OpenGL: Triangle Strips

An OpenGL triangle strip primitive reduces
this redundancy by sharing vertices:
glBegin(GL_TRIANGLE_STRIP);
glVertex3fv(v0);
glVertex3fv(v1);
v0
glVertex3fv(v2);
glVertex3fv(v3);
glVertex3fv(v4);
glVertex3fv(v5);
v1
glEnd();
–
–
–
–
David Luebke
v2
v4
v5
v3
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)
7/27/2016
OpenGL: Specifying Color

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  (transparency)

David Luebke
Generally want  = 1.0 (opaque);
7/27/2016
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 points, till next glNormal()
– So flat-shaded Phong 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…)
David Luebke
7/27/2016
Recap: Lighting

Recall the Phong lighting model:
I total  ka I ambient 
David Luebke
#lights

i 1

 
I i  kd Nˆ  Lˆ  k s Vˆ  Rˆ


nshiny


7/27/2016
OpenGL: Lighting



OpenGL binds our Phong lighting coefficients
(ka, kd, ks, nshiny) into materials
Calling glMaterialfv() sets the current
material (actually just a single attribute)
Example:
float green[] = {0, 1,
float white[] = {1, 1,
glMaterialfv(GL_FRONT,
glMaterialfv(GL_FRONT,
David Luebke
0, 1};
1, 1};
GL_DIFFUSE, green);
GL_SPECULAR, white);
7/27/2016
OpenGL: Lighting

OpenGL supports at least 8 lights, with
parameters set by the glLight() call:
float l_amb [] = {.1, .1, .1, 1.0};
float l_diff[] = {1, 0, 0, 1};
float l_spec[] = {1, 1, 1, 1};
float l_pos[] = {10, 100, 30, 0};
glLightfv(GL_LIGHT0, GL_AMBIENT, l_amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l_diff);
glLightfv(GL_LIGHT0, GL_SPECULAR, l_spec);
glLightf(GL_LIGHT0, GL_SHININESS, 50.0);
glLightfv(GL_LIGHT0, GL_POSITION, l_pos);
– What might the 4th coordinate in l_pos be for?
David Luebke
7/27/2016