Skip to main content
gamedev.net
🔒 Locked

Generating an image?

Started by menorian 4 replies 1k views
Original Post
menorian
menorian
hello, i need some help to understand how i can convert my generated image into an unsigned char array that i need for "gluBuild2DMipmaps()" function. the code i wrote to generate the image: short iSize = 128; int x, y; unsigned char data[128][128][4]; for ( x = 0; x < iSize; x++ ) { for ( y = 0; y < iSize; y++ ) { int c = ((((x&0x20)==0)^((y&0x20))==0)); c = c ? 255 : 128; data[x][y][0] = c; data[x][y][1] = c; data[x][y][2] = c; data[x][y][3] = 255; } } unsigned char *imageDATA; // this will eventually become the image data. could anyone help me with this, i dont know how the pixel offset for image data works. Thanks, [Edited by - menorian on April 12, 2009 6:53:03 PM]
Erik Rufelt
Erik Rufelt
Simply use 'data'.. you don't need to do anything with it. Then use gluBuild2DMipmaps(GL_TEXTURE_2D, 4, 128, 128, GL_RGBA, GL_UNSIGNED_BYTE, data). I tried it with your code, and it works fine.
V-man
V-man
I guess your question is, how to make a texture.
http://www.opengl.org/wiki/Texture_Mapping
http://www.opengl.org/wiki/Common_Mistakes#gluBuild2DMipmaps
menorian
menorian
well it doesn't work.
i do an unsigned char cast (just using data didn't work)
like this:

Texture[0]->imageData = (unsigned char*)data;

but the end result looks like this, and i don't understand why:


i have no idea why those first pixels are broken, any ideas?
Erik Rufelt
Erik Rufelt
Show your entire code, for the part that creates the image and sends it to OpenGL, including any texture parameter settings and the texture creation.
Simulacrum
Simulacrum
You may want to try filling your image data scanline by scanline. Something like this:

const int width(128);const int height (128);     //height equal the number of scanlines in the imageconst int bytesPerPixel(4);const int scanlineSize(width * bytesPerPixel);const unsigned char *imageData(new char[scanlineSize * height]);int offset(0);for(int i(0);i < height;++i){     //pick the value you wish to initialize with     c = (c) ? 255 : 128;     //fill the scanline with the value you want     std::memset(          &imageData[0] + offset,          c,          scanlineSize);      // ^^^ or have another loop here to set each       // pixel value in the scanline...      //for(j(offset);j < (offset + scalineSize);++j){ imageData[j] = "whatever"; }     //housekeeping - update your offset into your image data     offset += scanlineSize;}//housekeeping - delete the dynamic array - Opengl now owns your texture//so you don't need it any longerdelete [] imageData;


I don't know what you expect your texture to look like but perhaps this helps you understand how to use "pixel offsets into image data"?
The difference between a dream and reality is only what you choose to do about either of them.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.