-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.cpp
397 lines (338 loc) · 14 KB
/
image.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "image.h"
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <SDL/SDL_image.h>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include "window.h"
using namespace std;
Image::Image()
{
alias = "";
}
void Image::setAlias(string name)
{
alias = name;
}
void Image::setTexture(GLuint image)
{
glTexture = image;
}
string Image::getAlias()
{
return alias;
}
GLuint* Image::getTexture()
{
return &glTexture;
}
int power_of_two(int input)
{
int value = 1;
while ( value < input )
{
value <<= 1;
}
return value;
}
void SDL_GL_LoadTexture(SDL_Surface* surface, Image* texture)
{
int w, h;
SDL_Surface *image;
SDL_Rect area;
Uint32 saved_flags;
Uint8 saved_alpha;
w = power_of_two(surface->w);
h = power_of_two(surface->h);
image = SDL_CreateRGBSurface(
SDL_SWSURFACE,
w, h,
32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN //OpenGL RGBA masks
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);
if ( image == NULL )
{
return;
}
//Save the alpha blending attributes that were on the SDL surface
saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
saved_alpha = surface->format->alpha;
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
{
SDL_SetAlpha(surface, 0, 0);
}
//Copy the surface data to be placed in the opengl texture
area.x = 0;
area.y = 0;
area.w = surface->w;
area.h = surface->h;
SDL_BlitSurface(surface, &area, image, &area);
//Restore the alpha blending attributes into the new opengl texture
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
SDL_SetAlpha(surface, saved_flags, saved_alpha);
}
//image = SDL_DisplayFormatAlpha(image);
//Create an OpenGL texture to hold the image
GLuint tempTexture;
glGenTextures(1, (texture->getTexture()));
glBindTexture(GL_TEXTURE_2D, *(texture->getTexture()));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
w, h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
surface->pixels);
SDL_FreeSurface(image); //Free the data on the SDL surface
// texture->setTexture(tempTexture);
}
void LoadImage(Image* image, string filename)
{
SDL_Surface* surface; //Create an SDL to load the image on for converting.
surface = IMG_Load(filename.c_str()); //Load the image onto the surface.
//SDL_LockSurface( surface );
//Convert the SDL surface that has the image into an OpenGL texture.
SDL_GL_LoadTexture(surface, image);
//SDL_UnlockSurface( surface );
SDL_FreeSurface( surface ); //Free the memory being used with the surface.
// *NOTE* - SDL loads images upsidedown, remember to draw them correctly
// when you draw them or else they will come out upside down.
}
void RenderImage(Image image, double left_x, double right_x, double lower_y,
double upper_y)
{
//Taking the note from the LoadImage function, we understand that SDL
//surface load textures upside down. We need to adjust this by creating new
//variables that reverse the y points so the image is drawn correctly.
double dX1 = left_x;
double dX2 = right_x;
double dY1 = upper_y;
double dY2 = lower_y;
//Enable transparency and ready opengl for image rendering prior to actually
//rendering.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *(image.getTexture()));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//Map the parts of the image to the given coordinates and render it.
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(dX1, dY1); // Bottom left
glTexCoord2f(1.0, 0.0); glVertex2f(dX2, dY1); // Bottom right
glTexCoord2f(1.0, 1.0); glVertex2f(dX2, dY2); // Top right
glTexCoord2f(0.0, 1.0); glVertex2f(dX1, dY2); // Top left
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
void RenderImage(Image image, double angle, double left_x, double right_x, double lower_y,
double upper_y)
{
//Taking the note from the LoadImage function, we understand that SDL
//surface load textures upside down. We need to adjust this by creating new
//variables that reverse the y points so the image is drawn correctly.
double dX1 = - (right_x - left_x) / 2.0;
double dX2 = (right_x - left_x) / 2.0;
double dY1 = (upper_y - lower_y) / 2.0;
double dY2 = - (upper_y - lower_y) / 2.0;
//Enable transparency and ready opengl for image rendering prior to actually
//rendering.
glLoadIdentity();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glLoadIdentity();
glTranslated((left_x + right_x) / 2.0, (upper_y + lower_y) / 2.0, 0);
glRotated(angle, 0, 0, 1);
glBindTexture(GL_TEXTURE_2D, *(image.getTexture()));
//Map the parts of the image to the given coordinates and render it.
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(dX1, dY1); // Bottom left
glTexCoord2f(1.0f, 0.0f); glVertex2f(dX2, dY1); // Bottom right
glTexCoord2f(1.0f, 1.0f); glVertex2f(dX2, dY2); // Top right
glTexCoord2f(0.0f, 1.0f); glVertex2f(dX1, dY2); // Top left
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glLoadIdentity();
}
////////////////////////////////////////////////////////////////////////////////
// Function: RenderImage
//
// Last Modified by: Roby Atadero
//
// Last Modified: Oct 13, 2007
//
// Function: Takes a GLuint image mapping and opengl coordinates and
// image coordinates and maps the image coordinates part of
// the image and places it on the opengl coordinates part
// of the screen.
//
// Inputs: GLuint& image - The variable that will hold the
// mapped out image data to be used
// for output.
// double left_x - The left x of the image to be
// drawn in opengl.
// double right_x - The right x of the image to be
// drawn in opengl.
// double lower_y - The lower y of the image to be
// drawn in opengl.
// double upper_y - The upper y of the image to be
// drawn in opengl.
// double left_xTexCoord_ - The left x part of the image that
// we start getting info from.
// double right_xTexCoord_ - The right x part of the image
// that we stop getting info from.
// double lower_yTexCoord_ - The lower y part of the image
// that we start getting info from.
// double upper_yTexCoord_ - The upper y part of the image
// that we stop getting info from.
//
// Outputs: Draws the desired part of the image to the screen that
// spans the given coordinates.
//
// Returns: None.
////////////////////////////////////////////////////////////////////////////////
void RenderImage(Image image, double left_x, double right_x, double lower_y,
double upper_y, double left_xTexCoord, double right_xTexCoord,
double lower_yTexCoord, double upper_yTexCoord)
{
//Taking the note from the LoadImage function, we understand that SDL
//surface load textures upside down. We need to adjust this by creating new
//variables that reverse the y points so the image is drawn correctly.
double dX1 = left_x;
double dX2 = right_x;
double dY1 = upper_y;
double dY2 = lower_y;
//Enable transparency and ready opengl for image rendering prior to actually
//rendering.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *(image.getTexture()));
if(left_xTexCoord < 0 || right_xTexCoord > 1 || upper_yTexCoord > 1 || lower_yTexCoord < 0)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
glBegin(GL_QUADS);
//Bottom left
glTexCoord2f(left_xTexCoord, lower_yTexCoord); glVertex2f(dX1, dY1);
//Bottom right
glTexCoord2f(right_xTexCoord, lower_yTexCoord); glVertex2f(dX2, dY1);
//Top Right
glTexCoord2f(right_xTexCoord, upper_yTexCoord); glVertex2f(dX2, dY2);
//Top left
glTexCoord2f(left_xTexCoord, upper_yTexCoord); glVertex2f(dX1, dY2);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
////////////////////////////////////////////////////////////////////////////////
// Function: RenderImage
//
// Last Modified by: Roby Atadero
//
// Last Modified: Oct 13, 2007
//
// Function: Takes a GLuint image mapping and opengl coordinates and
// image coordinates and maps the image coordinates part of
// the image and places it on the opengl coordinates part
// of the screen rotated by angle degrees.
//
// Inputs: GLuint& image - The variable that will hold the
// mapped out image data to be used
// for output.
// double angle - The angle to rotate the image in
// degrees.
// double left_x - The left x of the image to be
// drawn in opengl.
// double right_x - The right x of the image to be
// drawn in opengl.
// double lower_y - The lower y of the image to be
// drawn in opengl.
// double upper_y - The upper y of the image to be
// drawn in opengl.
// double left_xTexCoord_ - The left x part of the image that
// we start getting info from.
// double right_xTexCoord_ - The right x part of the image
// that we stop getting info from.
// double lower_yTexCoord_ - The lower y part of the image
// that we start getting info from.
// double upper_yTexCoord_ - The upper y part of the image
// that we stop getting info from.
//
// Outputs: Draws the desired part of the image to the screen that
// spans the given coordinates.
//
// Returns: None.
////////////////////////////////////////////////////////////////////////////////
void RenderImage(Image image, double angle, double left_x, double right_x, double lower_y,
double upper_y, double left_xTexCoord, double right_xTexCoord,
double lower_yTexCoord, double upper_yTexCoord)
{
//Taking the note from the LoadImage function, we understand that SDL
//surface load textures upside down. We need to adjust this by creating new
//variables that reverse the y points so the image is drawn correctly.
double dX1 = - (right_x - left_x) / 2.0;
double dX2 = (right_x - left_x) / 2.0;
double dY1 = (upper_y - lower_y) / 2.0;
double dY2 = - (upper_y - lower_y) / 2.0;
//Enable transparency and ready opengl for image rendering prior to actually
//rendering.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
//glBindTexture(GL_TEXTURE_2D, *(image.getTexture()));
glLoadIdentity();
glTranslated((left_x + right_x) / 2.0, (upper_y + lower_y) / 2.0, 0);
glRotated(angle, 0, 0, 1);
glBindTexture(GL_TEXTURE_2D, *(image.getTexture()));
if(left_xTexCoord < 0 || right_xTexCoord > 1 || upper_yTexCoord > 1 || lower_yTexCoord < 0)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
glBegin(GL_QUADS);
//Bottom left
glTexCoord2f(left_xTexCoord, lower_yTexCoord); glVertex2f(dX1, dY1);
//Bottom right
glTexCoord2f(right_xTexCoord, lower_yTexCoord); glVertex2f(dX2, dY1);
//Top Right
glTexCoord2f(right_xTexCoord, upper_yTexCoord); glVertex2f(dX2, dY2);
//Top left
glTexCoord2f(left_xTexCoord, upper_yTexCoord); glVertex2f(dX1, dY2);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glLoadIdentity();
}