118 lines
2.2 KiB
C
118 lines
2.2 KiB
C
#include "gpu.h"
|
|
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define W 800
|
|
#define H 600
|
|
|
|
static const vertex_t vertices[] = {
|
|
{ { 0.000, 0.612, 0.000 }, { 1, 0, 0 } },
|
|
{ { -0.500, -0.204, -0.289 }, { 0, 1, 0 } },
|
|
{ { 0.500, -0.204, -0.289 }, { 0, 0, 1 } },
|
|
{ { 0.000, -0.204, 0.577 }, { 1, 1, 1 } },
|
|
};
|
|
|
|
static const uint16_t indices[] = {
|
|
0, 3, 1, 0, 1, 2, 0, 2, 3, 3, 2, 1,
|
|
};
|
|
|
|
typedef enum {
|
|
AXIS_X,
|
|
AXIS_Y,
|
|
AXIS_Z,
|
|
} axis_t;
|
|
|
|
static axis_t rot_axis = AXIS_Y;
|
|
|
|
static void
|
|
key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
|
{
|
|
(void)window;
|
|
(void)scancode;
|
|
(void)mods;
|
|
|
|
if (action != GLFW_PRESS)
|
|
return;
|
|
|
|
switch (key) {
|
|
case GLFW_KEY_Q:
|
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
|
break;
|
|
|
|
case GLFW_KEY_X:
|
|
rot_axis = AXIS_X;
|
|
break;
|
|
case GLFW_KEY_Y:
|
|
rot_axis = AXIS_Y;
|
|
break;
|
|
case GLFW_KEY_Z:
|
|
rot_axis = AXIS_Z;
|
|
break;
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
// Set up window
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
|
GLFWwindow *window = glfwCreateWindow(W, H, "Foo", nullptr, nullptr);
|
|
assert(window);
|
|
|
|
// Register input callbacks
|
|
glfwSetKeyCallback(window, key_callback);
|
|
|
|
// Initialise GPU
|
|
const gpu_config_t gpu_config = {
|
|
.application_name = "Foo",
|
|
.window = window,
|
|
.vertex_data = vertices,
|
|
.vertex_data_size = sizeof(vertices),
|
|
.index_data = indices,
|
|
.index_data_size = sizeof(indices),
|
|
};
|
|
gpu_ctx_t gpu;
|
|
gpu_init(&gpu, &gpu_config);
|
|
|
|
const vec3_t cam = { 2, 2, -2 };
|
|
const vec3_t tgt = { 0, 0, 0 };
|
|
mvp_t mvp = {
|
|
.model = MAT4_SCALE(2),
|
|
.view = mat4_lookat(cam, tgt, (vec3_t) { 0, 1, 0 }),
|
|
.proj = mat4_perspective((float)W / H, PI / 6, 0.1, 10.0),
|
|
};
|
|
|
|
const mat4_t rotx = MAT4_ROTATE_X(PI / 128);
|
|
const mat4_t roty = MAT4_ROTATE_Y(PI / 128);
|
|
const mat4_t rotz = MAT4_ROTATE_Z(PI / 128);
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
glfwPollEvents();
|
|
|
|
switch (rot_axis) {
|
|
case AXIS_X:
|
|
mvp.model = mat4_mul(rotx, mvp.model);
|
|
break;
|
|
case AXIS_Y:
|
|
mvp.model = mat4_mul(roty, mvp.model);
|
|
break;
|
|
case AXIS_Z:
|
|
mvp.model = mat4_mul(rotz, mvp.model);
|
|
break;
|
|
}
|
|
|
|
gpu_render(&gpu, &mvp);
|
|
}
|
|
|
|
// Clean up
|
|
gpu_cleanup(&gpu);
|
|
glfwDestroyWindow(window);
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
}
|