forked from giogix2/PlanetGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSphere.cpp
623 lines (518 loc) · 17 KB
/
PSphere.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
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
#include "OGRE/Ogre.h"
#include "PSphere.h"
#include "simplexnoise1234.h"
// Let's set texture dimensions this way for a time being
#define TEX_WIDTH 1024
#define TEX_HEIGHT 512
PSphere::PSphere(){
vertexes = NULL;
vNorms = NULL;
colours = NULL;
texCoords = NULL;
indexes = NULL;
image = NULL;
}
void PSphere::calculate(Ogre::Vector3 vertex, Ogre::Real radius, Ogre::ColourValue colour)
{
Ogre::Vector3 vertexXYprojection, LongitudeRef, LatitudeRef;
Ogre::Real u, v;
// Make vector as long as sphere radius
vertex = vertex * radius/vertex.length();
vertexes[vertexCount] = vertex;
colours[vertexCount] = colour;
/* Calculates equirectangular projection AKA plate carrée
* Coordinate convention for textures:
* texture(0.0 <= u <= 1.0, 0.0 <= v <= 1.0)
* z-direction is polar. model(rnd, rnd, +z) => texture(rnd, >0.5)
* model(+x, 0, rnd) => texture(0, rnd) and model(0, +y, rnd) => texture(0.25, rnd)
* and model(0, -y, rnd) => texture(0.75, rnd) */
vertexXYprojection = Ogre::Vector3(vertex.x, vertex.y, 0.0f); // vertex projection in xy-plane
// if x and y are both zero, result for u is nan. Guarding against it.
if(vertexXYprojection.length() != 0)
{
LongitudeRef = Ogre::Vector3(1.0f, 0.0f, 0.0f);
u = acosf( vertexXYprojection.dotProduct(LongitudeRef)
/(vertexXYprojection.length()*LongitudeRef.length()) );
if(vertex.y < 0)
u = Ogre::Math::TWO_PI - u;
u = u/Ogre::Math::TWO_PI;
}
else
{
u = 0.0f; // x = 0 and y = 0, set u = 0;
}
// -z is defined as southpole
LatitudeRef = Ogre::Vector3(0.0f, 0.0f, -1.0f);
v = acosf(vertex.dotProduct(LatitudeRef)/(vertex.length()*LatitudeRef.length()));
v = v/Ogre::Math::PI;
texCoords[vertexCount] = Ogre::Vector2(u, v);
// increase count
vertexCount++;
}
/* Get spherical coordinates from texture coordinate */
Ogre::Vector3 PSphere::calculateSphereCoordsFromTexCoords(Ogre::Vector2 *texCoord)
{
Ogre::Vector3 sphereCoord;
Ogre::Real alfa, beta;
alfa = texCoord->x * Ogre::Math::TWO_PI;
beta = texCoord->y * Ogre::Math::PI;
sphereCoord.x = radius * cosf(alfa)*sinf(beta);
sphereCoord.y = radius * sinf(alfa)*sinf(beta);
sphereCoord.z = radius * cosf(beta);
return sphereCoord;
}
Ogre::Real PSphere::heightNoise(Ogre::uint32 octaves, Ogre::Real *amplitudes,
Ogre::Real *frequencys, Ogre::Vector3 Point)
{
Ogre::uint32 i;
Ogre::Real height = 0.0f;
// Run through the octaves
for(i=0; i < octaves; i++)
{
height += amplitudes[i] * SimplexNoise1234::noise(Point.x/frequencys[i],
Point.y/frequencys[i],
Point.z/frequencys[i]);
}
return height;
}
/* Fix a seam by adding vertex duplicates with texture u going over 1.0 */
void PSphere::fixTextureSeam()
{
Ogre::uint32 i, j, affectedTriangleCount=0, vCntBeforeFix;
Ogre::Real absDiff1, absDiff2, absDiff3;
vCntBeforeFix = vertexCount;
for(i=0; i < indexCount; i = i + 3)
{
absDiff1 = Ogre::Math::Abs(texCoords[indexes[i]].x - texCoords[indexes[i+1]].x);
absDiff2 = Ogre::Math::Abs(texCoords[indexes[i]].x - texCoords[indexes[i+2]].x);
absDiff3 = Ogre::Math::Abs(texCoords[indexes[i+1]].x - texCoords[indexes[i+2]].x);
/* Check for an abrupt change in triangles u-coordinates
* (texture-coordinate(u, v)). */
if(absDiff1 > 0.3f || absDiff2 > 0.3f || absDiff3 > 0.3f)
{
for(j=0; j < 3; j++)
{
if(texCoords[indexes[i+j]].x < 0.3f)
{
// Duplicate offending vertex data
vertexes[vertexCount] = vertexes[indexes[i+j]];
vNorms[vertexCount] = vNorms[indexes[i+j]];
colours[vertexCount] = colours[indexes[i+j]];
texCoords[vertexCount] = texCoords[indexes[i+j]];
// Give correct u
texCoords[vertexCount].x += 1.0f;
// update index to point to the new vertex
indexes[i+j] = vertexCount;
vertexCount++;
}
}
affectedTriangleCount++;
}
}
/* FIXME: Might still have some problems in the poles. Revise if necessary */
std::cout << "fixTextureSeam:" << std::endl
<< " number of fixed triangles "
<< affectedTriangleCount << std::endl
<< " number of individual vertexes duplicated "
<< vertexCount - vCntBeforeFix << std::endl;
}
/* Example that shows procedural generation of textures */
void PSphere::generateImage(Ogre::uint32 octaves, Ogre::Real *amplitudes, Ogre::Real *frequencys,
Ogre::Real seaHeight, Ogre::Real top, Ogre::Real bottom)
{
Ogre::Vector3 spherePoint;
Ogre::Vector2 texCoords;
Ogre::Real height;
Ogre::uint32 x, y;
Ogre::uint8 red, green, blue, tempVal;
// Guard against multiple memory allocations to avoid memory leaks
if(image != NULL)
delete[] image;
image = new unsigned char[TEX_WIDTH*TEX_HEIGHT*3];
for(y=0; y < TEX_HEIGHT; y++)
{
for(x=0; x < TEX_WIDTH; x++)
{
texCoords.x = (Ogre::Real(x)+0.5f)/TEX_WIDTH;
texCoords.y = (Ogre::Real(y)+0.5f)/TEX_HEIGHT;
// Get a point that corresponds to a given pixel
spherePoint = calculateSphereCoordsFromTexCoords(&texCoords);
// Get height of a point
height = heightNoise(octaves, amplitudes, frequencys, spherePoint);
// Set sea-colors, deeper part is slighly deeper blue.
if(height < seaHeight)
{
red = 100-Ogre::uchar((seaHeight-height)/(seaHeight-bottom)*50.0f);
green = 255-Ogre::uchar((seaHeight-height)/(seaHeight-bottom)*50.0f);
blue = 255;
}
else
{
// Set low elevations green and higher brown
red = 0+Ogre::uchar((height-seaHeight)/(top-seaHeight)*250.0f);
green = 255-Ogre::uchar((height-seaHeight)/(top-seaHeight)*250.0f);
blue = 0;
// Highest elevations are bright grey and go toward white
if(Ogre::uchar((height-seaHeight)/(top-seaHeight)*250.0f) > 150)
{
tempVal = Ogre::uchar((height-seaHeight)/(top-seaHeight)*250.0f)-150;
if(tempVal > 75)
tempVal = 75;
red = 180+tempVal;
green = 180+tempVal;
blue = 180+tempVal;
}
}
// Write pixel to image
image[((TEX_HEIGHT-1-y)*TEX_WIDTH+x)*3] = red;
image[((TEX_HEIGHT-1-y)*TEX_WIDTH+x)*3+1] = green;
image[((TEX_HEIGHT-1-y)*TEX_WIDTH+x)*3+2] = blue;
}
}
}
// Experimenting with some noise
void PSphere::deform(Ogre::Real seaFraction)
{
Ogre::uint32 i, j, octaves, histogram[100] = {0}, histoTotal = 0;
Ogre::Real height, top, bottom, seaHeight;
Ogre::Vector3 vertex, unity;
Ogre::Real frequencys[2], amplitudes[2];
Ogre::uint32 accumulator = 0;
// Variate height of a point in a sphere.
octaves = 2;
// Amplitude controls height of features
amplitudes[0] = 0.15f;
amplitudes[1] = 0.05f;
/* frequency controls how wide features are. This is now actually
* inverse of frequency */
frequencys[0] = 3.0f;
frequencys[1] = 0.5f;
/* highest possible feature height, assuming noise range exactly
* [-1 >= noise <= 1]. */
top = 0.0f;
for(i=0; i < octaves; i++)
top += amplitudes[i];
bottom = -top;
for(i=0; i < vertexCount; i++)
{
vertex = vertexes[i];
height = heightNoise(octaves, amplitudes, frequencys, vertex);
unity = vertex;
unity.normalise(); // direction vector that scalar height multiplies
vertexes[i] = vertex + (unity * height);
// Any better ways to create histograms?
j = 0;
while( height > (j/99.0f*(top-bottom) + bottom) )
j++;
histogram[j]++;
histoTotal++;
}
// Find out histogram-bracket that exceeds wanted fraction of all values
for(i=0; i < 100; i++)
{
accumulator += histogram[i];
if(Ogre::Real(accumulator) > Ogre::Real(histoTotal)*seaFraction)
break;
}
// Figure out offset with i
seaHeight = Ogre::Real(i) / 99.0f * (top-bottom) + bottom;
// Raise low elevations to sealevel
for(i=0; i < vertexCount; i++)
{
if( vertexes[i].length() < (seaHeight + radius) )
{
// First make vertex unit length, then scale it by a scalar value.
vertexes[i].normalise();
vertexes[i] = vertexes[i]*(seaHeight+radius);
}
}
// Procedurally generated image for texturing
generateImage(octaves, amplitudes, frequencys, seaHeight, top, bottom);
}
void PSphere::calculateNormals()
{
Ogre::uint32 i;
Ogre::Vector3 p1, p2, normal;
// Simple normal calculation, 1 normal for 3 vertices of a triangle
/* for(i=0; i < indexCount; i = i + 3)
{
p1 = vertexes[indexes[i]] - vertexes[indexes[i+1]];
p2 = vertexes[indexes[i+1]] - vertexes[indexes[i+2]];
normal = p1.crossProduct(p2);
normal.normalise();
vNorms[indexes[i]] = normal;
vNorms[indexes[i+1]] = normal;
vNorms[indexes[i+2]] = normal;
}*/
// Blended normals for a vertex
// first, zero our normals, so that we can use it as a accumulator
for(i=0; i < vertexCount; i++)
{
vNorms[i].x = 0.0f;
vNorms[i].y = 0.0f;
vNorms[i].z = 0.0f;
}
/* calculate normals for every triangle. Multiple normals are added together
* for each vertex, so the result is average of all the normals. */
for(i=0; i < indexCount; i = i + 3)
{
p1 = vertexes[indexes[i]] - vertexes[indexes[i+1]];
p2 = vertexes[indexes[i+1]] - vertexes[indexes[i+2]];
normal = p1.crossProduct(p2);
// FIXME: Probably more correct to normalize before adding, investigate later.
vNorms[indexes[i]] += normal;
vNorms[indexes[i+1]] += normal;
vNorms[indexes[i+2]] += normal;
}
// Normalization pass
for(i=0; i < vertexCount; i++)
{
vNorms[i].normalise();
}
}
// Makes a sphere out of a cube that is made of 6 squares
void PSphere::create(Ogre::Real diameter, Ogre::Real seaFraction, Ogre::uint32 iters)
{
Ogre::Vector3 vertex;
Ogre::uint32 i, j, idx = 0;
Ogre::uint32 *indexBuf;
// Iters less than 3 are pointless
if(iters < 3)
{
iters = 3;
std::cout << "Sphere needs atleast 3 iters" << std::endl;
}
radius = diameter/2.0f;
vertexCount = 0;
indexCount = 0;
/* Calling Sphere::create more than once would cause memory leak if we
* wouldn't delete old allocations first */
if(vertexes != NULL)
delete[] vertexes;
if(vNorms != NULL)
delete[] vNorms;
if(colours != NULL)
delete[] colours;
if(texCoords != NULL)
delete[] texCoords;
if(indexes != NULL)
delete[] indexes;
/* +iter*8 is for texture seam fix, duplicating some vertexes.
* Approximate, but should be on a safe side */
vertexes = new Ogre::Vector3[iters*iters*6 + iters*8];
vNorms = new Ogre::Vector3[iters*iters*6 + iters*8];
colours = new Ogre::ColourValue[iters*iters*6 + iters*8];
texCoords = new Ogre::Vector2[iters*iters*6 + iters*8];
indexes = new Ogre::uint32[(iters-1)*(iters-1)*6*6];
Ogre::Vector3 *vBuf = new Ogre::Vector3[iters*iters]; // Allocate memory for the square buffer
indexBuf = new Ogre::uint32[(iters-1)*(iters-1)*6]; // Allocate index buffer for the square
// Make a square in xy-plane, elevated in z-axis by diameter/2
vertex.z = diameter/2.0f;
for(i=0; i < iters; i++)
{
vertex.x = -diameter/2.0f + diameter * Ogre::Real(i) / Ogre::Real(iters-1);
for(j=0; j < iters; j++)
{
vertex.y = -diameter/2.0f + diameter * Ogre::Real(j) / Ogre::Real(iters-1);
vBuf[idx] = vertex;
idx++;
}
}
// Create indexes to build trianges. To make other side of a square visible reverse indexes.
idx = 0;
for(i=0; i < iters-1; i++)
{
for(j=0; j < iters-1; j++)
{
// Triangle 1
indexBuf[idx]=i*iters+j;
idx++;
indexBuf[idx]=i*iters+j+iters+1;
idx++;
indexBuf[idx]=i*iters+j+1;
idx++;
// Triangle 2, in other words, a quad.
indexBuf[idx]=i*iters+j+iters+1;
idx++;
indexBuf[idx]=i*iters+j;
idx++;
indexBuf[idx]=i*iters+j+iters;
idx++;
}
}
// Now just rotate the square to create a cube
// Red face, +z
for(i=0; i < iters*iters; i++)
{
calculate(vBuf[i], diameter/2.0f, Ogre::ColourValue(1.0f, 0.0f, 0.0f));
}
for(i=0; i < (iters-1)*(iters-1)*6; i++)
{
indexes[indexCount] = indexBuf[i];
indexCount++;
}
// Green face, -z
for(i=0; i < iters*iters; i++)
{
vertex.x = vBuf[i].x;
vertex.y = vBuf[i].y;
vertex.z = -vBuf[i].z;
calculate(vertex, diameter/2.0f, Ogre::ColourValue(0.0f, 1.0f, 0.0f));
}
// Reverse
// Iterate to 1 instead of zero to avoid unsigned number wraparound
for(i=(iters-1)*(iters-1)*6; i >= 1; i--)
{
indexes[indexCount] = iters*iters+indexBuf[i-1];
indexCount++;
}
// Blue face, +x
for(i=0; i < iters*iters; i++)
{
vertex.x = vBuf[i].z;
vertex.y = vBuf[i].x;
vertex.z = vBuf[i].y;
calculate(vertex, diameter/2.0f, Ogre::ColourValue(0.0f, 0.0f, 1.0f));
}
for(i=0; i < (iters-1)*(iters-1)*6; i++)
{
indexes[indexCount] = iters*iters*2+indexBuf[i];
indexCount++;
}
// Violet face, -x
for(i=0; i < iters*iters; i++)
{
vertex.x = -vBuf[i].z;
vertex.y = vBuf[i].x;
vertex.z = vBuf[i].y;
calculate(vertex, diameter/2.0f, Ogre::ColourValue(1.0f, 0.0f, 1.0f));
}
// Reverse
for(i=(iters-1)*(iters-1)*6; i >= 1; i--)
{
indexes[indexCount] = iters*iters*3+indexBuf[i-1];
indexCount++;
}
// Cyan face, +y
for(i=0; i < iters*iters; i++)
{
vertex.x = vBuf[i].x;
vertex.y = vBuf[i].z;
vertex.z = vBuf[i].y;
calculate(vertex, diameter/2.0f, Ogre::ColourValue(0.0f, 1.0f, 1.0f));
}
// Reverse
for(i=(iters-1)*(iters-1)*6; i >= 1; i--)
{
indexes[indexCount] = iters*iters*4+indexBuf[i-1];
indexCount++;
}
// Yellow face, -y
for(i=0; i < iters*iters; i++)
{
vertex.x = vBuf[i].x;
vertex.y = -vBuf[i].z;
vertex.z = vBuf[i].y;
calculate(vertex, diameter/2.0f, Ogre::ColourValue(1.0f, 1.0f, 0.0f));
}
for(i=0; i < (iters-1)*(iters-1)*6; i++)
{
indexes[indexCount] = iters*iters*5+indexBuf[i];
indexCount++;
}
// Release square buffers
delete[] vBuf;
delete[] indexBuf;
deform(seaFraction);
calculateNormals();
fixTextureSeam(); // Call this after calculateNormals
}
/* Release resources allocated by Sphere class */
void PSphere::destroy()
{
/* FIXME: Should we consider failing graciously in case of incorrect use like:
*
* Sphere oneSphere;
* oneSphere.destroy(); */
delete[] colours;
delete[] indexes;
delete[] texCoords;
delete[] vertexes;
delete[] vNorms;
delete[] image;
}
void PSphere::loadToBuffers(const std::string &meshName, const std::string &textureName)
{
Ogre::uint32 i, j;
// Create mesh and subMesh
Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton()
.createManual(meshName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
Ogre::SubMesh *subMesh = mesh->createSubMesh();
mesh->sharedVertexData = new Ogre::VertexData;
// Pointer to declaration of vertexData
Ogre::VertexDeclaration* vertexDecl = mesh->sharedVertexData->vertexDeclaration;
mesh->sharedVertexData->vertexCount = vertexCount;
// define elements position, normal and tex coordinate
vertexDecl->addElement(0, 0, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
vertexDecl->addElement(0, sizeof(float)*3, Ogre::VET_FLOAT3, Ogre::VES_NORMAL);
vertexDecl->addElement(0, sizeof(float)*6, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES);
// Vertex buffer
Ogre::HardwareVertexBufferSharedPtr vBuf = Ogre::HardwareBufferManager::getSingleton()
.createVertexBuffer(8*sizeof(float), vertexCount,
Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
mesh->sharedVertexData->vertexBufferBinding->setBinding(0, vBuf);
// Lock the buffer and write vertex data to it
float *pVertex = static_cast<float *>(vBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
for(i=0; i < vertexCount; i++)
{
pVertex[i*8+0] = vertexes[i].x;
pVertex[i*8+1] = vertexes[i].y;
pVertex[i*8+2] = vertexes[i].z;
pVertex[i*8+3] = vNorms[i].x;
pVertex[i*8+4] = vNorms[i].y;
pVertex[i*8+5] = vNorms[i].z;
pVertex[i*8+6] = texCoords[i].x;
pVertex[i*8+7] = texCoords[i].y;
}
vBuf->unlock();
// Index buffer
Ogre::HardwareIndexBufferSharedPtr iBuf = Ogre::HardwareBufferManager::getSingleton()
.createIndexBuffer(Ogre::HardwareIndexBuffer::IT_32BIT, indexCount,
Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
// Lock index buffer
unsigned int *pIdx = static_cast<unsigned int *>(iBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
for(i=0; i < indexCount; i++)
{
pIdx[i] = indexes[i];
}
iBuf->unlock();
subMesh->useSharedVertices = true;
subMesh->indexData->indexBuffer = iBuf;
subMesh->indexData->indexCount = indexCount;
subMesh->indexData->indexStart = 0;
mesh->_setBounds(Ogre::AxisAlignedBox(-radius, -radius, -radius, radius, radius, radius));
mesh->_setBoundingSphereRadius(radius);
mesh->load();
// Texture stuff
Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton()
.createManual(textureName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Ogre::TEX_TYPE_2D, TEX_WIDTH, TEX_HEIGHT, 0, Ogre::PF_R8G8B8, Ogre:: TU_DYNAMIC);
Ogre::HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer();
pixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
const Ogre::PixelBox &pixelBox = pixelBuffer->getCurrentLock();
Ogre::uint8 *surfaceTexture = static_cast<Ogre::uint8*>(pixelBox.data);
for(i=0; i < TEX_HEIGHT; i++)
{
for(j=0; j < TEX_WIDTH; j++)
{
/* FIXME: Might be unnecessary memory copy, but was convenient. */
/* TextureManager did not honor Ogre::PF_R8G8B8, so need to swap red and blue,
* plus hardware wants alfa channel values too */
surfaceTexture[(i*TEX_WIDTH+j)*4] = image[(i*TEX_WIDTH+j)*3+2]; // blue
surfaceTexture[(i*TEX_WIDTH+j)*4+1] = image[(i*TEX_WIDTH+j)*3+1]; // green
surfaceTexture[(i*TEX_WIDTH+j)*4+2] = image[(i*TEX_WIDTH+j)*3]; // red
surfaceTexture[(i*TEX_WIDTH+j)*4+3] = 255; // Alfa
}
}
pixelBuffer->unlock();
}