125 lines
3.8 KiB
C
125 lines
3.8 KiB
C
#include <GL/glew.h>
|
|
#include <SDL3/SDL.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "SDL3/SDL_events.h"
|
|
#include "SDL3/SDL_keycode.h"
|
|
#include "camera.h"
|
|
#include "shader.h"
|
|
#include "window.h"
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
wn_window window = {0};
|
|
if (!wn_window_init(&window)) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize wn_window");
|
|
return EXIT_FAILURE;
|
|
}
|
|
wn_shader_code_location shader_code_location = {
|
|
.vertex_shader_source_path = "shaders/vert.glsl",
|
|
.fragment_shader_source_path = "shaders/frag.glsl"};
|
|
|
|
wn_shader shader = wn_shader_init(shader_code_location);
|
|
if (shader.success == false) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize shader");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
// set up vertex data (and buffer(s)) and configure vertex attributes
|
|
float vertices[] = {
|
|
0.5f, 0.5f, 0.0f, // top right
|
|
0.5f, -0.5f, 0.0f, // bottom right
|
|
-0.5f, -0.5f, 0.0f, // bottom left
|
|
-0.5f, 0.5f, 0.0f // top left
|
|
};
|
|
unsigned int indices[] = {
|
|
0, 1, 3, // first Triangle
|
|
1, 2, 3 // second Triangle
|
|
};
|
|
unsigned int VBO, VAO, EBO;
|
|
glGenVertexArrays(1, &VAO);
|
|
glGenBuffers(1, &VBO);
|
|
glGenBuffers(1, &EBO);
|
|
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and
|
|
// then configure vertex attributes(s).
|
|
glBindVertexArray(VAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
|
|
GL_STATIC_DRAW);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
// note that this is allowed, the call to glVertexAttribPointer registered VBO
|
|
// as the vertex attribute's bound vertex buffer object so afterwards we can
|
|
// safely unbind
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
// remember: do NOT unbind the EBO while a VAO is active as the bound element
|
|
// buffer object IS stored in the VAO; keep the EBO bound.
|
|
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
// You can unbind the VAO afterwards so other VAO calls won't accidentally
|
|
// modify this VAO, but this rarely happens. Modifying other VAOs requires a
|
|
// call to glBindVertexArray anyways so we generally don't unbind VAOs (nor
|
|
// VBOs) when it's not directly necessary.
|
|
glBindVertexArray(0);
|
|
|
|
// Initialise camera
|
|
Camera camera = Camera_default;
|
|
|
|
bool wants_running = true;
|
|
while (wants_running) {
|
|
// TODO: Create event handler files
|
|
while (SDL_PollEvent(&window.event)) {
|
|
switch (window.event.type) {
|
|
case SDL_EVENT_QUIT:
|
|
wants_running = false;
|
|
break;
|
|
case SDL_EVENT_KEY_DOWN:
|
|
if (window.event.key.key == SDLK_ESCAPE) {
|
|
wants_running = false;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Handle camera
|
|
camera_tick(&camera);
|
|
|
|
// TODO: make shader handler
|
|
mat4 mvp;
|
|
camera_calc_mvp(&mvp, &camera);
|
|
int mvp_uniform_location =
|
|
glGetUniformLocation(shader.shaderProgram, "MVP");
|
|
glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]);
|
|
|
|
// render
|
|
glClearColor(0.1f, 0.3f, 0.4f, 1.f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
// draw our first triangle
|
|
glUseProgram(shader.shaderProgram);
|
|
glBindVertexArray(
|
|
VAO); // seeing as we only have a single VAO there's no need to bind it
|
|
// every time, but we'll do so to keep things a bit more organized
|
|
// glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
// glBindVertexArray(0); // no need to unbind it every time
|
|
|
|
wn_swapwindow(&window);
|
|
}
|
|
|
|
glDeleteVertexArrays(1, &VAO);
|
|
glDeleteBuffers(1, &VBO);
|
|
glDeleteBuffers(1, &EBO);
|
|
|
|
wn_shader_deinit(shader);
|
|
wn_window_deinit(&window);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|