ecse-4750 computer graphics fall 2004 prof. michael wozny [email protected] ta. abhishek gattani...
TRANSCRIPT
ECSE-4750 Computer Graphics Fall 2004
Prof. Michael [email protected]
TA. Abhishek [email protected]
We have discussed
Polygons – square, cube, etc. Quadric surfaces – disk, cylinder, sphere
etc. Bezier Curves, Bezier Surfaces, B-splines,
Nurbs surfaces etc.
Based on these basic elements, we can build objects with very complex shape -> render.
Why we need textures?
Thousands of small polygons to give the appearance of a real object.
Expensive to compute, render and manage Object too smooth and regular to look real Texture mapping allows real images to be
applied to objects and is less expensive computationally
Texture Mapping to add realism
Steps in Texture Mapping
Create and specify a texture object. Specify texture application method Enable texture mapping mode Draw (map texture co-ordinates to object
points)!
Texture Dimensions
2D (common) 1D = 2D with Height =1
– e.g. texture vary only in one direction
3D = Layers of 2D textures– e.g. MRI/CT data
Ref: Rosalee Wolfe’s book
Texture Data
RGBA Depth Luminance Intensity
Texel: one texture element
Texture Application Modes (texture functions)
Replace Modulate/Scale Blend
Loading texture data
jpeg, png, tiff, gif ?? Idea: convert to BMP and load.Note: Handle all errors when you write I/O code.
#include <GL/glaux.h> //for reading bitmap image
Function prototype– AUX_RGBImageRec *LoadBMP(char
*Filename) // Loads A Bitmap Image– Read about AUX_RGBImageRec in more detail.
Loading texture data (contd..)
Create a file handle– FILE *File=NULL; // File Handle
Try to open the file– File=fopen(Filename,"r"); // Check To See If The File Exists
Close the file and load bitmapif (File) // Does The File Exist?
{ fclose(File); // Close The Handle
// Load The Bitmap And Return A Pointer
return auxDIBImageLoad(Filename);}
Using the texture data
Gluint texture[1]; //Storage for one texture Create Storage Space For The Texture
– AUX_RGBImageRec *TextureImage[1];
Load the texture– TextureImage[0]=LoadBMP("Data/NeHe.bmp")
Naming a texture object– glGenTextures(1, &texture[0]);– Non-zero unsigned integer may be used as a texture names
Using the texture data (contd..)
Bind the named texture to texture target– glBindTexture(GL_TEXTURE_2D, texture[0]);
Defined a 2-D Texture– glTexImage2D(GL_TEXTURE_2D, 0, 3,
TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexImage2D
void glTexImage2D( GLenum target, //The target texture. Must be GL_TEXTURE_2D.
GLint level,//The level-of-detail number.
GLint internalformat,//The number of color components in the texture
GLsizei width,//Must be 2n + 2(border) for some integer n. GLsizei height,//Must be 2m + 2(border) for some integer m. GLint border,//The width of the border. Must be either 0 or 1. GLenum format,//The format of the pixel data.
GLenum type, //GL_UNSIGNED_BYTE, GL_BYTE, GL_BITMAP
const GLvoid *pixels //pointer to the image data in memory
);
Texture Parameters
void glTexParameterf(GLenum target, GLenum pname, GLfloat param );
– glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
– glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
– For GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T you can specify GL_CLAMP or GL_REPEAT.
Free Up Space
Image Dataif (TextureImage[0])// If Texture Exists
{if (TextureImage[0]->data)// If Texture Image Exists{
// Free The Texture Image Memoryfree(TextureImage[0]->data);
}free(TextureImage[0]);// Free The Image Structure
} Texture Data
– void glDeleteTextures( GLsizei n, const GLuint *textures);– Deletes named textures
Changes to Init
int InitGL(GLvoid) {
if (!LoadGLTextures())// Jump To Texture Loading Routine ( NEW ){
return FALSE;// If Texture Didn't Load Return FALSE ( NEW )}
glEnable(GL_TEXTURE_2D);// Enable Texture Mapping ( NEW )glShadeModel(GL_SMOOTH);// Enable Smooth ShadingglClearColor(0.0f, 0.0f, 0.0f, 0.5f);// Black BackgroundglClearDepth(1.0f);// Depth Buffer SetupglEnable(GL_DEPTH_TEST);// Enables Depth TestingglDepthFunc(GL_LEQUAL);// The Type Of Depth Testing To DoglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculationsreturn TRUE;// Initialization Went OK
} Mode prescedence: Gl_TEXTURE_ID < GL_TEXTURE_2D <
GL_TEXTURE_3D < GL_TEXTURE_CUBE_MAP.
Texture Compression
Compress While Loading– GL_COMPRESSED_* in internalformat of
glTexImage*D
Load a Compressed Texture Image– glCompressedTextImage*D– Compression format should be supported by
graphics card.
Texture Borders
Texture repetition has problems at borders esp. with linear filtering.
Each texel has eight neighbors except the borders. So where do edge pixels get their neighbors? From the texture border!
Simple approach: Copy the values of the adjacent texture into the border.
Texture rendering
Texture mapping is image resampling Some common problems
Mipmaps
Why do we need them? – Shimmering, flashing, scintillations when texture
is scaled to fit smaller objects in dynamic scenes.
Mipmaps: Pre-filtered texture maps at different resolutions
OpenGL figures out which map to use when No Free Lunch! Increased storage and
computation
Why we need Mipmaps?
Without With
Mipmaps
Texture averaging/interpolation
Nearest (Linear)– texel nearest to the pixel under consideration
Linear (Bilinear)– weighted average of the 4 nearest texels to the
pixel under consideration
Trade off between image quality and speed
Mipmaps (Contd..)
Specifying maps– Manually: glTextImage2D called each time for
each map with different parameters– Automatically: build using gluBuild*DMipmaps()
gluBuild2DMipmaps(GL_TEXTURE_2D,3,p->sizeX,p->sizeY,GL_RGB,GL_UNSIGNED_BYTE, p->data)