[go: up one dir, main page]

Menu

[f35cf5]: / media / Texture_Image.cc  Maximize  Restore  History

Download this file

311 lines (274 with data), 8.2 kB

  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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Texture_Image.cc - 2D textures from PNG images.
//
// Copyright (C) 2001--2005 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vamos is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vamos. If not, see <http://www.gnu.org/licenses/>.
#include "Texture_Image.h"
#include <cassert>
#include <stdio.h> // libpng uses FILE.
#include <png.h>
// Windows puts OpenGL 1.2 and 1.3 stuff in glext.h.
#if HAVE_GL_GLEXT_H
# include <GL/glext.h>
#endif
#include <GL/glu.h>
#include <iostream>
using namespace Vamos_Media;
struct Cached_Image
{
GLuint texure_name;
size_t width_pixels;
size_t height_pixels;
size_t reference_count;
Cached_Image (GLuint name = 0, size_t width = 0, size_t height = 0)
: texure_name (name),
width_pixels (width),
height_pixels (height),
reference_count (1)
{};
};
static std::map <std::string, Cached_Image> ms_image_cache;
Texture_Image::Texture_Image (std::string file_name,
bool smooth,
bool mip_map,
int texture_wrap)
: m_file_name (file_name),
m_width (1.0),
m_height (1.0)
{
initialize (smooth, mip_map, texture_wrap);
}
Texture_Image::Texture_Image (std::string file_name,
bool smooth,
bool mip_map,
double width,
double height,
int texture_wrap)
: m_file_name (file_name),
m_width (width),
m_height (height),
m_texture_name (0)
{
initialize (smooth, mip_map, texture_wrap);
}
void
Texture_Image::initialize (bool smooth, bool mip_map, int texture_wrap)
{
if (m_file_name.empty ())
return;
else if (ms_image_cache.find (m_file_name) != ms_image_cache.end ())
{
Cached_Image& image (ms_image_cache [m_file_name]);
m_texture_name = image.texure_name;
m_width_pixels = image.width_pixels;
m_height_pixels = image.height_pixels;
image.reference_count++;
activate ();
}
else
{
unsigned char* data = read_png_file (m_file_name);
GLuint texture_id;
glGenTextures (1, &texture_id);
glBindTexture (GL_TEXTURE_2D, texture_id);
set_gl_parameters (data, smooth, mip_map, texture_wrap);
m_texture_name = texture_id;
delete [] data;
ms_image_cache [m_file_name] =
Cached_Image (m_texture_name, m_width_pixels, m_height_pixels);
}
}
Texture_Image::~Texture_Image ()
{
if (ms_image_cache.find (m_file_name) != ms_image_cache.end ())
{
Cached_Image& image (ms_image_cache [m_file_name]);
if (--image.reference_count == 0)
{
glDeleteTextures (1, &m_texture_name);
ms_image_cache.erase (m_file_name);
}
}
}
unsigned char*
Texture_Image::read_png_file (std::string file_name)
{
// See if the file is a PNG file.
FILE *fp = fopen (file_name.c_str (), "rb");
if (!fp)
{
throw Missing_Texture_File (file_name);
}
png_byte header [8];
size_t bytes_read = fread (header, 1, 8, fp);
if (bytes_read != 8)
throw Missing_Texture_File (file_name);
bool is_png = !png_sig_cmp (header, 0, 8);
if (!is_png)
{
throw Missing_Texture_File (file_name);
}
// Initialize the structures.
png_structp png_ptr =
png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (png_ptr == 0)
{
throw Missing_Texture_File (file_name);
}
png_infop info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == 0)
{
png_destroy_read_struct (&png_ptr, 0, 0);
throw Missing_Texture_File (file_name);
}
png_infop end_info = png_create_info_struct (png_ptr);
if (end_info == 0)
{
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
throw Missing_Texture_File (file_name);
}
png_init_io (png_ptr, fp);
png_set_sig_bytes (png_ptr, 8);
png_read_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, 0);
fclose (fp);
png_bytep* row_pointers = png_get_rows (png_ptr, info_ptr);
m_width_pixels = png_get_image_width (png_ptr, info_ptr);
m_height_pixels = png_get_image_height (png_ptr, info_ptr);
m_channels = png_get_channels (png_ptr, info_ptr);
int row_size = m_width_pixels * m_channels;
size_t data_size = row_size * m_height_pixels;
unsigned char* data = new unsigned char [data_size];
for (int i = 0; i < m_height_pixels; i++)
{
for (int j = 0; j < row_size; j++)
{
data [i * row_size + j] = row_pointers [i][j];
}
}
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
return data;
}
void
Texture_Image::set_gl_parameters (unsigned char* data,
bool smooth,
bool mip_map,
int texture_wrap)
{
assert (data != 0);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texture_wrap);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texture_wrap);
GLint format = 0;
switch (m_channels)
{
case 1:
format = GL_LUMINANCE;
break;
case 3:
format = GL_RGB;
break;
case 4:
format = GL_RGBA;
break;
default:
assert (false);
}
if (mip_map)
{
if (smooth)
{
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
}
else
{
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST_MIPMAP_NEAREST);
}
gluBuild2DMipmaps (GL_TEXTURE_2D, format,
m_width_pixels, m_height_pixels,
format, GL_UNSIGNED_BYTE,
data);
}
else
{
if (smooth)
{
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
else
{
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
glTexImage2D (GL_TEXTURE_2D, 0, format,
m_width_pixels, m_height_pixels,
0, format, GL_UNSIGNED_BYTE,
data);
}
}
void
Texture_Image::activate () const
{
glBindTexture (GL_TEXTURE_2D, m_texture_name);
}
//* Class Facade
Facade::Facade (std::string image_name, bool draw_back)
: Texture_Image (image_name, true, true, 1.0, 1.0, GL_CLAMP_TO_EDGE),
m_draw_back (draw_back),
m_x_offset (0.0),
m_y_offset (0.0),
m_z_offset (0.0)
{
}
void
Facade::set_radius (double radius)
{
set_width (2.0 * radius * aspect_ratio ());
set_height (2.0 * radius);
m_x_offset = -width () / 2.0;
m_y_offset = -height () / 2.0;
}
void
Facade::draw () const
{
activate ();
glColor3d (1.0, 1.0, 1.0);
glEnable (GL_CULL_FACE);
glBegin (GL_QUADS);
glNormal3f (0.0, 0.0, 1.0);
glTexCoord2d (0.0, 1.0);
glVertex3d (m_x_offset, m_y_offset, m_z_offset);
glTexCoord2d (1.0, 1.0);
glVertex3d (m_x_offset + width (), m_y_offset, m_z_offset);
glTexCoord2d (1.0, 0.0);
glVertex3d (m_x_offset + width (), m_y_offset + height (), m_z_offset);
glTexCoord2d (0.0, 0.0);
glVertex3d (m_x_offset, m_y_offset + height (), m_z_offset);
if (m_draw_back)
{
glNormal3f (0.0, 0.0, -1.0);
glVertex3d (m_x_offset, m_y_offset, m_z_offset);
glVertex3d (m_x_offset, m_y_offset + height (), m_z_offset);
glVertex3d (m_x_offset + width (), m_y_offset + height (), m_z_offset);
glVertex3d (m_x_offset + width (), m_y_offset, m_z_offset);
}
glEnd ();
glDisable (GL_CULL_FACE);
glBindTexture (GL_TEXTURE_2D, 0);
}