Overview
In the previous discussions, we covered sending vertex data - Position and color. In this post we will try to send texture vertex data. It's implemented in TexturedCube class.
It looks as shown at the bottom. It looks elongated because aspect ratio is not applied.
Details
A Texture is 2D image that can be wrapped around a 3D object like a gift wrapper. For example, resources\textures\bricks2.jpg is a 2D Texture file.
Unlike cartesian coordinates, Texture follow UV System as shown below.
Loading Texture
OpenGL supports up to 80 Textures and each with its own identifier. Loading textures is implemented in LoadTexture method in TextureUtil class.
SOIL_load_OGL_texture(filename.c_str(), SOIL_LOAD_AUTO, 0, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y);
MIPMAP
Mipmapping is a technique where a high-resolution texture is downscaled and filtered so that each subsequent mip level is a quarter of the area of the previous level. This means that the texture and all of its generated mips requires no more than 1.5 times the original texture size. An example is shown below.
Inversion of Y Axis
After loading the image needs to be inverted since the V axis of the texture and Y axis of OpenGL have opposite polarity.
Wrapping
As shown below, determines how the texture is wrapped.
Texture Filtering
Texture filtering is a method that is used to improve the texture quality in a scene. Without texture filtering, artifacts like aliasing generally look worse. Texture filtering makes textures look better and less blocky.
Applying Texture
As discussed previously, Texture coordinates are passed during rendering. The following mapping in relation to the UxV axes is used as texture coordinates for the vertices consecutively.
//first triangle { 0.0, 1.0, 0.0 }, { -1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, //second triangle { 1.0, 0.0, 0.0 }, { 0.0, 0.0, -1.0 }, { 0.0, 0.0, 1.0 },
System class diagram
TextureUtil
TextureUtil implements handling textures from image files and bitmaps. It's used by multiple purposes such as rendering 3D objects or text.
Members
| Name | Description |
|---|---|
| textureID | Unique texture handle returned after creating the texture. |
| texunit | Unique texture unit. It's value should be one of the 80 values supported by the system. |
Methods
| Name | Description |
|---|---|
| Cleanup | Releases resources. |
| Init | Assigns unique texture unit to texunit. |
| LoadTextTexture | Used to render text. It generates texture from GDI bitmap. |
| LoadTextTextureImage | Used to render text. It wraps 2D image from GDI bitmap. |
| LoadTexture | Loads texture from an image file. |
| MakeActive | Makes the texture active. |
TexturedCube
TexturedCube is derived from BaseGeometry class. It assembles vertex data generated by mesh in VBO and EBO buffers. Later sent to GPU using shader programs.
| Name | Description |
|---|---|
| filename | File name of the image file containing the texture. |
| texutl | Texture utility to aid rendering texture. |
Methods
| Name | Description |
|---|---|
| Init | This method overrides the base class Init method. It creates the mesh object. Generates the vertices and texture data in non indexed mode and later sets them up in VBO buffers of the GPU. It later loads the texture from the image file. |
| UpdateUniforms | This method overrides the base class UpdateUniforms method. The base class method updates the "transform" matrix uniform by calling GetTransformationMatrix method. It updates the uniform "tex" containing the texture coordinates from to the loaded texture. |
| vertexShaderSource | This method overrides the base class vertexShaderSource method. It returns the vertex shader code to render the cube object. |
| fragmentShaderSource | This method overrides the base class fragmentShaderSource method. It returns the fragment shader code to render the cube object. |
| Cleanup | Releases resources. Calls base class method and the texutl. |




No comments:
Post a Comment