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

Revision 2769, 2.7 KB checked in by mattausch, 17 years ago (diff)
Line 
1#include "Texture.h"
2#include "glInterface.h"
3#include "common.h"
4
5
6#include <IL/il.h>
7#include <assert.h>
8#include <iostream>
9
10
11namespace CHCDemo
12{
13
14using namespace std;
15
16
17void startil()
18{
19        ilInit();
20        assert(ilGetError() == IL_NO_ERROR);
21}
22
23
24void stopil()
25{
26        ilShutDown();
27        assert(ilGetError() == IL_NO_ERROR);
28}
29
30
31Texture::Texture(const std::string &filename):
32mWidth(0), mHeight(0), mFormat(0), mImage(NULL), mTexId(-1), mName(filename)
33{
34        startil();
35       
36        if (!ilLoadImage(ILstring(filename.c_str())))
37        {
38                cerr << "Image load error " << ilGetError() << ": " << filename << endl;
39        }
40        else
41        {
42                mWidth = ilGetInteger(IL_IMAGE_WIDTH);
43                mHeight = ilGetInteger(IL_IMAGE_HEIGHT);
44
45                mFormat = FORMAT_RGBA;
46                mByteSize = mHeight * GetRowLength();
47                mImage = malloc(GetByteSize());
48
49                Debug << "successfully loaded texture " << filename << " w " << mWidth << " h " << mHeight << endl;
50
51                ilCopyPixels(0, 0, 0, mWidth, mHeight, 1, IL_RGBA, IL_UNSIGNED_BYTE, mImage);
52                assert(ilGetError() == IL_NO_ERROR);
53        }
54       
55        stopil();
56
57        Create();
58}
59
60
61void Texture::Create()
62{
63        glEnable(GL_TEXTURE_2D);
64        glGenTextures(1, &mTexId);
65        glBindTexture(GL_TEXTURE_2D, mTexId);
66        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
67        //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
68        //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
69       
70        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
72
73        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
74        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
75
76        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
77        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
78
79        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
80        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
81}
82
83
84int Texture::GetFormat() const
85{
86        return mFormat;
87}
88
89
90int Texture::GetWidth() const
91{
92        return mWidth;
93}
94
95
96int Texture::GetHeight() const
97{
98        return mHeight;
99}
100
101
102void *Texture::GetImage() const
103{
104        return mImage;
105}
106
107
108Texture::~Texture()
109{
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.