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