PDA

View Full Version : Loading texture using Lodepng



mrbo2302
06-19-2014, 03:57 AM
hi guys,
I'm new in opengl.I've tried to load a texture from png file using Lodepng library.Everything works fine...but the result is not good as I expect.
here is loadtexture function which will use decode function from lodepng to decode png file to unsigned char vector.this function I'm confused with some lines.

std::vector<unsigned char> loadTexture(const char* filedir, unsigned int w, unsigned int h)
{
std::vector<unsigned char> image;
//lodepng::load_file(rawimage, filedir);
LodePNGState state;
lodepng_state_init(&state);
unsigned error = decode(image, w,h,filedir);
if (error != 0)
{
cout << "error " << error << ": " << lodepng_error_text(error) << endl;
}

// Texture size must be power of two for the primitive OpenGL version this is written for.Find next power of two.
u2 = 1; while (u2 < w) u2 *= 2;
v2 = 1; while (v2 < h) v2 *= 2;
cout << "Size image in POT: " << u2 << " x " << v2;
// Ratio for power of two version compared to actual version, to render the non power of two image with proper size.
u3 = (double)w / u2;
v3 = (double)h / v2;

// Make power of two version of the image.
std::vector<unsigned char> image2(u2 * v2 * 4);
for (size_t y = 0; y < h; y++)
for (size_t x = 0; x < w; x++)
for (size_t c = 0; c < 4; c++)
{
//image2[4 * u2 * y + 4 * x + c] = image[4 * width * y + 4 * x + c]; this is original lines but it came with bug...cuz size of this image2 vector.So i fixed it...did i do right ?
image2[4 * y + 4 * x + c] = image[4 * y + 4 * x + c];
}
//return image2;
return image;
}

here is function i used to bind texure

void createTexture(const char* filedir,GLsizei w, GLsizei h)
{
imageready = loadTexture(filedir, 128, 160);
//flip the texture
unsigned char *imgPtr = &imageready[0];
int numColorComponents = 4;
int wInc = w * 4;//width in char
unsigned char* top = NULL;
unsigned char* bot = NULL;
unsigned char temp = 0;
/*flip texture*/
for (int i = 0; i < h / 2; i++)
{
top = imgPtr + i*wInc;
bot = imgPtr + (h - i - 1) * wInc;
for (int j = 0; j < wInc; j++)
{

// Swap the chars around.
temp = *top;
*top = *bot;
*bot = temp;
++top;
++bot;
}
}
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, &imageready[0]); //128x256 is the POT size of image that i used in this exam
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//GL_NEAREST
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//GL_NEAREST
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


}

and I draw it

glBindTexture(GL_TEXTURE_2D, texture[0]);


glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
here is the result......so what is the problem ? I want to render that png file just a small full image.
1344
Thanks in advance