This commit is contained in:
Joseph Ferano 2023-11-01 17:09:31 +07:00
parent d7ccfcec8a
commit 72dc81ccb8
2 changed files with 37 additions and 18 deletions

49
main.c
View File

@ -12,7 +12,7 @@
#include "lib.h"
//Screen dimension constants
const int SCREEN_WIDTH = 1024;
const int SCREEN_WIDTH = 1920;
const int SCREEN_HEIGHT = 768;
int main(void) {
@ -54,7 +54,7 @@ int main(void) {
SDL_GL_SetSwapInterval(1);
int res = 8;
int res = 16;
int width, height, c;
unsigned char *terrain_img = stbi_load("terrain.png", &width, &height, &c, 0);
int w = width / res;
@ -69,7 +69,7 @@ int main(void) {
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+1] = *(terrain_img + (row * width + col) * c * res) * 0.01f + 1.0f;
terrainVerts[idx+2] = -th/2.0f + (float)row/h * th;
// *(v+1) = y * scale_y + shift_y;
// *(v+1) = -h/2 + row
@ -81,25 +81,40 @@ int main(void) {
// 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;
int icount = (h-1) * (w) * 2 *4;
unsigned int *terrainIndices = malloc(icount * sizeof(unsigned int));
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++;
for (int col = 0; col < w-1; col++) {
unsigned int topleft = row * w + col;
unsigned int botleft = (row+1) * w + col;
unsigned int botright = (row+1) * w + col+1;
unsigned int topright = row * w + col+1;
terrainIndices[i++] = topleft;
terrainIndices[i++] = botleft;
terrainIndices[i++] = botright;
terrainIndices[i++] = topleft;
terrainIndices[i++] = topright;
terrainIndices[i++] = botright;
}
}
// 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++;
// }
// }
// float terrainVerts[] = {
// float tv[] = {
@ -231,7 +246,7 @@ int main(void) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
glBindVertexArray(terrainVAO);
glDrawElements(GL_TRIANGLE_STRIP, icount, GL_UNSIGNED_INT, (void*)0);
glDrawElements(GL_TRIANGLES, icount, GL_UNSIGNED_INT, (void*)0);
SDL_GL_SwapWindow(window);
}

View File

@ -5,5 +5,9 @@ out vec4 FragColor;
void main() {
float h = (Height + 16)/32.0f; // shift and scale the height into a grayscale value
// FragColor = vec4(h, h, h, 1.0);
FragColor = vec4(1, 1, 1, 1.0);
if (Height < 1.01f) {
FragColor = vec4(0.2f, 0.3f, 0.3f, 1.0f);
} else {
FragColor = vec4(1, 1, 1, 1.0);
}
}