| 1 | 1 |
deleted file mode 100755 |
| ... | ... |
@@ -1,159 +0,0 @@ |
| 1 |
-// Lab 4, terrain generation |
|
| 2 |
- |
|
| 3 |
- // uses framework Cocoa |
|
| 4 |
- // uses framework OpenGL |
|
| 5 |
-#include "MicroGlut.h" |
|
| 6 |
-#include "GL_utilities.h" |
|
| 7 |
-#include "VectorUtils3.h" |
|
| 8 |
-#include "LittleOBJLoader.h" |
|
| 9 |
-#include "LoadTGA.h" |
|
| 10 |
- |
|
| 11 |
-mat4 projectionMatrix; |
|
| 12 |
- |
|
| 13 |
-Model* GenerateTerrain(TextureData *tex) |
|
| 14 |
-{
|
|
| 15 |
- int vertexCount = tex->width * tex->height; |
|
| 16 |
- int triangleCount = (tex->width-1) * (tex->height-1) * 2; |
|
| 17 |
- int x, z; |
|
| 18 |
- |
|
| 19 |
- vec3 *vertexArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
| 20 |
- vec3 *normalArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
| 21 |
- vec2 *texCoordArray = (vec2 *)malloc(sizeof(GLfloat) * 2 * vertexCount); |
|
| 22 |
- GLuint *indexArray = malloc(sizeof(GLuint) * triangleCount*3); |
|
| 23 |
- |
|
| 24 |
- printf("bpp %d\n", tex->bpp);
|
|
| 25 |
- for (x = 0; x < tex->width; x++) |
|
| 26 |
- for (z = 0; z < tex->height; z++) |
|
| 27 |
- {
|
|
| 28 |
-// Vertex array. You need to scale this properly |
|
| 29 |
- vertexArray[(x + z * tex->width)].x = x / 1.0; |
|
| 30 |
- vertexArray[(x + z * tex->width)].y = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 100.0; |
|
| 31 |
- vertexArray[(x + z * tex->width)].z = z / 1.0; |
|
| 32 |
-// Normal vectors. You need to calculate these. |
|
| 33 |
- normalArray[(x + z * tex->width)].x = 0.0; |
|
| 34 |
- normalArray[(x + z * tex->width)].y = 1.0; |
|
| 35 |
- normalArray[(x + z * tex->width)].z = 0.0; |
|
| 36 |
-// Texture coordinates. You may want to scale them. |
|
| 37 |
- texCoordArray[(x + z * tex->width)].x = x; // (float)x / tex->width; |
|
| 38 |
- texCoordArray[(x + z * tex->width)].y = z; // (float)z / tex->height; |
|
| 39 |
- } |
|
| 40 |
- for (x = 0; x < tex->width-1; x++) |
|
| 41 |
- for (z = 0; z < tex->height-1; z++) |
|
| 42 |
- {
|
|
| 43 |
- // Triangle 1 |
|
| 44 |
- indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width; |
|
| 45 |
- indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width; |
|
| 46 |
- indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width; |
|
| 47 |
- // Triangle 2 |
|
| 48 |
- indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width; |
|
| 49 |
- indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width; |
|
| 50 |
- indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width; |
|
| 51 |
- } |
|
| 52 |
- |
|
| 53 |
- // End of terrain generation |
|
| 54 |
- |
|
| 55 |
- // Create Model and upload to GPU: |
|
| 56 |
- |
|
| 57 |
- Model* model = LoadDataToModel( |
|
| 58 |
- vertexArray, |
|
| 59 |
- normalArray, |
|
| 60 |
- texCoordArray, |
|
| 61 |
- NULL, |
|
| 62 |
- indexArray, |
|
| 63 |
- vertexCount, |
|
| 64 |
- triangleCount*3); |
|
| 65 |
- |
|
| 66 |
- return model; |
|
| 67 |
-} |
|
| 68 |
- |
|
| 69 |
- |
|
| 70 |
-// vertex array object |
|
| 71 |
-Model *m, *m2, *tm; |
|
| 72 |
-// Reference to shader program |
|
| 73 |
-GLuint program; |
|
| 74 |
-GLuint tex1, tex2; |
|
| 75 |
-TextureData ttex; // terrain |
|
| 76 |
- |
|
| 77 |
-void init(void) |
|
| 78 |
-{
|
|
| 79 |
- // GL inits |
|
| 80 |
- glClearColor(0.4,0.6,1.0,0); |
|
| 81 |
- glEnable(GL_DEPTH_TEST); |
|
| 82 |
- glDisable(GL_CULL_FACE); |
|
| 83 |
- printError("GL inits");
|
|
| 84 |
- |
|
| 85 |
- projectionMatrix = frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 50.0); |
|
| 86 |
- |
|
| 87 |
- // Load and compile shader |
|
| 88 |
- program = loadShaders("terrain.vert", "terrain.frag");
|
|
| 89 |
- glUseProgram(program); |
|
| 90 |
- printError("init shader");
|
|
| 91 |
- |
|
| 92 |
- glUniformMatrix4fv(glGetUniformLocation(program, "projMatrix"), 1, GL_TRUE, projectionMatrix.m); |
|
| 93 |
- glUniform1i(glGetUniformLocation(program, "tex"), 0); // Texture unit 0 |
|
| 94 |
- LoadTGATextureSimple("maskros512.tga", &tex1);
|
|
| 95 |
- |
|
| 96 |
-// Load terrain data |
|
| 97 |
- |
|
| 98 |
- LoadTGATextureData("44-terrain.tga", &ttex);
|
|
| 99 |
- tm = GenerateTerrain(&ttex); |
|
| 100 |
- printError("init terrain");
|
|
| 101 |
- |
|
| 102 |
- 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");
|
|
| 103 |
- |
|
| 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 = {0, 5, 8};
|
|
| 120 |
- vec3 lookAtPoint = {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 = Mult(camMatrix, modelView); |
|
| 126 |
- glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m); |
|
| 127 |
- |
|
| 128 |
- glBindTexture(GL_TEXTURE_2D, tex1); // Bind Our Texture tex1 |
|
| 129 |
- |
|
| 130 |
- DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord"); |
|
| 131 |
- |
|
| 132 |
- printError("display 2");
|
|
| 133 |
- |
|
| 134 |
- glutSwapBuffers(); |
|
| 135 |
-} |
|
| 136 |
- |
|
| 137 |
-void mouse(int x, int y) |
|
| 138 |
-{
|
|
| 139 |
- // This function is included in case you want some hints about using passive mouse movement. |
|
| 140 |
- // Uncomment to see mouse coordinates: |
|
| 141 |
- // printf("%d %d\n", x, y);
|
|
| 142 |
-} |
|
| 143 |
- |
|
| 144 |
-int main(int argc, char **argv) |
|
| 145 |
-{
|
|
| 146 |
- glutInit(&argc, argv); |
|
| 147 |
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); |
|
| 148 |
- glutInitContextVersion(3, 2); |
|
| 149 |
- glutInitWindowSize (600, 600); |
|
| 150 |
- glutCreateWindow ("TSBK07 Lab 4");
|
|
| 151 |
- glutDisplayFunc(display); |
|
| 152 |
- init (); |
|
| 153 |
- glutRepeatingTimer(20); |
|
| 154 |
- |
|
| 155 |
- glutPassiveMotionFunc(mouse); |
|
| 156 |
- |
|
| 157 |
- glutMainLoop(); |
|
| 158 |
- exit(0); |
|
| 159 |
-} |
| 1 | 1 |
new file mode 100755 |
| ... | ... |
@@ -0,0 +1,159 @@ |
| 1 |
+// Lab 4, terrain generation |
|
| 2 |
+ |
|
| 3 |
+ // uses framework Cocoa |
|
| 4 |
+ // uses framework OpenGL |
|
| 5 |
+#include "MicroGlut.h" |
|
| 6 |
+#include "GL_utilities.h" |
|
| 7 |
+#include "VectorUtils3.h" |
|
| 8 |
+#include "LittleOBJLoader.h" |
|
| 9 |
+#include "LoadTGA.h" |
|
| 10 |
+ |
|
| 11 |
+mat4 projectionMatrix; |
|
| 12 |
+ |
|
| 13 |
+Model* GenerateTerrain(TextureData *tex) |
|
| 14 |
+{
|
|
| 15 |
+ int vertexCount = tex->width * tex->height; |
|
| 16 |
+ int triangleCount = (tex->width-1) * (tex->height-1) * 2; |
|
| 17 |
+ int x, z; |
|
| 18 |
+ |
|
| 19 |
+ vec3 *vertexArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
| 20 |
+ vec3 *normalArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount); |
|
| 21 |
+ vec2 *texCoordArray = (vec2 *)malloc(sizeof(GLfloat) * 2 * vertexCount); |
|
| 22 |
+ GLuint *indexArray = malloc(sizeof(GLuint) * triangleCount*3); |
|
| 23 |
+ |
|
| 24 |
+ printf("bpp %d\n", tex->bpp);
|
|
| 25 |
+ for (x = 0; x < tex->width; x++) |
|
| 26 |
+ for (z = 0; z < tex->height; z++) |
|
| 27 |
+ {
|
|
| 28 |
+// Vertex array. You need to scale this properly |
|
| 29 |
+ vertexArray[(x + z * tex->width)].x = x / 1.0; |
|
| 30 |
+ vertexArray[(x + z * tex->width)].y = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 100.0; |
|
| 31 |
+ vertexArray[(x + z * tex->width)].z = z / 1.0; |
|
| 32 |
+// Normal vectors. You need to calculate these. |
|
| 33 |
+ normalArray[(x + z * tex->width)].x = 0.0; |
|
| 34 |
+ normalArray[(x + z * tex->width)].y = 1.0; |
|
| 35 |
+ normalArray[(x + z * tex->width)].z = 0.0; |
|
| 36 |
+// Texture coordinates. You may want to scale them. |
|
| 37 |
+ texCoordArray[(x + z * tex->width)].x = x; // (float)x / tex->width; |
|
| 38 |
+ texCoordArray[(x + z * tex->width)].y = z; // (float)z / tex->height; |
|
| 39 |
+ } |
|
| 40 |
+ for (x = 0; x < tex->width-1; x++) |
|
| 41 |
+ for (z = 0; z < tex->height-1; z++) |
|
| 42 |
+ {
|
|
| 43 |
+ // Triangle 1 |
|
| 44 |
+ indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width; |
|
| 45 |
+ indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width; |
|
| 46 |
+ indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width; |
|
| 47 |
+ // Triangle 2 |
|
| 48 |
+ indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width; |
|
| 49 |
+ indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width; |
|
| 50 |
+ indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width; |
|
| 51 |
+ } |
|
| 52 |
+ |
|
| 53 |
+ // End of terrain generation |
|
| 54 |
+ |
|
| 55 |
+ // Create Model and upload to GPU: |
|
| 56 |
+ |
|
| 57 |
+ Model* model = LoadDataToModel( |
|
| 58 |
+ vertexArray, |
|
| 59 |
+ normalArray, |
|
| 60 |
+ texCoordArray, |
|
| 61 |
+ NULL, |
|
| 62 |
+ indexArray, |
|
| 63 |
+ vertexCount, |
|
| 64 |
+ triangleCount*3); |
|
| 65 |
+ |
|
| 66 |
+ return model; |
|
| 67 |
+} |
|
| 68 |
+ |
|
| 69 |
+ |
|
| 70 |
+// vertex array object |
|
| 71 |
+Model *m, *m2, *tm; |
|
| 72 |
+// Reference to shader program |
|
| 73 |
+GLuint program; |
|
| 74 |
+GLuint tex1, tex2; |
|
| 75 |
+TextureData ttex; // terrain |
|
| 76 |
+ |
|
| 77 |
+void init(void) |
|
| 78 |
+{
|
|
| 79 |
+ // GL inits |
|
| 80 |
+ glClearColor(0.4,0.6,1.0,0); |
|
| 81 |
+ glEnable(GL_DEPTH_TEST); |
|
| 82 |
+ glDisable(GL_CULL_FACE); |
|
| 83 |
+ printError("GL inits");
|
|
| 84 |
+ |
|
| 85 |
+ projectionMatrix = frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 50.0); |
|
| 86 |
+ |
|
| 87 |
+ // Load and compile shader |
|
| 88 |
+ program = loadShaders("terrain.vert", "terrain.frag");
|
|
| 89 |
+ glUseProgram(program); |
|
| 90 |
+ printError("init shader");
|
|
| 91 |
+ |
|
| 92 |
+ glUniformMatrix4fv(glGetUniformLocation(program, "projMatrix"), 1, GL_TRUE, projectionMatrix.m); |
|
| 93 |
+ glUniform1i(glGetUniformLocation(program, "tex"), 0); // Texture unit 0 |
|
| 94 |
+ LoadTGATextureSimple("maskros512.tga", &tex1);
|
|
| 95 |
+ |
|
| 96 |
+// Load terrain data |
|
| 97 |
+ |
|
| 98 |
+ LoadTGATextureData("44-terrain.tga", &ttex);
|
|
| 99 |
+ tm = GenerateTerrain(&ttex); |
|
| 100 |
+ printError("init terrain");
|
|
| 101 |
+ |
|
| 102 |
+ 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");
|
|
| 103 |
+ |
|
| 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 = {0, 5, 8};
|
|
| 120 |
+ vec3 lookAtPoint = {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 = Mult(camMatrix, modelView); |
|
| 126 |
+ glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m); |
|
| 127 |
+ |
|
| 128 |
+ glBindTexture(GL_TEXTURE_2D, tex1); // Bind Our Texture tex1 |
|
| 129 |
+ |
|
| 130 |
+ DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord"); |
|
| 131 |
+ |
|
| 132 |
+ printError("display 2");
|
|
| 133 |
+ |
|
| 134 |
+ glutSwapBuffers(); |
|
| 135 |
+} |
|
| 136 |
+ |
|
| 137 |
+void mouse(int x, int y) |
|
| 138 |
+{
|
|
| 139 |
+ // This function is included in case you want some hints about using passive mouse movement. |
|
| 140 |
+ // Uncomment to see mouse coordinates: |
|
| 141 |
+ // printf("%d %d\n", x, y);
|
|
| 142 |
+} |
|
| 143 |
+ |
|
| 144 |
+int main(int argc, char **argv) |
|
| 145 |
+{
|
|
| 146 |
+ glutInit(&argc, argv); |
|
| 147 |
+ glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); |
|
| 148 |
+ glutInitContextVersion(3, 2); |
|
| 149 |
+ glutInitWindowSize (600, 600); |
|
| 150 |
+ glutCreateWindow ("TSBK07 Lab 4");
|
|
| 151 |
+ glutDisplayFunc(display); |
|
| 152 |
+ init (); |
|
| 153 |
+ glutRepeatingTimer(20); |
|
| 154 |
+ |
|
| 155 |
+ glutPassiveMotionFunc(mouse); |
|
| 156 |
+ |
|
| 157 |
+ glutMainLoop(); |
|
| 158 |
+ exit(0); |
|
| 159 |
+} |