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

Revision 3261, 3.3 KB checked in by mattausch, 16 years ago (diff)

worked on powerplant loading

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