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

Revision 2850, 3.0 KB checked in by mattausch, 16 years ago (diff)

debug version before siggraph

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