Title: Model Formats, Compositing and Blending
1Model Formats, Compositing and Blending
2Representing a Mesh
- Consider a mesh
- There are 8 nodes and 12 edges
- 5 interior polygons
- 6 interior (shared) edges
- Each vertex has a location vi (xi yi zi)
e2
v5
v6
e3
e9
e8
v8
v4
e1
e11
e10
v7
e4
e7
v1
e12
v2
v3
e6
e5
3Simple Representation
- Define each polygon by the geometric locations of
its vertices - Leads to OpenGL code such as
- Inefficient and unstructured
- Consider moving a vertex to a new location
glBegin(GL_POLYGON) glVertex3f(x1, y1, z1)
glVertex3f(x6, y6, z6) glVertex3f(x7, y7,
z7) glEnd()
4Inward and Outward Facing Polygons
- The order v1, v6, v7 and v6, v7, v1 are
equivalent in that the same polygon will be
rendered by OpenGL but the order v1, v7, v6 is
different - The first two describe outwardly
- facing polygons
- Use the right-hand rule
- counter-clockwise encirclement
- of outward-pointing normal
- OpenGL can treat inward and
- outward facing polygons differently
5Geometry vs Topology
- Generally it is a good idea to look for data
structures that separate the geometry from the
topology - Geometry locations of the vertices
- Topology organization of the vertices and edges
- Example a polygon is an ordered list of vertices
with an edge connecting successive pairs of
vertices and the last to the first - Topology holds even if geometry changes
6Vertex Lists
- Put the geometry in an array
- Use pointers from the vertices into this array
- Introduce a polygon list
x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4 x5 y5 z5. x6
y6 z6 x7 y7 z7 x8 y8 z8
v1 v7 v6
P1 P2 P3 P4 P5
v8 v5 v6
topology
geometry
7Shared Edges
- Vertex lists will draw filled polygons correctly
but if we draw the polygon by its edges, shared
edges are drawn twice - Can store mesh by edge list
8Edge List
x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4 x5 y5 z5. x6
y6 z6 x7 y7 z7 x8 y8 z8
e1 e2 e3 e4 e5 e6 e7 e8 e9
v1 v6
Note polygons are not represented
9Modeling a Cube
Model a color cube for rotating cube
program Define global arrays for vertices and
colors
- GLfloat vertices3 -1.0,-1.0,-1.0,
- 1.0,-1.0,-1.0,1.0,1.0,-1.0, -1.0,1.0,-1.0,
- -1.0,-1.0,1.0,1.0,-1.0,1.0, 1.0,1.0,1.0,
- -1.0,1.0,1.0
GLfloat colors3 0.0,0.0,0.0,1.0,0.0,0.0
, 1.0,1.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0,
1.0,0.0,1.0, 1.0,1.0,1.0, 0.0,1.0,1.0
10Drawing a polygon from a list of indices
- Draw a quadrilateral from a list of indices into
the array vertices and use color corresponding to
first index
void polygon(int a, int b, int c , int d)
glBegin(GL_POLYGON) glColor3fv(colorsa)
glVertex3fv(verticesa)
glVertex3fv(verticesb)
glVertex3fv(verticesc)
glVertex3fv(verticesd) glEnd()
11Draw cube from faces
- void colorcube( )
-
- polygon(0,3,2,1)
- polygon(2,3,7,6)
- polygon(0,4,7,3)
- polygon(1,2,6,5)
- polygon(4,5,6,7)
- polygon(0,1,5,4)
5
6
2
1
7
4
0
3
Note that vertices are ordered so that we obtain
correct outward facing normals
12Efficiency
- The weakness of our approach is that we are
building the model in the application and must do
many function calls to draw the cube - Drawing a cube by its faces in the most straight
forward way requires - 6 glBegin, 6 glEnd
- 6 glColor
- 24 glVertex
- More if we use texture and lighting
13Vertex Arrays
- OpenGL provides a facility called vertex arrays
that allows us to store array data in the
implementation - Six types of arrays supported
- Vertices
- Colors
- Color indices
- Normals
- Texture coordinates
- Edge flags
- We will need only colors and vertices
14Initialization
- Using the same color and vertex data, first we
enable - glEnableClientState(GL_COLOR_ARRAY)
- glEnableClientState(GL_VERTEX_ARRAY)
- Identify location of arrays
- glVertexPointer(3, GL_FLOAT, 0, vertices)
- glColorPointer(3, GL_FLOAT, 0, colors)
data array
data contiguous
3d arrays
stored as floats
15Mapping indices to faces
- Form an array of face indices
- Each successive four indices describe a face of
the cube - Draw through glDrawElements which replaces all
glVertex and glColor calls in the display
callback
GLubyte cubeIndices24 0,3,2,1,2,3,7,6
0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4
16Drawing the cube
number of indices
what to draw
for(i0 ilt6 i) glDrawElements(GL_POLYGON, 4,
GL_UNSIGNED_BYTE, cubeIndices4i)
format of index data
start of index data
glDrawElements(GL_QUADS, 24,
GL_UNSIGNED_BYTE, cubeIndices)
Draws cube with 1 function call!!
17Need to read model data from a file
18Displaying a PPM Image
- PPM is a very simple format
- Each image file consists of a header followed by
all the pixel data - Header
P3 comment 1 comment 2 . comment
n rows columns maxvalue pixels
19Reading the Header
FILE fd int k, nm char c int i char
b100 float s int red, green,
blue printf("enter file name\n") scanf("s",
b) fd fopen(b, "r") fscanf(fd,"\n
",b) if(b0!'P' b1 ! '3') printf("s is
not a PPM file!\n", b) exit(0) printf("s is
a PPM file\n",b)
check for P3 in first line
20Reading the Header (cont)
fscanf(fd, "c",c) while(c '')
fscanf(fd, "\n ", b) printf("s\n",b)
fscanf(fd, "c",c) ungetc(c,fd)
skip over comments by looking for in first
column
21Reading the Data
fscanf(fd, "d d d", n, m, k) printf("d
rows d columns max value d\n",n,m,k) nm
nm imagemalloc(3sizeof(GLuint)nm) s255./k
for(i0iltnmi) fscanf(fd,"d d d",red,
green, blue ) image3nm-3i-3red image3
nm-3i-2green image3nm-3i-1blue
scale factor
22Model Formats
- .3ds, .max (3D Studio Max)
- .lws, .lwo (Lightwave 3D)
- .tri (Alias)
- .obj (Wavefront)
- .x (Direct X)
- .dxf (AutoCad)
- .iob (ION Storm)
- .lp (Lightscape)
- .pov (POVLAB, PovRay)
- .slp (Pro/E)
- .3dmf (Quickdraw)
- .vis (Strata)
- .cob (Truespace)
- .wrl (VRML)
23Model Files
- Most major programs like 3D Studio, Lightwave,
Wavefront, etc. import/export to most other major
formats - Some formats (i.e. Poser) also have special
purpose constructs - Bones
- Joints
- Elasticity
24How do we use these files?
- Neither OpenGL, GLUT, nor Java 3D can read these
files natively - Parsing the files based on specifications
provided by the companies - Use a third-party graphics engine
25Examples
26Compositing and Blending
- Learn to use the A component in RGBA color for
- Blending for translucent surfaces
- Compositing images
- Antialiasing
27Opacity and Transparency
- Opaque surfaces permit no light to pass through
- Transparent surfaces permit all light to pass
- Translucent surfaces pass some light
- translucency 1 opacity (a)
opaque surface a 1
28Physical Models
- Dealing with translucency in a physically correct
manner is difficult due to - the complexity of the internal interactions of
light and matter - Using a pipeline renderer
29Writing Model
- Use A component of RGBA (or RGBa) color to store
opacity - During rendering we can expand our writing model
to use RGBA values
blend
source blending factor
destination component
source component
destination blending factor
Color Buffer
30Blending Equation
- We can define source and destination blending
factors for each RGBA component - s sr, sg, sb, sa
- d dr, dg, db, da
- Suppose that the source and destination colors
are - b br, bg, bb, ba
- c cr, cg, cb, ca
- Blend as
- c br sr cr dr, bg sg cg dg , bb sb cb db ,
ba sa ca da
31OpenGL Blending and Compositing
- Must enable blending and pick source and
destination factors - glEnable(GL_BLEND)
- glBlendFunc(source_factor,
- destination_factor)
- Only certain factors supported
- GL_ZERO, GL_ONE
- GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
- GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA
- See Redbook for complete list
32Example
- Suppose that we start with the opaque background
color (R0,G0,B0,1) - This color becomes the initial destination color
- We now want to blend in a translucent polygon
with color (R1,G1,B1,a1) - Select GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as
the source and destination blending factors - R1 a1 R1 (1- a1) R0,
- Note this formula is correct if polygon is either
opaque or transparent
33Clamping and Accuracy
- All the components (RGBA) are clamped and stay in
the range (0,1) - However, in a typical system, RGBA values are
only stored to 8 bits - Can easily loose accuracy if we add many
components together - Example add together n images
- Divide all color components by n to avoid
clamping - Blend with source factor 1, destination factor
1 - But division by n loses bits
34Order Dependency
- Is this image correct?
- Probably not
- Polygons are rendered
- in the order they pass
- down the pipeline
- Blending functions
- are order dependent
35Opaque and Translucent Polygons
- Suppose that we have a group of polygons some of
which are opaque and some translucent - How do we use hidden-surface removal?
- Opaque polygons block all polygons behind them
and affect the depth buffer - Translucent polygons should not affect depth
buffer - Render with glDepthMask(GL_FALSE) which makes
depth buffer read-only - Sort polygons first to remove order dependency
36Fog
- We can composite with a fixed color and have the
blending factors depend on depth - Simulates a fog effect
- Blend source color Cs and fog color Cf by
- Csf Cs (1-f) Cf
- f is the fog factor
- Exponential
- Gaussian
- Linear (depth cueing)
-
37Fog Functions
38OpenGL Fog Functions
- GLfloat fcolor4
- glEnable(GL_FOG)
- glFogf(GL_FOG_MODE, GL_EXP)
- glFogf(GL_FOG_DENSITY, 0.5)
- glFOgv(GL_FOG, fcolor)
39Line Aliasing
- Ideal raster line is one pixel wide
- All line segments, other than vertical and
horizontal segments, partially cover pixels - Simple algorithms color
- only whole pixels
- Lead to the jaggies
- or aliasing
- Similar issue for polygons
40Antialiasing
- Can try to color a pixel by adding a fraction of
its color to the frame buffer - Fraction depends on percentage of pixel covered
by fragment - Fraction depends on whether there is overlap
no overlap
overlap
41 Area Averaging
- Use average area a1a2-a1a2 as blending factor
42OpenGL Antialiasing
- Can enable separately for points, lines, or
polygons
glEnable(GL_POINT_SMOOTH) glEnable(GL_LINE_SMOOTH
) glEnable(GL_POLYGON_SMOOTH) glEnable(GL_BLEND
) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPH
A)
43Accumulation Buffer
- Compositing and blending are limited by
resolution of the frame buffer - Typically 8 bits per color component
- The accumulation buffer is a high resolution
buffer (16 or more bits per component) that
avoids this problem - Write into it or read from it with a scale factor
- Slower than direct compositing into the frame
buffer
44Applications
- Compositing
- Image Filtering (convolution)
- Whole scene antialiasing
- Motion effects