... | ... |
@@ -1,87 +1,80 @@ |
1 |
-// Lab 1-1. |
|
2 |
-// This is the same as the first simple example in the course book, |
|
3 |
-// but with a few error checks. |
|
4 |
-// Remember to copy your file to a new on appropriate places during the lab so you keep old results. |
|
5 |
-// Note that the files "lab1-1.frag", "lab1-1.vert" are required. |
|
6 |
- |
|
7 |
-#include "GL_utilities.h" |
|
1 |
+/// Inlcudes |
|
8 | 2 |
#include "MicroGlut.h" |
9 |
-// uses framework OpenGL |
|
10 |
-// uses framework Cocoa |
|
3 |
+#include "GL_utilities.h" |
|
4 |
+ |
|
5 |
+ |
|
6 |
+/// Defines |
|
7 |
+#define ARRAY_COUNT(ARRAY) (sizeof(ARRAY)/sizeof(*(ARRAY))) |
|
11 | 8 |
|
12 |
-// Globals |
|
13 |
-// Data would normally be read from files |
|
14 |
-GLfloat vertices[] = |
|
9 |
+ |
|
10 |
+/// Globals |
|
11 |
+GLuint vertexArray; |
|
12 |
+GLfloat positions[] = |
|
15 | 13 |
{ |
16 |
- -0.5f,-0.5f,0.0f, |
|
17 |
- -0.5f,0.5f,0.0f, |
|
18 |
- 0.5f,-0.5f,0.0f |
|
14 |
+ -0.5f, -0.5f, 0.0f, |
|
15 |
+ -0.5f, +0.5f, 0.0f, |
|
16 |
+ +0.5f, -0.5f, 0.0f, |
|
19 | 17 |
}; |
20 | 18 |
|
21 |
-// vertex array object |
|
22 |
-unsigned int vertexArrayObjID; |
|
23 | 19 |
|
20 |
+/// Init |
|
24 | 21 |
void init(void) |
25 | 22 |
{ |
26 |
- // vertex buffer object, used for uploading the geometry |
|
27 |
- unsigned int vertexBufferObjID; |
|
28 |
- // Reference to shader program |
|
29 |
- GLuint program; |
|
30 |
- |
|
31 |
- dumpInfo(); |
|
32 |
- |
|
33 |
- // GL inits |
|
34 |
- glClearColor(0.2,0.2,0.5,0); |
|
23 |
+ // GL inits. |
|
24 |
+ glClearColor(0.2, 0.2, 0.5, 0.0); |
|
35 | 25 |
glDisable(GL_DEPTH_TEST); |
36 |
- printError("GL inits"); |
|
26 |
+ printError("init GL"); |
|
37 | 27 |
|
38 |
- // Load and compile shader |
|
39 |
- program = loadShaders("lab1-1.vert", "lab1-1.frag"); |
|
28 |
+ // Load and compile shader. |
|
29 |
+ GLuint program = loadShaders("lab1-1.vert", "lab1-1.frag"); |
|
40 | 30 |
printError("init shader"); |
41 |
- |
|
42 |
- // Upload geometry to the GPU: |
|
43 |
- |
|
44 |
- // Allocate and activate Vertex Array Object |
|
45 |
- glGenVertexArrays(1, &vertexArrayObjID); |
|
46 |
- glBindVertexArray(vertexArrayObjID); |
|
47 |
- // Allocate Vertex Buffer Objects |
|
48 |
- glGenBuffers(1, &vertexBufferObjID); |
|
49 |
- |
|
50 |
- // VBO for vertex data |
|
51 |
- glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID); |
|
52 |
- glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), vertices, GL_STATIC_DRAW); |
|
53 |
- glVertexAttribPointer(glGetAttribLocation(program, "in_Position"), 3, GL_FLOAT, GL_FALSE, 0, 0); |
|
54 |
- glEnableVertexAttribArray(glGetAttribLocation(program, "in_Position")); |
|
55 |
- |
|
56 |
- // End of upload of geometry |
|
57 |
- |
|
58 |
- printError("init arrays"); |
|
31 |
+ |
|
32 |
+ // Allocate and activate Vertex Array Object (VAO), used for uploading |
|
33 |
+ // the geometry. |
|
34 |
+ glGenVertexArrays(1, &vertexArray); |
|
35 |
+ glBindVertexArray(vertexArray); |
|
36 |
+ |
|
37 |
+ // Allocate and activate Vertex Buffer Objects (VBO), for vertex data. |
|
38 |
+ GLint positionLocation = glGetAttribLocation(program, "inPosition"); |
|
39 |
+ GLuint positionBuffer; |
|
40 |
+ glGenBuffers(1, &positionBuffer); |
|
41 |
+ glBindBuffer(GL_ARRAY_BUFFER, positionBuffer); |
|
42 |
+ glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); |
|
43 |
+ glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); |
|
44 |
+ glEnableVertexAttribArray(positionLocation); |
|
45 |
+ printError("init VBOs"); |
|
59 | 46 |
} |
60 | 47 |
|
61 | 48 |
|
49 |
+/// Display |
|
62 | 50 |
void display(void) |
63 | 51 |
{ |
64 |
- printError("pre display"); |
|
65 |
- |
|
66 |
- // clear the screen |
|
52 |
+ // Clear the screen. |
|
67 | 53 |
glClear(GL_COLOR_BUFFER_BIT); |
68 | 54 |
|
69 |
- glBindVertexArray(vertexArrayObjID); // Select VAO |
|
70 |
- glDrawArrays(GL_TRIANGLES, 0, 3); // draw object |
|
71 |
- |
|
72 |
- printError("display"); |
|
73 |
- |
|
55 |
+ // Select VAO. |
|
56 |
+ glBindVertexArray(vertexArray); |
|
57 |
+ |
|
58 |
+ // Draw. |
|
59 |
+ glDrawArrays(GL_TRIANGLES, 0, ARRAY_COUNT(positions)/3); |
|
60 |
+ printError("display draw"); |
|
61 |
+ |
|
62 |
+ // Show on the screen. |
|
74 | 63 |
glutSwapBuffers(); |
75 | 64 |
} |
76 | 65 |
|
77 |
-int main(int argc, char *argv[]) |
|
66 |
+ |
|
67 |
+/// Main |
|
68 |
+int main(int argc, char * argv[]) |
|
78 | 69 |
{ |
79 | 70 |
glutInit(&argc, argv); |
80 | 71 |
glutInitContextVersion(3, 2); |
81 | 72 |
glutInitWindowSize(600, 600); |
82 |
- glutCreateWindow ("GL3 white triangle example"); |
|
83 |
- glutDisplayFunc(display); |
|
84 |
- init (); |
|
73 |
+ glutCreateWindow("TSBK07"); |
|
74 |
+ |
|
75 |
+ glutDisplayFunc(display); |
|
76 |
+ |
|
77 |
+ dumpInfo(); |
|
78 |
+ init(); |
|
85 | 79 |
glutMainLoop(); |
86 |
- return 0; |
|
87 | 80 |
} |
... | ... |
@@ -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 |
} |
... | ... |
@@ -1,17 +1,17 @@ |
1 | 1 |
#version 150 |
2 | 2 |
|
3 |
-in vec3 inPosition; |
|
4 |
-in vec3 inNormal; |
|
3 |
+in vec3 inPosition; |
|
4 |
+in vec3 inNormal; |
|
5 | 5 |
in vec2 inTexCoord; |
6 |
+ |
|
6 | 7 |
out vec2 texCoord; |
7 | 8 |
|
8 |
-// NY |
|
9 |
-uniform mat4 projMatrix; |
|
10 |
-uniform mat4 mdlMatrix; |
|
9 |
+uniform mat4 projection; |
|
10 |
+uniform mat4 modelView; |
|
11 |
+ |
|
11 | 12 |
|
12 | 13 |
void main(void) |
13 | 14 |
{ |
14 |
- mat3 normalMatrix1 = mat3(mdlMatrix); |
|
15 |
- texCoord = inTexCoord; |
|
16 |
- gl_Position = projMatrix * mdlMatrix * vec4(inPosition, 1.0); |
|
15 |
+ gl_Position = projection * modelView * vec4(inPosition, 1.0); |
|
16 |
+ texCoord = inTexCoord; |
|
17 | 17 |
} |