1 | #include "Texture.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 | #include "common.h"
|
---|
4 |
|
---|
5 | #include <IL/il.h>
|
---|
6 | #include <assert.h>
|
---|
7 | #include <iostream>
|
---|
8 |
|
---|
9 |
|
---|
10 | namespace CHCDemoEngine
|
---|
11 | {
|
---|
12 |
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 |
|
---|
16 | void startil()
|
---|
17 | {
|
---|
18 | ilInit();
|
---|
19 | assert(ilGetError() == IL_NO_ERROR);
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | void stopil()
|
---|
24 | {
|
---|
25 | ilShutDown();
|
---|
26 | assert(ilGetError() == IL_NO_ERROR);
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | Texture::Texture(const std::string &filename):
|
---|
31 | mWidth(0),
|
---|
32 | mHeight(0),
|
---|
33 | mFormat(0),
|
---|
34 | mImage(NULL),
|
---|
35 | mTexId(-1),
|
---|
36 | mName(filename),
|
---|
37 | mBoundaryModeS(REPEAT),
|
---|
38 | mBoundaryModeT(REPEAT),
|
---|
39 | mUseMipMap(true)
|
---|
40 | {
|
---|
41 | startil();
|
---|
42 |
|
---|
43 | if (!ilLoadImage(ILstring(filename.c_str())))
|
---|
44 | {
|
---|
45 | cerr << "Image load error " << ilGetError() << ": " << filename << endl;
|
---|
46 | }
|
---|
47 | else
|
---|
48 | {
|
---|
49 | mWidth = ilGetInteger(IL_IMAGE_WIDTH);
|
---|
50 | mHeight = ilGetInteger(IL_IMAGE_HEIGHT);
|
---|
51 |
|
---|
52 | mFormat = FORMAT_RGBA;
|
---|
53 | mByteSize = mHeight * GetRowLength();
|
---|
54 | mImage = malloc(GetByteSize());
|
---|
55 |
|
---|
56 | Debug << "loaded texture " << filename << " w " << mWidth << " h " << mHeight << endl;
|
---|
57 | //cout << "loaded texture " << filename << " w " << mWidth << " h " << mHeight << endl;
|
---|
58 |
|
---|
59 | ilCopyPixels(0, 0, 0, mWidth, mHeight, 1, IL_RGBA, IL_UNSIGNED_BYTE, mImage);
|
---|
60 | assert(ilGetError() == IL_NO_ERROR);
|
---|
61 | }
|
---|
62 |
|
---|
63 | stopil();
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | static int GetGlWrapMode(int mode)
|
---|
68 | {
|
---|
69 | switch (mode)
|
---|
70 | {
|
---|
71 | case Texture::REPEAT: return GL_REPEAT;
|
---|
72 | case Texture::CLAMP: return GL_CLAMP_TO_EDGE;
|
---|
73 | case Texture::BORDER: return GL_CLAMP_TO_BORDER;
|
---|
74 | default: return GL_CLAMP_TO_EDGE;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return GL_CLAMP_TO_EDGE;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | void Texture::Create()
|
---|
82 | {
|
---|
83 | glEnable(GL_TEXTURE_2D);
|
---|
84 |
|
---|
85 | glGenTextures(1, &mTexId);
|
---|
86 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
87 |
|
---|
88 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
---|
89 |
|
---|
90 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 4);
|
---|
91 |
|
---|
92 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GetGlWrapMode(mBoundaryModeS));
|
---|
93 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GetGlWrapMode(mBoundaryModeT));
|
---|
94 |
|
---|
95 | if (mUseMipMap)
|
---|
96 | {
|
---|
97 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
98 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
103 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (!mUseMipMap)
|
---|
107 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
|
---|
108 | else
|
---|
109 | gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
|
---|
110 |
|
---|
111 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | int Texture::GetFormat() const
|
---|
116 | {
|
---|
117 | return mFormat;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | int Texture::GetWidth() const
|
---|
122 | {
|
---|
123 | return mWidth;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | int Texture::GetHeight() const
|
---|
128 | {
|
---|
129 | return mHeight;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | void *Texture::GetImage() const
|
---|
134 | {
|
---|
135 | return mImage;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | Texture::~Texture()
|
---|
140 | {
|
---|
141 | glDeleteTextures(1, &mTexId);
|
---|
142 | free(mImage);
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | std::string Texture::GetName() const
|
---|
147 | {
|
---|
148 | return mName;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | int Texture::GetRowLength() const
|
---|
153 | {
|
---|
154 | if (mFormat == FORMAT_RGBA)
|
---|
155 | return mWidth * 4 * sizeof (unsigned char);
|
---|
156 | else
|
---|
157 | return mWidth * 3 * sizeof (unsigned char);
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | int Texture::GetPixelSize() const
|
---|
162 | {
|
---|
163 | if (mFormat == FORMAT_RGBA)
|
---|
164 | return 4 * sizeof(unsigned char);
|
---|
165 | else
|
---|
166 | return 3 * sizeof(unsigned char);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | void Texture::Bind() const
|
---|
171 | {
|
---|
172 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | } |
---|