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