OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

48
OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007 http://graphics.stanford.edu/courses/cs248-07/
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    4

Transcript of OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

Page 1: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

OpenGL

Kurt Akeley

CS248 Lecture 2

27 September 2007

http://graphics.stanford.edu/courses/cs248-07/

Page 2: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

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

Page 3: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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 // yuck!#include <GL/glut.h>

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();}

OpenGL

GLUT

Page 4: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 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(); // force completion

Page 5: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 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(); // force completion

Page 6: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

State tables

OpenGL 2.0 Spec, Table 6.21. Framebuffer Control

COLOR_CLEAR_VALUE 0,0,0,0

Page 7: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

State tables

OpenGL 2.0, Table 6.5. Current Values and Associated Data

Page 8: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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.

Page 9: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

The OpenGL Vertex Pipeline

Page 10: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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();

Page 11: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Page 12: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Vertex assembly (data types)

struct { float x,y,z,w; float r,g,b,a;} vertex;

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Page 13: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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

Especially between Begin and End

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Page 14: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Vertex assembly (in our case)

struct { float x,y,z,w; // 11, 31, 0, 1 float r,g,b,a; // 0, 0.5, 0, 1} vertex;

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

glColor3f(0, 0.5, 0);glVertex2i(11, 31);glVertex2i(37, 71);glColor3f(0.5, 0, 0); // no effect

struct { float x,y,z,w; // 37, 71, 0, 1 float r,g,b,a; // 0, 0.5, 0, 1} vertex;

Framebuffer

Page 15: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Vertex operations

OpenGL

Transform coordinates 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 assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Page 16: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Primitive assembly (data types)

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

struct { float x,y,z,w; float r,g,b,a;} vertex;

struct { vertex v0,v1,v2;

} triangle;

struct { vertex v0,v1; } line;

struct { vertex v0; } point;

or

or Framebuffer

Page 17: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Primitive assembly

OpenGL Group vertexes into primitives:

points, lines, or triangles

Decompose polygons to triangles Duplicate vertexes in strips or

fans

In our case: Create two triangles from a strip:

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

glBegin(GL_TRIANGLE_STRIP);glColor(green);glVertex2i(…); // 0glVertex2i(…); // 1glColor(red);glVertex2i(…); // 2glVertex2i(…); // 3glEnd();

0

1 3

2

Framebuffer

Page 18: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Primitive operations

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

Nothing happens

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Page 19: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Rasterization (data types)

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

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; Framebuffer

Page 20: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Rasterization

OpenGL

Determine which pixels are included in the primitive Generate a fragment for each

such pixel

Assign attributes (e.g., color) to each fragment

In our case:

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Page 21: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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:

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Page 22: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Framebuffer (2-D array of pixels)

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

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;

struct { int depth; byte r,g,b,a;} pixel;

Framebuffer

Page 23: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Fragment framebuffer operations

OpenGL

Color blending

Depth testing (aka z-buffering)

Conversion to pixels

In our case, conversion to pixels:

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Key idea: images are built in the

framebuffer, not just placed

there!

Framebuffer

Page 24: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Discussion

Page 25: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

OpenGL is mechanism

Programmer specifies operation modes:

Vertex Transformation

Lighting

Primitive 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” …

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Page 26: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Page 27: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Abstraction

From the OpenGL specification, Section 1.5:

Thus OpenGL is an abstraction

It doesn’t specify what does happen

It specifies what appears to happen

“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.”

Page 28: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Implementation = abstraction

L2

FB

SP SP

L1

TF

Th

rea

d P

roc

es

so

r

Vtx Thread Issue

Setup / Rstr / ZCull

Geom Thread Issue Pixel Thread Issue

Data Assembler

Host

SP SP

L1

TF

SP SP

L1

TF

SP SP

L1

TF

SP SP

L1

TF

SP SP

L1

TF

SP SP

L1

TF

SP SP

L1

TF

L2

FB

L2

FB

L2

FB

L2

FB

L2

FB

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Vertex operations

Application

Primitive operations

NVIDIA GeForce 8800

OpenGL Pipeline

Framebuffer

Page 29: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 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

Page 30: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Direct3D 10OpenGL 2.1

Graphics software stacks

GeForce 8800

VRML

web application

Radeon 9600

Unreal engine

Gears of War

GPU

graphics API

scene graph

application

Page 31: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Direct3D 10OpenGL 2.1

Levels of abstraction

GeForce 8800

VRML

web application

Radeon 9600

Unreal engine

Gears of War

Imperative

Declarative

Specific, high leverage

General, low leverage

Page 32: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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:

Can separate architecture’s interface and mechanism:

Direct3D 10 (interface)

WGF 2.0 (mechanism)

OpenGL 2.1

GeForce 8800

Page 33: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Additional OpenGL mechanisms

Pixel pipeline

Client-server operation

Display lists

Error behavior and reporting

Page 34: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

OpenGL pixel pipeline

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Texture memory

Pixel assembly(unpack)

Pixel operations

Pixel pack

Vertex pipelinePixel pipeline

Application

Page 35: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Client-server operation

OpenGL Client

OpenGL Server

Network

Application

Display (you)

Client side

Server side

Page 36: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Display lists

glNewList(listname, …);

glColor3f(…);

glVertex3f(…);

glEndList();

glCallList(listname);

OpenGL Client

OpenGL Server

Network

Application

Display (you)

Client side

Server side

Displaylist

state

Page 37: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Error behavior and reporting

No undefined operations

Architectural integrity

Application portability

Queried, not returned

Why ?

Enumerated error types

INVALID_ENUM

INVALID_VALUE

INVALID_OPERATION

Should query after rendering

OpenGL Client

OpenGL Server

Network

Application

Display (you)

Page 38: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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

Page 39: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

OpenGL abstraction properties

Immediate mode

Orthogonality

Programmability

Details

Ordered operation

Modal state model

Synchronous operation

Page 40: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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

OpenGL Client

OpenGL Server

Network

Application

Display (you)

Page 41: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Orthogonality

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Display

Vertex operations

Application

Primitive operations

Framebuffer

Texture memory

Pixel assembly(unpack)

Pixel operations

Pixel pack

Vertex pipelinePixel pipeline

Application

All primitives (including pixels)

are rasterized

All vertexes are treated equally (e.g., lighted)

All fragments are treated equally

(e.g., z-buffered)

Page 42: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Programmability

Programmable “shaders,” of course

Multi-pass composition Intended from the start Requires repeatability

Invariance and equivariance Appendix A in OpenGL spec

Vertex assembly

Primitive assembly

Rasterization

Fragment operations

Vertex operations

Application

Primitive operations

Framebuffer

Application programmable

Page 43: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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

Page 44: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

Interface details

Object naming (programmer-assigned, why)

Lots of geometry-specification entry points (why)

Page 45: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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

Page 46: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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

Page 47: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

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

Page 48: OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

CS248 Lecture 2 Kurt Akeley, Fall 2007

End