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

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