OpenGL - Stanford University

Download Report

Transcript OpenGL - Stanford University

OpenGL
Kurt Akeley
CS248 Lecture 2
27 September 2007
http://graphics.stanford.edu/courses/cs248-07/
OpenGL
OpenGL

Is a mechanism to create images in a frame buffer

Is an API to access that mechanism

Is well specified
OpenGL

Is not a window system

Is not a user interface

Is not a display mechanism

Does not even own the framebuffer

It is owned by the window system so it can be shared

But OpenGL defines its attributes carefully
CS248 Lecture 2
Kurt Akeley, Fall 2007
White-square code
// Draw a white square against a black background
#include <windows.h>
#include <stdio.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
OpenGL
GLUT
CS248 Lecture 2
// yuck!
void draw() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(0, 4, 0, 4, -1, 1);
glBegin(GL_POLYGON);
glVertex2i(1, 1);
glVertex2i(3, 1);
glVertex2i(3, 3);
glVertex2i(1, 3);
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("whitesquare");
glutDisplayFunc(draw);
glutMainLoop();
}
Kurt Akeley, Fall 2007
OpenGL portion of white-square code
glClear(GL_COLOR_BUFFER_BIT); // black background
glLoadIdentity();
// allow multipass
glOrtho(0, 4, 0, 4, -1, 1);
// int cast to double
glBegin(GL_POLYGON);
// draw white square
glVertex2i(1, 1);
glVertex2i(3, 1);
glVertex2i(3, 3);
glVertex2i(1, 3);
glEnd();
glFlush();
CS248 Lecture 2
// force completion
Kurt Akeley, Fall 2007
Red-book example 1-1 OpenGL code
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glFlush();
CS248 Lecture 2
// force completion
Kurt Akeley, Fall 2007
State tables
COLOR_CLEAR_VALUE
0,0,0,0
OpenGL 2.0 Spec, Table 6.21. Framebuffer Control
CS248 Lecture 2
Kurt Akeley, Fall 2007
State tables
OpenGL 2.0, Table 6.5. Current Values and Associated Data
CS248 Lecture 2
Kurt Akeley, Fall 2007
A snippet from the OpenGL 2.0 spec
Vertices are specified by giving their coordinates in two,
three, or four dimensions. This is done using one of
several versions of the Vertex command:
void Vertex{234}{sifd}( T coords );
void Vertex{234}{sifd}v( T coords );
A call to any Vertex command specifies four coordinates:
x, y, z, and w. The x coordinate is the first coordinate, y
is second, z is third, and w is fourth. A call to Vertex2
sets the x and y coordinates; the z coordinate is
implicitly set to zero and the w coordinate to one.
CS248 Lecture 2
Kurt Akeley, Fall 2007
The OpenGL Vertex Pipeline
CS248 Lecture 2
Kurt Akeley, Fall 2007
OpenGL shaded-quad code
glClearColor(1, 1, 1, 1);
// white
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(0, 100, 0, 100, -1, 1);
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0, 0.5, 0); // dark green
glVertex2i(11, 31);
glVertex2i(37, 71);
glColor3f(0.5, 0, 0); // dark red
glVertex2i(91, 38);
glVertex2i(65, 71);
glEnd();
glFlush();
CS248 Lecture 2
Kurt Akeley, Fall 2007
OpenGL vertex pipeline
Emphasis is on data types
Diagram ignores

Pixel pipeline

Texture memory

Display lists

…
Display is not part of OpenGL
Application
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Vertex assembly (data types)
Application
struct {
float x,y,z,w;
float r,g,b,a;
} vertex;
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Vertex assembly (OpenGL)
Vertex assembly
 Force input to canonical format
Convert to internal representation
– E.g., x, y to float
 Initialize unspecified values
– E.g., z = 0, w=1
 Insert current modal state
– E.g., color to 0,0.5,0,1

Or create using evaluators
Error detection
 INVALID_ENUM
 INVALID_VALUE
 INVALID_OPERATION
Application
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations


Rasterization
Fragment operations
Framebuffer
Especially between Begin and End
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Vertex assembly (in our case)
glColor3f(0, 0.5, 0);
glVertex2i(11, 31);
glVertex2i(37, 71);
glColor3f(0.5, 0, 0); // no effect
Application
Vertex assembly
Vertex operations
Primitive assembly
struct {
float x,y,z,w; // 11, 31, 0, 1
float r,g,b,a; // 0, 0.5, 0, 1
} vertex;
struct {
float x,y,z,w; // 37, 71, 0, 1
float r,g,b,a; // 0, 0.5, 0, 1
} vertex;
CS248 Lecture 2
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
Kurt Akeley, Fall 2007
Vertex operations
Application
OpenGL

Transform coordinates

Vertex assembly
4x4 matrix arithmetic

Compute (vertex) lighting

Compute texture coordinates

…
In our case:

Scale (arbitrary 100x100) coordinates
to fit window

No lighting, no texture coordinates
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Primitive assembly (data types)
Application
struct {
float x,y,z,w;
float r,g,b,a;
} vertex;
struct {
vertex v0,v1,v2;
} triangle;
or
struct {
vertex v0,v1;
} line;
CS248 Lecture 2
or
struct {
vertex v0;
} point;
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
Kurt Akeley, Fall 2007
Primitive assembly
Application
OpenGL

Group vertexes into primitives:

points,

lines, or

triangles
Vertex assembly
Vertex operations

Decompose polygons to triangles

Duplicate vertexes in strips or fans
Primitive assembly
Primitive operations
In our case:

Rasterization
Create two triangles from a strip:
glBegin(GL_TRIANGLE_STRIP);
glColor(green);
glVertex2i(…); // 0
glVertex2i(…); // 1
glColor(red);
glVertex2i(…); // 2
glVertex2i(…); // 3
glEnd();
CS248 Lecture 2
0
1
3
Fragment operations
Framebuffer
2
Display
Kurt Akeley, Fall 2007
Primitive operations
Application
OpenGL

Clip to the window boundaries


Actually to the frustum surfaces
Perform back-face / front-face ops

Culling

Color assignment for 2-side lighting
In our case

Vertex assembly
Nothing happens
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Rasterization (data types)
Application
struct {
float x,y,z,w;
float r,g,b,a;
} vertex;
struct {
vertex v0,v1,v2
} triangle;
struct {
short int x,y;
float depth;
float r,g,b,a;
} fragment;
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Rasterization
Application
OpenGL

Determine which pixels are included in
the primitive
Vertex assembly
Generate a fragment for each such
pixel
Vertex operations


Assign attributes (e.g., color) to each
fragment
In our case:
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Fragment operations
OpenGL

Texture mapping

Fragment lighting (OpenGL 2.0)

Fog

Scissor test

Alpha test
In our case, nothing happens:
Application
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Framebuffer (2-D array of pixels)
struct {
float x,y,z,w;
float r,g,b,a;
} vertex;
struct {
vertex v0,v1,v2
} triangle;
struct {
short int x,y;
float depth;
float r,g,b,a;
} fragment;
CS248 Lecture 2
struct {
int depth;
byte r,g,b,a;
} pixel;
Application
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
Kurt Akeley, Fall 2007
Fragment  framebuffer operations
OpenGL
Application
Vertex assembly

Color blending

Depth testing (aka z-buffering)
Vertex operations

Conversion to pixels
Primitive assembly
In our case, conversion to pixels:
Primitive operations
Rasterization
Key idea: images
are built in the
framebuffer, not
just placed there!
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
Discussion
CS248 Lecture 2
Kurt Akeley, Fall 2007
OpenGL is mechanism
Programmer specifies operation modes:


Vertex

Transformation

Lighting
Primitive


Application
Vertex assembly
Vertex operations
Primitive assembly
Back-face culling
Fragment

Shading (texture and lighting)

Framebuffer (blending and depth
testing)
Great example is the diagram at the back of
the old (out of print) “blue book” …
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 2
Kurt Akeley, Fall 2007
CS248 Lecture 2
Kurt Akeley, Fall 2007
Abstraction
From the OpenGL specification, Section 1.5:
“An implementation must produce results
conforming to those produced by the specified
methods, but there may be ways to carry out a
particular computation that are more efficient
than the one specified.”
Thus OpenGL is an abstraction

It doesn’t specify what does happen

It specifies what appears to happen
CS248 Lecture 2
Kurt Akeley, Fall 2007
Implementation = abstraction
Application
Host
Data Assembler
Setup / Rstr / ZCull
Vtx Thread Issue
Geom Thread Issue Pixel Thread Issue
Vertex assembly
SP
SP
TF
SP
SP
TF
L1
SP
SP
TF
L1
SP
SP
SP
SP
TF
TF
L1
SP
TF
L1
L1
SP
SP
SP
TF
L1
SP
SP
TF
L1
L1
Thread Processor
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
L2
FB
L2
FB
L2
FB
L2
FB
L2
FB
NVIDIA GeForce 8800
CS248 Lecture 2
L2
FB
Framebuffer
OpenGL Pipeline
Kurt Akeley, Fall 2007
Declarative vs. imperative abstractions
Declarative (what, not how)

Descriptive: specify input and desired result


E.g., OmitHiddenSurfaces();
Example systems:

RenderMan scene description

Inventor and Performer scene graphs
Imperative (how, not what)

Procedural: specify actions to create the result


E.g., EnableDepthBuffer();
Example systems

PostScript and Xlib

OpenGL and Direct3D
CS248 Lecture 2
Kurt Akeley, Fall 2007
Graphics software stacks
web application
application
Gears of War
VRML
scene graph
Unreal engine
OpenGL 2.1
graphics API
Direct3D 10
GeForce 8800
GPU
Radeon 9600
CS248 Lecture 2
Kurt Akeley, Fall 2007
Levels of abstraction
Specific, high leverage
Declarative
web application
Gears of War
VRML
Unreal engine
OpenGL 2.1
Direct3D 10
GeForce 8800
Radeon 9600
Imperative
General, low leverage
CS248 Lecture 2
Kurt Akeley, Fall 2007
An imperative API defines an architecture
We mean architecture in the Brooks/Blaauw sense:

Separate from and above implementation
OpenGL defines an architecture:
OpenGL 2.1
GeForce 8800
Can separate architecture’s interface and mechanism:

Direct3D 10 (interface)

WGF 2.0 (mechanism)
CS248 Lecture 2
Kurt Akeley, Fall 2007
Additional OpenGL mechanisms
Pixel pipeline
Client-server operation
Display lists
Error behavior and reporting
CS248 Lecture 2
Kurt Akeley, Fall 2007
OpenGL pixel pipeline
Pixel pipeline
Application
Vertex pipeline
Vertex assembly
Pixel assembly
(unpack)
Vertex operations
Primitive assembly
Pixel operations
Primitive operations
Pixel pack
Rasterization
Texture memory
Fragment operations
Application
Framebuffer
CS248 Lecture 2
KurtDisplay
Akeley, Fall 2007
Client-server operation
Application
OpenGL Client
Network
Client side
Server side
OpenGL Server
Display (you)
CS248 Lecture 2
Kurt Akeley, Fall 2007
Display lists
Application
glNewList(listname, …);
glColor3f(…);
glVertex3f(…);
OpenGL Client
…
glEndList();
Network
Client side
Server side
OpenGL Server
glCallList(listname);
Display
list
state
Display (you)
CS248 Lecture 2
Kurt Akeley, Fall 2007
Error behavior and reporting
No undefined operations

Architectural integrity

Application portability
Queried, not returned

Application
OpenGL Client
Why ?
Enumerated error types

INVALID_ENUM

INVALID_VALUE

INVALID_OPERATION
Network
OpenGL Server
Should query after rendering
Display (you)
CS248 Lecture 2
Kurt Akeley, Fall 2007
Check for errors after rendering
void CheckErrors() {
bool got_error = false;
while (int i = glGetError()) {
got_error = true;
fprintf(stderr, "ERROR: 0x%x\n", i);
}
if (got_error)
exit(1);
}
Example application-defined error routine
CS248 Lecture 2
Kurt Akeley, Fall 2007
OpenGL abstraction properties
Immediate mode
Orthogonality
Programmability
Details

Ordered operation

Modal state model

Synchronous operation
CS248 Lecture 2
Kurt Akeley, Fall 2007
Immediate mode
Different meanings to different people

Begin/End command sequences

Abstraction doesn’t maintain object
descriptions


Vertex arrays stored on client side

Display lists don’t count
Implementation doesn’t queue
indefinitely

E.g., doesn’t wait for all primitives
before beginning to render
Application
OpenGL Client
Network
OpenGL Server
Display (you)
CS248 Lecture 2
Kurt Akeley, Fall 2007
Orthogonality
Pixel pipeline
Pixel assembly
(unpack)
Pixel operations
Application
All vertexes are
treated equally (e.g.,
lighted)
All primitives
(including pixels) are
rasterized
Pixel pack
Vertex pipeline
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Texture memory
Fragment operations
Application
CS248 Lecture 2
All fragments are
treated equally (e.g.,
z-buffered)
Framebuffer
KurtDisplay
Akeley, Fall 2007
Programmability
Programmable “shaders,” of course
Application
Vertex assembly
Multi-pass composition

Intended from the start

Requires repeatability

Invariance and equivariance

Appendix A in OpenGL spec
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
 Application programmable
CS248 Lecture 2
Kurt Akeley, Fall 2007
Abstraction details
Ordered operation

Implementation may be out of order

But results must be as though computed in order
Modal state model

Also in-order

Pipeline implementations require care
Synchronous operation (as in synchronous i/o)

Data bound at call

Queries stall until data are ready and returned
CS248 Lecture 2
Kurt Akeley, Fall 2007
Interface details
Object naming (programmer-assigned, why)
Lots of geometry-specification entry points (why)
CS248 Lecture 2
Kurt Akeley, Fall 2007
Other things you should know
Programming tips (Red book, Appendix G)

Correctness tips (e.g., pixel-exact 2-D rendering)

Performance tips (user/implementer contract)
Extensions (www.opengl.org)

Recent ones include history and examples

Great way to understand graphics algorithms
CS248 Lecture 2
Kurt Akeley, Fall 2007
Summary
OpenGL is an imperative abstraction (aka architecture)

Mechanism (the OpenGL pipeline)

Interface (API) to feed and manage the mechanism
The pipeline is best understood in terms of data types:

Vertex

Primitive (point, line, triangle)

Fragment

Pixel
You should trust and be familiar with the OpenGL
specification
CS248 Lecture 2
Kurt Akeley, Fall 2007
Reading assignment
Before Tuesday’s class, read

FvD 14.10 Aliasing and antialiasing

Return to the OpenGL specification

E.g., state tables
Optional:

Set up your OpenGL/GLUT programming
environment
CS248 Lecture 2
Kurt Akeley, Fall 2007
End
CS248 Lecture 2
Kurt Akeley, Fall 2007