Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
78 jab 1
#include <assert.h>
2
#include <stdio.h>
3
#ifdef WIN32
4
#include <windows.h>
5
#include <io.h>
6
#endif
7
#include <string.h>
8
#include <stdlib.h>
9
 
10
#include <iostream>
11
#include <CGLA/Vec2i.h>
12
#include <CGLA/Vec2f.h>
13
#include "Mesh/Texmap.h"
14
#include <IL/il.h>
15
#include <IL/ilu.h>
16
#include <GL/gl.h>
17
 
18
using namespace std;
19
using namespace CGLA;
20
 
21
namespace Mesh
22
{
23
	Texmap::Texmap(): image(0), bpp(0), size_x(0), 
24
										size_y(0), load_size_x(0), load_size_y(0)
25
	{}
26
 
27
	Texmap::~Texmap() 
28
	{
29
	}
30
 
31
	Vec3f Texmap::get_texel(int u, int v) 
32
	{
33
		int bits = bpp/8;
34
		Vec3f value;
35
		value[0] = image[(u*size_x+v)*bits + 0];
36
		value[1] = image[(u*size_x+v)*bits + 1];
37
		value[2] = image[(u*size_x+v)*bits + 2];
38
		return value/256.0f;
39
	}
40
 
41
	bool Texmap::load(const std::string& _name)
42
	{
43
		image = 0;
44
		bpp = 0;
45
		size_x = 0;
46
		size_y = 0;
47
		load_size_x = 0;
48
		load_size_y = 0;
49
		name = _name;
50
 
51
		ILenum Error;
52
		ILuint  ImgId;
53
 
54
		static bool washere = false;
55
		if(!washere)
56
			{
57
				ilInit();
58
				iluInit();
59
				washere=true;
60
			}
61
		ilEnable(IL_CONV_PAL);
62
		ilEnable(IL_ORIGIN_SET);
63
		ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
64
		ilGenImages(1, &ImgId);
65
		ilBindImage(ImgId);
66
		char* name_cstr = const_cast<char*>(name.c_str());
67
		if(!ilLoadImage(name_cstr))
68
			{
69
				cout << "could not load <" << name  << ">" << endl;
70
				return false;
71
			}
72
 
73
		load_size_x = ilGetInteger(IL_IMAGE_WIDTH);
74
		load_size_y = ilGetInteger(IL_IMAGE_HEIGHT);
75
		bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
76
 
77
		if (bpp==24)
78
			ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
79
		else if (bpp==32)
80
			ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
81
		else if (bpp==8)
82
			ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
83
		else 
84
			assert(0);
85
 
86
		int i;
87
		for (i=2;i<=4096 ;i*=2) 
88
			if (i>=load_size_x) 
89
				break;
90
		size_x = i;
91
		for (i=2;i<=4096 ;i*=2) 
92
			if (i>=load_size_y) 
93
				break;
94
		size_y = i;
95
 
96
		if(size_x != load_size_x || size_y != load_size_y)
97
			{
98
				iluImageParameter(ILU_FILTER, ILU_BILINEAR);
99
				iluScale(size_x, size_y, 1);
100
			}
101
 
102
		const int image_size =size_x*size_y*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
103
		image = new unsigned char[image_size];
104
		memcpy(image, ilGetData(), image_size);
105
		ilDeleteImages(1, &ImgId);
106
 
107
		while ((Error = ilGetError())) {
108
			cout << __LINE__ << "Error: " << iluErrorString(Error) << endl;
109
			return false;
110
		}
111
		return true;
112
	}
113
 
114
	float log2(float val) 
115
	{
116
		return log(val)/log(2.0f);
117
	}
118
 
119
	void Texmap::gl_bind()
120
	{
121
    glBindTexture(GL_TEXTURE_2D, id);
122
	}
123
 
124
 
125
  void Texmap::gl_init()
126
  {
127
    GLint internalFormat=0;
128
    GLenum format=0;
129
 
130
    glGenTextures(1, &id);
131
    switch (bpp) 
132
      {
133
      case 8:
134
				internalFormat =  GL_LUMINANCE;
135
				format = GL_LUMINANCE;
136
				break;
137
      case 24:
138
				internalFormat =  GL_RGB;
139
				format = GL_RGB;
140
				break;
141
      case 32:
142
				internalFormat =  GL_RGBA;
143
				format = GL_RGBA;
144
				break;
145
      default:
146
				assert(0);
147
      }
148
 
149
    glBindTexture(GL_TEXTURE_2D, id);
150
    glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, size_x, size_y, 0, 
151
								 format, GL_UNSIGNED_BYTE, image); 
152
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
153
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
154
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
155
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
156
  }
157
 
158
}