TEXTURE.CPP
上传用户:zbjingming
上传日期:2010-01-02
资源大小:2436k
文件大小:3k
- #include "radio.h"
- #include <string.h>
- extern TEXTURE_2D **TextureList;
- /********************************/
- /* function : OpenTexImage */
- /********************************/
- unsigned char *OpenTexImage( INT2U TexIndex, INT2U *rslx, INT2U *rsly )
- {
- unsigned char *image;
- FILE *fp;
- INT2U srcx, srcy, x, y;
- INT4U i, j, rslp, srcp, srcp2;
- unsigned short *rbuf, *gbuf, *bbuf;
- float daltarx, daltary;
- char ImageName[30];
- unsigned char *SImageData;
- int rc;
- long width, height;
- strcpy( ImageName, TextureList[TexIndex]->fname);
-
- /* load a image */
- fp = fopen(ImageName,"rb");
- if(!fp) return 0;
- fseek(fp,18L,0);
- rc=fread(&width,sizeof(long),1,fp);
- rc=fread(&height,sizeof(long),1,fp);
- *rslx=srcx=width; *rsly=srcy=height;
- fseek(fp,54L,0);
- image = (unsigned char *)malloc(width*height*3);
- rc=fread(image,width*height*3,1,fp);
- fclose(fp);
- SImageData = (unsigned char *)malloc(srcx*srcy*3);
- for(i=0; i<srcx; i++) {
- for(j=0; j<srcy; j++) {
- (unsigned char)*(SImageData+i*srcx*3+j*3+0) = (unsigned char)*(image+i*srcx*3+j*3+2);
- (unsigned char)*(SImageData+i*srcx*3+j*3+1) = (unsigned char)*(image+i*srcx*3+j*3+1);
- (unsigned char)*(SImageData+i*srcx*3+j*3+2) = (unsigned char)*(image+i*srcx*3+j*3+0);
- }
- }
- free(image);
- printf("%s : %ld=%ldn", ImageName, srcx*srcy*3,i*j*3);
- return( SImageData );
- }
- /********************************/
- /* function : InitTex */
- /********************************/
- void InitTex( int TexIndex )
- {
- INT2U rslx, rsly, TextType;
- char *ImageData;
- static int OldIndex = -1;
- if(TexIndex<=0) return;
- if(TexIndex == OldIndex)
- {
- glEnable(GL_TEXTURE_2D);
- return;
- }
-
- ImageData = (char *)ImageDatas[TexIndex-1];
- TextType = TextureList[TexIndex]->type;
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- if( TextType == CLAMP_TEXTURE )
- {
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- }
- else
- {
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- }
- glTexImage2D( GL_TEXTURE_2D, 0, 3, rslxs[TexIndex-1], rslys[TexIndex-1],
- 0,GL_RGB, GL_UNSIGNED_BYTE, ImageData );
- glEnable(GL_TEXTURE_2D);
- OldIndex = TexIndex;
- }
- /********************************/
- /* function : CloseTex */
- /********************************/
- void CloseTex()
- {
- glDisable(GL_TEXTURE_2D);
- }
- void LoadAllTexture()
- {
- int i;
-
- for (i=0; i <texnum ; i++)
- ImageDatas[i] = OpenTexImage( i+1, &rslxs[i], &rslys[i] );
- }
- void FreeAllTexture()
- {
- int i;
-
- for (i=0; i <texnum ; i++)
- free(ImageDatas[i]);
- }