/* ******************************************************************************** Copyright 2006, 2007 Ben Ruyl This file is part of Sokoban 3D. Sokoban 3D is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Sokoban 3D is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Sokoban 3D; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ******************************************************************************** */ #include #include #include #include #include "texture.h" #include #include "tga.h" //#include struct tImage { int channels; int sizeX; int sizeY; unsigned char *data; }; //unsigned char *loadJPEG(const char *filename, unsigned int *w, unsigned int *h, unsigned int *components) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE * infile; unsigned char *buffer, *ptr; unsigned int nval; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); if ((infile = fopen(filename, "rb")) == NULL) { fprintf(stderr, "Can't open '%s'\n", filename); jpeg_destroy_decompress(&cinfo); return NULL; } jpeg_stdio_src(&cinfo, infile); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); *w = cinfo.output_width; *h = cinfo.output_height; *components = cinfo.output_components; nval = cinfo.output_width * cinfo.output_components * cinfo.output_height; buffer = (unsigned char *)malloc(sizeof(unsigned char) * nval); if (!buffer) { fprintf(stderr, "Out of memory\n"); jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(infile); return NULL; } /* We want to flip the image over the x-axis, so start reading at the last row and move backwards. */ ptr = buffer + (cinfo.output_width * cinfo.output_components) * (cinfo.output_height - 1); while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo, &ptr, 1); ptr -= cinfo.output_width * cinfo.output_components; } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(infile); return buffer; } /*int LoadJPG(string strFileName) { struct jpeg_decompress_struct cinfo; tImage *pImageData = NULL; FILE *pFile; if((pFile = fopen(strFileName.c_str(), "rb")) == NULL) { // MessageBox(hWnd, "Can't load JPG image!", "ERROR", MB_OK | MB_ICONINFORMATION); return 0; } jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, pFile); pImageData = (tImage*)malloc(sizeof(tImage)); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); pImageData->channels = cinfo.num_components; pImageData->sizeX = cinfo.image_width; pImageData->sizeY = cinfo.image_height; int rowSpan = cinfo.image_width * cinfo.num_components; pImageData->data = ((unsigned char*)malloc(sizeof(unsigned char)*rowSpan*pImageData->sizeY)); unsigned char** rowPtr = new unsigned char*[pImageData->sizeY]; for (int i = 0; i < pImageData->sizeY; i++) rowPtr[i] = &(pImageData->data[i * rowSpan]); int rowsRead = cinfo.output_height-1; while (cinfo.output_scanline < cinfo.output_height) { rowsRead -= jpeg_read_scanlines(&cinfo, &rowPtr[rowsRead], cinfo.output_height - rowsRead); } delete [] rowPtr; jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(pFile); GLuint texid; glGenTextures(1, &texid); glBindTexture(GL_TEXTURE_2D, texid); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, FGameOptions.FAnisotropicFilter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, pImageData->sizeX, pImageData->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pImageData->data); return texid; }*/ int LoadBitmap(string szFileName, GLuint &texid) // Creates Texture From A Bitmap File { HBITMAP hBMP; // Handle Of The Bitmap BITMAP BMP; // Bitmap Structure glGenTextures(1, &texid); // Create The Texture hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), szFileName.c_str(), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE ); if (!hBMP) // Does The Bitmap Exist? return FALSE; // If Not Return False GetObject(hBMP, sizeof(BMP), &BMP); // Get The Object // &BMP: Buffer For Object Information // glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Pixel Storage Mode (Word Alignment / 4 Bytes) // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBindTexture(GL_TEXTURE_2D, texid); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, FGameOptions.FAnisotropicFilter); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gluBuild2DMipmaps (GL_TEXTURE_2D, GL_RGB8, BMP.bmWidth, BMP.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits); DeleteObject(hBMP); // Delete The Object return 1; // Loading Was Successful } GLuint LoadTGA1(string filename) // Loads A TGA File Into Memory { return loadTGATexture(filename.c_str()); // Texture Building Went Ok, Return True }