add test cube
This commit is contained in:
parent
5ba261c72c
commit
3b44c516ac
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -1,3 +1,6 @@
|
|||
[submodule "vendor/glfw"]
|
||||
path = vendor/glfw
|
||||
url = https://github.com/glfw/glfw
|
||||
[submodule "vendor/cglm"]
|
||||
path = vendor/cglm
|
||||
url = https://github.com/recp/cglm
|
||||
|
|
|
@ -6,6 +6,7 @@ cmake_minimum_required (VERSION 3.8)
|
|||
project ("schroedinger-solver")
|
||||
|
||||
add_subdirectory("vendor/glfw")
|
||||
add_subdirectory("vendor/cglm")
|
||||
add_subdirectory("vendor/glad")
|
||||
|
||||
# Include sub-projects.
|
||||
|
|
|
@ -11,9 +11,11 @@ add_executable(solver
|
|||
"renderer/context.c"
|
||||
"renderer/buffer.c"
|
||||
"renderer/shader.c"
|
||||
"renderer/camera.c"
|
||||
)
|
||||
|
||||
target_link_libraries(solver PRIVATE
|
||||
glfw
|
||||
glad
|
||||
cglm
|
||||
)
|
||||
|
|
|
@ -4,9 +4,15 @@
|
|||
#include <errno.h>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cglm/affine.h>
|
||||
|
||||
#include "renderer/context.h"
|
||||
|
||||
static void on_window_size_change(Application* app, int width, int height)
|
||||
{
|
||||
set_camera_perspective(&app->camera, 90.0f, (float)width / (float)height);
|
||||
}
|
||||
|
||||
static void destroy_application(Application* app)
|
||||
{
|
||||
assert(app);
|
||||
|
@ -20,25 +26,49 @@ int init_application(Application* app, const char* name)
|
|||
assert(app);
|
||||
glfwInit();
|
||||
|
||||
app->window = create_managed_window(name, 800, 800);
|
||||
if (app->window == NULL) {
|
||||
if (create_managed_window(&app->window, name, 800, 800) != 0)
|
||||
{
|
||||
destroy_application(app);
|
||||
return 1;
|
||||
}
|
||||
|
||||
app->window.user_data = app;
|
||||
app->window.on_size_change = on_window_size_change;
|
||||
|
||||
// Create quad for testing
|
||||
create_vao(&app->object);
|
||||
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f
|
||||
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // front bottom left
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // front top left
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // front top right
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, // front bottom right
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, // back bottom left
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // back top left
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // back top right
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // back bottom right
|
||||
};
|
||||
attach_vertex_buffer(&app->object, vertices, sizeof(vertices));
|
||||
|
||||
unsigned indices[] = {
|
||||
0, 1, 2,
|
||||
0, 2, 3
|
||||
0, 1, 2, // front
|
||||
0, 2, 3,
|
||||
|
||||
3, 2, 6, // right
|
||||
3, 6, 7,
|
||||
|
||||
4, 5, 1, // left
|
||||
4, 1, 0,
|
||||
|
||||
7, 6, 5, // back
|
||||
7, 5, 4,
|
||||
|
||||
1, 5, 6, // top
|
||||
1, 6, 2,
|
||||
|
||||
4, 0, 3, // bottom
|
||||
4, 3, 7
|
||||
};
|
||||
attach_element_buffer(&app->object, indices, sizeof(indices));
|
||||
|
||||
|
@ -56,9 +86,13 @@ int init_application(Application* app, const char* name)
|
|||
""
|
||||
"out vec3 i_col;"
|
||||
""
|
||||
"uniform mat4 model;"
|
||||
"uniform mat4 view;"
|
||||
"uniform mat4 projection;"
|
||||
""
|
||||
"void main() {"
|
||||
" i_col = col;"
|
||||
" gl_Position = vec4(pos, 1.0);"
|
||||
" gl_Position = projection * view * model * vec4(pos, 1.0);"
|
||||
"}",
|
||||
|
||||
"#version 460 core\n"
|
||||
|
@ -76,21 +110,39 @@ int init_application(Application* app, const char* name)
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (create_camera(&app->camera) != 0)
|
||||
{
|
||||
destroy_application(app);
|
||||
return 1;
|
||||
}
|
||||
|
||||
glm_mat4_identity(app->model);
|
||||
|
||||
ctx_enable(GL_DEPTH_TEST);
|
||||
ctx_front_face(GL_CW);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int launch_application(Application* app)
|
||||
{
|
||||
while (!glfwWindowShouldClose(app->window))
|
||||
while (!glfwWindowShouldClose(app->window.window))
|
||||
{
|
||||
glfwPollEvents();
|
||||
|
||||
// rotate cube
|
||||
glm_rotate(app->model, 0.03f, (vec3) { 0.0f, 1.0f, 0.0f });
|
||||
|
||||
ctx_clear_screen(0.3f, 0.1f, 0.8f, 1.0f);
|
||||
|
||||
bind_shader(app->shader);
|
||||
set_uniform_mat4(app->shader, "model", app->model);
|
||||
set_uniform_mat4(app->shader, "view", app->camera.view);
|
||||
set_uniform_mat4(app->shader, "projection", app->camera.projection);
|
||||
|
||||
ctx_draw_elements(&app->object);
|
||||
|
||||
glfwSwapBuffers(app->window);
|
||||
glfwSwapBuffers(app->window.window);
|
||||
}
|
||||
|
||||
destroy_application(app);
|
||||
|
|
|
@ -4,13 +4,16 @@
|
|||
#include "renderer/window.h"
|
||||
#include "renderer/buffer.h"
|
||||
#include "renderer/shader.h"
|
||||
#include "renderer/camera.h"
|
||||
|
||||
typedef struct Application
|
||||
{
|
||||
GLFWwindow* window;
|
||||
Window window;
|
||||
|
||||
VertexArrayObject object;
|
||||
int shader;
|
||||
Camera camera;
|
||||
mat4 model;
|
||||
} Application;
|
||||
|
||||
int init_application(Application* app, const char* name);
|
||||
|
|
23
src/renderer/camera.c
Normal file
23
src/renderer/camera.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "camera.h"
|
||||
|
||||
#include <cglm/affine.h>
|
||||
#include <cglm/cam.h>
|
||||
#include <assert.h>
|
||||
|
||||
int create_camera(Camera* camera)
|
||||
{
|
||||
assert(camera);
|
||||
|
||||
glm_mat4_identity(camera->view);
|
||||
glm_rotate(camera->view, glm_rad(35.0f), (vec3) { 1.0f, 0.0f, 0.0f });
|
||||
glm_translate(camera->view, (vec3) { 0.0f, -1.0f, -2.0f });
|
||||
|
||||
glm_perspective(glm_rad(90.0f), 1.0f, 0.001f, 100.0f, camera->projection);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void set_camera_perspective(Camera* camera, float fov, float ar)
|
||||
{
|
||||
glm_perspective(glm_rad(fov), ar, 0.01f, 100.0f, camera->projection);
|
||||
}
|
15
src/renderer/camera.h
Normal file
15
src/renderer/camera.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include <cglm/mat4.h>
|
||||
|
||||
typedef struct Camera
|
||||
{
|
||||
mat4 view;
|
||||
mat4 projection;
|
||||
} Camera;
|
||||
|
||||
int create_camera(Camera* camera);
|
||||
void set_camera_perspective(Camera* camera, float fov, float ar);
|
||||
|
||||
#endif // CAMERA_H
|
|
@ -18,7 +18,7 @@ void ctx_viewport(int x, int y, int w, int h)
|
|||
void ctx_clear_screen(float r, float g, float b, float a)
|
||||
{
|
||||
glClearColor(r, g, b, a);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void ctx_draw_elements(VertexArrayObject* vao)
|
||||
|
@ -26,3 +26,13 @@ void ctx_draw_elements(VertexArrayObject* vao)
|
|||
bind_vao(*vao);
|
||||
glDrawElements(GL_TRIANGLES, vao->elements, GL_UNSIGNED_INT, (void*)0);
|
||||
}
|
||||
|
||||
void ctx_enable(int feature)
|
||||
{
|
||||
glEnable(feature);
|
||||
}
|
||||
|
||||
void ctx_front_face(int front)
|
||||
{
|
||||
glFrontFace(front);
|
||||
}
|
|
@ -6,6 +6,8 @@ typedef struct VertexArrayObject VertexArrayObject;
|
|||
int ctx_init();
|
||||
|
||||
void ctx_viewport(int x, int y, int w, int h);
|
||||
void ctx_enable(int feature);
|
||||
void ctx_front_face(int front);
|
||||
|
||||
void ctx_clear_screen(float r, float g, float b, float a);
|
||||
void ctx_draw_elements(VertexArrayObject* vao);
|
||||
|
|
|
@ -92,3 +92,9 @@ void bind_shader(int shader)
|
|||
{
|
||||
glUseProgram(shader);
|
||||
}
|
||||
|
||||
void set_uniform_mat4(int shader, const char* name, mat4 mat)
|
||||
{
|
||||
int location = glGetUniformLocation(shader, name);
|
||||
glUniformMatrix4fv(location, 1, GL_FALSE, (float*)mat);
|
||||
}
|
|
@ -1,9 +1,12 @@
|
|||
#ifndef SHADER_H
|
||||
#define SHADER_H
|
||||
|
||||
#include <cglm/mat4.h>
|
||||
|
||||
int create_shader(const char* vertex_shader_code, const char* fragment_shader_code);
|
||||
void destroy_shader(int shader);
|
||||
|
||||
void bind_shader(int shader);
|
||||
void set_uniform_mat4(int shader, const char* name, mat4 mat);
|
||||
|
||||
#endif // SHADER_H
|
|
@ -1,6 +1,8 @@
|
|||
#include "window.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "context.h"
|
||||
|
@ -8,28 +10,39 @@
|
|||
void default_framebuffer_size_callback(GLFWwindow* window, int vw, int vh)
|
||||
{
|
||||
ctx_viewport(0, 0, vw, vh);
|
||||
|
||||
Window* managed = (Window*)glfwGetWindowUserPointer(window);
|
||||
if (managed->on_size_change)
|
||||
{
|
||||
managed->on_size_change(managed->user_data, vw, vh);
|
||||
}
|
||||
}
|
||||
|
||||
GLFWwindow* create_managed_window(const char* title, int width, int height)
|
||||
int create_managed_window(Window* window, const char* title, int width, int height)
|
||||
{
|
||||
GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL);
|
||||
assert(window);
|
||||
|
||||
window->window = glfwCreateWindow(width, height, title, NULL, NULL);
|
||||
if (window == NULL) {
|
||||
const char* error;
|
||||
int error_code = glfwGetError(&error);
|
||||
|
||||
fprintf(stderr, "Failed to create window (%d): %s\n", error_code, error);
|
||||
return NULL;
|
||||
return error_code;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwMakeContextCurrent(window->window);
|
||||
ctx_init();
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, default_framebuffer_size_callback);
|
||||
glfwSetFramebufferSizeCallback(window->window, default_framebuffer_size_callback);
|
||||
|
||||
return window;
|
||||
glfwSetWindowUserPointer(window->window, (void*)window);
|
||||
window->on_size_change = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void destroy_window(GLFWwindow* window)
|
||||
void destroy_window(Window window)
|
||||
{
|
||||
glfwDestroyWindow(window);
|
||||
glfwDestroyWindow(window.window);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,14 @@
|
|||
|
||||
typedef struct GLFWwindow GLFWwindow;
|
||||
|
||||
GLFWwindow* create_managed_window(const char* title, int width, int height);
|
||||
void destroy_window(GLFWwindow* window);
|
||||
typedef struct Window
|
||||
{
|
||||
GLFWwindow* window;
|
||||
void(*on_size_change)(void*, int, int);
|
||||
void* user_data;
|
||||
} Window;
|
||||
|
||||
int create_managed_window(Window* window, const char* title, int width, int height);
|
||||
void destroy_window(Window window);
|
||||
|
||||
#endif // WINDOW_H
|
1
vendor/cglm
vendored
Submodule
1
vendor/cglm
vendored
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 199d1fa031f8d71c72f79f4a46de7ee064fc1a72
|
Loading…
Reference in a new issue