Subversion Repositories gelsvn

Rev

Rev 79 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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