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

Revision 2800, 2.7 KB checked in by mattausch, 16 years ago (diff)

friendly culling debug version with timers, no materials

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