I was following this tutorial: http://nehe.gamedev.net/data/lessons....asp?lesson=06
But as I don't have the GLaux library and didn't feel like I wanted it I went ahead studied how to use libpng with OpenGL following this tutorial: http://www.macdevcenter.com/pub/a/ma...ps.html?page=1
The resulting code was copied and slightly modified code from the site,
Code:
unsigned char* glxReadPNG (TCHAR* file, png_uint_32* w, png_uint_32* h)
{
FILE* f;
unsigned char sig[8];
png_structp png_ptr;
png_infop info_ptr;
unsigned char* image_data;
int bit_depth;
int color_type;
unsigned int rowbytes;
png_uint_32 i;
png_bytepp row_pointers;
if ((f = fopen(file, "rb")) == NULL)
return NULL;
fread(sig, sizeof(*sig), sizeof(sig), f);
if (!png_check_sig(sig, sizeof(*sig))) {
fclose(f);
return NULL;
}
if ((png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) {
fclose(f);
return NULL;
}
if ((info_ptr = png_create_info_struct(png_ptr)) == NULL) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
fclose(f);
return NULL;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(f);
return NULL;
}
png_ptr->io_ptr = (png_voidp) f;
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, w, h, &bit_depth, &color_type, NULL, NULL, NULL);
if (color_type & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha(png_ptr);
if (bit_depth > 8)
png_set_strip_16(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
png_read_update_info(png_ptr, info_ptr);
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
if ((image_data = malloc(*h * rowbytes)) == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(f);
return NULL;
}
if ((row_pointers = malloc(*h * sizeof(png_bytep))) == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(f);
free(image_data);
return NULL;
}
for (i = 0; i < *h; i++)
row_pointers[*h - 1 - i] = image_data + i*rowbytes;
png_read_image(png_ptr, row_pointers);
free(row_pointers);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(f);
return image_data;
}
Also following the same site, I loaded this texture using the lines found on page 3 and probably a mix from the NeHe tutorial.
Global variables and static variables for wndproc (window procedure for the OpenGL window):
Code:
//global
int w;
int h;
GLfloat xrot = 0.0f;
GLfloat yrot = 0.0f;
GLuint imgname;
//static variables for wndproc
static HDC hdc;
static HGLRC hrc;
static HFONT font;
static RECT re = {0, 0, 0, 0};
static unsigned char* image;
static int imgw, imgh;
WM_CREATE for wndproc:
Code:
case WM_CREATE:
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(pfd),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
if ((hdc = GetDC(hwnd)) == NULL ||
!SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd) ||
(hrc = wglCreateContext(hdc)) == NULL ||
!wglMakeCurrent(hdc, hrc))
{
Error();
PostQuitMessage(1);
break;
}
font = CreateFont(-MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "MS Shell Dlg");
if (font == NULL) {
Error();
PostQuitMessage(1);
break;
}
SetTextColor(hdc, RGB(0, 0xFF, 0));
SetBkMode(hdc, TRANSPARENT);
if ((image = glxReadPNG(TEXT("ogl.png"), &imgw, &imgh)) == NULL) {
Error();
PostQuitMessage(1);
break;
}
glGenTextures(1, &imgname);
glBindTexture(GL_TEXTURE_2D, imgname);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
free(image);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
break;
}
The rendering function is pretty much identical with the one from the OpenGL tutorial, the exception being that I still have the triangle from earlier lessons still drawn but I think that shouldn't matter, also, this message is probably already too long to drop more code.
I don't know where it fails, when the screen was first drawn I saw nothing. Then I thought it was probably because the color was set to black so I changed it to white and that gave me a white cube, with no textures on it.
I don't think the PNG-loading function is failing as I wrote a test program in SDL to test it out. It returns the right width, height and pixel data as my test program had no problem drawing the picture it loaded (except upside down and horizontally flipped, which it should do according to the libpng guide).
Compiled using MinGW and libpng 1.2.8. Attaching ogl.c (main program), ogl.png (texture) and test.c (SDL program to test libpng).
I'm suffering from a cold at the moment and can't think clearly so I'm sorry if this thread isn't as helpful as it could. I'm gonna go grab some painkillers after posting this.