1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,67 @@ |
1 |
+/// Inlcudes |
|
2 |
+#include "VectorUtils4.h" |
|
3 |
+#include "LittleOBJLoader.h" |
|
4 |
+#include "LoadTGA.h" |
|
5 |
+ |
|
6 |
+ |
|
7 |
+/// Generate terrain |
|
8 |
+Model * GenerateTerrain(const char * filename) |
|
9 |
+{ |
|
10 |
+ TextureData textureData; |
|
11 |
+ if (!LoadTGATextureData(filename, &textureData)) |
|
12 |
+ return nullptr; |
|
13 |
+ |
|
14 |
+ int vertexCount = textureData.width * textureData.height; |
|
15 |
+ int triangleCount = (textureData.width-1) * (textureData.height-1) * 2; |
|
16 |
+ |
|
17 |
+ vec3 * position = (vec3 *) malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
18 |
+ vec3 * normal = (vec3 *) malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
19 |
+ vec2 * texCoord = (vec2 *) malloc(sizeof(GLfloat) * 2 * vertexCount); |
|
20 |
+ GLuint * indices = (GLuint *)malloc(sizeof(GLuint) * 3 * triangleCount); |
|
21 |
+ |
|
22 |
+ int bytesPerPixel = textureData.bpp / 8; |
|
23 |
+ for (unsigned int x = 0; x < textureData.width; ++x) |
|
24 |
+ for (unsigned int z = 0; z < textureData.height; ++z) |
|
25 |
+ { |
|
26 |
+ int indexBase = (x + z * textureData.width); |
|
27 |
+ // Vertex array. You need to scale this properly. |
|
28 |
+ GLubyte data = textureData.imageData[indexBase * bytesPerPixel]; |
|
29 |
+ position[indexBase].x = x / 1.0; |
|
30 |
+ position[indexBase].y = data / 100.0; |
|
31 |
+ position[indexBase].z = z / 1.0; |
|
32 |
+ // Normal vectors. You need to calculate these. |
|
33 |
+ normal[indexBase].x = 0.0; |
|
34 |
+ normal[indexBase].y = 1.0; |
|
35 |
+ normal[indexBase].z = 0.0; |
|
36 |
+ // Texture coordinates. You may want to scale them. |
|
37 |
+ texCoord[indexBase].x = x; // (float)x / textureData.width; |
|
38 |
+ texCoord[indexBase].y = z; // (float)z / textureData.height; |
|
39 |
+ } |
|
40 |
+ for (unsigned int x = 0; x < textureData.width-1; ++x) |
|
41 |
+ for (unsigned int z = 0; z < textureData.height-1; ++z) |
|
42 |
+ { |
|
43 |
+ int indexBase = (x + z * (textureData.width-1))*6; |
|
44 |
+ // Triangle 1 |
|
45 |
+ indices[indexBase + 0] = (x+0) + (z+0) * textureData.width; |
|
46 |
+ indices[indexBase + 1] = (x+0) + (z+1) * textureData.width; |
|
47 |
+ indices[indexBase + 2] = (x+1) + (z+0) * textureData.width; |
|
48 |
+ // Triangle 2 |
|
49 |
+ indices[indexBase + 3] = (x+1) + (z+0) * textureData.width; |
|
50 |
+ indices[indexBase + 4] = (x+0) + (z+1) * textureData.width; |
|
51 |
+ indices[indexBase + 5] = (x+1) + (z+1) * textureData.width; |
|
52 |
+ } |
|
53 |
+ |
|
54 |
+ // Create Model and upload to GPU: |
|
55 |
+ Model * model = LoadDataToModel( |
|
56 |
+ position, |
|
57 |
+ normal, |
|
58 |
+ texCoord, |
|
59 |
+ nullptr, |
|
60 |
+ indices, |
|
61 |
+ vertexCount, |
|
62 |
+ triangleCount * 3 |
|
63 |
+ ); |
|
64 |
+ |
|
65 |
+ free(textureData.imageData); |
|
66 |
+ return model; |
|
67 |
+} |