Skip to content

Instantly share code, notes, and snippets.

@cjacobwade
Last active August 29, 2015 14:17
Show Gist options
  • Save cjacobwade/9b122ea511183ae216de to your computer and use it in GitHub Desktop.
Save cjacobwade/9b122ea511183ae216de to your computer and use it in GitHub Desktop.
void imageTexture::LoadTexture()
{
if (tObject == 0) // We don't yet have an OpenGL texture target
{
// This code counts the number of images and if there are none simply
// returns without doing anything
int nImages = 0;
while (tName[nImages][0] != '\0' && nImages < MAX_IMAGES)
nImages++;
if (nImages < 1)
return;
// To Do
//
// Generate a texture object and place the object's value in the "tObject"
// member, then bind the object to the 2D texture target
glGenTextures(nImages, &tObject);
glBindTexture(GL_TEXTURE_2D, tObject);
for (int nImage = 0; nImage < nImages; nImage++)
{
// This code loads the texture using the windows library's "BitmapFile" object
BitmapFile texture;
if (!texture.Read(tName[nImage]))
complain("Couldn't read texture %s", tName);
GLuint srcFormat, targFormat;
// To Do
//
// First decide which format the texture is. If the texture has 4 bytes
// per pixel then it should be an RGBA texture, if it is 3 bytes per pixel
// then it is an RGB image. Notice though that the byte order for the BitmatFile
// object is reversed, so you need to take that into account in the "source" format
if( texture.BytesPerPixel() == 3 )
{
srcFormat = GL_BGR;
targFormat = GL_RGB;
}
else
{
srcFormat = GL_BGRA;
targFormat = GL_RGBA;
}
// Then you need to set the unpack alignment to tell OpenGL about the structure
// of the data in the image and send the data to OpenGL. If there are multiple files
// then we are manually creating a mipmap here and you will use the "level" parameter
// of glTexImage2D to tell OpenGL which mipmap level is being set. The levels are
// set in the same order as they are stored in the image list.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D,
nImage,
targFormat,
texture.Width(),
texture.Height(),
0,
srcFormat,
GL_UNSIGNED_BYTE,
texture.ImageData());
}
// Finally, if there is only one image, you need to tell OpenGL to generate a mipmap
if( nImages == 1)
{
glGenerateMipmap(GL_TEXTURE_2D);
}
}
// Here you need to bind the texture to the 2D texture target and set the texture parameters
// You need to set the wrap mode, the minification and magnification filters.
glBindTexture(GL_TEXTURE_2D, tObject);
glTexParameteri(tObject, GL_TEXTURE_WRAP_S, tHorzWrap); // S -> U -> X
glTexParameteri(tObject, GL_TEXTURE_WRAP_T, tVertWrap); // T -> V -> Y
glTexParameteri(tObject, GL_TEXTURE_MIN_FILTER, tMinFilter);
glTexParameteri(tObject, GL_TEXTURE_MAG_FILTER, tMagFilter);
// To Do
//
// For advanced antialiasing set the number of anisotropic samples
if( tAnisotropy > 1 )
{
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, tAnisotropy);
float aniso=0.0f;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
}
GLERR;
}
void imageTexture::EnableTexture(int tTextureUnit)
{
// To Do
//
// Set the active texture unit to the desired texture unit,
// The LoadTexture call will bind the texture to this texture unit.
glActiveTexture( GL_TEXTURE0 + tTextureUnit );
LoadTexture();
}
void imageTexture::DisableTexture(int tTextureUnit)
{
// To Do
//
// Set the active texture unit and then unbind the texture and disable texturing.
// How do you "unbind" a tetxture?
glActiveTexture( GL_TEXTURE0 + tTextureUnit );
glBindTexture( GL_TEXTURE_2D, 0 );
//glDisable(GL_TEXTURE_2D);
}
void multiTextureShader::BeginShading(void)
{
glslShader::BeginShading();
// To Do
//
// Set the uniform matrix transformation "uvTransform"
glUniformMatrix4fv( glGetUniformLocation( glProgramObj, "uvTransform" ), 1, false, uvTransform.AsArray() );
curScene->RegisterUniformParameters(glProgramObj);
int i = 0;
for (textures.MoveFirst(); !textures.AtEnd(); textures.MoveNext(), i++)
{
texture *tex = textures.GetCurrent();
tex->EnableTexture(i);
tex->RegisterUniformParameters(i, glProgramObj);;
}
}
void texture::RegisterUniformParameters(int tTextureUnit, GLuint glProgramObj)
{
char texName[256];
if (strlen(name) == 0)
sprintf(texName, "texure%d", tTextureUnit);
else
sprintf(texName, "%s", name);
// To Do
//
// Use the texName string built above to set the uniform sampler for this
// texture to the proper texture unit, i.e. tTextureUnit
glUniform1i(glGetUniformLocation( glProgramObj, texName), tTextureUnit );
char transformName[256];
sprintf(transformName, "%sTransform", texName);
// To Do
//
// Use the transformName built above to set the uniform sampler for this
// textures transform to the transform stored in this object's
// textureTransform member.
glUniformMatrix4fv(glGetUniformLocation( glProgramObj, transformName ), 1, GL_FALSE, textureTransform.AsArray());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment