2005-04-18 15:27:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <zoom/texture.h>
|
|
|
|
|
|
|
|
|
|
|
|
#pragma pack(push,2)
|
|
|
|
typedef struct _TEXHEADER {
|
|
|
|
unsigned char t;
|
|
|
|
unsigned char x;
|
|
|
|
unsigned long w;
|
|
|
|
unsigned long h;
|
|
|
|
} TEXHEADER;
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
|
|
|
|
int nTex;
|
|
|
|
TEXLIST *texlist;
|
|
|
|
|
|
|
|
GLuint LoadTexture(char *filename) {
|
|
|
|
GLuint tex;
|
|
|
|
FILE *file;
|
|
|
|
TEXHEADER txh;
|
|
|
|
unsigned char *data;
|
|
|
|
int i;
|
|
|
|
char name[100];
|
|
|
|
|
|
|
|
for(i = 0; i < nTex; i++) {
|
|
|
|
if(strcasecmp(filename, texlist[i].name) == 0) return texlist[i].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(name, "tex/");
|
|
|
|
strcat(name, filename);
|
|
|
|
file = fopen(name, "rb");
|
|
|
|
if(!file) return 0;
|
|
|
|
|
|
|
|
fread(&txh, sizeof(txh), 1, file);
|
|
|
|
if(txh.t != 'T' || txh.x != 'X') {
|
|
|
|
fclose(file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = malloc(txh.w*txh.h*4);
|
|
|
|
if(!data) {
|
|
|
|
fclose(file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fread(data, txh.w*txh.h*4, 1, file);
|
|
|
|
|
|
|
|
glGenTextures(1, &tex);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tex);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
2006-10-24 15:04:02 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2005-04-18 15:27:00 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, 4, txh.w, txh.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
|
|
|
|
|
|
free(data);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
nTex++;
|
|
|
|
|
|
|
|
if(nTex == 1) texlist = malloc(sizeof(TEXLIST));
|
|
|
|
else texlist = realloc(texlist, sizeof(TEXLIST)*nTex);
|
|
|
|
|
|
|
|
strcpy(texlist[nTex-1].name, filename);
|
|
|
|
texlist[nTex-1].id = tex;
|
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|