summaryrefslogtreecommitdiff
path: root/common/texture.cpp
blob: 8c92e679a145e413a46830b3701158d468bb627b (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Texture object.
//

#include <string.h>
#include <stdlib.h>
#include "opengl.h"
#include "file.h"
#include "texture.h"
#include "project.h"
#include "globals.h"
#include "image.h"
#include "library.h"
#include "lc_application.h"

// =============================================================================
// Static functions

static void* ResizeImage (GLubyte* old_image, int components, int srcw, int srch, int destw, int desth)
{
  int i, j, k;
  float sx, sy;
  GLubyte* new_image;

  new_image = (GLubyte*)malloc (destw*desth*components*sizeof(GLubyte));
  if (new_image == NULL)
    return NULL;

  if (destw > 1)
    sx = (GLfloat) (srcw-1) / (GLfloat) (destw-1);
  else
    sx = (GLfloat) (srcw-1);
  if (desth > 1)
    sy = (GLfloat) (srch-1) / (GLfloat) (desth-1);
  else
    sy = (GLfloat) (srch-1);

  for (i = 0; i < desth; i++)
  {
    GLint ii = (GLint)(i * sy);
    for (j = 0; j < destw; j++)
    {
      GLint jj = (GLint)(j * sx);
      GLubyte *src = old_image + (ii * srcw + jj) * components;
      GLubyte *dst = new_image + (i * destw + j) * components;

      for (k = 0; k < components; k++)
	*dst++ = *src++;
    }
  }

  return new_image;
}

/////////////////////////////////////////////////////////////////////////////
// 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_nFormat = GL_LUMINANCE_ALPHA;
    m_nFileSize = m_nWidth*m_nHeight;
    break;
  case LC_RGB:
    m_nFormat = GL_RGB;
    m_nFileSize = m_nWidth*m_nHeight*3;
    break;
  case LC_RGBA:
    m_nFormat = GL_RGBA;
    m_nFileSize = m_nWidth*m_nHeight*4;
    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)
{
  char filename[LC_MAXPATH];
  FileDisk bin;
  void* bits;

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

  if (m_nFormat == GL_LUMINANCE_ALPHA)
    bits = malloc (m_nFileSize*2);
  else
    bits = malloc (m_nFileSize);

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

  FinishLoadImage (bFilter, bits);

  free(bits);
}

bool Texture::LoadFromFile (char* strFilename, bool bFilter)
{
  Image image;
  
  if (image.FileLoad (strFilename))
  {
    image.ResizePow2 ();

    m_nWidth = image.Width ();
    m_nHeight = image.Height ();

    if (image.Alpha ())
      m_nFormat = GL_RGBA;
    else
      m_nFormat = GL_RGB;

    if (FinishLoadImage (bFilter, image.GetData ()) == true)
      return true;
  }

  if (m_nID != 0)
  {
    glDeleteTextures(1, &m_nID);
    m_nID = 0;
  }

  m_nWidth = 0;
  m_nHeight = 0;
  m_nFileSize = 0;

  return false;
}

bool Texture::FinishLoadImage (bool bFilter, void *data)
{
  GLint w, h, level, maxsize;
  GLint i, j, k, pow2;
  GLint components;

  if (data == NULL || m_nWidth < 1 || m_nHeight < 1)
    return false;

  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);

  switch (m_nFormat)
  {
  case GL_LUMINANCE_ALPHA: components = 2; break;
  case GL_RGB: components = 3; break;
  case GL_RGBA: components = 4; break;
  default: return false;
  }

  // create an alpha channel for the texture 
  if (m_nFormat == GL_LUMINANCE_ALPHA)
    for (i = m_nWidth*m_nHeight-1; i >= 0; i--)
      ((GLubyte*)data)[i*2+1] = ((GLubyte*)data)[i*2] = ((GLubyte*)data)[i];

  glGetIntegerv (GL_MAX_TEXTURE_SIZE, &maxsize);

  for (pow2 = 1; pow2 < m_nWidth; pow2 = pow2 << 1);
  w = (pow2 == m_nWidth) ? m_nWidth : (pow2 << 1);
 
  for (pow2 = 1; pow2 < m_nHeight; pow2 = pow2 << 1);
  h = (pow2 == m_nHeight) ? m_nHeight : (pow2 << 1);

  if (w > maxsize) w = maxsize;
  if (h > maxsize) h = maxsize;

  if (w != m_nWidth || h != m_nHeight)
  {
    data = ResizeImage ((GLubyte*)data, components, m_nWidth, m_nHeight, w, h);
    m_nWidth = w;
    m_nHeight = h;
    if (data == NULL)
      return false;
  }
  else
  {
    void *tmp = malloc (w*h*components);
    memcpy (tmp, data, w*h*components);
    data = tmp;
  }

  glTexImage2D (GL_TEXTURE_2D, 0, components, w, h, 0, m_nFormat, GL_UNSIGNED_BYTE, data);

  if (bFilter)
    for (level = 1; ((w != 1) || (h != 1)); level++)
    {
      GLubyte *out, *in;
      int row;

      row = w * components;
      if (w != 1) w >>= 1;
      if (h != 1) h >>= 1;
      in = out = (GLubyte*)data;

      for (i = 0; i < h; i++, in+=row)
	for (j = 0; j < w; j++, out+=components, in+=2*components)
	  for (k = 0; k < components; k++)
	    out[k] = (in[k] + in[k+components] + in[row] + in[row+k+components])>>2;

      glTexImage2D (GL_TEXTURE_2D, level, components, w, h, 0, m_nFormat, GL_UNSIGNED_BYTE, data);
    }

  free (data);
  return true;
}