AS

Texture Mapping Part Two

Goals for Texture Mapping

  • Learn to use various types of texture maps in OpenGL.
  • Understand different methods for generating textures.

File-Based Textures

  • Textures are often read from files rather than being computed within the program.
  • Example: Applying a checkerboard texture to a vase object from previous examples.
  • The process involves extracting texture coordinates from an OBJ file.

Extracting Texture Coordinates

  • Texture coordinates are stored after vertex coordinates and normal vectors.
  • Example code:
  int nt = shapes[0].mesh.texcoords.size();  
  texcoords = new GLfloat[nt];  
  for(i=0; i<nt; i++) {  
      texcoords[i] = shapes[0].mesh.texcoords[i];  
  }
  glBufferData(GL_ARRAY_BUFFER, (nv+nn+nt) * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
  glBufferSubData(GL_ARRAY_BUFFER, (nv+nn) * sizeof(GLfloat),  nt * sizeof(GLfloat), texcoords);

Texture Mapping with Aliasing

  • Initial texture coordinates may cause aliasing.
  • Using GL_LINEAR filter can help partly reduce this aliasing effect.
  • Texture example comparison: GL_NEAREST vs. GL_LINEAR.

Applying Mipmaps

  • Mipmaps are used to enhance texture quality.
  • Use OpenGL function glGenerateMipmap for automatic mipmap generation.
  • Code snippet for setting up mipmaps:
  glGenTextures(1, &texName);
  glBindTexture(GL_TEXTURE_2D, texName);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, &checkImage[0][0][0]);
  glGenerateMipmap(GL_TEXTURE_2D);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

Shaders: Vertex and Fragment

Vertex Shader
in vec4 vPosition;
 in vec3 vNormal;
 in vec2 vTexcoord;
 uniform mat4 modelView;
 uniform mat4 projection;
 out vec2 texCoord;
 out vec3 normal;
 void main(void) {
     gl_Position = projection * modelView * vPosition;
     normal = normalMat * vNormal;
     texCoord = vTexcoord;
 }
Fragment Shader
in vec2 texCoord;
 uniform sampler2D tex;
 void main(void) {
     gl_FragColor = texture(tex, texCoord);
 }

Reading JPEG Image Files

  • JPEGs are utilized for texture storage. Use the FreeImage library for manipulation.
  • Install FreeImage via package manager (e.g., sudo apt install libfreeimage-dev).
  • Image representation structure:
  struct textureStruct {
      int height;
      int width;
      int bytes;
      unsigned char *data;
  };

Load Image Procedure

  1. Read image and fill texture structure.
  2. Manage image line storage, indices, and unload after processing.

Multiple Textures

  • Combine textures for a more complex appearance. They might serve different roles in lighting models.
  • For multiple textures, assign each to a texture unit using glActiveTexture.

Environment Mapping

  • Advanced texturing technique using six textures corresponding to a cubic environment.
  • Reflective properties achieved by sampling the environment map using reflection vectors calculated in shaders.
  • Example of an environment map setup and rendering in OpenGL:
  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, texture->width, texture->height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->data[i]);

Texture Mapping Techniques

  • Bump Mapping: Used to create the illusion of surface detail.
  • Displacement Mapping: Alters actual surface positions for enhanced realism.

Summary

  • Explored OpenGL texture mapping techniques, including reading textures from files, applying mipmaps, and using advanced procedures such as environment mapping and multiple texture techniques. More on procedural textures will be discussed later.