Sign in to follow this  
raphaelvanier@videotron.ca

Using Alpha component in OpenGL texturing

Recommended Posts

Hi there,

I am currently following the OpenGL Nehe tutorial:

[url="http://nehe.gamedev.net/lesson.asp?index=02"]http://nehe.gamedev....on.asp?index=02[/url]

I am at the point where he put texture on his cube. He is using a BMP image. That's all dandy and well, but I really plan to use lossless format with alpha channels like .TGA or .PNG. I have been trying to tweek his code to first add an alpha component to his BMP texture loading. Here is a class I did:

[code]
#pragma once

#ifndef OPENFL_DECL
#define OPENFL_DECL

#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input/Output
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library

#endif //OPENFL_DECL

#include <string>
#include <bitset>
using namespace std;

class Texture
{
private:
GLuint TextureHandle[1]; // Storage For One Texture
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

int SizeX, SizeY;
unsigned char * imageData;
public:
Texture(void);
~Texture(void);

void DeleteAndFree();

bool LoadTexture(char *Filename); // Load texture

// for BMPs
AUX_RGBImageRec *LoadFromBMP(char *Filename); // Loads A Bitmap Image
void TransferAndConvertBMP();

bool PowerOf2Test();

void Bind();
};
[/code]

[code]
#include "Texture.h"

Texture::Texture(void)
{
}

Texture::~Texture(void)
{
DeleteAndFree();
}

void Texture::DeleteAndFree()
{
if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}

free(TextureImage[0]); // Free The Image Structure
}

if(imageData)
{
free(imageData);
}
}

bool Texture::PowerOf2Test()
{
std::bitset<32> bit_setX(TextureImage[0]->sizeX);
std::bitset<32> bit_setY(TextureImage[0]->sizeY);
return (bit_setX.count()<2 && bit_setY.count()<2);
}

bool Texture::LoadTexture(char *Filename)
{
int Status=FALSE;

string name = Filename;
size_t found;

found = name.find(string(".bmp"));
if(found)
{ // Status Indicator
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadFromBMP("res/textures/NeHe.bmp"))
{
if(PowerOf2Test()) // both sizes must be power of 2
{
Status=TRUE; // Set The Status To TRUE

TransferAndConvertBMP();

glGenTextures(1, &TextureHandle[0]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap
// glBindTexture(GL_TEXTURE_2D, TextureHandle[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 4, SizeX, SizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
// glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
else
{
DeleteAndFree();
}
}
}

found = name.find(string(".png"));
if(found)
{ // Status Indicator

}

return Status;
}

AUX_RGBImageRec *Texture::LoadFromBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}

File=fopen(Filename,"r"); // Check To See If The File Exists

if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}

return NULL; // If Load Failed Return NULL
}

void Texture::TransferAndConvertBMP()
{
SizeX = TextureImage[0]->sizeX;
SizeY = TextureImage[0]->sizeY;

int GlobalSize = SizeX * SizeY;
imageData = new unsigned char [GlobalSize * 4];

for(int i = 0; i < GlobalSize; i++)
{
imageData[i*4+0] = TextureImage[0]->data[i*3+0];
imageData[i*4+1] = TextureImage[0]->data[i*3+1];
imageData[i*4+2] = TextureImage[0]->data[i*3+2];
imageData[i*4+3] = 255; // full alpha since its a BMP
}
}

void Texture::Bind()
{
glBindTexture(GL_TEXTURE_2D, TextureHandle[0]);
}
[/code]

Right now, all I'm getting is a white spinning cube with no texture. if I put the line (glTexImage2D(GL_TEXTURE_2D, 0, 4, SizeX, SizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);) in comment and I remove the comment of the one after it, it works! I get a perfectly well textured spinning cube. If I can get the alpha component to work, the next thing I'll do is to make a few functions to load .PNG and .TGA manually.

So, any help with this?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this