Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 5802edf655275e808bdd45111f8e40a8a7a7a3e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
	Ronny Andr�Reierstad
	http://www.morrowland.com
	http://www.morrowland.com/apron/tut_gl.php
	apron@morrowland.com
*/

#include "filesystem/filesystem.h"
#include "render/tga.h"
#include "sys/sys.h"
#include "GL/gl.h"
#include <GL/glu.h>

namespace render
{

/////////////////////////////////////////////////////////////////////////////////////////////////
//										TGA TEXTURE LOADER
/////////////////////////////////////////////////////////////////////////////////////////////////
bool TGA::texture(GLuint textureArray[], const char *filename, int ID)
{
	if (!filename)
		return false;
		
	image *pBitMap = load(filename);
	if (!pBitMap) {
		con_warn << "Could not load " << filename << std::endl;
		return false;
	}
	
	glGenTextures(1, &textureArray[ID]);
	glBindTexture(GL_TEXTURE_2D, textureArray[ID]);
	int textureType = GL_RGB;
	if (pBitMap->channels == 4)	textureType = GL_RGBA;
	gluBuild2DMipmaps(GL_TEXTURE_2D, pBitMap->channels, pBitMap->size_x, pBitMap->size_y, textureType, GL_UNSIGNED_BYTE, pBitMap->data);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
	
	if (pBitMap) {
		if (pBitMap->data) {
			free(pBitMap->data);
		}
		free(pBitMap);
	}

	return true;
}


TGA::image *TGA::load(const char *filename)
{
	image *pImgData			= NULL;
	//FILE *pFile			= NULL;
	GLushort width			= 0;
	GLushort height			= 0;
	GLubyte length			= 0;
	GLubyte imgType			= 0;
	GLubyte bits			= 0;
	GLushort channels		= 0;
	GLushort stride			= 0;
	
	filesystem::File *f = filesystem::open(filename);
	if (!f)
		return 0;
		
	pImgData = (image*)malloc(sizeof(image));
	
	f->read((void *)&length, sizeof(GLubyte));
	f->skip(1);
	f->read((void *)&imgType, sizeof(GLubyte));
	f->skip(9);
	f->read((void *)&width,  sizeof(GLushort));
	f->read((void *)&height, sizeof(GLushort));
	f->read((void *)&bits,   sizeof(GLubyte));
	f->skip(length +1);
	
	con_debug << "  " << filename << " " << width << "x" << height << "x" << (int) bits << "bpp" << std::endl;
	
	if (imgType != TGA_RLE) {
		// Check for 24 or 32 Bit
		if (bits == 24 || bits == 32) {
		
			channels = bits / 8;
			stride = channels * width;
			pImgData->data = new unsigned char[stride * height];
			
			for (GLushort y = 0; y < height; y++) {
				unsigned char *pLine = &(pImgData->data[stride * y]);
				
				f->read((void *)pLine, stride);
				
				for (GLushort i = 0; i < stride; i += channels) {
					int temp     = pLine[i];
					pLine[i]     = pLine[i + 2];
					pLine[i + 2] = temp;
				}
			}
		}
		
		// Check for 16 Bit
		else if (bits == 16) {
			unsigned short pixels = 0;
			int r=0, g=0, b=0;
			
			channels = 3;
			stride = channels * width;
			pImgData->data = new unsigned char[stride * height];
			
			for (int i = 0; i < width*height; i++) {
				f->read((void *)&pixels, sizeof(GLushort));
				
				b = (pixels & 0x1f) << 3;
				g = ((pixels >> 5) & 0x1f) << 3;
				r = ((pixels >> 10) & 0x1f) << 3;
				
				pImgData->data[i * 3 + 0] = r;
				pImgData->data[i * 3 + 1] = g;
				pImgData->data[i * 3 + 2] = b;
			}
		} else
			return NULL;
	} else {
		GLubyte rleID = 0;
		int colorsRead = 0;
		channels = bits / 8;
		stride = channels * width;
		
		pImgData->data = new unsigned char[stride * height];
		GLubyte *pColors = new GLubyte [channels];
		
		int i = 0;
		while (i < width*height) {
		
			f->read((void *)&rleID, sizeof(GLubyte));
			
			
			if (rleID < 128)	{
				rleID++;
				
				while (rleID) {
					f->read((void *)pColors, sizeof(GLubyte) * channels);
					
					pImgData->data[colorsRead + 0] = pColors[2];
					pImgData->data[colorsRead + 1] = pColors[1];
					pImgData->data[colorsRead + 2] = pColors[0];
					
					if (bits == 32)	pImgData->data[colorsRead + 3] = pColors[3];
					
					i++;
					rleID--;
					colorsRead += channels;
				}
			} else {
				rleID -= 127;
				
				f->read((void *)pColors, sizeof(GLubyte) * channels);
				
				while (rleID) {
					pImgData->data[colorsRead + 0] = pColors[2];
					pImgData->data[colorsRead + 1] = pColors[1];
					pImgData->data[colorsRead + 2] = pColors[0];
					
					if (bits == 32)	pImgData->data[colorsRead + 3] = pColors[3];
					
					i++;
					rleID--;
					colorsRead += channels;
				}
			}
		}
		delete pColors;
	}
	
	filesystem::close(f);
	
	
	pImgData->channels  = channels;
	pImgData->size_x    = width;
	pImgData->size_y    = height;
	
	return pImgData;
}

}