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 << "successfully loaded texture " << filename << " w " << mWidth << " h " << mHeight << endl;
|
---|
57 |
|
---|
58 | ilCopyPixels(0, 0, 0, mWidth, mHeight, 1, IL_RGBA, IL_UNSIGNED_BYTE, mImage);
|
---|
59 | assert(ilGetError() == IL_NO_ERROR);
|
---|
60 | }
|
---|
61 |
|
---|
62 | stopil();
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | void Texture::Create()
|
---|
67 | {
|
---|
68 | glEnable(GL_TEXTURE_2D);
|
---|
69 |
|
---|
70 | glGenTextures(1, &mTexId);
|
---|
71 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
72 |
|
---|
73 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
---|
74 |
|
---|
75 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 4);
|
---|
76 |
|
---|
77 | int glWrapModeS = (mBoundaryModeS == REPEAT) ? GL_REPEAT : GL_CLAMP_TO_EDGE;
|
---|
78 | int glWrapModeT = (mBoundaryModeT == REPEAT) ? GL_REPEAT : GL_CLAMP_TO_EDGE;
|
---|
79 |
|
---|
80 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, glWrapModeS);
|
---|
81 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, glWrapModeT);
|
---|
82 |
|
---|
83 | if (mUseMipMap)
|
---|
84 | {
|
---|
85 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
86 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
91 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (!mUseMipMap)
|
---|
95 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
|
---|
96 | else
|
---|
97 | gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
|
---|
98 |
|
---|
99 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | int Texture::GetFormat() const
|
---|
104 | {
|
---|
105 | return mFormat;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | int Texture::GetWidth() const
|
---|
110 | {
|
---|
111 | return mWidth;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | int Texture::GetHeight() const
|
---|
116 | {
|
---|
117 | return mHeight;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | void *Texture::GetImage() const
|
---|
122 | {
|
---|
123 | return mImage;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | Texture::~Texture()
|
---|
128 | {
|
---|
129 | glDeleteTextures(1, &mTexId);
|
---|
130 | free(mImage);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | std::string Texture::GetName() const
|
---|
135 | {
|
---|
136 | return mName;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | int Texture::GetRowLength() const
|
---|
141 | {
|
---|
142 | if (mFormat == FORMAT_RGBA)
|
---|
143 | return mWidth * 4 * sizeof (unsigned char);
|
---|
144 | else
|
---|
145 | return mWidth * 3 * sizeof (unsigned char);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | int Texture::GetPixelSize() const
|
---|
150 | {
|
---|
151 | if (mFormat == FORMAT_RGBA)
|
---|
152 | return 4 * sizeof(unsigned char);
|
---|
153 | else
|
---|
154 | return 3 * sizeof(unsigned char);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | void Texture::Bind() const
|
---|
159 | {
|
---|
160 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 | } |
---|