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