Jump to content
  • Advertisement
Sign in to follow this  
Yashin

OpenGL Accessing individual pixels of texture

This topic is 4577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!