source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Texture.cpp @ 3214

Revision 3214, 3.2 KB checked in by mattausch, 16 years ago (diff)

worked on lense flare

RevLine 
[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]10namespace CHCDemoEngine
[2757]11{
12
13using namespace std;
14
15
16void startil()
17{
18        ilInit();
19        assert(ilGetError() == IL_NO_ERROR);
20}
21
22
23void stopil()
24{
25        ilShutDown();
26        assert(ilGetError() == IL_NO_ERROR);
27}
28
29
30Texture::Texture(const std::string &filename):
[2846]31mWidth(0),
32mHeight(0),
33mFormat(0),
34mImage(NULL),
35mTexId(-1),
36mName(filename),
37mBoundaryModeS(REPEAT),
[2850]38mBoundaryModeT(REPEAT),
39mUseMipMap(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]66static 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]80void 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
114int Texture::GetFormat() const
115{
116        return mFormat;
117}
118
119
120int Texture::GetWidth() const
121{
122        return mWidth;
123}
124
125
126int Texture::GetHeight() const
127{
128        return mHeight;
129}
130
131
132void *Texture::GetImage() const
133{
134        return mImage;
135}
136
137
138Texture::~Texture()
139{
[2817]140        glDeleteTextures(1, &mTexId);
[2757]141        free(mImage);
142}
143
144
145std::string Texture::GetName() const
146{
147        return mName;
148}
149
150
151int 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
160int 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
169void Texture::Bind() const
170{
171        glBindTexture(GL_TEXTURE_2D, mTexId);
172}
173
[2850]174
175
176
[2757]177}
Note: See TracBrowser for help on using the repository browser.