Finally rendering the heightmap
This commit is contained in:
		
							parent
							
								
									91034e4154
								
							
						
					
					
						commit
						d7ccfcec8a
					
				
							
								
								
									
										175
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										175
									
								
								main.c
									
									
									
									
									
								
							| @ -6,7 +6,7 @@ | ||||
| #include "libs/glad/include/glad/glad.h" | ||||
| #include <SDL2/SDL.h> | ||||
| // #include <SDL2/SDL_events.h>
 | ||||
| // #include "stb_image.h"
 | ||||
| #include "stb_image.h" | ||||
| // #include <SDL2/SDL_image.h>
 | ||||
| // #include <SDL2/SDL_render.h>
 | ||||
| #include "lib.h" | ||||
| @ -54,87 +54,75 @@ int main(void) { | ||||
| 
 | ||||
|     SDL_GL_SetSwapInterval(1); | ||||
| 
 | ||||
|     // int w, h, c;
 | ||||
|     // unsigned char *terrain_img = stbi_load("terrain.png", &w, &h, &c, 0);
 | ||||
|     int w = 3; | ||||
|     int h = 3; | ||||
|     int res = 8; | ||||
|     int width, height, c; | ||||
|     unsigned char *terrain_img = stbi_load("terrain.png", &width, &height, &c, 0); | ||||
|     int w = width / res; | ||||
|     int h = height / res; | ||||
|     float tw = 100.0f, th = 35.0f; | ||||
|     // int w = 20;
 | ||||
|     // int h = 20;
 | ||||
|     int vcount = w * h * 3; | ||||
|     // float scale_y = 1.0f;
 | ||||
|     // float shift_y = 16.0f;
 | ||||
|     // for (int row = 0; row < h; row++) {
 | ||||
|     //     for (int col = 0; col < w; col++) {
 | ||||
|     //         unsigned char *texel = terrain_img + (row * w + col) * c;
 | ||||
|     //         unsigned char y = texel[0];
 | ||||
| 
 | ||||
|     //         float *v = &verts[(row * w + col) * 3];
 | ||||
|     //         *(v+0) = -w/2 + col;
 | ||||
|     //         *(v+1) = y * scale_y + shift_y;
 | ||||
|     //         *(v+2) = -h/2 + row;
 | ||||
|     //     }
 | ||||
|     // }
 | ||||
|     float *terrainVerts = malloc(vcount * sizeof(float)); | ||||
|     for (int row = 0; row < h; row++) { | ||||
|         for (int col = 0; col < w; col++) { | ||||
|             int idx = (row * w + col) * 3; | ||||
|             terrainVerts[idx+0] = -tw/2.0f + (float)col/w * tw; | ||||
|             terrainVerts[idx+1] = *(terrain_img + (row * width + col) * c * res) * 0.015f + 1.0f; | ||||
|             terrainVerts[idx+2] = -th/2.0f + (float)row/h * th; | ||||
|             // *(v+1) = y * scale_y + shift_y;
 | ||||
|             // *(v+1) = -h/2 + row
 | ||||
|         } | ||||
|     } | ||||
|     stbi_image_free(terrain_img); | ||||
| 
 | ||||
|     // float *verts = malloc(vcount * sizeof(float));
 | ||||
|     // float terrainVerts[vcount];
 | ||||
|     // There's 1 less row, so (h-1) * width, then we need to add the extra two
 | ||||
|     // indices for the degenerate triangles per row, except the first row
 | ||||
|     // which only has one extra index, since the first index does not need degeneracy
 | ||||
|     // And then since there are duplicate indices in each row
 | ||||
|     int icount = (h-1) * (w) * 2 + ((h - 1) * 2 - 1) - 1; | ||||
|     unsigned int *terrainIndices = malloc(icount * sizeof(unsigned int)); | ||||
| 
 | ||||
|     float terrainVerts[] = { | ||||
|         0.f, 0.f, 0.f, | ||||
|         1.f, 0.f, 0.f, | ||||
|         2.f, 0.f, 0.f, | ||||
|         3.f, 0.f, 0.f, | ||||
|         0.f, 1.f, 0.f, | ||||
|         1.f, 1.f, 0.f, | ||||
|         2.f, 1.f, 0.f, | ||||
|         3.f, 1.f, 0.f, | ||||
|     int i = 0; | ||||
|     for (int row = 0; row < h-1; row++) { | ||||
|         if (row != 0) { | ||||
|             // Duplicate next num
 | ||||
|             terrainIndices[i++] = row * w; | ||||
|         } | ||||
|         for (int col = 0; col < w; col++) { | ||||
|             terrainIndices[i++] = (row + 0) * w + col; | ||||
|             terrainIndices[i++] = (row + 1) * w + col; | ||||
|         } | ||||
|         // Duplicate last num
 | ||||
|         if (row < h-2) { | ||||
|             terrainIndices[i] = terrainIndices[i-1]; | ||||
|             i++; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|         0.f, 2.f, 0.f, | ||||
|         1.f, 2.f, 0.f, | ||||
|         2.f, 2.f, 0.f, | ||||
|         3.f, 2.f, 0.f, | ||||
|         0.f, 3.f, 0.f, | ||||
|         1.f, 3.f, 0.f, | ||||
|         2.f, 3.f, 0.f, | ||||
|         3.f, 3.f, 0.f, | ||||
|     }; | ||||
| 
 | ||||
|     // for (int row = 1; row < h / 2; row += 2) {
 | ||||
|     //     for (int col = 0; col < w; col++) {
 | ||||
|     //         int idx1 = ((row-1) * w + col);
 | ||||
|     //         int idx2 = (row * w + col);
 | ||||
|     //         terrainVerts[idx1*3+0] = (float)row-1;
 | ||||
|     //         terrainVerts[idx1*3+1] = (float)col;
 | ||||
|     //         terrainVerts[idx1*3+2] = 0.0f;
 | ||||
|     //         terrainVerts[idx1*3+3] = (float)row;
 | ||||
|     //         terrainVerts[idx1*3+4] = (float)col;
 | ||||
|     //         terrainVerts[idx1*3+5] = 0.0f;
 | ||||
|     //     }
 | ||||
|     // }
 | ||||
| 
 | ||||
|     // unsigned int terrainIndices[w*(h-1)*2];
 | ||||
|     // int i = 0;
 | ||||
|     // for (int row = 0; row < h-1; row++) {
 | ||||
|     //     for (int col = 0; col < w; col++) {
 | ||||
|     //         terrainIndices[i++] = row * w + col;
 | ||||
|     //         terrainIndices[i++] = (row+1) * w + (w+1);
 | ||||
|     //         // printf("1 %d 2 %d\n", idx1, idx2);
 | ||||
|     //     }
 | ||||
|     // }
 | ||||
|     unsigned int terrainIndices[] = { | ||||
|         0,4,1,5,2,6,3,7,7, | ||||
|         4,4,8,5,9,6,10,7,11,11 | ||||
|     }; | ||||
| 
 | ||||
| 
 | ||||
|     // = {
 | ||||
|     //     0, 1, 3, // first triangle
 | ||||
|     //     1, 2, 3  // second triangle
 | ||||
|     // float terrainVerts[] = {
 | ||||
|     // float tv[] = {
 | ||||
|     //     0.f, 0.f, 0.f,
 | ||||
|     //     1.f, 0.f, 0.f,
 | ||||
|     //     2.f, 0.f, 0.f,
 | ||||
|     //     3.f, 0.f, 0.f,
 | ||||
|     //     0.f, 1.f, 0.f,
 | ||||
|     //     1.f, 1.f, 0.f,
 | ||||
|     //     2.f, 1.f, 0.f,
 | ||||
|     //     3.f, 1.f, 0.f,
 | ||||
|     //     // Second row
 | ||||
|     //     0.f, 2.f, 0.f,
 | ||||
|     //     1.f, 2.f, 0.f,
 | ||||
|     //     2.f, 2.f, 0.f,
 | ||||
|     //     3.f, 2.f, 0.f,
 | ||||
|     //     0.f, 3.f, 0.f,
 | ||||
|     //     1.f, 3.f, 0.f,
 | ||||
|     //     2.f, 3.f, 0.f,
 | ||||
|     //     3.f, 3.f, 0.f,
 | ||||
|     // };
 | ||||
| 
 | ||||
| 
 | ||||
|     printf("Loaded %d vertices\n", w * h); | ||||
|     // stbi_image_free(terrain_img);
 | ||||
| 
 | ||||
|     // const unsigned int NUM_STRIPS = h - 1;
 | ||||
|     // const unsigned int NUM_VERTS_PER_STRIP = w * 2;
 | ||||
| 
 | ||||
|     unsigned int terrainVAO, terrainVBO, terrainIBO; | ||||
|     glGenVertexArrays(1, &terrainVAO); | ||||
| @ -143,7 +131,7 @@ int main(void) { | ||||
|     glGenBuffers(1, &terrainVBO); | ||||
|     glBindBuffer(GL_ARRAY_BUFFER, terrainVBO); | ||||
|     // glBufferData(GL_ARRAY_BUFFER, vcount * sizeof(float), verts, GL_STATIC_DRAW);
 | ||||
|     glBufferData(GL_ARRAY_BUFFER, sizeof(terrainVerts), terrainVerts, GL_STATIC_DRAW); | ||||
|     glBufferData(GL_ARRAY_BUFFER, vcount * sizeof(float), terrainVerts, GL_STATIC_DRAW); | ||||
| 
 | ||||
|     // position attribute
 | ||||
|     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); | ||||
| @ -151,7 +139,8 @@ int main(void) { | ||||
| 
 | ||||
|     glGenBuffers(1, &terrainIBO); | ||||
|     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrainIBO); | ||||
|     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(terrainIndices), terrainIndices, GL_STATIC_DRAW); | ||||
|     int ebosize = icount * sizeof(unsigned int); | ||||
|     glBufferData(GL_ELEMENT_ARRAY_BUFFER, ebosize, terrainIndices, GL_STATIC_DRAW); | ||||
|     // Disable depth test and face culling.
 | ||||
|     glDisable(GL_DEPTH_TEST); | ||||
|     glDisable(GL_CULL_FACE); | ||||
| @ -169,9 +158,10 @@ int main(void) { | ||||
|     Uint64 now = SDL_GetPerformanceCounter(); | ||||
|     Uint64 last = 0; | ||||
|     double dt = 0; | ||||
|     bool draw_wireframe = false; | ||||
|     bool draw_wireframe = true; | ||||
|     float x = 0.0f, y = 0.0f; | ||||
|     Uint64 frame_count = 0; | ||||
|     float scroll = -10.0f; | ||||
|     while (quit == false) { | ||||
|         frame_count++; | ||||
|         last = now; | ||||
| @ -179,7 +169,7 @@ int main(void) { | ||||
|         dt = (double)((now - last)*1000 / (double)SDL_GetPerformanceFrequency()); | ||||
|         (void)dt; | ||||
|         const Uint8* keystates = SDL_GetKeyboardState(NULL); | ||||
|         float speed = 1.1f; | ||||
|         float speed = 0.5f; | ||||
|         if (keystates[SDL_SCANCODE_W]) { | ||||
|             y += speed; | ||||
|         } | ||||
| @ -202,6 +192,13 @@ int main(void) { | ||||
|                     draw_wireframe = !draw_wireframe; | ||||
|                 } | ||||
|             } | ||||
|             if (e.type == SDL_MOUSEWHEEL) { | ||||
|                 if (e.wheel.y > 0) { | ||||
|                     scroll -= 2.0f; | ||||
|                 } else if(e.wheel.y < 0) { | ||||
|                     scroll += 2.0f; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         mat4 projection; | ||||
| @ -212,9 +209,9 @@ int main(void) { | ||||
|         glm_mat4_identity(model); | ||||
|         mat4 view; | ||||
|         glm_mat4_identity(view); | ||||
|         glm_translate(model, (vec3){x, 0.0f, y}); | ||||
|         glm_translate(model, (vec3){0.0f, 0.0f, -5.0f}); | ||||
|         // glm_rotate(model, frame_count * 0.01f, (vec3){0.0f, 0.0f, 1.0f});
 | ||||
|         glm_translate(view, (vec3){x, -y, scroll}); | ||||
|         // glm_translate(view, (vec3){0.0f, 0.0f, -10.0f});
 | ||||
|         glm_rotate(model, 45.0f, (vec3){1.0f, 0.0f, 0.0f}); | ||||
| 
 | ||||
| 
 | ||||
|         matrixLocation = glGetUniformLocation(shaderProgram, "view"); | ||||
| @ -227,28 +224,14 @@ int main(void) { | ||||
|         glUseProgram(shaderProgram); | ||||
|         glViewport(0, 0, screen_width, screen_height); | ||||
|         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||||
|         // for (unsigned strip = 0; strip < NUM_STRIPS; strip++) {
 | ||||
|         //     glDrawElements(GL_TRIANGLE_STRIP,   // primitive type
 | ||||
|         //                    NUM_VERTS_PER_STRIP,   // number of indices to render
 | ||||
|         //                    GL_UNSIGNED_INT,     // index data type
 | ||||
|         //                    (void*)(sizeof(unsigned) * (NUM_VERTS_PER_STRIP) * strip)); // offset to starting index
 | ||||
|         // }
 | ||||
| 
 | ||||
|         if (draw_wireframe) { | ||||
|             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | ||||
|         } else { | ||||
|             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); | ||||
|         } | ||||
|         // glEnableClientState(GL_VERTEX_ARRAY);
 | ||||
|         // glVertexPointer(3, GL_FLOAT, terrainVerts);
 | ||||
|         // glDrawArrays(GL_TRIANGLE_STRIP, 0, w*h);
 | ||||
|         // glDisableClientState(GL_VERTEX_ARRAY);
 | ||||
|         glBindVertexArray(terrainVAO); | ||||
|         // glDrawElements(GL_TRIANGLES, 8 * 3, GL_UNSIGNED_INT, 0);
 | ||||
|         // glDrawArrays(GL_TRIANGLES, 0, w*h*3);
 | ||||
|         size_t s = w*2 * sizeof(unsigned int); | ||||
|         // glDrawElements(GL_TRIANGLE_STRIP, (w*h)+(w-1)*(h-2), GL_UNSIGNED_INT, (void*)s);
 | ||||
|         glDrawElements(GL_TRIANGLE_STRIP, w*3*2, GL_UNSIGNED_INT, 0); | ||||
|         glDrawElements(GL_TRIANGLE_STRIP, icount, GL_UNSIGNED_INT, (void*)0); | ||||
| 
 | ||||
|         SDL_GL_SwapWindow(window); | ||||
|     } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user