Textures, Texture Mapping, World Collision
Now i''ve already looked this stuff up. The textures I can''t get a working version I can compile with Dev-C++ on either Nehe or GameTutorials. I can''t compile those because of GLAUX.DLL errors, already been through discussions on that, and gave up on that and was told to find an alternate route. So I''m looking for a good header file with procedures that I can look from, learn from, whatever for both World Collision, Texture Loading, Texture Mapping, everything with those 3 things basically. I found someone with a really nice zip containing easy to use Camera procedures. If you have something like that then send it to me.
Higher Forces
forget aux and use sdl.
bmp loading algorithm:
tiff loading algoritm:
png loading algorithm (requires libpng) (works well with gnu-linux/gcc/icc - you might have to tweak around to get it working on windows)
that should be enough for loading processes.
now the texure one:
bmp loading algorithm:
/************************bmploader.h****************************/#ifndef __bmp_loader_header_#define __bmp_loader_header_#ifdef __cplusplusextern "C" {#endif struct bmpImage { unsigned long width; unsigned long height; unsigned char *data; }; typedef struct bmpImage bmpImage; int loadBMPImage(char*, struct bmpImage*); #ifdef __cplusplus }#endif #endif/*************************bmploader.c***************************/#include "../include/bmploader.h"#include <stdio.h> int loadBMPImage(char *filename, struct bmpImage *image) { FILE *file; unsigned long size; unsigned long i; unsigned short int planes; unsigned short int bpp; char temp; if ((file = fopen(filename, "rb"))==NULL) { printf("File Not Found : %s\n", filename); return 0; } fseek(file, 18, SEEK_CUR); if ((i = fread(&image->width, 4, 1, file)) != 1) { printf("Error reading width from %s.\n", filename); return 0; } if ((i = fread(&image->height, 4, 1, file)) != 1) { printf("Error reading height from %s.\n", filename); return 0; } size = image->width * image->height * 3; if ((fread(&planes, 2, 1, file)) != 1) { printf("Error reading planes from %s.\n", filename); return 0; } if (planes != 1) { printf("Planes from %s is not 1: %u\n", filename, planes); return 0; } if ((i = fread(&bpp, 2, 1, file)) != 1) { printf("Error reading bpp from %s.\n", filename); return 0; } if (bpp != 24) { printf("Bpp from %s is not 24: %u\n", filename, bpp); return 0; } fseek(file, 24, SEEK_CUR); image->data = (unsigned char*) malloc(size); if (image->data == NULL) { printf("Error allocating memory for color-corrected image data"); return 0; } if ((i = fread(image->data, size, 1, file)) != 1) { printf("Error reading image data from %s.\n", filename); return 0; } for (i=0;i<size;i+=3) { temp = image->data[i]; image->data[i] = image->data[i+2]; image->data[i+2] = temp; } return 1; }
tiff loading algoritm:
/*************************tiffloader.h**************************/#ifndef __tiff_loader_header_#define __tiff_loader_header_#ifdef __cplusplusextern "C" {#endif struct TIFFImage { unsigned int width, height; unsigned char* data; }; typedef struct TIFFImage tiffImage; int loadTIFFImage(char* filename, struct TIFFImage* tiff); #ifdef __cplusplus }#endif#endif/************************tiffloader.c***************************/#include "../include/tiffloader.h"#include <assert.h>#include <tiffio.h> int loadTIFFImage(char* filename, struct TIFFImage* tiff) { TIFF* tiffile = TIFFOpen(filename, "r"); uint32 *raster; if(! tiffile) { printf("could not load \'%s\'. aborting\n", filename); return 0xf000; } TIFFGetField(tiffile, TIFFTAG_IMAGEWIDTH, &tiff -> width); TIFFGetField(tiffile, TIFFTAG_IMAGELENGTH, &tiff -> height); size_t pixels = tiff -> width * tiff -> height; //size_t is in anjuta rot. int waer' blau. raster = (uint32*)_TIFFmalloc(pixels * 4); tiff -> data = (unsigned char*)malloc(pixels * 4); assert(raster && tiff -> data && "could not allocate memory"); assert(TIFFReadRGBAImage(tiffile, tiff -> width, tiff -> height, raster, 0) && "not a valid tiff file"); memcpy(tiff -> data, raster, pixels*4); _TIFFfree(raster); TIFFClose(tiffile); }
png loading algorithm (requires libpng) (works well with gnu-linux/gcc/icc - you might have to tweak around to get it working on windows)
/***************************pngloader.h*************************/#ifndef __png_loader_header_#define __png_loader_header_#include <png.h>#ifdef __cplusplusextern "C" {#endif struct pngImage { unsigned long width, height; /* typedef long png_uint_32 ^_^;;*/ int bit_depth, color_type, filter_type, compression_type, interlace_type; png_structp png_ptr; png_infop info_ptr, end_info; int rowbytes; png_colorp palette; int num_palette; unsigned int bpp; unsigned char *data; }; typedef struct pngImage pngImage; int loadPNGImage(char *fileName, struct pngImage *png); #ifdef __cplusplus }#endif#endif/*************************pngloader.c***************************/#include "../include/pngloader.h"#include <stdio.h>int loadPNGImage(char *fileName, struct pngImage *png) { FILE* pngFile = fopen(fileName, "r"); char *signature[8]; if(! pngFile) { printf("could not load \'%s\'. aborting\n", fileName); return 0xf000; } fread(signature, 8, 1, pngFile); int is_png = !png_sig_cmp((png_bytep)signature, 0, 8); if (!is_png) { printf("file \'%s\' is not in png format\n", fileName); return 0xf010; } png -> png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)0, 0, 0); if(! png -> png_ptr) { printf("could not create png pointer\n"); return 0xf011; } png -> info_ptr = png_create_info_struct(png -> png_ptr); if (!png -> info_ptr) { png_destroy_read_struct(&png -> png_ptr, (png_infopp)NULL, (png_infopp)NULL); return 0xf012; } png -> end_info = png_create_info_struct(png -> png_ptr); if (!png -> end_info) { png_destroy_read_struct(&png -> png_ptr, &png -> info_ptr, (png_infopp)NULL); return 0xf013; } png_init_io(png -> png_ptr, pngFile); png_set_sig_bytes(png -> png_ptr, 8); png_read_info(png -> png_ptr, png -> info_ptr); png_get_IHDR(png -> png_ptr, png -> info_ptr, &png -> width, &png -> height, &png -> bit_depth, &png -> color_type, &png -> interlace_type, &png -> compression_type, &png -> filter_type); if(png -> info_ptr -> channels == 1) png -> bpp = 8; else if(png -> info_ptr -> channels == 2) png -> bpp = 16; else if(png -> info_ptr -> channels == 3) png -> bpp = 24; else if(png -> info_ptr -> channels == 4) png -> bpp = 32; else printf("libpng -> error in bit depth of file \'%s\'\n", fileName); png -> rowbytes = png_get_rowbytes(png -> png_ptr, png -> info_ptr); png -> data = (unsigned char*)malloc(png -> rowbytes * png -> height * sizeof(char)); png_bytep *row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * png -> height); int row; for (row = (int)png->height-1; row >= 0 ; row--) row_pointers[((int)png->height-1)-row] = &png -> data[png -> rowbytes * row]; png_read_image(png -> png_ptr, row_pointers); free(row_pointers); png_read_end(png -> png_ptr, png -> info_ptr); fclose(pngFile); return 0; }
that should be enough for loading processes.
now the texure one:
#ifndef __image_file_header_#define __image_file_header_#include <GL/gl.h>class Image { private: unsigned int width, height, bitdepth, specialGLFormat; unsigned char* data; public: Image(void); ~Image(void); unsigned int getWidth(void), getHeight(void), getBitDepth(void); unsigned char* getDataPointer(void); GLenum getGLFormat(void); void setWidth(unsigned int), setHeight(unsigned int), setBitDepth(unsigned int), setGLFormat(GLenum); void setDataPointer(unsigned char*); };#ifndef __root_node_header_class objectRoot; //it's just used for output#endif class ImageIO { private: objectRoot* rootnode; Image* tempimg; void load_png_image(char* filename); void load_bmp_image(char* filename); void load_tga_image(char* filename); void load_tif_image(char* filename); public: ImageIO(objectRoot*); ~ImageIO(void); Image* loadImage(char* filename); int saveImage(char* filename, Image* image); };#endif/****************************image.cpp***************************/#include "../include/image.h"#include "../include/bmploader.h"#include "../include/pngloader.h"#include "../include/tgaloader.h"#include "../include/tiffloader.h"#include "../include/root.h"#include <stdio.h>#include <stdlib.h>#include <assert.h>Image :: Image(void) { width = 0; height = 0; data = 0; }Image :: ~Image(void) { if(data) free(data); } unsigned int Image ::getWidth(void) { return width; }unsigned int Image :: getHeight(void) { return height; }unsigned char* Image :: getDataPointer(void) { return data; } GLenum Image :: getGLFormat(void) { if(specialGLFormat) return specialGLFormat; if(bitdepth == 8) return GL_LUMINANCE; if(bitdepth == 16) return GL_LUMINANCE_ALPHA; if(bitdepth == 24) return GL_RGB; if(bitdepth == 32) return GL_RGBA; return GL_RGBA; } unsigned int Image :: getBitDepth(void) { return bitdepth; } void Image :: setWidth(unsigned int newWidth) { this -> width = newWidth; }void Image :: setHeight(unsigned int newHeight) { this -> height = newHeight; }void Image :: setBitDepth(unsigned int newBitdepth) { this -> bitdepth = newBitdepth; }void Image :: setGLFormat(GLenum format) { this -> specialGLFormat = format; }void Image :: setDataPointer(unsigned char* newPointer) { this -> data = newPointer; }ImageIO :: ImageIO(objectRoot* rootnode) { this -> rootnode = rootnode; } ImageIO :: ~ImageIO(void) { } Image* ImageIO :: loadImage(char* filename) { char* end = filename + strlen(filename) - 3; if(!strcmp(end, "png")) load_png_image(filename); else if(!strcmp(end, "bmp")) load_bmp_image(filename); else if(!strcmp(end, "tga")) load_tga_image(filename); else if(!strcmp(end, "tif") || !strcmp(end, "iff")) load_tif_image(filename); else assert(!"this point should never be reached"); assert(tempimg && "error with image memory (general protection fault)"); fprintf(rootnode -> getDefaultOutputStream(), "file '%s' was loaded successfully\nformat: %ix%i:%i\nmemory: start: %x(%%ebp), end: %x(%%ebp)\n\n", filename, tempimg -> getWidth(), tempimg -> getHeight(), tempimg -> getBitDepth(), tempimg -> getDataPointer(), ((tempimg -> getWidth() * tempimg -> getHeight() * tempimg -> getBitDepth())/8) + tempimg -> getDataPointer()); return tempimg; } int ImageIO :: saveImage(char* filename, Image* img) { char* end = filename + strlen(filename) - 3; if(strcmp(end, "tga")) return 0; return tgaSave(filename, img -> getWidth(), img -> getHeight(), img -> getBitDepth(), img -> getDataPointer()); }
#ifndef __texture_header_#define __texture_header_#include "../include/image.h"#include <GL/gl.h>class texture { private: unsigned long int width, height; GLenum format; GLubyte *data; public: texture(char*, int); texture(Image*); texture(unsigned long int, unsigned long int, GLenum, unsigned char*); texture(unsigned long int, unsigned long int, GLenum, float*); void loadUp(void); void loadMipmapsUp(void); GLubyte *getDataPointer(void); GLuint getWidth(), getHeight(), getBytesPerPixel(); };/****************************texture.cpp************************/#include "../include/texture.h"#include <stdlib.h>#include <assert.h>#include <GL/glu.h>texture :: texture(Image *i) { assert(i && "image pointer is NULL"); width = i -> getWidth(); height = i -> getHeight(); format = i -> getGLFormat(); data = i -> getDataPointer(); } texture :: texture(unsigned long int width, unsigned long int height, GLenum format, unsigned char *data) { assert(width > 0 && height > 0 && data && "error in texture initialization"); this -> width = width; this -> height = height; this -> format = format; this -> data = data; } texture :: texture(unsigned long int width, unsigned long int height, GLenum format, float *data) { assert(width > 0 && height > 0 && data && "error in texture initialization"); this -> width = width; this -> height = height; this -> format = format; int i, j; switch(format) { case GL_ALPHA: j = 1; break; case GL_LUMINANCE: j = 1; break; case GL_LUMINANCE_ALPHA: j = 2; break; case GL_RGB: j = 3; break; case GL_RGBA: j = 4; break; } this -> data = (unsigned char*)malloc(sizeof(unsigned char)*width*height*j); for(i = 0; i < width*height*j; i++) this -> data[i] = (unsigned char)(data[i]*255.0f); } void texture :: loadUp(void) { glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); } void texture :: loadMipmapsUp(void) { gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, format, GL_UNSIGNED_BYTE, data); } GLubyte *texture :: getDataPointer(void) { return data; }GLuint texture :: getWidth() { return width; }GLuint texture :: getHeight() { return height; }GLuint texture :: getBytesPerPixel() { int j; switch(format) { case GL_ALPHA: j = 1; break; case GL_LUMINANCE: j = 1; break; case GL_LUMINANCE_ALPHA: j = 2; break; case GL_RGB: j = 3; break; case GL_RGBA: j = 4; break; } return j; }#endif [source]<SPAN CLASS=editedby>[edited by - 666_1337 on June 19, 2003 2:57:48 AM]</SPAN>
yeah i appreciate the help, but it''s asking for a root file and a tgaloader. i don''t know if these are completely necessary files, but could you please include them
Higher Forces
Higher Forces
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement