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

Revision 2845, 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
64        glGenTextures(1, &mTexId);
65        glBindTexture(GL_TEXTURE_2D, mTexId);
66       
67        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
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_CLAMP_TO_EDGE);
76        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
77
78        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
79        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
80
81        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
82        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
83
84        glBindTexture(GL_TEXTURE_2D, 0);
85}
86
87
88int Texture::GetFormat() const
89{
90        return mFormat;
91}
92
93
94int Texture::GetWidth() const
95{
96        return mWidth;
97}
98
99
100int Texture::GetHeight() const
101{
102        return mHeight;
103}
104
105
106void *Texture::GetImage() const
107{
108        return mImage;
109}
110
111
112Texture::~Texture()
113{
114        glDeleteTextures(1, &mTexId);
115        free(mImage);
116}
117
118
119std::string Texture::GetName() const
120{
121        return mName;
122}
123
124
125int Texture::GetRowLength() const
126{
127        if (mFormat == FORMAT_RGBA)
128                return mWidth * 4 * sizeof (unsigned char);
129        else
130                return mWidth * 3 * sizeof (unsigned char);
131}
132
133
134int Texture::GetPixelSize() const
135{
136        if (mFormat == FORMAT_RGBA)
137                return 4 * sizeof(unsigned char);
138        else
139                return 3 * sizeof(unsigned char);
140}
141
142
143void Texture::Bind() const
144{
145        glBindTexture(GL_TEXTURE_2D, mTexId);
146}
147
148}
Note: See TracBrowser for help on using the repository browser.