summaryrefslogtreecommitdiff
path: root/common/texture.cpp
blob: 066fa6a224c51fc369965d902fd0791500326bc2 (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
// Texture object.
//

#ifdef LC_WINDOWS
#include "stdafx.h"
#endif
#include "GL/glu.h"
#include <string.h>
#include <stdlib.h>
#include "file.h"
#include "texture.h"
#include "project.h"
#include "globals.h"
#include "image.h"

/////////////////////////////////////////////////////////////////////////////
// Texture construction/destruction

// Only called for the background image, use LoadIndex()
Texture::Texture()
{
	m_nRef = 1;
	m_nID = 0;
}

Texture::~Texture()
{
}

/////////////////////////////////////////////////////////////////////////////
// Texture attributes

void Texture::AddRef(bool bFilter)
{
	if (m_nRef == 0)
		Load(bFilter);

	m_nRef++;
}

void Texture::DeRef()
{
	m_nRef--;
	if (m_nRef == 0)
		Unload();
}

/////////////////////////////////////////////////////////////////////////////
// Load methods

void Texture::LoadIndex(File* idx)
{
	unsigned char bt;

	// TODO: don't change ref. if reloading
	m_nRef = 0;
	m_nID = 0;

	idx->Read(m_strName, 8);
	idx->ReadShort(&m_nWidth, 1);
	idx->ReadShort(&m_nHeight, 1);
	idx->ReadByte(&bt, 1);

	switch (bt)
	{
	case LC_INTENSITY: m_nType = GL_LUMINANCE; break;
	case LC_RGB: m_nType = GL_RGB; break;
	case LC_RGBA: m_nType = GL_RGBA; break;
	}

	idx->ReadLong(&m_nOffset, 1);
}

void Texture::Unload()
{
	if (m_nID != 0)
		glDeleteTextures(1, &m_nID);
	m_nID = 0;
}

// Load from textures.bin file
void Texture::Load(bool bFilter)
{
	unsigned char* bits;
	char filename[LC_MAXPATH];
	FileDisk bin;
	int size;

	strcpy(filename, project->GetLibraryPath());
	strcat(filename, "textures.bin");
	if (!bin.Open(filename, "rb"))
		return;

	size = m_nWidth*m_nHeight;
	if (m_nType == GL_RGB)
		size *= 3;
	if (m_nType == GL_RGBA)
		size *= 4;
	bits = (unsigned char*)malloc(size);

	bin.Seek(m_nOffset, SEEK_SET);
	bin.Read(bits, size);
	bin.Close();

	if (m_nID == 0)
		glGenTextures(1, &m_nID);

	glBindTexture(GL_TEXTURE_2D, m_nID);
	glDisable(GL_TEXTURE_GEN_S);
	glDisable(GL_TEXTURE_GEN_T);
//	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	if (m_nType == GL_LUMINANCE)
	{
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY4, m_nWidth, m_nHeight,
			0, m_nType, GL_UNSIGNED_BYTE, bits);
	}
	else
	{
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, bFilter ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, bFilter ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, m_nWidth, m_nHeight,
			0, m_nType, GL_UNSIGNED_BYTE, bits);

		if (bFilter)
			gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB5_A1, 
				m_nWidth, m_nHeight, m_nType, GL_UNSIGNED_BYTE, bits);
	}

	free(bits);
}

bool Texture::LoadFromFile(char* strFilename, bool bFilter)
{
	LC_IMAGE* image = OpenImage(strFilename);

	if (image == NULL)
	{
		if (m_nID != 0)
		{
			glDeleteTextures(1, &m_nID);
			m_nID = 0;
		}
		m_nWidth = 0;
		m_nHeight = 0;

		return false;
	}

	m_nWidth = image->width;
	m_nHeight = image->height;
	m_nType = GL_RGB;

	if (m_nID == 0)
		glGenTextures(1, &m_nID);

	glBindTexture(GL_TEXTURE_2D, m_nID);
	glDisable(GL_TEXTURE_GEN_S);
	glDisable(GL_TEXTURE_GEN_T);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, bFilter ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, bFilter ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, m_nWidth, m_nHeight,
		0, m_nType, GL_UNSIGNED_BYTE, image->bits);

	if (bFilter)
		gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 
			m_nWidth, m_nHeight, m_nType, GL_UNSIGNED_BYTE, image->bits);

	free(image);

	return true;
}