1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,162 +0,0 @@ |
1 |
-/// Inlcudes |
|
2 |
-#define MAIN |
|
3 |
-#include "MicroGlut.h" |
|
4 |
-#include "GL_utilities.h" |
|
5 |
-#include "VectorUtils4.h" |
|
6 |
-#include "LittleOBJLoader.h" |
|
7 |
-#include "LoadTGA.h" |
|
8 |
- |
|
9 |
- |
|
10 |
-/// Globals |
|
11 |
-GLuint program; |
|
12 |
-mat4 projection; |
|
13 |
-Model * terrain; |
|
14 |
-GLuint tex; |
|
15 |
- |
|
16 |
- |
|
17 |
-/// Generate terrain |
|
18 |
-Model * GenerateTerrain(const char * filename) |
|
19 |
-{ |
|
20 |
- TextureData textureData; |
|
21 |
- if (!LoadTGATextureData(filename, &textureData)) |
|
22 |
- return nullptr; |
|
23 |
- |
|
24 |
- int vertexCount = textureData.width * textureData.height; |
|
25 |
- int triangleCount = (textureData.width-1) * (textureData.height-1) * 2; |
|
26 |
- |
|
27 |
- vec3 * position = (vec3 *) malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
28 |
- vec3 * normal = (vec3 *) malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
29 |
- vec2 * texCoord = (vec2 *) malloc(sizeof(GLfloat) * 2 * vertexCount); |
|
30 |
- GLuint * indices = (GLuint *)malloc(sizeof(GLuint) * 3 * triangleCount); |
|
31 |
- |
|
32 |
- int bytesPerPixel = textureData.bpp / 8; |
|
33 |
- for (unsigned int x = 0; x < textureData.width; ++x) |
|
34 |
- for (unsigned int z = 0; z < textureData.height; ++z) |
|
35 |
- { |
|
36 |
- int indexBase = (x + z * textureData.width); |
|
37 |
- // Vertex array. You need to scale this properly. |
|
38 |
- GLubyte data = textureData.imageData[indexBase * bytesPerPixel]; |
|
39 |
- position[indexBase].x = x / 1.0; |
|
40 |
- position[indexBase].y = data / 100.0; |
|
41 |
- position[indexBase].z = z / 1.0; |
|
42 |
- // Normal vectors. You need to calculate these. |
|
43 |
- normal[indexBase].x = 0.0; |
|
44 |
- normal[indexBase].y = 1.0; |
|
45 |
- normal[indexBase].z = 0.0; |
|
46 |
- // Texture coordinates. You may want to scale them. |
|
47 |
- texCoord[indexBase].x = x; // (float)x / textureData.width; |
|
48 |
- texCoord[indexBase].y = z; // (float)z / textureData.height; |
|
49 |
- } |
|
50 |
- for (unsigned int x = 0; x < textureData.width-1; ++x) |
|
51 |
- for (unsigned int z = 0; z < textureData.height-1; ++z) |
|
52 |
- { |
|
53 |
- int indexBase = (x + z * (textureData.width-1))*6; |
|
54 |
- // Triangle 1 |
|
55 |
- indices[indexBase + 0] = (x+0) + (z+0) * textureData.width; |
|
56 |
- indices[indexBase + 1] = (x+0) + (z+1) * textureData.width; |
|
57 |
- indices[indexBase + 2] = (x+1) + (z+0) * textureData.width; |
|
58 |
- // Triangle 2 |
|
59 |
- indices[indexBase + 3] = (x+1) + (z+0) * textureData.width; |
|
60 |
- indices[indexBase + 4] = (x+0) + (z+1) * textureData.width; |
|
61 |
- indices[indexBase + 5] = (x+1) + (z+1) * textureData.width; |
|
62 |
- } |
|
63 |
- |
|
64 |
- // Create Model and upload to GPU: |
|
65 |
- Model * model = LoadDataToModel( |
|
66 |
- position, |
|
67 |
- normal, |
|
68 |
- texCoord, |
|
69 |
- nullptr, |
|
70 |
- indices, |
|
71 |
- vertexCount, |
|
72 |
- triangleCount * 3 |
|
73 |
- ); |
|
74 |
- |
|
75 |
- free(textureData.imageData); |
|
76 |
- return model; |
|
77 |
-} |
|
78 |
- |
|
79 |
- |
|
80 |
-/// Init |
|
81 |
-void init(void) |
|
82 |
-{ |
|
83 |
- // GL inits. |
|
84 |
- glClearColor(0.2, 0.2, 0.5, 0.0); |
|
85 |
- glEnable(GL_DEPTH_TEST); |
|
86 |
- glDisable(GL_CULL_FACE); |
|
87 |
- printError("init GL"); |
|
88 |
- |
|
89 |
- // Load and compile shader. |
|
90 |
- program = loadShaders("terrain.vert", "terrain.frag"); |
|
91 |
- glUseProgram(program); |
|
92 |
- printError("init shader"); |
|
93 |
- |
|
94 |
- // Matrices. |
|
95 |
- projection = frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 50.0); |
|
96 |
- glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_TRUE, projection.m); |
|
97 |
- printError("init matrices"); |
|
98 |
- |
|
99 |
- // Textures. |
|
100 |
- LoadTGATextureSimple("../assets/terrain/dandelion.tga", &tex); |
|
101 |
- glBindTexture(GL_TEXTURE_2D, tex); |
|
102 |
- glUniform1i(glGetUniformLocation(program, "tex"), 0); |
|
103 |
- printError("init textures"); |
|
104 |
- |
|
105 |
- // Generate terrain. |
|
106 |
- terrain = GenerateTerrain("../assets/terrain/simple-4.tga"); |
|
107 |
- printError("init terrain"); |
|
108 |
-} |
|
109 |
- |
|
110 |
- |
|
111 |
-/// Display |
|
112 |
-void display(void) |
|
113 |
-{ |
|
114 |
- // Clear the screen. |
|
115 |
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
116 |
- |
|
117 |
- // Uniforms. |
|
118 |
- mat4 view = lookAt( |
|
119 |
- vec3(0, 5, 8), // Position. |
|
120 |
- vec3(2, 0, 2), // Target. |
|
121 |
- vec3(0, 1, 0) // Up. |
|
122 |
- ); |
|
123 |
- mat4 model = IdentityMatrix(); |
|
124 |
- mat4 modelView = view * model; |
|
125 |
- glUniformMatrix4fv(glGetUniformLocation(program, "modelView"), 1, GL_TRUE, modelView.m); |
|
126 |
- printError("display uniforms"); |
|
127 |
- |
|
128 |
- // Draw. |
|
129 |
- DrawModel(terrain, program, "inPosition", nullptr, "inTexCoord"); |
|
130 |
- printError("display draw"); |
|
131 |
- |
|
132 |
- // Show on the screen. |
|
133 |
- glutSwapBuffers(); |
|
134 |
-} |
|
135 |
- |
|
136 |
- |
|
137 |
-/// Mouse |
|
138 |
-void mouse(int x, int y) |
|
139 |
-{ |
|
140 |
- // This function is included in case you want some hints about using |
|
141 |
- // passive mouse movement. Uncomment to see mouse coordinates: |
|
142 |
- // printf("%d %d\n", x, y); |
|
143 |
-} |
|
144 |
- |
|
145 |
- |
|
146 |
-/// Main |
|
147 |
-int main(int argc, char * argv[]) |
|
148 |
-{ |
|
149 |
- glutInit(&argc, argv); |
|
150 |
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); |
|
151 |
- glutInitContextVersion(3, 2); |
|
152 |
- glutInitWindowSize(600, 600); |
|
153 |
- glutCreateWindow("TSBK07"); |
|
154 |
- |
|
155 |
- glutRepeatingTimer(20); |
|
156 |
- glutDisplayFunc(display); |
|
157 |
- glutPassiveMotionFunc(mouse); |
|
158 |
- |
|
159 |
- dumpInfo(); |
|
160 |
- init(); |
|
161 |
- glutMainLoop(); |
|
162 |
-} |
... | ... |
@@ -1,7 +1,4 @@ |
1 |
-// Lab 4, terrain generation |
|
2 |
- |
|
3 |
-// uses framework Cocoa |
|
4 |
-// uses framework OpenGL |
|
1 |
+/// Inlcudes |
|
5 | 2 |
#define MAIN |
6 | 3 |
#include "MicroGlut.h" |
7 | 4 |
#include "GL_utilities.h" |
... | ... |
@@ -9,149 +6,157 @@ |
9 | 6 |
#include "LittleOBJLoader.h" |
10 | 7 |
#include "LoadTGA.h" |
11 | 8 |
|
12 |
-mat4 projectionMatrix; |
|
13 | 9 |
|
14 |
-Model* GenerateTerrain(TextureData *tex) |
|
10 |
+/// Globals |
|
11 |
+GLuint program; |
|
12 |
+mat4 projection; |
|
13 |
+Model * terrain; |
|
14 |
+GLuint tex; |
|
15 |
+ |
|
16 |
+ |
|
17 |
+/// Generate terrain |
|
18 |
+Model * GenerateTerrain(const char * filename) |
|
15 | 19 |
{ |
16 |
- int vertexCount = tex->width * tex->height; |
|
17 |
- int triangleCount = (tex->width-1) * (tex->height-1) * 2; |
|
18 |
- unsigned int x, z; |
|
19 |
- |
|
20 |
- vec3 *vertexArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
21 |
- vec3 *normalArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
22 |
- vec2 *texCoordArray = (vec2 *)malloc(sizeof(GLfloat) * 2 * vertexCount); |
|
23 |
- GLuint *indexArray = (GLuint *) malloc(sizeof(GLuint) * triangleCount*3); |
|
24 |
- |
|
25 |
- printf("bpp %d\n", tex->bpp); |
|
26 |
- for (x = 0; x < tex->width; x++) |
|
27 |
- for (z = 0; z < tex->height; z++) |
|
28 |
- { |
|
29 |
-// Vertex array. You need to scale this properly |
|
30 |
- vertexArray[(x + z * tex->width)].x = x / 1.0; |
|
31 |
- vertexArray[(x + z * tex->width)].y = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 100.0; |
|
32 |
- vertexArray[(x + z * tex->width)].z = z / 1.0; |
|
33 |
-// Normal vectors. You need to calculate these. |
|
34 |
- normalArray[(x + z * tex->width)].x = 0.0; |
|
35 |
- normalArray[(x + z * tex->width)].y = 1.0; |
|
36 |
- normalArray[(x + z * tex->width)].z = 0.0; |
|
37 |
-// Texture coordinates. You may want to scale them. |
|
38 |
- texCoordArray[(x + z * tex->width)].x = x; // (float)x / tex->width; |
|
39 |
- texCoordArray[(x + z * tex->width)].y = z; // (float)z / tex->height; |
|
40 |
- } |
|
41 |
- for (x = 0; x < tex->width-1; x++) |
|
42 |
- for (z = 0; z < tex->height-1; z++) |
|
43 |
- { |
|
20 |
+ TextureData textureData; |
|
21 |
+ if (!LoadTGATextureData(filename, &textureData)) |
|
22 |
+ return nullptr; |
|
23 |
+ |
|
24 |
+ int vertexCount = textureData.width * textureData.height; |
|
25 |
+ int triangleCount = (textureData.width-1) * (textureData.height-1) * 2; |
|
26 |
+ |
|
27 |
+ vec3 * position = (vec3 *) malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
28 |
+ vec3 * normal = (vec3 *) malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
29 |
+ vec2 * texCoord = (vec2 *) malloc(sizeof(GLfloat) * 2 * vertexCount); |
|
30 |
+ GLuint * indices = (GLuint *)malloc(sizeof(GLuint) * 3 * triangleCount); |
|
31 |
+ |
|
32 |
+ int bytesPerPixel = textureData.bpp / 8; |
|
33 |
+ for (unsigned int x = 0; x < textureData.width; ++x) |
|
34 |
+ for (unsigned int z = 0; z < textureData.height; ++z) |
|
35 |
+ { |
|
36 |
+ int indexBase = (x + z * textureData.width); |
|
37 |
+ // Vertex array. You need to scale this properly. |
|
38 |
+ GLubyte data = textureData.imageData[indexBase * bytesPerPixel]; |
|
39 |
+ position[indexBase].x = x / 1.0; |
|
40 |
+ position[indexBase].y = data / 100.0; |
|
41 |
+ position[indexBase].z = z / 1.0; |
|
42 |
+ // Normal vectors. You need to calculate these. |
|
43 |
+ normal[indexBase].x = 0.0; |
|
44 |
+ normal[indexBase].y = 1.0; |
|
45 |
+ normal[indexBase].z = 0.0; |
|
46 |
+ // Texture coordinates. You may want to scale them. |
|
47 |
+ texCoord[indexBase].x = x; // (float)x / textureData.width; |
|
48 |
+ texCoord[indexBase].y = z; // (float)z / textureData.height; |
|
49 |
+ } |
|
50 |
+ for (unsigned int x = 0; x < textureData.width-1; ++x) |
|
51 |
+ for (unsigned int z = 0; z < textureData.height-1; ++z) |
|
52 |
+ { |
|
53 |
+ int indexBase = (x + z * (textureData.width-1))*6; |
|
44 | 54 |
// Triangle 1 |
45 |
- indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width; |
|
46 |
- indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width; |
|
47 |
- indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width; |
|
55 |
+ indices[indexBase + 0] = (x+0) + (z+0) * textureData.width; |
|
56 |
+ indices[indexBase + 1] = (x+0) + (z+1) * textureData.width; |
|
57 |
+ indices[indexBase + 2] = (x+1) + (z+0) * textureData.width; |
|
48 | 58 |
// Triangle 2 |
49 |
- indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width; |
|
50 |
- indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width; |
|
51 |
- indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width; |
|
52 |
- } |
|
53 |
- |
|
54 |
- // End of terrain generation |
|
55 |
- |
|
56 |
- // Create Model and upload to GPU: |
|
57 |
- |
|
58 |
- Model* model = LoadDataToModel( |
|
59 |
- vertexArray, |
|
60 |
- normalArray, |
|
61 |
- texCoordArray, |
|
62 |
- NULL, |
|
63 |
- indexArray, |
|
64 |
- vertexCount, |
|
65 |
- triangleCount*3); |
|
59 |
+ indices[indexBase + 3] = (x+1) + (z+0) * textureData.width; |
|
60 |
+ indices[indexBase + 4] = (x+0) + (z+1) * textureData.width; |
|
61 |
+ indices[indexBase + 5] = (x+1) + (z+1) * textureData.width; |
|
62 |
+ } |
|
66 | 63 |
|
64 |
+ // Create Model and upload to GPU: |
|
65 |
+ Model * model = LoadDataToModel( |
|
66 |
+ position, |
|
67 |
+ normal, |
|
68 |
+ texCoord, |
|
69 |
+ nullptr, |
|
70 |
+ indices, |
|
71 |
+ vertexCount, |
|
72 |
+ triangleCount * 3 |
|
73 |
+ ); |
|
74 |
+ |
|
75 |
+ free(textureData.imageData); |
|
67 | 76 |
return model; |
68 | 77 |
} |
69 | 78 |
|
70 | 79 |
|
71 |
-// vertex array object |
|
72 |
-Model *m, *m2, *tm; |
|
73 |
-// Reference to shader program |
|
74 |
-GLuint program; |
|
75 |
-GLuint tex1, tex2; |
|
76 |
-TextureData ttex; // terrain |
|
77 |
- |
|
80 |
+/// Init |
|
78 | 81 |
void init(void) |
79 | 82 |
{ |
80 |
- // GL inits |
|
81 |
- glClearColor(0.2,0.2,0.5,0); |
|
83 |
+ // GL inits. |
|
84 |
+ glClearColor(0.2, 0.2, 0.5, 0.0); |
|
82 | 85 |
glEnable(GL_DEPTH_TEST); |
83 | 86 |
glDisable(GL_CULL_FACE); |
84 |
- printError("GL inits"); |
|
85 |
- |
|
86 |
- projectionMatrix = frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 50.0); |
|
87 |
+ printError("init GL"); |
|
87 | 88 |
|
88 |
- // Load and compile shader |
|
89 |
+ // Load and compile shader. |
|
89 | 90 |
program = loadShaders("terrain.vert", "terrain.frag"); |
90 | 91 |
glUseProgram(program); |
91 | 92 |
printError("init shader"); |
92 |
- |
|
93 |
- glUniformMatrix4fv(glGetUniformLocation(program, "projMatrix"), 1, GL_TRUE, projectionMatrix.m); |
|
94 |
- glUniform1i(glGetUniformLocation(program, "tex"), 0); // Texture unit 0 |
|
95 |
- LoadTGATextureSimple("maskros512.tga", &tex1); |
|
96 |
- |
|
97 |
-// Load terrain data |
|
98 |
- |
|
99 |
- LoadTGATextureData("44-terrain.tga", &ttex); |
|
100 |
- tm = GenerateTerrain(&ttex); |
|
101 |
- printError("init terrain"); |
|
102 | 93 |
|
103 |
- printf("Note: The call to DrawModel will report warnings about inNormal not existing. This is because inNormal is not used in the shader yet so it is optimized away.\n"); |
|
94 |
+ // Matrices. |
|
95 |
+ projection = frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 50.0); |
|
96 |
+ glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_TRUE, projection.m); |
|
97 |
+ printError("init matrices"); |
|
98 |
+ |
|
99 |
+ // Textures. |
|
100 |
+ LoadTGATextureSimple("../assets/terrain/dandelion.tga", &tex); |
|
101 |
+ glBindTexture(GL_TEXTURE_2D, tex); |
|
102 |
+ glUniform1i(glGetUniformLocation(program, "tex"), 0); |
|
103 |
+ printError("init textures"); |
|
104 |
+ |
|
105 |
+ // Generate terrain. |
|
106 |
+ terrain = GenerateTerrain("../assets/terrain/simple-4.tga"); |
|
107 |
+ printError("init terrain"); |
|
104 | 108 |
} |
105 | 109 |
|
110 |
+ |
|
111 |
+/// Display |
|
106 | 112 |
void display(void) |
107 | 113 |
{ |
108 |
- // clear the screen |
|
114 |
+ // Clear the screen. |
|
109 | 115 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
110 |
- |
|
111 |
- mat4 total, modelView, camMatrix; |
|
112 |
- |
|
113 |
- printError("pre display"); |
|
114 |
- |
|
115 |
- glUseProgram(program); |
|
116 | 116 |
|
117 |
- // Build matrix |
|
118 |
- |
|
119 |
- vec3 cam = vec3(0, 5, 8); |
|
120 |
- vec3 lookAtPoint = vec3(2, 0, 2); |
|
121 |
- camMatrix = lookAt(cam.x, cam.y, cam.z, |
|
122 |
- lookAtPoint.x, lookAtPoint.y, lookAtPoint.z, |
|
123 |
- 0.0, 1.0, 0.0); |
|
124 |
- modelView = IdentityMatrix(); |
|
125 |
- total = camMatrix * modelView; |
|
126 |
- glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m); |
|
127 |
- |
|
128 |
- glBindTexture(GL_TEXTURE_2D, tex1); // Bind Our Texture tex1 |
|
129 |
- DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord"); |
|
130 |
- |
|
131 |
- printError("display 2"); |
|
132 |
- |
|
117 |
+ // Uniforms. |
|
118 |
+ mat4 view = lookAt( |
|
119 |
+ vec3(0, 5, 8), // Position. |
|
120 |
+ vec3(2, 0, 2), // Target. |
|
121 |
+ vec3(0, 1, 0) // Up. |
|
122 |
+ ); |
|
123 |
+ mat4 model = IdentityMatrix(); |
|
124 |
+ mat4 modelView = view * model; |
|
125 |
+ glUniformMatrix4fv(glGetUniformLocation(program, "modelView"), 1, GL_TRUE, modelView.m); |
|
126 |
+ printError("display uniforms"); |
|
127 |
+ |
|
128 |
+ // Draw. |
|
129 |
+ DrawModel(terrain, program, "inPosition", nullptr, "inTexCoord"); |
|
130 |
+ printError("display draw"); |
|
131 |
+ |
|
132 |
+ // Show on the screen. |
|
133 | 133 |
glutSwapBuffers(); |
134 | 134 |
} |
135 | 135 |
|
136 |
+ |
|
137 |
+/// Mouse |
|
136 | 138 |
void mouse(int x, int y) |
137 | 139 |
{ |
138 |
- // This function is included in case you want some hints about using passive mouse movement. |
|
139 |
- // Uncomment to see mouse coordinates: |
|
140 |
+ // This function is included in case you want some hints about using |
|
141 |
+ // passive mouse movement. Uncomment to see mouse coordinates: |
|
140 | 142 |
// printf("%d %d\n", x, y); |
141 | 143 |
} |
142 |
-int main(int argc, char **argv) |
|
144 |
+ |
|
145 |
+ |
|
146 |
+/// Main |
|
147 |
+int main(int argc, char * argv[]) |
|
143 | 148 |
{ |
144 | 149 |
glutInit(&argc, argv); |
145 | 150 |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); |
146 | 151 |
glutInitContextVersion(3, 2); |
147 |
- glutInitWindowSize (600, 600); |
|
148 |
- glutCreateWindow ("TSBK07 Lab 4"); |
|
149 |
- glutDisplayFunc(display); |
|
150 |
- init (); |
|
152 |
+ glutInitWindowSize(600, 600); |
|
153 |
+ glutCreateWindow("TSBK07"); |
|
154 |
+ |
|
151 | 155 |
glutRepeatingTimer(20); |
152 |
- |
|
156 |
+ glutDisplayFunc(display); |
|
153 | 157 |
glutPassiveMotionFunc(mouse); |
154 | 158 |
|
159 |
+ dumpInfo(); |
|
160 |
+ init(); |
|
155 | 161 |
glutMainLoop(); |
156 |
- exit(0); |
|
157 | 162 |
} |
find . -type f -exec chmod -x {} +
Robert Cranston authored on 27/02/2024 02:27:271 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,157 @@ |
1 |
+// Lab 4, terrain generation |
|
2 |
+ |
|
3 |
+// uses framework Cocoa |
|
4 |
+// uses framework OpenGL |
|
5 |
+#define MAIN |
|
6 |
+#include "MicroGlut.h" |
|
7 |
+#include "GL_utilities.h" |
|
8 |
+#include "VectorUtils4.h" |
|
9 |
+#include "LittleOBJLoader.h" |
|
10 |
+#include "LoadTGA.h" |
|
11 |
+ |
|
12 |
+mat4 projectionMatrix; |
|
13 |
+ |
|
14 |
+Model* GenerateTerrain(TextureData *tex) |
|
15 |
+{ |
|
16 |
+ int vertexCount = tex->width * tex->height; |
|
17 |
+ int triangleCount = (tex->width-1) * (tex->height-1) * 2; |
|
18 |
+ unsigned int x, z; |
|
19 |
+ |
|
20 |
+ vec3 *vertexArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
21 |
+ vec3 *normalArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
22 |
+ vec2 *texCoordArray = (vec2 *)malloc(sizeof(GLfloat) * 2 * vertexCount); |
|
23 |
+ GLuint *indexArray = (GLuint *) malloc(sizeof(GLuint) * triangleCount*3); |
|
24 |
+ |
|
25 |
+ printf("bpp %d\n", tex->bpp); |
|
26 |
+ for (x = 0; x < tex->width; x++) |
|
27 |
+ for (z = 0; z < tex->height; z++) |
|
28 |
+ { |
|
29 |
+// Vertex array. You need to scale this properly |
|
30 |
+ vertexArray[(x + z * tex->width)].x = x / 1.0; |
|
31 |
+ vertexArray[(x + z * tex->width)].y = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 100.0; |
|
32 |
+ vertexArray[(x + z * tex->width)].z = z / 1.0; |
|
33 |
+// Normal vectors. You need to calculate these. |
|
34 |
+ normalArray[(x + z * tex->width)].x = 0.0; |
|
35 |
+ normalArray[(x + z * tex->width)].y = 1.0; |
|
36 |
+ normalArray[(x + z * tex->width)].z = 0.0; |
|
37 |
+// Texture coordinates. You may want to scale them. |
|
38 |
+ texCoordArray[(x + z * tex->width)].x = x; // (float)x / tex->width; |
|
39 |
+ texCoordArray[(x + z * tex->width)].y = z; // (float)z / tex->height; |
|
40 |
+ } |
|
41 |
+ for (x = 0; x < tex->width-1; x++) |
|
42 |
+ for (z = 0; z < tex->height-1; z++) |
|
43 |
+ { |
|
44 |
+ // Triangle 1 |
|
45 |
+ indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width; |
|
46 |
+ indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width; |
|
47 |
+ indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width; |
|
48 |
+ // Triangle 2 |
|
49 |
+ indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width; |
|
50 |
+ indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width; |
|
51 |
+ indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width; |
|
52 |
+ } |
|
53 |
+ |
|
54 |
+ // End of terrain generation |
|
55 |
+ |
|
56 |
+ // Create Model and upload to GPU: |
|
57 |
+ |
|
58 |
+ Model* model = LoadDataToModel( |
|
59 |
+ vertexArray, |
|
60 |
+ normalArray, |
|
61 |
+ texCoordArray, |
|
62 |
+ NULL, |
|
63 |
+ indexArray, |
|
64 |
+ vertexCount, |
|
65 |
+ triangleCount*3); |
|
66 |
+ |
|
67 |
+ return model; |
|
68 |
+} |
|
69 |
+ |
|
70 |
+ |
|
71 |
+// vertex array object |
|
72 |
+Model *m, *m2, *tm; |
|
73 |
+// Reference to shader program |
|
74 |
+GLuint program; |
|
75 |
+GLuint tex1, tex2; |
|
76 |
+TextureData ttex; // terrain |
|
77 |
+ |
|
78 |
+void init(void) |
|
79 |
+{ |
|
80 |
+ // GL inits |
|
81 |
+ glClearColor(0.2,0.2,0.5,0); |
|
82 |
+ glEnable(GL_DEPTH_TEST); |
|
83 |
+ glDisable(GL_CULL_FACE); |
|
84 |
+ printError("GL inits"); |
|
85 |
+ |
|
86 |
+ projectionMatrix = frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 50.0); |
|
87 |
+ |
|
88 |
+ // Load and compile shader |
|
89 |
+ program = loadShaders("terrain.vert", "terrain.frag"); |
|
90 |
+ glUseProgram(program); |
|
91 |
+ printError("init shader"); |
|
92 |
+ |
|
93 |
+ glUniformMatrix4fv(glGetUniformLocation(program, "projMatrix"), 1, GL_TRUE, projectionMatrix.m); |
|
94 |
+ glUniform1i(glGetUniformLocation(program, "tex"), 0); // Texture unit 0 |
|
95 |
+ LoadTGATextureSimple("maskros512.tga", &tex1); |
|
96 |
+ |
|
97 |
+// Load terrain data |
|
98 |
+ |
|
99 |
+ LoadTGATextureData("44-terrain.tga", &ttex); |
|
100 |
+ tm = GenerateTerrain(&ttex); |
|
101 |
+ printError("init terrain"); |
|
102 |
+ |
|
103 |
+ printf("Note: The call to DrawModel will report warnings about inNormal not existing. This is because inNormal is not used in the shader yet so it is optimized away.\n"); |
|
104 |
+} |
|
105 |
+ |
|
106 |
+void display(void) |
|
107 |
+{ |
|
108 |
+ // clear the screen |
|
109 |
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
110 |
+ |
|
111 |
+ mat4 total, modelView, camMatrix; |
|
112 |
+ |
|
113 |
+ printError("pre display"); |
|
114 |
+ |
|
115 |
+ glUseProgram(program); |
|
116 |
+ |
|
117 |
+ // Build matrix |
|
118 |
+ |
|
119 |
+ vec3 cam = vec3(0, 5, 8); |
|
120 |
+ vec3 lookAtPoint = vec3(2, 0, 2); |
|
121 |
+ camMatrix = lookAt(cam.x, cam.y, cam.z, |
|
122 |
+ lookAtPoint.x, lookAtPoint.y, lookAtPoint.z, |
|
123 |
+ 0.0, 1.0, 0.0); |
|
124 |
+ modelView = IdentityMatrix(); |
|
125 |
+ total = camMatrix * modelView; |
|
126 |
+ glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m); |
|
127 |
+ |
|
128 |
+ glBindTexture(GL_TEXTURE_2D, tex1); // Bind Our Texture tex1 |
|
129 |
+ DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord"); |
|
130 |
+ |
|
131 |
+ printError("display 2"); |
|
132 |
+ |
|
133 |
+ glutSwapBuffers(); |
|
134 |
+} |
|
135 |
+ |
|
136 |
+void mouse(int x, int y) |
|
137 |
+{ |
|
138 |
+ // This function is included in case you want some hints about using passive mouse movement. |
|
139 |
+ // Uncomment to see mouse coordinates: |
|
140 |
+ // printf("%d %d\n", x, y); |
|
141 |
+} |
|
142 |
+int main(int argc, char **argv) |
|
143 |
+{ |
|
144 |
+ glutInit(&argc, argv); |
|
145 |
+ glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); |
|
146 |
+ glutInitContextVersion(3, 2); |
|
147 |
+ glutInitWindowSize (600, 600); |
|
148 |
+ glutCreateWindow ("TSBK07 Lab 4"); |
|
149 |
+ glutDisplayFunc(display); |
|
150 |
+ init (); |
|
151 |
+ glutRepeatingTimer(20); |
|
152 |
+ |
|
153 |
+ glutPassiveMotionFunc(mouse); |
|
154 |
+ |
|
155 |
+ glutMainLoop(); |
|
156 |
+ exit(0); |
|
157 |
+} |