Accessing individual pixels of texture
Hello everyone. I've modeled something in opengl. I have also applied textures. It is very important for me to access the matrix of my loaded texture, but i can't figure out how to do it using the codes that i used in my project(which i got from NeHe)
Here is the code, i need to access the texture 'matrix' somewhere, please help
AUX_RGBImageRec *LoadBMP(char *Filename){
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
}
BOOL LoadGLTexture(GLuint *texPntr, char* name)
{
BOOL success = FALSE;
AUX_RGBImageRec *TextureImage = NULL;
glGenTextures(1, texPntr); // Generate 1 texture
FILE* test=NULL;
TextureImage = NULL;
test = fopen(name, "r"); // test to see if the file exists
if (test != NULL) { // if it does
fclose(test); // close the file
TextureImage = auxDIBImageLoad(name);
}
if (TextureImage != NULL) { // if it loaded
success = TRUE;
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, *texPntr);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
if (TextureImage->data)
free(TextureImage->data);
return success;
}
TextureImage->data should be a pointer to your texture. Then you can acces
to a pixel calculating the position of the pixel in the array of pixels.
TextureImage->data[0] (Component R of the first pixel)
TextureImage->data[1] (Component G of the first pixel)
TextureImage->data[2] (Component B of the first pixel)
TextureImage->data[3] (Component R of the second pixel)
TextureImage->data[4] (Component G of the second pixel)
TextureImage->data[5] (Component B of the second pixel)
...
Well, i said you have to calculate because the texture really its a matrix
of pixels.
So TextureImage->data[0] is the R component of pixel [0][0] of the matrix
TextureImage->data[3] is the R component of pixel [0][1] of the matrix
...
TextureImage->data[TextureImage->sizeX*3] is the R component of pixel [1][0]
TextureImage->data[TextureImage->sizeX*3+1] is the R component of pixel [1][1]
...
I hope i'm not wrong and this can help you
to a pixel calculating the position of the pixel in the array of pixels.
TextureImage->data[0] (Component R of the first pixel)
TextureImage->data[1] (Component G of the first pixel)
TextureImage->data[2] (Component B of the first pixel)
TextureImage->data[3] (Component R of the second pixel)
TextureImage->data[4] (Component G of the second pixel)
TextureImage->data[5] (Component B of the second pixel)
...
Well, i said you have to calculate because the texture really its a matrix
of pixels.
So TextureImage->data[0] is the R component of pixel [0][0] of the matrix
TextureImage->data[3] is the R component of pixel [0][1] of the matrix
...
TextureImage->data[TextureImage->sizeX*3] is the R component of pixel [1][0]
TextureImage->data[TextureImage->sizeX*3+1] is the R component of pixel [1][1]
...
I hope i'm not wrong and this can help you
Sorry, when i said
TextureImage->data[TextureImage->sizeX*3+1] is the R component of pixel [1][1]
should say
TextureImage->data[(TextureImage->sizeX+1)*3] is the R component of pixel [1][1]
TextureImage->data[TextureImage->sizeX*3+1] is the R component of pixel [1][1]
should say
TextureImage->data[(TextureImage->sizeX+1)*3] is the R component of pixel [1][1]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement