Repository navigation
Expand file tree
/
Copy pathShaderManager.cpp
More file actions
627 lines (475 loc) · 19.3 KB
/
Copy pathShaderManager.cpp
File metadata and controls
627 lines (475 loc) · 19.3 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// +------------------------------------------------------------+
// | University Racer |
// | Projekt do PGR a GMU, FIT VUT v Brne, 2011 |
// +------------------------------------------------------------+
// | Autori: Tomáš Kimer, xkimer00@stud.fit.vutbr.cz |
// | Tomáš Sychra, xsychr03@stud.fit.vutbr.cz |
// | David Šabata, xsabat01@stud.fit.vutbr.cz |
// +------------------------------------------------------------+
#include "ShaderManager.h"
#ifdef _DEBUG
#define new MYDEBUG_NEW
#endif
using namespace std;
#define DEFAULT_PROGRAM "diffuse"
#define SHADERS_PATH "materials/"
#define TEXTURES_PATH "materials/textures/"
// init statickych promennych
ShaderManager::PROGRAMBINDING ShaderManager::currentProgram;
map<string, ShaderManager::PROGRAMBINDING> ShaderManager::programs = map<string, ShaderManager::PROGRAMBINDING>();
map<string, GLuint> ShaderManager::textures = map<string, GLuint>();
GLenum ShaderManager::activeTexture = GL_NONE;
vector<string> ShaderManager::shadersToLoad = vector<string>();
map<string, string> ShaderManager::shaderSubstitutions;
void ShaderManager::loadPrograms()
{
for (vector<string>::iterator it = shadersToLoad.begin(); it != shadersToLoad.end(); it++)
{
if (!loadProgram(*it)) {
cerr << "Warning: Shader for material '" << (*it) << "' not found" << endl;
}
}
// fallback, musi byt dostupny vzdy
loadDefaultProgram();
}
bool ShaderManager::loadProgram(string material)
{
// pokud jiz program existuje, neni treba jej nacitat znovu
if (programs.find(material) != programs.end())
return true;
PROGRAMBINDING mat;
// VS
string vsPath = SHADERS_PATH + material + ".vert";
string vsSource;
try { vsSource = loadFile(vsPath.c_str()); }
catch (String_Exception e) { return false; }
GLuint vs = compileShader(GL_VERTEX_SHADER, vsSource.c_str());
// FS
string fsPath = SHADERS_PATH + material + ".frag";
string fsSource;
try { fsSource = loadFile(fsPath.c_str()); }
catch (String_Exception e) { return false; }
GLuint fs = compileShader(GL_FRAGMENT_SHADER, fsSource.c_str());
// Program
mat.program = linkShader(2, vs, fs);
// Vstupy
mat.positionAttrib = glGetAttribLocation(mat.program, "position");
mat.normalAttrib = glGetAttribLocation(mat.program, "normal");
mat.tangentAttrib = glGetAttribLocation(mat.program, "tangent");
mat.texposAttrib = glGetAttribLocation(mat.program, "texpos");
mat.mViewUniform = glGetUniformLocation(mat.program, "view");
mat.mModelViewUniform = glGetUniformLocation(mat.program, "modelView");
mat.mModelViewProjectionUniform = glGetUniformLocation(mat.program, "modelViewProjection");
mat.mMVInverseTranspose = glGetUniformLocation(mat.program, "mv_inverse_transpose");
mat.iEnabledLightsUniform = glGetUniformLocation(mat.program, "enabledLights");
mat.vLightsUniform = glGetUniformLocation(mat.program, "lights");
mat.matParams.ambient = glGetUniformLocation(mat.program, "material.ambient");
mat.matParams.diffuse = glGetUniformLocation(mat.program, "material.diffuse");
mat.matParams.specular = glGetUniformLocation(mat.program, "material.specular");
mat.matParams.shininess = glGetUniformLocation(mat.program, "material.shininess");
mat.bDrawAmbientUniform = glGetUniformLocation(mat.program, "paintAmbient");
mat.bDrawDiffSpecUniform = glGetUniformLocation(mat.program, "paintDiffSpec");
mat.bUseTextureUniform = glGetUniformLocation(mat.program, "useTexture");
for (unsigned int i = 1; i <= 3; i++)
{
ostringstream uniformName;
uniformName << "texture" << i;
mat.textureUniforms.push_back( glGetUniformLocation(mat.program, uniformName.str().c_str()) );
}
// Nacist textury
//mat.textures = loadTextures(mat.program, vsSource + fsSource);
programs[material] = mat;
return true;
}
void ShaderManager::useMaterialParams(ShaderManager::MATERIALPARAMS params)
{
PROGRAMBINDING mat = getCurrentProgram();
// nastavi hodnoty materialu
glUniform4fv(mat.matParams.ambient, 1, glm::value_ptr(params.ambient));
glUniform4fv(mat.matParams.diffuse, 1, glm::value_ptr(params.diffuse));
glUniform4fv(mat.matParams.specular, 1, glm::value_ptr(params.specular));
glUniform1i(mat.matParams.shininess, params.shininess);
// aktivovat pozadovane textury
GLenum textureEnums[] = { GL_TEXTURE0, GL_TEXTURE1, GL_TEXTURE2, GL_TEXTURE3, GL_TEXTURE4, GL_TEXTURE5 };
for (unsigned int i = 0; i < params.textures.size(); i++)
{
if (activeTexture != textureEnums[i]) {
glActiveTexture(textureEnums[i]);
activeTexture = textureEnums[i];
}
glBindTexture(GL_TEXTURE_2D, params.textures[i]);
glUniform1i(mat.textureUniforms[i], i);
}
// natavit priznak zda jsme predali nejakou texturu
glUniform1i(mat.bUseTextureUniform, (params.textures.size() > 0));
/*
// postupne vse nastavit; pri samotnem kresleni jiz neni treba
for (unsigned int i = 0; i < mat.textures.size(); i++)
{
TEXTUREBINDING binding = mat.textures[i];
if (activeTexture != textureEnums[i]) {
glActiveTexture(textureEnums[i]);
activeTexture = textureEnums[i];
}
glBindTexture(GL_TEXTURE_2D, binding.texture);
// nastaveni parametru sampleru
if (binding.WRAP_S == GL_REPEAT || binding.WRAP_S == GL_CLAMP || binding.WRAP_S == GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, binding.WRAP_S);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
if (binding.WRAP_T == GL_REPEAT || binding.WRAP_T == GL_CLAMP || binding.WRAP_T == GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, binding.WRAP_T);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (binding.MAG_FILTER == GL_NEAREST || binding.MAG_FILTER == GL_LINEAR || binding.MAG_FILTER == GL_NEAREST_MIPMAP_LINEAR ||
binding.MAG_FILTER == GL_NEAREST_MIPMAP_NEAREST || binding.MAG_FILTER == GL_LINEAR_MIPMAP_LINEAR ||
binding.MAG_FILTER == GL_LINEAR_MIPMAP_NEAREST || binding.MAG_FILTER == GL_TEXTURE_MIN_LOD ||
binding.MAG_FILTER == GL_TEXTURE_MAX_LOD || binding.MAG_FILTER == GL_TEXTURE_BASE_LEVEL || binding.MAG_FILTER == GL_TEXTURE_MAX_LEVEL)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, binding.MAG_FILTER);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (binding.MIN_FILTER == GL_NEAREST || binding.MIN_FILTER == GL_LINEAR || binding.MIN_FILTER == GL_NEAREST_MIPMAP_LINEAR ||
binding.MIN_FILTER == GL_NEAREST_MIPMAP_NEAREST || binding.MIN_FILTER == GL_LINEAR_MIPMAP_LINEAR ||
binding.MIN_FILTER == GL_LINEAR_MIPMAP_NEAREST || binding.MIN_FILTER == GL_TEXTURE_MIN_LOD ||
binding.MIN_FILTER == GL_TEXTURE_MAX_LOD || binding.MIN_FILTER == GL_TEXTURE_BASE_LEVEL || binding.MIN_FILTER == GL_TEXTURE_MAX_LEVEL)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, binding.MIN_FILTER);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glUniform1i(binding.uniform, i);
}
*/
}
ShaderManager::PROGRAMBINDING ShaderManager::useProgram(string program)
{
// overit substituci shaderu
map<string, string>::iterator substIt = shaderSubstitutions.find(program);
if (substIt != shaderSubstitutions.end())
program = (*substIt).second;
map<string, PROGRAMBINDING>::iterator el = programs.find(program);
// fallback shaderu
if (el == programs.end())
el = programs.find(DEFAULT_PROGRAM);
PROGRAMBINDING mat = (*el).second;
// nastavi aktualni shader v manageru
currentProgram = mat;
// a v GL
glUseProgram(mat.program);
return mat;
}
ShaderManager::PROGRAMBINDING ShaderManager::getCurrentProgram()
{
return currentProgram;
}
GLuint ShaderManager::loadTexture(string path)
{
SDL_Surface * surface = SDL_LoadBMP(path.c_str());
if(surface == NULL) throw SDL_Exception();
GLuint handle;
glGenTextures(1, &handle);
glBindTexture(GL_TEXTURE_2D, handle);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automaticke mipmapy pro starsi gl
glGenerateMipmap(GL_TEXTURE_2D);
SurfaceImage2D(GL_TEXTURE_2D, 0, GL_RGB, surface);
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
return handle;
}
/*
vector<ShaderManager::TEXTUREBINDING> ShaderManager::loadTextures(GLuint program, string source)
{
vector<string> lines;
istringstream stream(source);
// rozsekat vstup na radky
while (stream.good())
{
string s;
getline(stream, s);
lines.push_back(s);
}
vector<TEXTUREBINDING> bindings;
TEXTUREBINDING binding;
binding.isValid = false;
// budeme postupne nacitat radky,
// pokud je radek anotace, naplnime ji strukturu,
// pokud je deklarace, vezmeme strukturu a ulozime,
// jinak strukturu zahodime
for (unsigned int i = 0; i < lines.size(); i++)
{
// anotace
size_t annot = lines[i].find("// @");
if (annot != string::npos)
{
string tmp = lines[i].substr(4); // preskoci '// @'
size_t space = tmp.find_first_of(' '); // mezera mezi nazvem anotace a hodnotou
string anName = tmp.substr(0, space); // nazev anotace
tmp.erase(0, space + 1); // odmaze vse pred hodnotou anotace
size_t eol = tmp.find_first_of(" \t\f\v\n\r"); // prvni prazdny znak za hodnotou
string anValue = tmp.substr(0, eol);
// nacitame texturu?
if (anName == "LOAD")
{
SDL_Surface * surface = SDL_LoadBMP(anValue.c_str());
if(surface == NULL) throw SDL_Exception();
glGenTextures(1, &binding.texture);
glBindTexture(GL_TEXTURE_2D, binding.texture);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automaticke mipmapy pro starsi gl
glGenerateMipmap(GL_TEXTURE_2D);
SurfaceImage2D(GL_TEXTURE_2D, 0, GL_RGB, surface);
binding.isValid = true;
continue; // dal tuto anotaci neresit
}
// dohledame nastavovany GL parametr
GLint* param;
if (anName == "MIN_FILTER") param = &binding.MIN_FILTER;
else if (anName == "MAG_FILTER") param = &binding.MAG_FILTER;
else if (anName == "WRAP_S") param = &binding.WRAP_S;
else if (anName == "WRAP_T") param = &binding.WRAP_T;
else
{
cerr << "Error: Unknown shader annotation '" << anName << "'" << endl;
return vector<TEXTUREBINDING>();
}
// dohledame nastavovanou GL hodnotu
if (anValue == "NEAREST") *param = GL_NEAREST;
else if (anValue == "LINEAR") *param = GL_LINEAR;
else if (anValue == "NEAREST_MIPMAP_NEAREST") *param = GL_NEAREST_MIPMAP_NEAREST;
else if (anValue == "LINEAR_MIPMAP_NEAREST") *param = GL_LINEAR_MIPMAP_NEAREST;
else if (anValue == "NEAREST_MIPMAP_LINEAR") *param = GL_NEAREST_MIPMAP_LINEAR;
else if (anValue == "LINEAR_MIPMAP_LINEAR") *param = GL_LINEAR_MIPMAP_LINEAR;
else if (anValue == "TEXTURE_MIN_LOD") *param = GL_TEXTURE_MIN_LOD;
else if (anValue == "TEXTURE_MAX_LOD") *param = GL_TEXTURE_MAX_LOD;
else if (anValue == "TEXTURE_BASE_LEVEL") *param = GL_TEXTURE_BASE_LEVEL;
else if (anValue == "TEXTURE_MAX_LEVEL") *param = GL_TEXTURE_MAX_LEVEL;
else if (anValue == "CLAMP") *param = GL_CLAMP;
else if (anValue == "CLAMP_TO_EDGE") *param = GL_CLAMP_TO_EDGE;
else if (anValue == "REPEAT") *param = GL_REPEAT;
else
{
cerr << "Error: Unknown shader annotation value '" << anValue << "'" << endl;
return vector<TEXTUREBINDING>();
}
continue; // dal tento radek neresit
} // anotace
// deklarace
size_t decl = lines[i].find("uniform sampler2D ");
if (decl != string::npos)
{
string identifier = lines[i].substr( string("uniform sampler2D ").size() );
size_t empty = identifier.find_first_of("; \t\f\v\n\r"); // pozice prvniho pripadneho prazdneho znaku za idtf.
if (empty != string::npos)
identifier.erase(empty);
binding.uniform = glGetUniformLocation(program, identifier.c_str()); // ulozime handle
// pokud byla definovana platna textura (anotace @load je povinna), ulozime binding
if (binding.isValid)
bindings.push_back(binding);
// neuplna anotace pred deklaraci vypise pouze varovani (neukonci)
else
cerr << "Warning: incomplete annotation near '" << identifier << "'" << endl;
// novy binding pro pristi radky
binding = TEXTUREBINDING();
binding.isValid = false;
continue;
} // deklarace
// na radku neni ani anotace, ani deklarace; jestlize existovala nejaka nepouzita anotace, zneplatnime ji
binding = TEXTUREBINDING();
binding.isValid = false;
} // for each line
#if 0
for (unsigned int i = 0; i < bindings.size(); i++)
{
TEXTUREBINDING b = bindings[i];
cout << "uniform: " << b.uniform << endl << "texture: " << b.texture << endl << "min_f: " << b.MIN_FILTER << endl;
cout << "mag_f: " << b.MAG_FILTER << endl << "wrap_s: " << b.WRAP_S << endl << "wrap_t: " << b.WRAP_T << endl;
cout << "---------------------------------------" << endl;
}
#endif
return bindings;
}
*/
void ShaderManager::loadDefaultProgram()
{
if (!loadProgram(DEFAULT_PROGRAM))
throw std::runtime_error("Default material shader not found");
}
ShaderManager::MATERIALPARAMS ShaderManager::getDefaultMaterial()
{
MATERIALPARAMS params;
params.ambient = glm::vec4(0.5, 0.5, 0.5, 1);
params.diffuse = glm::vec4(0.3, 0.3, 0.3, 1);
params.specular = glm::vec4(0.8, 0.8, 0.8, 1);
params.shininess = 10;
return params;
}
string ShaderManager::getTexturesPath()
{
return TEXTURES_PATH;
}
//////////////////////////////////////////////////////////////////////
// Kod ze cviceni
//////////////////////////////////////////////////////////////////////
// Nacte obrazek do textury
void ShaderManager::SurfaceImage2D(GLenum target, GLint level, GLint internalformat, SDL_Surface * surface)
{
// nejdriv vertikalne prevratit, at ma obrazek stejne souradnice jako GL
verticalSurfaceFlip(surface);
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
if( (surface->format->Rmask == 0xff0000) &&
(surface->format->Gmask == 0xff00) &&
(surface->format->Bmask == 0xff) &&
(surface->format->Amask == 0))
{
glTexImage2D(target, level, internalformat, surface->w, surface->h, 0, GL_BGR, GL_UNSIGNED_BYTE, surface->pixels);
}
else if((surface->format->Rmask == 0xff) &&
(surface->format->Gmask == 0xff00) &&
(surface->format->Bmask == 0xff0000) &&
(surface->format->Amask == 0))
{
glTexImage2D(target, level, internalformat, surface->w, surface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
}
else
{
throw std::runtime_error("unsupported surface format");
}
}
// Load whole file and return it as std::string
string ShaderManager::loadFile(const char * const file)
{
std::ifstream stream(file);
if(stream.fail()) throw String_Exception(std::string("Can't open \'") + file + "\'");
return std::string(std::istream_iterator<char>(stream >> std::noskipws), std::istream_iterator<char>());
}
// Info log contains errors and warnings from shader compilation
string ShaderManager::getShaderInfoLog(const GLuint shader)
{
int length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
std::string log(length, ' ');
glGetShaderInfoLog(shader, length, NULL, &log[0]);
return log;
}
// Info log contains errors and warnings from shader linking
string ShaderManager::getProgramInfoLog(const GLuint program)
{
int length;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
std::string log(length, ' ');
glGetProgramInfoLog(program, length, NULL, &log[0]);
return log;
}
GLuint ShaderManager::compileShader(const GLenum type, const char * source)
{
GLuint shader = glCreateShader(type);
if(shader == 0) throw GL_Exception("glCreateShader failed");
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
// vypisovat jenom pokud nastala chyba
string log = getShaderInfoLog(shader);
if (log.find("was successfully compiled") == string::npos)
std::cout << "Compile: " << log << endl;
int compileStatus;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
if(compileStatus == GL_FALSE) throw std::runtime_error("shader compilation failed");
return shader;
}
GLuint ShaderManager::linkShader(size_t count, ...)
{
// Create program object
GLuint program = glCreateProgram();
if(program == 0) throw GL_Exception("glCreateProgram failed");
// Attach arguments
va_list args;
va_start(args, count);
for(size_t i = 0; i < count; ++i)
{
glAttachShader(program, va_arg(args, GLuint));
}
va_end(args);
// Link program and check for errors
glLinkProgram(program);
// vypisovta jen pokud nastala chyba
string log = getProgramInfoLog(program);
if (log.find("Error") != string::npos || log.find("error") != string::npos || log.find("Warning") != string::npos || log.find("warning") != string::npos)
std::cout << "Link: " << log << endl;
int linkStatus;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if(linkStatus == GL_FALSE) throw std::runtime_error("shader linking failed");
return program;
}
////////////////////////////////////////////////////////////////////
// Kod z webu pro horizontalni flip SDL_surface, resp. textury
////////////////////////////////////////////////////////////////////
/**
* http://www.libsdl.org/docs/html/guidevideo.html
*/
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
/**
* http://www.libsdl.org/docs/html/guidevideo.html
*/
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
void ShaderManager::verticalSurfaceFlip(SDL_Surface*& image)
{
// create a copy of the image
SDL_Surface* flipped_image = SDL_CreateRGBSurface( SDL_SWSURFACE, image->w, image->h, image->format->BitsPerPixel,
image->format->Rmask, image->format->Gmask, image->format->Bmask, image->format->Amask );
// loop through pixels
for ( int y=0; y<image->h; y++ ) {
for ( int x=0; x<image->w; x++ ) {
// copy pixels, but reverse the y pixels!
putpixel( flipped_image, x, y, getpixel(image, x, image->h - y -1) );
//putpixel( flipped_image, x, y, getpixel(image, image->w - x - 1, y) );
}
}
// free original and assign flipped to it
SDL_FreeSurface( image );
image = flipped_image;
}