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

Revision 2817, 2.8 KB checked in by mattausch, 16 years ago (diff)
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), 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
60void Texture::Create()
61{
62        glEnable(GL_TEXTURE_2D);
63        glGenTextures(1, &mTexId);
64        glBindTexture(GL_TEXTURE_2D, mTexId);
65        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
66        //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
67        //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
68       
69        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
70        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
71
72        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
73        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
74
75        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
76        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
77
78        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
79        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
80}
81
82
83int Texture::GetFormat() const
84{
85        return mFormat;
86}
87
88
89int Texture::GetWidth() const
90{
91        return mWidth;
92}
93
94
95int Texture::GetHeight() const
96{
97        return mHeight;
98}
99
100
101void *Texture::GetImage() const
102{
103        return mImage;
104}
105
106
107Texture::~Texture()
108{
109        glDeleteTextures(1, &mTexId);
110        free(mImage);
111}
112
113
114std::string Texture::GetName() const
115{
116        return mName;
117}
118
119
120int Texture::GetRowLength() const
121{
122        if (mFormat == FORMAT_RGBA)
123                return mWidth * 4 * sizeof (unsigned char);
124        else
125                return mWidth * 3 * sizeof (unsigned char);
126}
127
128
129int Texture::GetPixelSize() const
130{
131        if (mFormat == FORMAT_RGBA)
132                return 4 * sizeof(unsigned char);
133        else
134                return 3 * sizeof(unsigned char);
135}
136
137
138void Texture::Bind() const
139{
140        glBindTexture(GL_TEXTURE_2D, mTexId);
141}
142
143}
Note: See TracBrowser for help on using the repository browser.