Download presentation
Presentation is loading. Please wait.
Published byQuentin Hampton Modified over 9 years ago
1
Computer Graphics Ben-Gurion University of the Negev Fall 2012
2
Until now, colors and shades determined the vertices color Texture is the characteristic physical structure given to an object by the size, shape, arrangement of its parts By texture mapping we apply image data to a geometric primitive 2
3
Three steps to apply a texture 1. Specify the texture read or generate image assign to texture enable texturing 2. Specify texture parameters wrapping, filtering 3. Assign texture coordinates to vertices Proper mapping function is left to application 3
4
4
5
Images and geometry flow through separate pipelines that join at the rasterizer - “complex” textures do not affect geometric complexity 5
6
Define a texture image from an array of texels (texture elements) in CPU memory Glubyte my_texels[512][512][3]; Enable texture mapping glEnable(GL_TEXTURE_2D) OpenGL supports 1-3 dimensional texture maps 6
7
Define a handle to different available texture states (image and params) glGenTextures(1, &texture[texture_num]) First argument tells OpenGL how many texture objects we would create Second argument is a pointer to the place where OpenGL will store the names (unsigned integers) of the texture objects Texture[ ] is of type GLuint 7
8
To specify the text object that we are going to define its specifics glBindTexture(GL_TEXTURE_2D, texture[texture_num]) 8
9
glTexImage2D( target, level, internalFormat, w, h, border, format, type, texels ) target: type of texture, e.g. GL_TEXTURE_2D level: used for mipmapping (discussed later) internalFormat: elements per texel w, h: width and height of texture image border: used for smoothing (discussed later) format and type: describe texels texels: pointer to texel array glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels); 9
10
OpenGL requires texture dimensions to be powers of 2 If dimensions of image are not powers of 2 gluScaleImage( format, w_in, h_in,type_in, *data_in, w_out, h_out,type_out, *data_out ); data_in is source image data_out is for destination image Image interpolated and filtered during scaling 10
11
Once a texture is uploaded to the graphics card, its resolution has no importance Texture is mapped to “texture coordinates” between 0.0 to 1.0 (s,t) glTexCoord*() specified at each vertex 11 s t 1, 1 0, 1 0, 01, 0 (s, t) = (0.2, 0.8) (0.4, 0.2) (0.8, 0.4) A BC a b c Texture Space Object Space
12
12
13
OpenGL a variety of parameter that determine how texture is applied Wrapping parameters determine what happens if s and t are outside the (0,1) range Filter modes allow us to use area averaging instead of point samples Mipmapping allows us to use textures at multiple resolutions Environment parameters determine how texture mapping interacts with shading 13
14
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) 14
15
15 Modes determined by glTexParameteri( target, type, mode ) glTexParameteri(GL_TEXTURE_2D, GL_TEXURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXURE_MIN_FILTER, GL_LINEAR);
16
16 A group of texels can cover a pixel (minification) or One texel can cover a group of pixels (magnification) Can use point sampling (nearest texel) or linear filtering ( 2 x 2 filter) to obtain texture values TexturePolygon MagnificationMinification PolygonTexture
17
17
18
18
19
Texturing far/close objects Mipmapping allows for pre-filtered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Declare mipmap level during texture definition glTexImage2D( GL_TEXTURE_*D, level, … ) GLU mipmap builder routines will build all the textures from a given image gluBuild*DMipmaps( … ) 19
20
20 N*N N/4*N/4 N/2*N/2 What about memory ?
21
Controls how texture is applied glTexEnv{fi}( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, param) GL_TEXTURE_ENV_MODE modes GL_MODULATE: modulates with computed shade GL_BLEND: blends with an environmental color GL_REPLACE: use only texture color GL_DECAL: alpha value is considered 21
22
void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); makeCheckImages(); glGenTextures(2, texName); glBindTexture(GL_TEXTURE_2D, texName[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage); 22
23
glBindTexture(GL_TEXTURE_2D, texName[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, otherImage); glEnable(GL_TEXTURE_2D); } 23
24
void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, texName[0]); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0); glEnd(); glBindTexture(GL_TEXTURE_2D, texName[1]); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421); glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421); glEnd(); glFlush(); } 24
25
OFF files are standard format to represent 3D model Starts with number of points, then number of polygons ( usually triangles ) List of points List of polygons – Easy to write OFF file reader and draw these models. 25
26
26
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.