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