Moved little cms from contrib to core. Need for parsing of ICC files.
[AROS-Contrib.git] / Demo / GLExcess / Texture.cpp
blob39e5a578244aa5543fa0eb248a720b080206ded0
1 /*
2 GLExcess v1.0 Demo
3 Copyright (C) 2001-2003 Paolo Martella
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
16 #include "Texture.h"
18 Texture::Texture() {tID=0;}
19 Texture::~Texture(){Kill();}
20 bool Texture::Create(char* FileName)
22 Kill();
23 glGenTextures(1, &tID);
24 tID++; // Use an offset of +1 to differentiate from non-initialized state.
25 glBindTexture(GL_TEXTURE_2D, tID-1);
26 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
27 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
28 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
29 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
30 return true;
33 void Texture::Kill()
35 if(tID)
37 tID--;
38 glDeleteTextures(1, &tID);
39 tID=0;
42 GLuint pow2(GLuint exp)
44 GLuint result=1;
45 if (!exp) return result;
46 for (GLuint a=0; a<exp; a++)
48 result*=2;
50 return result;
52 void Texture::Use()
54 glBindTexture(GL_TEXTURE_2D,tID-1);
56 bool Texture::Load(char* Opaque)
58 if(Opaque==0) return false;
59 if (!Create(Opaque)) return false;
60 char * rgbdata;
61 GLuint w,h;
62 FILE * fhandle=NULL;
63 fhandle=fopen(Opaque,"rb");
64 if (!fhandle) return false;
65 char ww,hh;
66 fread(&ww,sizeof(char),1,fhandle);
67 w=pow2(ww-48);
68 fread(&hh,sizeof(char),1,fhandle);
69 h=pow2(hh-48);
70 GLuint size=w*h*3;
71 rgbdata=new char[size];
72 GLuint read=fread(rgbdata,sizeof(char),size,fhandle);
73 if (read!=size) return false;
74 fclose(fhandle);
75 gluBuild2DMipmaps(GL_TEXTURE_2D,3,w,h,GL_RGB,GL_UNSIGNED_BYTE,rgbdata);
76 delete [] rgbdata;
77 return true;