Introduction to opengl part 3
1 / 94

Introduction to OpenGL (Part 3) - PowerPoint PPT Presentation

  • Uploaded on

Introduction to OpenGL (Part 3). Ref: OpenGL Programming Guide (The Red Book). Part 1 Introduction Geometry Viewing Light & Material. Part 2 Display List Alpha Channel Polygon Offset Part 3 Image Texture Mapping Part 4 FrameBuffers Selection & Feedback. Topics. OpenGL.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Introduction to OpenGL (Part 3)' - libby-reynolds

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introduction to opengl part 3

Introduction to OpenGL(Part 3)

Ref: OpenGL Programming Guide (The Red Book)


Part 1




Light & Material

Part 2

Display List

Alpha Channel

Polygon Offset

Part 3


Texture Mapping

Part 4


Selection & Feedback


Fall 2009 revised



Pixels, Bitmaps, and Images


  • A bitmap is a rectangular array of 0s and 1s that serves as a drawing mask for a rectangular portion of the window

  • Specification:

    • Specified, in hex codes, in one dimensional array, row by row, starting from lower-left corner

    • Width need not be multiples of 8

Fall 2009 revised


glBitmap (w, h, xbo, ybo, xbi, ybi, bitmapPtr)

xbo, ybo: origin

xbi, ybi: increment CRP after display

glBitmap(10, 12, 0, 0, 11, 0, bitmapPtr)


Fonts: series of bitmaps as display list

Fall 2009 revised

Bitmap cont
Bitmap (cont)

  • Note: You can't rotate bitmap fonts because the bitmap is always drawn aligned to the x and y framebuffer axes.

  • Use glColor* to set GL_CURRENT_RASTER_COLOR

Fall 2009 revised

Current raster position crp
Current Raster Position (CRP)

  • OpenGL maintains raster position, a 3D-position in world coordinate

  • Modified by:

    • glRasterPos()

      • specify the object coordinates (as in glVertex); transformed by MODELVIEW & PROJECTION, passed to the clipping stage.

      • If the position is not culled, raster position is updated; otherwise, it is not valid.

    • glBitmap()

  • glGetFloatv(GL_CURRENT_RASTER_POSITION, fptr) to obtain the CRP

  • glGetBooleanv (GL_CURRENT_RASTER_POSITION_VALID, &boolvar) to test validity of CRP

Fall 2009 revised

Introduction to opengl part 3


Fall 2009 revised

Bitmap editor
Bitmap Editor

  • This format is called xwindow bitmap, with xbm extension

  • Bitmaps can be created by the GIMP (GNU Image Manipulation Program)

  • Or, seek format converters

Fall 2009 revised

Another xbm editor here

A simple Tk Program; require Tcl/Tk installed

Get tcltk for Windows from ActiveTcl

Note that xbm and the opengl xbitmap format is slightly different (details)

Another XBM Editor (Here)

Fall 2009 revised

Ex xbm edit display
Ex: XBM Edit & Display

Fall 2009 revised

Apis for images pixel rectangles


Reading pixel data from framebuffer to processor memory.


Writing pixel data from processor memory to framebuffer


Copying pixel data within the framebuffer

APIs for Images (Pixel Rectangles)

Fall 2009 revised

Function arguments
Function Arguments

  • glReadPixels (x, y, w, h, F, T, ptr)

    • x,y: window coordinate

    • F: pixel format

    • T: is data type

    • ptr: pointer to image memory

  • glDrawPixels (w, h, F, T, ptr)

    • Draw to current raster position

  • glCopyPixels (x, y, w, h, buffer)


    • Equivalent to: Read then Draw

Fall 2009 revised

More on glcopypixels
More on glCopyPixels

  • Note that there's no need for a format or data parameter for glCopyPixels(), since the data is never copied into processor memory.

  • The read source buffer and the destination buffer of glCopyPixels() are specified by glReadBuffer() and glDrawBuffer() respectively

    • Default:

      • single-buffer: GL_FRONT

      • Double-buffered: GL_BACK

Fall 2009 revised

Pixel format
Pixel Format


GREY SCALE with alpha

Fall 2009 revised

Data type
Data Type

Fall 2009 revised


255: 0xFF

See also image.c for CopyPixels

Fall 2009 revised

Example image c
Example image.c

  • Copy the lower left corner to the CRP (where the mouse is)

  • For single-buffer version, only GL_FRONT is involved

  • While motion is in action, display is not called

  • Double-buffer version: [from the API doc] glutSwapBuffers promotes the contents of the back buffer to become the contents of the front buffer. The contents of the back buffer then become undefined. Reality is… two have same content

Fall 2009 revised

Pixelzoom xfactor yfactor
PixelZoom (xfactor,yfactor)

  • Enlarge/shrink images

  • Use negative factors for reflected images

Fall 2009 revised

Image loader tga
Image Loader (TGA)

  • simple TGA utility in gluit.rar

  • only load and save uncompressed images in greyscale, RGB or RGBA mode.

  • Info in TGA header:

    • image type [unsigned char]

      • 1 - colour map image

      • 2 - RGB(A) uncompressed

      • 3 - greyscale uncompressed

      • 9 - greyscale RLE (compressed)

      • 10 - RGB(A) RLE (compressed)

    • pixel depth [unsigned char]

      • 8 – greyscale | 24 – RGB | 32 - RGBA

Fall 2009 revised

Image loader png

Offline; Local copy at webhd2:game-lib

Image Loader (PNG)

Fall 2009 revised


Fall 2009 revised

Int pngloadraw filename rawinfo
int pngLoadRaw (filename,rawinfo)

Must be freed manually

Fall 2009 revised


  • Most graphics formats have image origin at upper/left corner

  • While OpenGL has the image origin at lower/left corner

  • Hence, if no correction is done, every image is drawn upside down.

  • Correction in PNG loader: pngSetStandardOrientation (1);

Fall 2009 revised

Imaging pipeline
Imaging Pipeline

Fall 2009 revised

Imaging pipeline cont
Imaging Pipeline(cont.)

  • glPixelStore()

    • Controlling Pixel-Storage Modes

    • glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

      • Packing and unpacking refer to the way that pixel data is written to and read from processor memory.

      • tells OpenGL not to skip bytes at the end of a row

Fall 2009 revised

Pixelstorei gl unpack alignment x

  • Specifies the alignment requirements for the start of each pixel row in memory. The allowable values are:

    • 1 (byte-alignment),

    • 2 (rows aligned to even-numbered bytes),

    • 4 (word-alignment [default]), and

    • 8 (rows start on double-word boundaries).




Fall 2009 revised


Assuming the RGB image is of size 3x2:

(three bytes per pixel)



In client memory, the start of each row of pixels is …

Byte aligned (1)

2nd row

1st row

Aligned to even bytes (2); Word aligned (4)

Double word aligned (8)

Fall 2009 revised


For RGBA images, it doesn’t matter (each pixel has 4 bytes: RGBARGBA…)

For RGB and luminance images and images with odd width, it should be set to byte-aligned (data are densely packed)




Fall 2009 revised

Improving pixel pipeline performance
Improving Pixel Pipeline Performance

  • A series of fragment operations is applied to pixels as they are drawn into the framebuffer. For optimum performance, disable all fragment operations (depth test, …)

  • While performing pixel operations, disable other costly states, such as texturing and lighting

  • It is usually faster to draw a large pixel rectangle than to draw several small ones, since the cost of transferring the pixel data can be amortized (分攤) over many pixels

Fall 2009 revised

Example depthbuffershow

Get depth buffer content


Reverse and scale!

Near (white) far (black)

Display it as a luminance image


Example (depthbuffershow)

Fall 2009 revised

Example rasterpos3

Illustrate that CRP is a point in R3

Image displayed is always parallel to projection plane

Fake perspective by zooming with distance to camera

Example (rasterpos3)

Fall 2009 revised

Example sprite

Process sprite image

Add alpha layer

Load png image

Sprite animation

“feel of depth”

Back-to-front rendering


Example (sprite)

Fall 2009 revised



Texture Mapping (Introduction)

Steps in texture mapping
Steps in Texture Mapping

  • Specify (create) the texture

  • Set texture parameters:

    • Indicate how the texture is to be applied to each pixel

  • Enable texture mapping

  • Draw the scene, supplying both texture and geometric coordinates

  • Works only in RGB mode

Fall 2009 revised

Specify the texture
Specify the Texture

  • glTexImage1D()

  • glTexImage2D()

  • glTexImage3D()

Fall 2009 revised

Two dimensional texture
Two Dimensional Texture

  • glTexImage2D (GLenum target, GLint level, GLint components, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)

    • target:GL_TEXTURE_2D

    • level:LOD number (0: base image)

    • components:number of color components (1|2|3|4)

    • Format:pixel data format

    • Border: 0 or 1

    • Width & height

      • 2m + 2(border bit)

      • w and h can be different

  • There are new extensions that removes this restriction

Fall 2009 revised

Glteximage d supplement
glTexImage*D supplement

  • In GL version 1.1 or greater, pixels may be a null pointer.

  • In this case texture memory is allocated to accommodate a texture of width width and height height. You can then download subtextures to initialize this texture memory.

  • The image is undefined if the user tries to apply an uninitialized portion of the texture image to a primitive.

Fall 2009 revised

Setting texture environment
Setting Texture Environment

  • glTexEnv{if}{v}(GLenum target, GLenum pname, TYPEparam);

  • Setting how textures are to be interpreted:

    • Target:GL_TEXTURE_ENV



Fall 2009 revised

Texture environment modes
Texture Environment Modes


  • GL_MODULATE (default)



    New environment modes:

  • GL_ADD: Cv = Cf + Ct

  • GL_COMBINE (ARB, see here)

Fall 2009 revised

Gl modulate

Color of polygon affects the display of texture


Tree: (r,g,b,a) acutout = 0

Polygon: (1,0,0)

Fall 2009 revised

Gl blend

Use with:





Cc: texture environment color

Fall 2009 revised

Gl replace

Appearance solely determined by texture


Tree: (r,g,b,a) acutout = 0

Polygon: (1,0,0)


Fall 2009 revised

Gl decal

Cp: replace

Tree: (r,g,b,a) acutout = 0

Polygon: (1,0,0)


Fall 2009 revised

Texture lighting
Texture + Lighting

  • To show fragment color, use GL_MODULATE

  • Apply specular color AFTER texture mapping:


    • See FAQ 21.040



Fall 2009 revised

Texture coordinates
Texture Coordinates

  • Texture coordinate:

    • Associate texture location (in texture space) with vertices in the polygon

  • glTexCoord2i (s, t); glVertex2i (x, y);

Order cannot be reversed!

[Think of TexCoord as state assignment]

Fall 2009 revised

Texture coordinates of quadrics

Most quadric primitives have default setting for texture coordinates

To turn on the default setting:

gluQuadricTexture (qobj, GL_TRUE)

Texture Coordinates of Quadrics

Fall 2009 revised

Introduction to opengl part 3

(1,1) coordinates




Polygon (in screen space) and texture coordinates




Texture map (4x4)

Texture Access and Lookup




Interpolate (s,t) and lookup (r,g,b,a) in the texture map

Fall 2009 revised

Magnification minification

1 coordinates






Magnification & Minification

Nature of problem:

  • Mismatch between texels and pixels

Fall 2009 revised


Magnification coordinates











Chooses the mipmap that most closely matches the size of the pixel being textured

Nearest: in Manhattan distance to the center of the pixel

Chooses the two mipmaps that most closely match the size of the pixel being textured

Fall 2009 revised

Magnification options

GL_LINEAR coordinates

Interpolate the texels

More time consuming but more accurate


Snap to the nearest texel

Magnification Options

Fall 2009 revised

Problem with minification
Problem with Minification coordinates

without mipmap

with mipmap

Fall 2009 revised


Level 3 coordinates

If a pixel covers


go to level 2

and get a texel.

d = the Level Of Detail (LOD)

Level 2


level 1


Level 1


m, n∈+

Level 0

Level 0 Texture

Mipmapping was invented by Lance Williams in 1983 and is described in his paper Pyramidal parametrics.


The most popular method of anti-aliasing for textures

‘Mip’ stands for “multum in parvo” = “many things in a small place”

The texture is downsampled to a quarter of the original area.

Fall 2009 revised

Minification options
Minification Options coordinates






Mipmap selection is done per pixel

Using not-yet-ready mipmap disables the texture mapping function.

Fall 2009 revised

Mipmap generation

gluBuild2DMipmaps coordinates

Storage overhead

Mipmap Generation

Fall 2009 revised

Texture wrap
Texture Wrap coordinates

  • When the texture coordinates are outside [0,1]

  • glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,param);

  • glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,param);


Fall 2009 revised

Parameters for wrapping

GL_CLAMP coordinates

use the border texel

Diagonal: corner texel


repeat the texels in [0,1]

Parameters for Wrapping

Fall 2009 revised

Texture object
Texture Object coordinates

  • A texture object stores texture data and makes it readily available

  • You can now control many textures and go back to textures that have been previously loaded into your texture resources

  • Using texture objects is usually the fastest way to apply textures, resulting in big performance gains

    • it is almost always much faster to bind (reuse) an existing texture object than it is to reload a texture image using glTexImage*D()

Fall 2009 revised

Texture object api
Texture Object API coordinates

  • glGenTextures

    • Allocate n textures (integer identifiers)

  • glBindTexture

    • Bind the current texture to specified identifier

  • glDeleteTextures

    • Free the allocated texture identifiers

    • Reverse of glGenTextures

Fall 2009 revised

Introduction to opengl part 3

checker.c coordinates

Fall 2009 revised

Introduction to opengl part 3

texbind.c coordinates

  • Once created, a named texture may be re-bound to the target of the matching dimensionality as often as needed. It is usually much faster to use glBindTexture to bind an existing named texture to one of the texture targets than it is to reload the texture image using glTexImage1D, glTexImage2D, or glTexImage3D.

Fall 2009 revised

Texture objects
Texture Objects coordinates

  • Texture properties saved in texture objects:

    • Minification and magnification filters, wrapping modes, border color and texture priority

  • When a texture object is bound again, one may edit the contents of the bound texture object. Any commands that change the above properties will change the currently bound texture as well as the current texture state.

  • Texture environment, texgen, etc. are NOT stored in texture objects

Fall 2009 revised

Texture loader png glpng rar
Texture Loader (PNG, glpng.rar) coordinates

  • Features

    • Build mipmap

    • Load and bind 2D textures

    • Load image data (image loader)

    • Handle transparent image (cut-out texture)

  • Important API

    • id = pngBind(filename, mipmap, trans, info, wrapst, minfilter, magfilter)

    • pngSetStencil(red, green, blue)

    • pngSetStandardOrientation(1)

    • pngLoad(filename, mipmap, trans, info)


Fall 2009 revised

Texture transforms
Texture Transforms coordinates

  • Texture coordinates are multiplied by a 4 by 4 matrix before any texture mapping occurs.

  • Texture animation: using this, you can make the texture slide over the surface, rotate around it, stretch and shrink, …

  • All matrix operations apply: Push/Pop/Mult/…

Fall 2009 revised

Texture suite

water coordinates





Texture Suite

Fall 2009 revised

Automatic tex ture coordinate gen eration
Automatic coordinatesTEXture Coordinate GENeration

  • void glTexGen{ifd}{v}(GLenum coord, GLenum pname, TYPEparam);

    • Coord: GL_S, GL_T, GL_R, GL_Q



  • Used when the tex coords are too cumbersome to specify, or will be changed with time.

  • Texture environment, texgen, etc. are NOT stored in texture objects.

Fall 2009 revised

1d texture according to height
1D Texture According to Height coordinates

Continuous color gradation

(0,1/32,2/32, …, 31/32)

Discrete white lines


Fall 2009 revised

Texgen object eye linear

for a vertex with object coordinates (x coordinateso,yo,zo,wo), generated coordinate = p1xo + p2yo + p3zo + p4wo

Meaning: when p1, p2, p3 are normalized, this coord corresponds to signed distance

Similarly, EYE_LINEAR applies to eye coordinates

TexGen: Object/Eye_Linear


Initially, all texture generation functions are set to GL_EYE_LINEAR and are disabled. Both s plane equations are (1, 0, 0, 0), both t plane equations are (0, 1, 0, 0), and all r and q plane equations are (0, 0, 0, 0).

Fall 2009 revised

Eye linear ref
Eye_linear ( coordinatesref)

  • Computing this distance in eye coordinates is a little tricky, since the plane and vertex must first be transformed to eye coordinates.

  • if M is the modelview matrix at the time glTexGenfv( GLX, GL EYE PLANE, plane ) is called, then the transformed vertex V’ is V’ = MV, let’s call V’ = (xe, ye, ze, we). The plane must also be transformed to get a new plane A’x + B’y + C’z + D’w = 0 We get (A’,B’,C’,D’) = (A,B,C,D)M−1 and M is the modelview matrix when glTexGen is invoked. The texture coordinate is then computed as A’xe+ B’ye+ C’ze+ D’we.

Fall 2009 revised

Eye linear summary
Eye_linear Summary coordinates

Fall 2009 revised

Multi texture
Multi-Texture coordinates

  • What this is about…

  • Extension Wrangler (GLEW)

Fall 2009 revised

Usages of multitexture
Usages of Multitexture coordinates

Fall 2009 revised

Arb multitexture ref

can cascade up to 32 TE coordinates’s

Note: default blending mode is GL_MODULATE

ARB_multitexture (ref)

  • (First) ARB-approved extension (1998)

  • Support up to 32 texture units*

  • Texture matrix and texgen affects the active texture environment

* glGetIntegerv (GL_MAX_TEXTURE_UNITS, &units);

My platform has only 4! OpenGL 3.1 requires minimum of 16 texture units

Fall 2009 revised

Details coordinates

  • OpenGL's multitexture support requires that every texture unit be fully functional and maintain state that is independent of any other texture units.

  • Each texture unit has its own texture coordinate generation state, texture matrix state, texture enable state, and texture environment state.

  • However, each texture unit within an OpenGL context shares the same set of texture objects.

Fall 2009 revised

Multi texture1
Multi Texture coordinates

Fall 2009 revised

Texgen with multitexture
TexGen with Multitexture coordinates

When the GL_ARB_multitexture extension is supported, glTexGen set the texture generation parameters for the currently active texture unit, selected with glActiveTexture.


texture2D;(s,t) given


texture1D; texGen

Fall 2009 revised

End of part 3

End of Part 3 coordinates

Results from xbm editor

From top to bottom coordinates

Byte swapping

#define sample_width 16

#define sample_height 10

static char sample_bits[] = {











Results from Xbm Editor

Fall 2009 revised

Converting to opengl bitmap
Converting to OpenGL Bitmap coordinates

  • Bottom to top & byte swapping

#define sample_width 16

#define sample_height 10

static char sample_bits[] = {











Fall 2009 revised

Solution coordinates

  • Byte swap: resolved by

    • glPixelStorei (GL_UNPACK_LSB_FIRST, 1);

  • Bottom to top:

    • Recall width of bitmap need not be multiples of 8, but it is stored in units of unsigned characters

    • But, the XBM Editor only allows width of multiples of 8 (8,16,24,…)

    • See the example code on reading it reversely

Fall 2009 revised

Almost useless api

There seems to no way to make the bitmap larger (than it should be, pixels)

glPixelZoom won’t work!

Therefore, its use is limited

Nevertheless, the color is free to change…

(almost) Useless API!


Fall 2009 revised

Texture mapping
Texture Mapping should be, pixels)

  • A way to render realistic picture w/o using too many polygons

Parallax mapping

Fall 2009 revised

Texture coordinate system
Texture Coordinate System should be, pixels)

  • Texture: a 2D array of color values, each unit is called texel (texture element)

  • Every texture map has (s,t) coordinates [0,0] to [1,1]

  • Each vertex in a polygon specifies its texture coordinates (s,t), then map to the given image to determine the corresponding texture

Fall 2009 revised


Texture space view should be, pixels)

OpenGL code

Texture Map

Screen space view


Fall 2009 revised


Screen space view should be, pixels)

Texture space view

OpenGL code

Texture Map



Fall 2009 revised

Introduction to opengl part 3

Fall 2009 revised should be, pixels)

Introduction to opengl part 3

Fall 2009 revised should be, pixels)

Introduction to opengl part 3

Fall 2009 revised should be, pixels)

Introduction to opengl part 3

BACK should be, pixels)

Fall 2009 revised

Opengl extension wikipedia
OpenGL Extension (Wikipedia) should be, pixels)

  • The OpenGL standard allows individual vendors to provide additional functionality through extensions as new technology is created. Extensions may introduce new functions and new constants, and may relax or remove restrictions on existing OpenGL functions.

  • NV: Each vendor has an alphabetic abbreviation that is used in naming their new functions and constants. For example, NVIDIA's abbreviation (NV) is used in defining their proprietary function glCombinerParameterfvNV() and their constant GL_NORMAL_MAP_NV.

  • EXT: It may happen that more than one vendor agrees to implement the same extended functionality. In that case, the abbreviation EXT is used.

  • ARB: It may further happen that the Architecture Review Board "blesses" the extension. It then becomes known as a standard extension, and the abbreviation ARB is used.

Fall 2009 revised

Introduction should be, pixels)

  • The second ARB extension was GL_ARB_multitexture, introduced in version 1.2.1. Following the official extension promotion path, multitexturing is no longer an optionally implemented ARB extension, but has been a part of the OpenGL core API since version 1.3.

  • Before using an extension a program must first determine its availability, and then obtain pointers to any new functions the extension defines. The mechanism for doing this is platform-specific and libraries such as GLEW and GLEE exist to simplify the process.

  • Specifications for nearly all extensions can be found at the official extension registry

Fall 2009 revised

Introduction should be, pixels)

  • What extension wrangler does:

    • Your application may find some extensions already available through Microsoft's opengl32.lib. However, depending on your OpenGL device and device driver, a particular vendor-specific extension may or may not be present at link time.

    • If it's not present in opengl32.lib, you'll need to obtain the address of the extension's entry points at run time from the device's ICD.

    • After you obtain the entry point addresses of the extension functions you wish to use, simply call through them as normal function pointers:

  • FAQ:

    • How to know which version of OpenGL you are running?

    • What’s the difference among versions?

    • How to know what extensions are supported?

  • Ref:

    • All About OpenGL Extensions

Fall 2009 revised


GLEW: OpenGL extension wrangler should be, pixels)

Glview: OpenGL extension viewer



Fall 2009 revised

Commonly used extensions
Commonly Used Extensions should be, pixels)

  • GL_ARB_multitexture

  • GL_ARB_texture_cube_map

  • GL_ARB_texture_env_dot3

  • GL_ARB_shadow

  • GL_ARB_vertex_program, GL_ARB_fragment_program

  • GL_ARB_occlusion _query

  • GL_ARB_texture_non_power_of_two

  • GL_EXT_texture3D

  • GL_EXT_framebuffer_object

ARB – Extensions officially approved by the OpenGL Architecture Review Board

EXT – Extensions agreed upon by multiple OpenGL vendors

NV – NVIDIA Corporation

SGI – Silicon Graphics

SGIX – Silicon Graphics (experimental)


Fall 2009 revised