Game Development Reference
In-Depth Information
Listing 4-48. Texture Class Constructor
public Texture(Context context, int ResourceId)
{
// Create new Texture resource from ResourceId
m_Context = context;
InitTexture(ResourceId);
// Setup Default Texture Parameters
SetTextureWRAP_MIN_FILTER(GLES20.GL_NEAREST);
SetTextureWRAP_MAG_FILTER(GLES20.GL_LINEAR);
SetTextureWRAP_S(GLES20.GL_CLAMP_TO_EDGE);
SetTextureWRAP_T(GLES20.GL_CLAMP_TO_EDGE);
}
The
InitTexture()
function loads in the texture and initializes it to be a 2D texture object
(see Listing 4-49).
Listing 4-49. Initializing the Texture
boolean InitTexture(int ResourceId)
{
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
m_TextureId = textures[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, m_TextureId);
// Loads in Texture from Resource File
LoadTexture(ResourceId);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, m_Bitmap, 0);
return true;
}
The function initializes the texture by
Calling
glGenTextures()
to get an unused texture name from OpenGL
1.
Calling nexgt
glBindTexture()
to create a new texture object of 2 dimensions
that has length and width
2.
Calling next
LoadTexture()
to read in the texture from a resource file and
store it in our bitmap variable
m_Bitmap
3.
Calling finally the
GLUtils.texImage2D()
function to define the 2D texture as
that data in our
m_Bitmap
variable that holds our texture data that we loaded
in from the
LoadTexture()
function
4.
The
LoadTexture()
function does the actual work of loading in the texture image from a file. The resource
image file is opened for reading and is attached to an
InputStream. BitmapFactory.decodeStream()
is
then used to read in the file and convert the data into bitmap form. See Listing 4-50.