source: GTP/trunk/App/Demos/Vis/CHC_revisited/Texture.cpp @ 2764

Revision 2764, 2.6 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "Texture.h"
2#include <IL/il.h>
3#include "glInterface.h"
4#include <assert.h>
5#include <iostream>
6
7
8namespace CHCDemo
9{
10
11using namespace std;
12
13
14void startil()
15{
16        ilInit();
17        assert(ilGetError() == IL_NO_ERROR);
18}
19
20
21void stopil()
22{
23        ilShutDown();
24        assert(ilGetError() == IL_NO_ERROR);
25}
26
27
28Texture::Texture(const std::string &filename):
29mWidth(0), mHeight(0), mFormat(0), mImage(NULL), mTexId(-1), mName(filename)
30{
31        startil();
32       
33        if (!ilLoadImage(ILstring(filename.c_str())))
34        {
35                cerr << "Image load error " << ilGetError() << ": " << filename << endl;
36        }
37        else
38        {
39                mWidth = ilGetInteger(IL_IMAGE_WIDTH);
40                mHeight = ilGetInteger(IL_IMAGE_HEIGHT);
41
42                mFormat = FORMAT_RGBA;
43                mImage = malloc(GetByteSize());
44
45                cout << "successfully loaded texture " << filename << endl;
46                ilCopyPixels(0, 0, 0, mWidth, mHeight, 1, IL_RGBA, IL_UNSIGNED_BYTE, mImage);
47                assert(ilGetError() == IL_NO_ERROR);
48        }
49       
50        stopil();
51
52        Create();
53}
54
55
56void Texture::Create()
57{
58        glEnable(GL_TEXTURE_2D);
59        glGenTextures(1, &mTexId);
60        glBindTexture(GL_TEXTURE_2D, mTexId);
61        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
62        //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
63        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
64        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
65        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
66        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
67        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
68
69        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, mWidth, mHeight,
70                                          GL_RGBA, GL_UNSIGNED_BYTE, mImage);
71
72        //glTexImage2D(GL_TEXTURE_2D, 0, 3, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
73}
74
75
76int Texture::GetFormat() const
77{
78        return mFormat;
79}
80
81
82int Texture::GetWidth() const
83{
84        return mWidth;
85}
86
87
88int Texture::GetHeight() const
89{
90        return mHeight;
91}
92
93
94void *Texture::GetImage() const
95{
96        return mImage;
97}
98
99
100Texture::~Texture()
101{
102        free(mImage);
103}
104
105
106std::string Texture::GetName() const
107{
108        return mName;
109}
110
111
112int Texture::GetRowLength() const
113{
114        if (mFormat == FORMAT_RGBA)
115                return mWidth * 4 * sizeof (unsigned char);
116        else
117                return mWidth * 3 * sizeof (unsigned char);
118}
119
120
121int Texture::GetPixelSize() const
122{
123        if (mFormat == FORMAT_RGBA)
124                return 4 * sizeof(unsigned char);
125        else
126                return 3 * sizeof(unsigned char);
127}
128
129
130int Texture::GetByteSize() const
131{
132        return mHeight * GetRowLength();
133}
134
135
136void Texture::Bind() const
137{
138        glBindTexture(GL_TEXTURE_2D, mTexId);
139}
140
141}
Note: See TracBrowser for help on using the repository browser.