This commit is contained in:
Joseph Ferano 2023-10-31 18:33:35 +07:00
parent e1878bc565
commit 91034e4154
2 changed files with 89 additions and 59 deletions

146
main.c
View File

@ -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"
@ -56,10 +56,9 @@ int main(void) {
// int w, h, c;
// unsigned char *terrain_img = stbi_load("terrain.png", &w, &h, &c, 0);
int w = 20;
int h = 20;
int w = 3;
int h = 3;
int vcount = w * h * 3;
float *verts = malloc(vcount * sizeof(float));
// float scale_y = 1.0f;
// float shift_y = 16.0f;
// for (int row = 0; row < h; row++) {
@ -74,44 +73,66 @@ int main(void) {
// }
// }
// for (int row = 0; row < h; row++) {
// float *verts = malloc(vcount * sizeof(float));
// float terrainVerts[vcount];
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,
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++) {
// float *v = &verts[(row * w + col) * 3];
// *(v+0) = -w/2 + col;
// *(v+1) = 0.0f;
// *(v+2) = -h/2 + row;
// 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;
// }
// }
float quadVerts[] = {
// Position
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 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
};
unsigned int quadIndices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
// = {
// 0, 1, 3, // first triangle
// 1, 2, 3 // second triangle
// };
printf("Loaded %d vertices\n", w * h);
// stbi_image_free(terrain_img);
// unsigned int icount = w * (h - 1) * 2;
// unsigned int *indices = malloc(icount * sizeof(unsigned int));
// for (int row = 0; row < h - 1; row++) {
// for (int col = 0; col < w; col++) {
// unsigned int idx1 = w * (row + 0) + col;
// unsigned int idx2 = w * (row + 1) + col;
// *(indices + idx1) = idx1;
// *(indices + idx2) = idx2;
// }
// }
// const unsigned int NUM_STRIPS = h - 1;
// const unsigned int NUM_VERTS_PER_STRIP = w * 2;
@ -122,7 +143,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(quadVerts), quadVerts, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(terrainVerts), terrainVerts, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
@ -130,7 +151,7 @@ int main(void) {
glGenBuffers(1, &terrainIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrainIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quadIndices), quadIndices, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(terrainIndices), terrainIndices, GL_STATIC_DRAW);
// Disable depth test and face culling.
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
@ -142,12 +163,7 @@ int main(void) {
compileShaderProgram("shaders/main.vert", "shaders/main.frag", NULL);
int matrixLocation;
mat4 projection;
glm_mat4_identity(projection);
float aspectRatio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;
glm_perspective(30.0f, aspectRatio, 1.0f, 1000.0f, projection);
glUseProgram(shaderProgram);
SDL_Event e;
bool quit = false;
Uint64 now = SDL_GetPerformanceCounter();
@ -155,11 +171,28 @@ int main(void) {
double dt = 0;
bool draw_wireframe = false;
float x = 0.0f, y = 0.0f;
Uint64 frame_count = 0;
while (quit == false) {
frame_count++;
last = now;
now = SDL_GetPerformanceCounter();
dt = (double)((now - last)*1000 / (double)SDL_GetPerformanceFrequency());
(void)dt;
const Uint8* keystates = SDL_GetKeyboardState(NULL);
float speed = 1.1f;
if (keystates[SDL_SCANCODE_W]) {
y += speed;
}
if (keystates[SDL_SCANCODE_S]) {
y -= speed;
}
if (keystates[SDL_SCANCODE_A]) {
x += speed;
}
if (keystates[SDL_SCANCODE_D]) {
x -= speed;
}
while (SDL_PollEvent( &e)) {
if (e.type == SDL_QUIT) {
quit = true;
@ -168,32 +201,20 @@ int main(void) {
if (key == '1') {
draw_wireframe = !draw_wireframe;
}
} else {
char key = e.key.keysym.sym;
float speed = 1.1f;
if (key == 'w') {
y += speed;
}
if (key == 's') {
y -= speed;
}
if (key == 'a') {
x += speed;
}
if (key == 'd') {
x -= speed;
}
}
}
mat4 projection;
glm_mat4_identity(projection);
float aspectRatio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;
glm_perspective(45.0f, aspectRatio, 0.1f, 1000.0f, projection);
mat4 model;
glm_mat4_identity(model);
mat4 view;
glm_mat4_identity(view);
glm_translate(model, (vec3){x, y, 0.0f});
// glm_translate(model, (vec3){10.0f, -10.0f, 0.0f});
// glm_rotate(model, now * 0.0000000001f, (vec3){0.0f, 0.0f, 1.0f});
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});
matrixLocation = glGetUniformLocation(shaderProgram, "view");
@ -203,6 +224,7 @@ int main(void) {
matrixLocation = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, model[0]);
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++) {
@ -217,8 +239,16 @@ int main(void) {
} 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, 6, GL_UNSIGNED_INT, 0);
// 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);
SDL_GL_SwapWindow(window);
}

View File

@ -7,5 +7,5 @@ uniform mat4 projection;
void main() {
Height = pos.y;
gl_Position = vec4(pos, 1.0);
gl_Position = projection * view * model * vec4(pos, 1.0);
}