SER332
Introduction to Graphics and
Game Development
Lecture 21:
Textures
Javier Gonzalez-Sanchez
javiergs@asu.edu
PERALTA 230U
Office Hours: By appointment
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 2
From this
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 3
To this
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 4
Texture Mapping | Create
1. Create a texture object
2. Bind the texture object with a texture target, such
as GL_TEXTURE_2D
3. Create or load a texture image
4. Specify texture mapping parameters, like wrapping
mode and filtering mode
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 5
Texture Mapping | Use
5. For each vertex specify texture coordinates (u, v)
6. Enable texture mapping
7. Bind the texture object with a texture target, such
as GL_TEXTURE_2D
8. Draw the Scene
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 6
1. Create a Texture Object
glGenTextures (size, name);
• Create an array of texture names
• Similar to display lists
• Note: Create texture names one by one (size=1).
// example
GLuint texture_name1;
GLuint texture_name2;
// size 1 à create an array with 1 texture names
glGenTextures(1, &texturename1);
glGenTextures(1, &texturename2);
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 7
2. Bind a Texture Object
glBindTexture (GL_TEXTURE_2D, texture_name1);
• When object is first bound, the texture object is
created with default values.
• Subsequent texture commands (e.g. glTexImage,
glTexParameter, etc.) affect the actively bound
texture
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 8
3. Load a Texture (Algorithmic)
// Create from algorithm
void codedTexture(UINT textureArray[], int n) {
const int TexHeight = 128; const int TexWidth = 128;
// create
GLubyte textureImage[TexHeight][TexWidth][4];
for (int i = 0; i < TexHeight; i++)
for (int j = 0; j < TexWidth; j++) {
textureImage[i][j][0] = 127+i; // red value from 0 to 255
textureImage[i][j][1] = 0; // green value from 0 to 255
textureImage[i][j][2] = 127+j; // blue value from 0 to 255
textureImage[i][j][3] = 1; // alpha value from 0 to 255
}
// setup texture
glGenTextures(1, &textureArray[n]);
glBindTexture(GL_TEXTURE_2D, textureArray[n]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // must set to 1 for compact data
// glTexImage2D Whith size and minification
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, TexWidth, TexHeight,
GL_BGR_EXT, GL_UNSIGNED_BYTE, textureImage);
}
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 9
3. Load a Texture (BMP file)
// load BMP file
void bmpTexture (UINT textureArray[], LPSTR file, int n) {
BITMAPINFO *bitmapInfo; // Bitmap information
GLubyte *bitmapBits; // Bitmap data
if (!file) {
cout << "texture file not found!" << endl; return;
}
// read file
bitmapBits = LoadDIBitmap(file, &bitmapInfo);
// setup texture
glGenTextures(1, &textureArray[n]);
glBindTexture(GL_TEXTURE_2D, textureArray[n]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // must set to 1 for compact data
// glTexImage2D Whith size and minification
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, // width, height,
GL_BGR_EXT, GL_UNSIGNED_BYTE, butmapsBits);
}
Javier Gonzalez-Sanchez | SER431 | Fall 2016 | 10
// Load a DIB/BMP file from disk.
GLubyte *LoadDIBitmap(const char *filename, BITMAPINFO **info) {
FILE *fp; // open file pointer
GLubyte * bits; // bitmap pixel bits
int bitsize; // Size of bitmap
int infosize; // Size of header information
BITMAPFILEHEADER header; // File header
// open the file; use "rb" mode to read this *binary* file.
// read the file header and any following bitmap information.
// allocate memory for the bitmap and read *it* in.
// OK, everything went fine - return the allocated bitmap.
}
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 11
4. Parameters
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
• read Red Book chapter 8 Pixel Packing and
Unpacking.
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 12
4. Parameters
glTexParameter*(GL_TEXTURE_2D, parameter)
• GL_TEXTURE_WRAP_S
• GL_TEXTURE_WRAP_T
• GL_TEXTURE_MAG_FILTER
• GL_TEXTURE_MIN_FILTER
• GL_TEXTURE_BASE_LEVEL
• GL_TEXTURE_MAX_LEVEL
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 13
4. Wrapping & Filtering Parameters
• Wrapping: GL_TEXTURE_WRAP_*(S,T,R)
o GL_CLAMP
o GL_CLAMP_TO_EDGE
o GL_REPEAT
• Filtering: GL_TEXTURE_MIN(MAG)_FILTER
o GL_NEAREST (MIN/MAG)
o GL_LINEAR (MIN/MAG)
o GL_NEAREST_MIPMAP_NEAREST (MIN)
o GL_NEAREST_MIPMAP_LINEAR (MIN)
o GL_LINEAR_MIPMAP_NEAREST (MIN)
o GL_LINEAR_MIPMAP_LINEAR (MIN)
• Note: If no mipmap present, you shouldn’t set
GL_XX_MIPMAP_XX parameter.
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 14
4. Mipmaps
• pre-calculated, optimized sequences of images, each of which is a
progressively lower resolution representation of the same image. The
height and width of each image, or level, in the mipmap is a power
of two smaller than the previous level.
• Mipmaps can be created manually (much work) with:
glTexImage*D()
The level parameter controls uploading to the corresponding level of
the mipmap
• Mipmaps can be created automatically:
gluBuild2DMipMapLevels()
gluBuild2DMipMaps()
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 15
5. Texture coordinates
Draw triangles and for each vertex specify:
• geometry (x, y, z)
• normals (nx, ny, nz)
• texture coordinates (u, v) //glTexCoord{2|3}{i|f}
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 16
Example | A cube
// create a triangulated cube
Mesh* createCube() {
Mesh *mesh = new Mesh;
// Define 8 Vertexes
mesh->dot_vertex.push_back(Vec3<GLfloat>( 0.0, 32.0, 32.0));
mesh->dot_vertex.push_back(Vec3<GLfloat>( 32.0, 32.0, 32.0));
mesh->dot_vertex.push_back(Vec3<GLfloat>( 32.0, 0.0, 32.0));
mesh->dot_vertex.push_back(Vec3<GLfloat>( 0.0, 0.0, 32.0));
// ..
// Define 6 Faces; 2 triangles each
mesh->face_index_vertex.push_back(0);
mesh->face_index_vertex.push_back(2);
mesh->face_index_vertex.push_back(1);
mesh->face_index_vertex.push_back(0);
mesh->face_index_vertex.push_back(3);
mesh->face_index_vertex.push_back(2);
// ..
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 17
Remember: Front and Back
Javier Gonzalez-Sanchez | SER431 | Fall 2016 | 18
// Define texture vertex
mesh->dot_texture.push_back(Vec2<GLfloat>(0.0, 1.0));
mesh->dot_texture.push_back(Vec2<GLfloat>(1.0, 1.0));
mesh->dot_texture.push_back(Vec2<GLfloat>(1.0, 0.0));
mesh->dot_texture.push_back(Vec2<GLfloat>(0.0, 0.0));
// Define texture faces
for (int t = 0; t<6; t++) {
mesh->face_index_texture.push_back(0);//0
mesh->face_index_texture.push_back(2);//1
mesh->face_index_texture.push_back(1);//2
mesh->face_index_texture.push_back(0);//0
mesh->face_index_texture.push_back(3);//2
mesh->face_index_texture.push_back(2);//3
}
return mesh;
}
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 19
Example
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 20
Example
// global
GLuint textureArray[8];
// init
void init() {
// ..
// TEXTURES
bmpTexture (textureArray, "../bmp files/oldbox.bmp", 0);
bmpTexture (textureArray, "../bmp files/brick.bmp", 1);
codedTexture(textureArray, 2);
// DISPLAY LIST
displayDiamond = meshToDisplayList(mesh1, 1);
displayPlane = meshToDisplayList(mesh2, 2);
displayMesh = meshToDisplayList(mesh3, 3);
// ..
}
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 21
6. Enable Texture Mapping
• glEnable( XXX );
• glDisable( XXX );
• typically: glEnable(GL_TEXTURE_2D)
• Other options:
o GL_TEXTURE_1D
o GL_TEXTURE_3D
o GL_TEXTURE_CUBE_MAP
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 22
6. Enable Texture Mapping
glTexEnvf(target, pname, param);
• Specify Texturing Function
• e.g. target = GL_TEXTURE_ENV
• e.g. pname = GL_TEXTURE_ENV_MODE
• e.g. param = GL_DECAL, GL_REPLACE,
GL_MODULATE, or GL_BLEND
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 23
7. Bind a Texture Object
glBindTexture (GL_TEXTURE_2D, texture_name1);
• When object is first bound, the texture object is
created with default values.
• Subsequent texture commands (e.g. glTexImage,
glTexParameter, etc.) affect the actively bound
texture
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 24
8. Draw the Scene
// draw
GLuint meshToDisplayList(Mesh* m, int id) {
GLuint listID = glGenLists(id);
glNewList(listID, GL_COMPILE);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, textureArray[id - 1]);
glBegin(GL_TRIANGLES);
for (unsigned int i = 0; i < m->face_index_vertex.size(); i++) {
// PER VERTEX NORMALS - glNormal3fv( …
// TEXTURES - glTexCoord2fv( …
// VERTEX - glVertex3fv( …
}
glEnd();
glDisable(GL_TEXTURE_2D);
glEndList();
return listID;
}
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 25
Test Yourself
• Use the example on Blackboard
https://github.com/javiergs/SER332/blob/master/L21/
• Create a cabin model as follows: 1 window, 1 door, 1 boxy body, 1
triangular roof, floor.
• Draw textures as BMP files: window, walls, door, roof, green floor.
• Apply textures
Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 26
Reading
• To get a general picture of texture mapping:
o Red Book (pdf): ch9 p247 ex9-1
• Pixel storage format
o Red Book ch8 Pixel Packing and Unpacking section
• Required reading for texture mapping:
o Red Book (pdf): ch9 p250-p274
o Hearn Baker: ch10 p628-p633, p648-p658
o Reading tip: mainly focus on the instructions discussed in the
slides. You are also encouraged to read all other instructions.
SER332 Introduction to Graphics
Javier Gonzalez-Sanchez
javiergs@asu.edu
Spring 2017
Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.

More Related Content

PPTX
Opengl texturing
PDF
CS253: Minimum spanning Trees (2019)
PDF
Grid help, Processing
PDF
Topic 1 adding & subtracting polynomials
KEY
Module 9 Topic 1 - Adding & Subtracting polynomials
PDF
gennder trinomials
PPT
4.1 implicit differentiation
PPTX
Polynomial operations
Opengl texturing
CS253: Minimum spanning Trees (2019)
Grid help, Processing
Topic 1 adding & subtracting polynomials
Module 9 Topic 1 - Adding & Subtracting polynomials
gennder trinomials
4.1 implicit differentiation
Polynomial operations

What's hot (6)

PPT
10 1 Adding Subtracting Polynomials
PPTX
7 2 adding and subtracting polynomials
PDF
Α Γυμνασίου §7.8 - §7.9 Λύσεις
PPTX
Unification and Refactoring of Clones
PDF
BazzucchiCampolmi
PPTX
Dual formulation example
10 1 Adding Subtracting Polynomials
7 2 adding and subtracting polynomials
Α Γυμνασίου §7.8 - §7.9 Λύσεις
Unification and Refactoring of Clones
BazzucchiCampolmi
Dual formulation example
Ad

Similar to 201707 SER332 Lecture 21 (20)

PPTX
Introduction to open gl in android droidcon - slides
PDF
201707 SER332 Lecture 15
PPT
OpenGL Texture Mapping
PPTX
Trident International Graphics Workshop 2014 1/5
PDF
OpenGL L05-Texturing
PDF
The fallowing program shows the simple transformation #define GLEW.pdf
PPT
CS 354 Texture Mapping
PDF
201707 SER332 Lecture18
PDF
201707 SER332 Lecture 05
PPTX
Render to Texture with Three.js
PDF
201707 SER332 Lecture 24
PDF
Texture Mapping
PDF
3Dtexture_doc_rep
PDF
2017 RM-URISA Track: Spatial SQL - The Best Kept Secret in the Geospatial World
PPTX
Bs webgl소모임004
PPTX
Approaching zero driver overhead
PDF
Making Games in WebGL - Aro Wierzbowski & Tomasz Szepczyński
PPTX
COLLADA & WebGL
PDF
OpenGL L07-Skybox and Terrian
Introduction to open gl in android droidcon - slides
201707 SER332 Lecture 15
OpenGL Texture Mapping
Trident International Graphics Workshop 2014 1/5
OpenGL L05-Texturing
The fallowing program shows the simple transformation #define GLEW.pdf
CS 354 Texture Mapping
201707 SER332 Lecture18
201707 SER332 Lecture 05
Render to Texture with Three.js
201707 SER332 Lecture 24
Texture Mapping
3Dtexture_doc_rep
2017 RM-URISA Track: Spatial SQL - The Best Kept Secret in the Geospatial World
Bs webgl소모임004
Approaching zero driver overhead
Making Games in WebGL - Aro Wierzbowski & Tomasz Szepczyński
COLLADA & WebGL
OpenGL L07-Skybox and Terrian
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11

Recently uploaded (20)

PPTX
The various Industrial Revolutions .pptx
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Modernising the Digital Integration Hub
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
Benefits of Physical activity for teenagers.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Getting started with AI Agents and Multi-Agent Systems
PPT
Module 1.ppt Iot fundamentals and Architecture
PPT
Geologic Time for studying geology for geologist
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
The various Industrial Revolutions .pptx
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Getting Started with Data Integration: FME Form 101
Modernising the Digital Integration Hub
observCloud-Native Containerability and monitoring.pptx
NewMind AI Weekly Chronicles – August ’25 Week III
A contest of sentiment analysis: k-nearest neighbor versus neural network
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
A review of recent deep learning applications in wood surface defect identifi...
Univ-Connecticut-ChatGPT-Presentaion.pdf
Benefits of Physical activity for teenagers.pptx
Group 1 Presentation -Planning and Decision Making .pptx
A novel scalable deep ensemble learning framework for big data classification...
Getting started with AI Agents and Multi-Agent Systems
Module 1.ppt Iot fundamentals and Architecture
Geologic Time for studying geology for geologist
Zenith AI: Advanced Artificial Intelligence
Final SEM Unit 1 for mit wpu at pune .pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...

201707 SER332 Lecture 21

  • 1. SER332 Introduction to Graphics and Game Development Lecture 21: Textures Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 2 From this
  • 3. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 3 To this
  • 4. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 4 Texture Mapping | Create 1. Create a texture object 2. Bind the texture object with a texture target, such as GL_TEXTURE_2D 3. Create or load a texture image 4. Specify texture mapping parameters, like wrapping mode and filtering mode
  • 5. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 5 Texture Mapping | Use 5. For each vertex specify texture coordinates (u, v) 6. Enable texture mapping 7. Bind the texture object with a texture target, such as GL_TEXTURE_2D 8. Draw the Scene
  • 6. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 6 1. Create a Texture Object glGenTextures (size, name); • Create an array of texture names • Similar to display lists • Note: Create texture names one by one (size=1). // example GLuint texture_name1; GLuint texture_name2; // size 1 à create an array with 1 texture names glGenTextures(1, &texturename1); glGenTextures(1, &texturename2);
  • 7. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 7 2. Bind a Texture Object glBindTexture (GL_TEXTURE_2D, texture_name1); • When object is first bound, the texture object is created with default values. • Subsequent texture commands (e.g. glTexImage, glTexParameter, etc.) affect the actively bound texture
  • 8. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 8 3. Load a Texture (Algorithmic) // Create from algorithm void codedTexture(UINT textureArray[], int n) { const int TexHeight = 128; const int TexWidth = 128; // create GLubyte textureImage[TexHeight][TexWidth][4]; for (int i = 0; i < TexHeight; i++) for (int j = 0; j < TexWidth; j++) { textureImage[i][j][0] = 127+i; // red value from 0 to 255 textureImage[i][j][1] = 0; // green value from 0 to 255 textureImage[i][j][2] = 127+j; // blue value from 0 to 255 textureImage[i][j][3] = 1; // alpha value from 0 to 255 } // setup texture glGenTextures(1, &textureArray[n]); glBindTexture(GL_TEXTURE_2D, textureArray[n]); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // must set to 1 for compact data // glTexImage2D Whith size and minification gluBuild2DMipmaps (GL_TEXTURE_2D, 3, TexWidth, TexHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, textureImage); }
  • 9. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 9 3. Load a Texture (BMP file) // load BMP file void bmpTexture (UINT textureArray[], LPSTR file, int n) { BITMAPINFO *bitmapInfo; // Bitmap information GLubyte *bitmapBits; // Bitmap data if (!file) { cout << "texture file not found!" << endl; return; } // read file bitmapBits = LoadDIBitmap(file, &bitmapInfo); // setup texture glGenTextures(1, &textureArray[n]); glBindTexture(GL_TEXTURE_2D, textureArray[n]); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // must set to 1 for compact data // glTexImage2D Whith size and minification gluBuild2DMipmaps (GL_TEXTURE_2D, 3, // width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, butmapsBits); }
  • 10. Javier Gonzalez-Sanchez | SER431 | Fall 2016 | 10 // Load a DIB/BMP file from disk. GLubyte *LoadDIBitmap(const char *filename, BITMAPINFO **info) { FILE *fp; // open file pointer GLubyte * bits; // bitmap pixel bits int bitsize; // Size of bitmap int infosize; // Size of header information BITMAPFILEHEADER header; // File header // open the file; use "rb" mode to read this *binary* file. // read the file header and any following bitmap information. // allocate memory for the bitmap and read *it* in. // OK, everything went fine - return the allocated bitmap. }
  • 11. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 11 4. Parameters glPixelStorei (GL_UNPACK_ALIGNMENT, 1); • read Red Book chapter 8 Pixel Packing and Unpacking.
  • 12. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 12 4. Parameters glTexParameter*(GL_TEXTURE_2D, parameter) • GL_TEXTURE_WRAP_S • GL_TEXTURE_WRAP_T • GL_TEXTURE_MAG_FILTER • GL_TEXTURE_MIN_FILTER • GL_TEXTURE_BASE_LEVEL • GL_TEXTURE_MAX_LEVEL
  • 13. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 13 4. Wrapping & Filtering Parameters • Wrapping: GL_TEXTURE_WRAP_*(S,T,R) o GL_CLAMP o GL_CLAMP_TO_EDGE o GL_REPEAT • Filtering: GL_TEXTURE_MIN(MAG)_FILTER o GL_NEAREST (MIN/MAG) o GL_LINEAR (MIN/MAG) o GL_NEAREST_MIPMAP_NEAREST (MIN) o GL_NEAREST_MIPMAP_LINEAR (MIN) o GL_LINEAR_MIPMAP_NEAREST (MIN) o GL_LINEAR_MIPMAP_LINEAR (MIN) • Note: If no mipmap present, you shouldn’t set GL_XX_MIPMAP_XX parameter.
  • 14. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 14 4. Mipmaps • pre-calculated, optimized sequences of images, each of which is a progressively lower resolution representation of the same image. The height and width of each image, or level, in the mipmap is a power of two smaller than the previous level. • Mipmaps can be created manually (much work) with: glTexImage*D() The level parameter controls uploading to the corresponding level of the mipmap • Mipmaps can be created automatically: gluBuild2DMipMapLevels() gluBuild2DMipMaps()
  • 15. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 15 5. Texture coordinates Draw triangles and for each vertex specify: • geometry (x, y, z) • normals (nx, ny, nz) • texture coordinates (u, v) //glTexCoord{2|3}{i|f}
  • 16. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 16 Example | A cube // create a triangulated cube Mesh* createCube() { Mesh *mesh = new Mesh; // Define 8 Vertexes mesh->dot_vertex.push_back(Vec3<GLfloat>( 0.0, 32.0, 32.0)); mesh->dot_vertex.push_back(Vec3<GLfloat>( 32.0, 32.0, 32.0)); mesh->dot_vertex.push_back(Vec3<GLfloat>( 32.0, 0.0, 32.0)); mesh->dot_vertex.push_back(Vec3<GLfloat>( 0.0, 0.0, 32.0)); // .. // Define 6 Faces; 2 triangles each mesh->face_index_vertex.push_back(0); mesh->face_index_vertex.push_back(2); mesh->face_index_vertex.push_back(1); mesh->face_index_vertex.push_back(0); mesh->face_index_vertex.push_back(3); mesh->face_index_vertex.push_back(2); // ..
  • 17. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 17 Remember: Front and Back
  • 18. Javier Gonzalez-Sanchez | SER431 | Fall 2016 | 18 // Define texture vertex mesh->dot_texture.push_back(Vec2<GLfloat>(0.0, 1.0)); mesh->dot_texture.push_back(Vec2<GLfloat>(1.0, 1.0)); mesh->dot_texture.push_back(Vec2<GLfloat>(1.0, 0.0)); mesh->dot_texture.push_back(Vec2<GLfloat>(0.0, 0.0)); // Define texture faces for (int t = 0; t<6; t++) { mesh->face_index_texture.push_back(0);//0 mesh->face_index_texture.push_back(2);//1 mesh->face_index_texture.push_back(1);//2 mesh->face_index_texture.push_back(0);//0 mesh->face_index_texture.push_back(3);//2 mesh->face_index_texture.push_back(2);//3 } return mesh; }
  • 19. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 19 Example
  • 20. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 20 Example // global GLuint textureArray[8]; // init void init() { // .. // TEXTURES bmpTexture (textureArray, "../bmp files/oldbox.bmp", 0); bmpTexture (textureArray, "../bmp files/brick.bmp", 1); codedTexture(textureArray, 2); // DISPLAY LIST displayDiamond = meshToDisplayList(mesh1, 1); displayPlane = meshToDisplayList(mesh2, 2); displayMesh = meshToDisplayList(mesh3, 3); // .. }
  • 21. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 21 6. Enable Texture Mapping • glEnable( XXX ); • glDisable( XXX ); • typically: glEnable(GL_TEXTURE_2D) • Other options: o GL_TEXTURE_1D o GL_TEXTURE_3D o GL_TEXTURE_CUBE_MAP
  • 22. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 22 6. Enable Texture Mapping glTexEnvf(target, pname, param); • Specify Texturing Function • e.g. target = GL_TEXTURE_ENV • e.g. pname = GL_TEXTURE_ENV_MODE • e.g. param = GL_DECAL, GL_REPLACE, GL_MODULATE, or GL_BLEND
  • 23. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 23 7. Bind a Texture Object glBindTexture (GL_TEXTURE_2D, texture_name1); • When object is first bound, the texture object is created with default values. • Subsequent texture commands (e.g. glTexImage, glTexParameter, etc.) affect the actively bound texture
  • 24. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 24 8. Draw the Scene // draw GLuint meshToDisplayList(Mesh* m, int id) { GLuint listID = glGenLists(id); glNewList(listID, GL_COMPILE); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_2D, textureArray[id - 1]); glBegin(GL_TRIANGLES); for (unsigned int i = 0; i < m->face_index_vertex.size(); i++) { // PER VERTEX NORMALS - glNormal3fv( … // TEXTURES - glTexCoord2fv( … // VERTEX - glVertex3fv( … } glEnd(); glDisable(GL_TEXTURE_2D); glEndList(); return listID; }
  • 25. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 25 Test Yourself • Use the example on Blackboard https://github.com/javiergs/SER332/blob/master/L21/ • Create a cabin model as follows: 1 window, 1 door, 1 boxy body, 1 triangular roof, floor. • Draw textures as BMP files: window, walls, door, roof, green floor. • Apply textures
  • 26. Javier Gonzalez-Sanchez | SER332 | Spring 2017 | 26 Reading • To get a general picture of texture mapping: o Red Book (pdf): ch9 p247 ex9-1 • Pixel storage format o Red Book ch8 Pixel Packing and Unpacking section • Required reading for texture mapping: o Red Book (pdf): ch9 p250-p274 o Hearn Baker: ch10 p628-p633, p648-p658 o Reading tip: mainly focus on the instructions discussed in the slides. You are also encouraged to read all other instructions.
  • 27. SER332 Introduction to Graphics Javier Gonzalez-Sanchez javiergs@asu.edu Spring 2017 Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.