Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
OpenGL – Part IV<br />
Weiguang Guan<br />
RHPCS, ABB 131-G<br />
Email: <strong>guanw@mcmaster</strong>.<strong>ca</strong><br />
<strong>Phone</strong>: 905-525-9140 x 22540
Programming with OpenGL<br />
1. Introduction<br />
2. Drawing geometric objects<br />
3. Viewing<br />
4. Color<br />
5. Lighting<br />
6. Blending, antialiasing, and fog<br />
7. Display list<br />
8. Drawing pixels, bitmaps, fonts, and<br />
images<br />
9. Texture mapping<br />
10. Evaluators and NURBS<br />
11. Error handling
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Image – (2D) rectangular array of data<br />
■ Bitmap – Binary image where each pixel is<br />
either 1 or 0.<br />
■ One usage of bitmap is creating fonts
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Imaging pipeline (images, textures)<br />
■ Drawing bitmap
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Pixel data storage<br />
glPixelStore*(GLenum<br />
glPixelStore*( GLenum pname, pname,<br />
TYPE param); param);<br />
pname (prefix: GL_PACK or GL_UNPACK):<br />
GL_UNPACK):<br />
ALIGNMENT – 1, 2, 4, 8<br />
ROW_LENGTH<br />
SKIP_ROWS<br />
SKIP_PIXELS<br />
SWAP_BYTES<br />
LSB_FIRST
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Pixel data transfer operations<br />
void glPixelTransfer*(GLenum pname, pname,<br />
TYPE<br />
param); param);<br />
pname: pname<br />
GL_MAP_COLOR, GL_MAP_STENCIL,<br />
GL_INDEX_SHIFT, GL_INDEX_OFFSET,<br />
GL_INDEX_OFFSET<br />
GL_RED_<br />
GL_GREEN_<br />
GL_BLUE_SCALE, GL_BLUE_SCALE,<br />
BIAS<br />
GL_ALPHA_<br />
GL_DEPTH_<br />
GL_HUE GL_HUE_SCALE<br />
_SCALE x HUE + GL_HUE GL_HUE_BIAS<br />
_BIAS
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Pixel data mapping<br />
void glPixelMap*(GLenum map, GLint<br />
mapsize, const TYPE *values);<br />
map: map:<br />
I_TO_I<br />
GL_PIXEL_MAP_ S_TO_S<br />
I_TO_R, G, B, A<br />
GL_PIXEL_MAP_H_TO_<br />
GL_PIXEL_MAP_ _TO_H H (H : R, G, B,<br />
A)
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Pixel data zooming<br />
void glPixelZoom(GLfloat zoomx, zoomx,<br />
GLfloat zoomy); zoomy);<br />
◆ Anisotropic zooming: zoomx ≠ zoomy<br />
◆ Mirroring: zoomx, zoomy < 0
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Bitmaps and Fonts<br />
◆ glRasterPos*(…);<br />
✦ Raster position goes through Model/View<br />
and projection transformations the same<br />
way as vertices.<br />
✦ Bitmap is drawn at the current raster<br />
position.<br />
✦ Raster position is invalid if it is outside the<br />
viewing frustum.<br />
✦ Nothing is drawn if the current raster<br />
position is invalid, even though part of the<br />
bitmap to be drawn might be inside the<br />
viewing frustum.
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
yb<br />
o<br />
◆ glBitmap(GLsizei width, width,<br />
GLsizei height, height,<br />
GLfloat xbo, xbo,<br />
GLfloat ybo, ybo,<br />
GLfloat xbi, xbi,<br />
GLfloat ybi, ybi,<br />
const GLubyte* bitmap); bitmap);<br />
width<br />
xbo xbi<br />
ybi<br />
height
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Code study<br />
◆ drawf.c<br />
✦ Fiddle with (xbo, ybo) and (xbi, ybi)<br />
✦ Change color of the bitmap font<br />
◆ font.c (using display list)<br />
■ Notice<br />
◆ Model/View transformations (s<strong>ca</strong>ling, rotation,<br />
translation) affect the current raster position<br />
only. They neither s<strong>ca</strong>le nor rotate bitmap.<br />
◆ GL_CURRENT_RASTER_COLOR is set to<br />
GL_CURRENT COLOR when glRasterPos(…) is<br />
<strong>ca</strong>lled.
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Reading Images<br />
◆ void glReadPixels(GLint x, , GLint y, ,<br />
GLsizei width, width,<br />
GLsizei height, height,<br />
GLenum<br />
format, format,<br />
GLenum type, type,<br />
GLvoid *pixels * pixels); );<br />
(x,y x,y): ): lower-left corner;<br />
format: format:<br />
GL_RGB, GL_RED, GL_ALPHA,<br />
GL_LUMINANCE,<br />
GL_DEPTH_COMPONENT, etc.<br />
type: type:<br />
GL_UNSIGNED_BYTE, GL_INT,<br />
GL_FLOAT, etc.
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
◆ glReadBuffer(GLenum mode) mode)<br />
if doubledoublebuffering or stereo or both are enabled.<br />
mode: mode:<br />
GL_BACK, GL_FRONT, GL_LEFT,<br />
GL_RIGHT, GL_BACK_LEFT, GL_BACK_RIGHT,<br />
GL_FRONT_LEFT, GL_FRONT_RIGHT.
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Drawing Images<br />
◆ void glDrawPixels(GLsizei width, width,<br />
GLsizei height, height,<br />
GLenum format, format,<br />
GLenum type, type,<br />
const GLvoid *pixels * pixels); );<br />
◆ void glDrawBuffer (GLenum mode); mode);<br />
■ Copying Images<br />
◆ void glCopyPixels(GLint x, , GLint y, ,<br />
GLsizei width, width,<br />
GLsizei height, height,<br />
GLenum<br />
buffer); buffer);
Drawing Pixels, Bitmaps, Fonts, and<br />
Images<br />
■ Code Study<br />
◆ image.c
Programming with OpenGL<br />
1. Introduction<br />
2. Drawing geometric objects<br />
3. Viewing<br />
4. Color<br />
5. Lighting<br />
6. Blending, antialiasing, and fog<br />
7. Display list<br />
8. Drawing pixels, bitmaps, fonts, and<br />
images<br />
9. Texture mapping<br />
10. Evaluators and NURBS<br />
11. Error handling
Texture Mapping<br />
■ What is texture?<br />
Image, texel<br />
■ Dimensionality: 1D, 2D, 3D<br />
■ What is texture mapping?<br />
Gluing texture on line (1D), object<br />
surface (2D), volume (3D)<br />
■ Hardware acceleration<br />
■ Only in RGBA mode
Texture Mapping<br />
■ Mipmap – Level of detail
Texture Mapping<br />
■ Type steps of using texture mapping<br />
◆ Create texture object and specify<br />
texture image<br />
◆ Specify how to modulate colors from<br />
texture and object<br />
◆ Enable texture mapping<br />
◆ Apply texture to object by specifying<br />
texture coordinates at vertices of<br />
object.
Texture Mapping<br />
■ Specifying texture image<br />
void glTexImage2D(GLenum<br />
glTexImage2D(GLenum<br />
target, target,<br />
Glint level, level,<br />
GLint internalFormat,<br />
internalFormat,<br />
GLsizei width, width,<br />
GLsizei height, height,<br />
GLint<br />
border, border,<br />
GLenum format, format,<br />
GLenum type, type,<br />
const GLvoid *pixels * pixels); );<br />
width, width,<br />
height must be power of 2.<br />
Example:<br />
glTexImage2D(GL_TEXTURE_2D, 0,<br />
GL_RGBA, 64, 64, 0, GL_RGBA,<br />
GL_UNSIGNED_BYTE, checkImage);
Texture Mapping<br />
■ Color modulation<br />
glTexEnv*(GLenum target, target,<br />
GLenum<br />
pname, pname,<br />
TYPE param); param);<br />
target: target:<br />
must be GL_TEXTURE_ENV,<br />
pname: pname:<br />
must be<br />
GL_TEXTURE_ENV_MODE.<br />
Example:<br />
glTexEnvf(GL_TEXTURE_ENV,<br />
GL_TEXTURE_ENV_MODE, GL_DECAL);
Texture Mapping<br />
■ Enable texture mapping<br />
glEnable(GL_TEXTURE_2D);<br />
■ Specifying texture coordinates<br />
glTexCoord*(…)<br />
Example:<br />
glTexCoord2f(0.0, 0.6);<br />
■ Other commands<br />
◆ glTexParameter*(…);<br />
◆ glTexSubImage2D(…);<br />
◆ glGenTexutres(…), glBindTexture(…),<br />
etc
Texture Mapping<br />
■ Code study<br />
◆ checker.c<br />
◆ texsub.c<br />
◆ mipmap.c
Programming with OpenGL<br />
1. Introduction<br />
2. Drawing geometric objects<br />
3. Viewing<br />
4. Color<br />
5. Lighting<br />
6. Blending, antialiasing, and fog<br />
7. Display list<br />
8. Drawing pixels, bitmaps, fonts, and<br />
images<br />
9. Texture mapping<br />
10. Evaluators and NURBS<br />
11. Error handling
Evaluators and NURBS<br />
■ Evaluator<br />
◆ Curve – 1D<br />
✦ void glMap1{fd}(GLenum target, target,<br />
TYPE u1, u1,<br />
TYPE u2, u2,<br />
GLint stride, stride,<br />
GLint order, order,<br />
const<br />
TYPE* points); points);<br />
✦ glEvalCoord1*(…)<br />
✦ Example:<br />
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4,<br />
&ctrlpoints[0][0]);<br />
glEvalCoord1f(0.0);<br />
glEvalCoord1f(1.0);
Evaluators and NURBS<br />
◆ Surface – 2D<br />
✦ void glMap2{fd}(GLenum target, target,<br />
TYPE u1, u1,<br />
TYPE u2, u2,<br />
GLint ustride, ustride,<br />
GLint uorder, uorder,<br />
TYPE<br />
v1, v1,<br />
TYPE v2, v2,<br />
GLint vstride, vstride,<br />
GLint vorder, vorder,<br />
const TYPE* points); points);<br />
✦ glEvalCoord2*(…)<br />
✦ Example:<br />
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0,<br />
1, 12, 4, &ctrlpoints[0][0][0]);<br />
glEvalCoord2f(0.0, 0.0);<br />
glEvalCoord2f(0.0, 1.0);<br />
glEvalCoord1f(1.0, 1.0);
Evaluators and NURBS<br />
■ Code study<br />
◆ bezcurve.c<br />
◆ bezsurf.c<br />
◆ bezmesh.c
Evaluators and NURBS<br />
■ NURBS (GLU)<br />
◆ gluNewNurbsRenderer();<br />
◆ gluNurbsProperty(…);<br />
◆ gluBeginSurface(…);<br />
✦ gluNurbsSurface(…);<br />
◆ gluEndSurface(…);<br />
■ Code study<br />
◆ surface.c
Programming with OpenGL<br />
1. Introduction<br />
2. Drawing geometric objects<br />
3. Viewing<br />
4. Color<br />
5. Lighting<br />
6. Blending, antialiasing, and fog<br />
7. Display list<br />
8. Drawing pixels, bitmaps, fonts, and<br />
images<br />
9. Texture mapping<br />
10. Evaluators and NURBS<br />
11. Error handling
Error Handling<br />
■ Commands that <strong>ca</strong>use error are<br />
mostly ignored, and have NO effect<br />
on<br />
◆ OpenGL state<br />
◆ Framebuffer contents<br />
■ An error code is recorded by OpenGL<br />
once it detects an error <strong>ca</strong>used by<br />
GL or GLU commands<br />
■ OpenGL error flag <strong>ca</strong>n record only<br />
one error code at a time.
Error Handling<br />
■ Using glGetError() to query and<br />
reset error flag. It returns<br />
◆ GL_NO_ERROR, if no error detected so<br />
far, or<br />
◆ An error code, such as,<br />
GL_INVALID_VALUE,<br />
GL_STACK_OVERFLOW, etc<br />
■ Using gluErrorString(GLenum<br />
errorCode) errorCode)<br />
to interpret the error<br />
code, it returns a string describing<br />
the error.
Error Handling<br />
■ Typi<strong>ca</strong>l usage<br />
GLenum errCode;<br />
const GLubyte *errString;<br />
if ((errCode = glGetError()) !<br />
=GL_NO_ERROR)<br />
{<br />
}<br />
errString = gluErrorString(errCode);<br />
fprintf (stderr, "OpenGL Error: %s\n",<br />
errString);
Assignment<br />
■ Graphi<strong>ca</strong>l “text editor” (referencing<br />
font.c)<br />
◆ Text that user types is dynami<strong>ca</strong>lly<br />
displayed<br />
◆ Supprt “Backspace” key to erase<br />
characters