//Original PNG pixel data stored from top-left corner, BUT OGLES Texture drawing is from bottom-left corner
//int pos = 0;
//for(int row = 0; row < height; row++)
//{
//for(int col = 0; col < (4 * width); col += 4)
//{
//rgba[pos++] = row_pointers[row][col]; // red
//rgba[pos++] = row_pointers[row][col + 1]; // green
//rgba[pos++] = row_pointers[row][col + 2]; // blue
//rgba[pos++] = row_pointers[row][col + 3]; // alpha
//}
//}
//unlike store the pixel data from top-left corner, store them from bottom-left corner for OGLES Texture drawing...
int pos = (width * height * 4) - (4 * width);
for(int row = 0; row < height; row++)
{
for(int col = 0; col < (4 * width); col += 4)
{
rgba[pos++] = row_pointers[row][col]; // red
rgba[pos++] = row_pointers[row][col + 1]; // green
rgba[pos++] = row_pointers[row][col + 2]; // blue
rgba[pos++] = row_pointers[row][col + 3]; // alpha
}
pos=(pos - (width * 4)*2); //move the pointer back two rows
}
ImageInfo* imageInfo = (ImageInfo*)kdMalloc(sizeof(ImageInfo));
imageInfo->pixelData = rgba;
imageInfo->imageHeight = height;
imageInfo->imageWidth = width;
//clean up after the read, and free any memory allocated
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(file);
return imageInfo;
}
//----------------------------------------------------------------------------------------------------------------
int createPNGTextureFromStream(const u8* pixelData, const u32& dataSize)
{
GLuint textureID;
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&textureID);
glBindTexture(GL_TEXTURE_2D,textureID);
ImageInfo* imageInfo = decodePNGFromStream(pixelData, dataSize);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,imageInfo->imageWidth,imageInfo->imageHeight,0,
GL_RGBA,GL_UNSIGNED_BYTE,imageInfo->pixelData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
delete[] imageInfo->pixelData;
delete imageInfo;
return textureID;
}
//----------------------------------------------------------------------------------------------------------------
void main()
{
//Testcase1: decoding png data from a raw png bufferstream and create the corresponding Texture
//假设我们从某个地方可以拿到一个unsigned char* 的PNG数据源pixelData.
int texId = createPNGTextureFromStream(pixelData, dataSize);
//Testcase2: decoding png data from a given png file and and create the corresponding Texture
char* fileName = "example.png";
int texId = createPNGTextureFromFile(fileName);
//现在我们就可以用创建出来的textureID来绘制纹理了。。。。
}