OpenGL Vertex Arrays OpenGL vertex arrays store vertex properties such as coordinates, normal...
-
Upload
brett-bennett -
Category
Documents
-
view
244 -
download
0
description
Transcript of OpenGL Vertex Arrays OpenGL vertex arrays store vertex properties such as coordinates, normal...
OpenGL Vertex Arrays OpenGL vertex arrays store vertex
properties such as coordinates, normal vectors, color values and
texture coordinates. These properties can be specified indirectly
using their positions in the arrays, instead of calling
glVertex*(), glNormal*(), glColor*() and glTexCoord*(). Procedures
to use vertex arrays: Enable vertex arrays. Specify data for the
arrays. Dereference the arrays and render primitives. Enable Vertex
Arrays void glEnableClientState(GLenum array)
void glDisableClientState(GLenum array) Disable vertex array array
Meaning GL_VERTEX_ARRAY Array with vertex coordinates
GL_COLOR_ARRAY Array with color values GL_NORMAL_ARRAY Array with
normal vectors GL_TEXTURE_COORD_ARRAY Array with texture
coordinates When using multitexture, glEnableClientState() and
glDisableClientState() only affect current client texture unit
specified by glClientActiveTexture() void
glClientActiveTexture(GLenum texUnit) Select current client texture
unit. texUnit is the texture unit identifier. Specify Data for the
Arrays
void glVertexPointer(GLint size, GLenum type, GLsizei stride, const
GLvoid *pointer) Specify array with vertex coordinates size:Number
of coordinates per vertex. Must be 2, 3 or 4. type:Data type for
vertex coordinates. GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE.
stride:Byte offset between consecutive vertices. If stride = 0, the
vertices are assumed to be tightly packed in the array.
pointer:Pointer to the array that stores vertex coordinates. void
glColorPointer(GLint size, GLenum type, GLsizei stride, const
GLvoid *pointer) void glNormalPointer(GLenum type, GLsizei stride,
const GLvoid *pointer) void glTexCoordPointer(GLint size, GLenum
type, GLsizei stride, const GLvoid *pointer) Command size type
glVertexPointer (Vertex coordinates) 2, 3, 4 GL_SHORT, GL_INT,
GL_FLOAT, GL_DOUBLE glColorPointer (Color values) 3, 4 GL_BYTE,
GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT,
GL_UNSIGNED_INT, glNormalPointer (Normal vectors) 3 GL_BYTE,
GL_SHORT, GL_INT, glTexCoordPointer (Texture coordinates) 1, 2, 3,
4 When using multitexture, glTexCoordPointer() only affects current
client texture unit specified by glClientActiveTexture()
Dereference the Arrays and Render Primitives
De-reference a Single Array Element void glArrayElement(GLint i)
Specify vertex properties for the ith vertex for all enabled
arrays. This command is usually called between glBegin( ) and
glEnd( ). This is equivalent to: if (color array is enabled)
glColor*v(cpointer + i * cstride); if (normal array is enabled)
glNormal*v(npointer + i * nstride); if (texture coordinate array is
enabled) glTexCoord*v(tpointer + i * tstride); if (vertex array is
enabled) glVertex*v(vpointer + i * vstride); De-reference a List of
Array Elements
void glDrawElements(GLenum mode, GLsizei count, GLenum type, GLvoid
*indices) Draw a sequence of primitives using array elements
referenced by an index array. mode:Primitive type. Same as the
argument in glBegin(). count:Number of elements to specify.
type:Data type for index array indices. GL_UNSIGNED_BYTE,
GL_UNSIGNED_SHORT, GL_UNSIGNED_INT. indices:Pointer to the index
array. Equivalent to: glBegin (mode); for (i = 0; i < count;
++i) glArrayElement (indices[i]); glEnd(); De-reference a Sequence
of Array Elements
void glDrawArrays(GLenum mode, GLint first, GLsizei count) Draw a
sequence of primitives using array elements starting from first and
ending at first + count 1. mode:Primitive type. Same as the
argument in glBegin(). first:Starting element index. count:Number
of elements to specify. Equivalent to: glBegin (mode); for (i = 0;
i < count; ++i) glArrayElement (first + i); glEnd(); Example y 8
9 10 11 4 5 6 7 x 1 2 3 Vertex Index Coordinates
1 2 3 Vertex Index Coordinates Texture Coordinates (0.0, 0.0, 0.0)
(0.0, 0.0) 1 (5.0, 0.0, 0.0) (1.0, 0.0) 2 (10.0, 0.0, 0.0) (2.0,
0.0) 3 (15.0, 0.0, 0.0) (3.0, 0.0) 4 (0.0, 4.0, 0.0) (0.0, 0.5) 5
(5.0, 4.0, 0.0) (1.0, 0.5) Vertex Index Coordinates Texture
Coordinates 6 (10.0, 4.0, 0.0) (2.0, 0.5) 7 (15.0, 4.0, 0.0) (3.0,
0.5) 8 (0.0, 8.0, 0.0) (0.0, 1.0) 9 (5.0, 8.0, 0.0) (1.0, 1.0) 10
(10.0, 8.0, 0.0) (2.0, 1.0) 11 (15.0, 8.0, 0.0) (3.0, 1.0) struct
VERTEX { float x[3]; // position float t[2];// texture coordinates
}; static VERTEX v[12] = { {0.0,0.0, 0.0, 0.0, 0.0}, {5.0,0.0, 0.0,
1.0, 0.0}, {10.0, 0.0, 0.0, 2.0, 0.0}, {15.0, 0.0, 0.0, 3.0, 0.0},
{0.0,4.0, 0.0, 0.0, 0.5}, {5.0,4.0, 0.0, 1.0, 0.5}, {10.0, 4.0,
0.0, 2.0, 0.5}, {15.0, 4.0, 0.0, 3.0, 0.5}, {0.0,8.0, 0.0, 0.0,
1.0}, {5.0,8.0, 0.0, 1.0, 1.0}, {10.0, 8.0, 0.0, 2.0, 1.0}, {15.0,
8.0, 0.0, 3.0, 1.0}}; glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3,
GL_FLOAT, 5*sizeof(VERTEX), v[0].x); glTexCoordPointer(2, GL_FLOAT,
5*sizeof(VERTEX), v[0].t); static unsigned int vindex[24]={
0,1,5,4, 1,2,6,5, 2,3,7,6, 4,5,9,8, 5,6,10, 9, 6,7,11, 10};
glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, tex_obj);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, vindex); Environment
Mapping Environment mapping is used to render reflective surface.
It assumes that the environment is much larger than the object and
far away from the object. Viewer N R V Environment Map Object
Surface NUnit Surface Normal VUnit Direction Vector to Viewer RUnit
Reflection Vector Environment Mapping Procedure
1. Generate Environment Map Place a camera at the center of the
object and take a panorama snapshot of the surrounding environment.
Store the image as a texture. This texture is called Environment
Map. 2. Obtain Reflection Vector 3. Generate Texture Coordinates
for Sampling in the Environment Map Using Reflection Vector R
Sphere Environment Mapping
Sphere Environment Mapped Object The surrounding environment is
projected onto an enclosing sphere
The surrounding environment is projected onto an enclosing sphere.
The image on the sphere surface is then mapped into a circular area
on a 2D texture. Unit Reflection Vector x R W z Texture
Coordinates: Cube Environment Mapped Object
Cube Environment Mapping Cube Environment Map Cube Environment
Mapped Object The surrounding environment is projected onto an
enclosing cube.
The images on the six sides of the cube are then stored as six 2D
textures. y z x x z y x y z z x z x x z x z y y y y z y x w = max
(|Rx|, |Ry|, |Rz|);
Cube Map Face Local x Local y x z y x z y x z R w = max (|Rx|,
|Ry|, |Rz|); if (w == |Rx| and Rx > 0) {Choose +x face; s = Rz /
w; t = Ry / w;} if (w == |Rx| and Rx < 0) {Choose x face; s = Rz
/ w; t = Ry / w;} if (w == |Ry| and Ry > 0) {Choose +y face; s =
Rx / w; t = Rz / w;} if (w == |Ry| and Ry < 0) {Choose y face; s
= Rx / w; t = Rz / w;} if (w == |Rz| and Rz > 0) {Choose +z
face; s = Rx / w; t = Ry / w;} if (w == |Rz| and Rz < 0) {Choose
z face; s = Rx / w; t = Ry / w;} s = 0.5(s + 1); t = 0.5(t + 1);
Light Map Light map: Texture that stores the lighting intensities
of a surface. Scene with base textures Light Maps = Textured scene
modulated by light maps For static scene under static lighting,
light maps can be pre-computed. Light map was first introduced in
Quake engine.
Without Light Map With Light Map Light Map Bump Mapping + Texture
Mapped Object Height Map = Bump Mapped Object Normal texture
mapping creates a flat looking surface.
Light Source Viewer Bump mapping applies a perturbation to surface
normal and uses the perturbed normal for lighting calculation. Thus
creates an illusion of surface bumpiness. Light Source Viewer
Create Bump Map from Height Map
Height Map h(x, y): A 2D texture that stores the height
perturbation of a surface. Bump Map Nb(x, y) :A 2D texture that
stores perturbed normal vectors of a surface. Calculate perturbed
normal: Use finite difference Normalization: Change component range
from [1, 1] to [0, 1]: Store nx, ny, nz in R, G, B components of
the bump map. To retrieve perturbed normal, do the inverse:
Transform Vectors from Object Space to Texture Space
Bump map stores perturbed surface normals in texture space. Thus
transformation from object space to texture space is needed. Obtain
base vectors for texture space N NUnit normal vector TUnit tangent
vector BUnit binormal vector B T For curved surface with parametric
equation P = P(u, v)
Example: Sphere z N B T P r y x For a point P with texture
coordinates (s, t) on the triangle surface
For triangle surface with texture coordinates specified for each
vertex N3 B3 Vertex Texture Coordinates P1 P2 P3 (s1, t1) (s2, t2)
(s3, t3) P3 T3 N1 B1 N2 P T1 P1 B2 P2 T2 For a point P with texture
coordinates (s, t) on the triangle surface Replace P with P2 and
P3: Solve the above equations to obtain T1 and B1:
T1 and B1 are then normalized to unit vectors T2 , B2 , T3 , B3 can
be obtained similarly. Transform vectors from object space to local
texture space For direction to light vector L For direction to
viewer vector V Lighting Calculation Use Nb, L' and V' for lighting
calculation. Displacement Mapping Bump Mapping only perturbs
surface in N direction and only uses perturbed normal for lighting
calculations. Displacement Mapping perturbs surface in N, T, B
directions. It not only uses perturbed normal for lighting
calculations, but also displaces surface position for real.
Parallax Occlusion Mapping Water Surface Animation and
Rendering
Plane Wave Equation Surface PointP = (x, y, z) AAmplitude
ffrequency LxWave length in x direction LyWave length in y
direction Surface Normal Reflection Rendering Transparent Effect z
xy plane
Camera N N: Surface normal V R Water Surface xy plane P Direction
to camera vector Reflection vector Use R to sample environment map
to obtain reflection color Transparent Effect Use color
blending