Small refactor

This commit is contained in:
Eric-Paul Ickhorn 2023-10-10 20:01:15 +02:00
parent 68614f805f
commit 6227c571aa
5 changed files with 302 additions and 144 deletions

View File

@ -0,0 +1,44 @@
#include "state.h"
#include <SDL2/SDL.h>
const char *tc_window_title_g = "Techneck";
void tc_init_renderer(techneck_s *techneck)
{
SDL_Init(SDL_INIT_EVERYTHING);
techneck->renderer.window = SDL_CreateWindow(
tc_window_title_g,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1200,
800,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
techneck->renderer.gl_context = SDL_GL_CreateContext(techneck->renderer.window);
gladLoadGLLoader(&SDL_GL_GetProcAddress);
gladLoadGL();
techneck->renderer.draw_shader = tc_make_shader_program("vertex_shader.glsl", "fragment_shader.glsl");
glEnable(GL_DEPTH_TEST);
}
techneck_s tc_init()
{
techneck_s techneck;
tc_init_renderer(&techneck);
return techneck;
}
void tc_cleanup()
{
SDL_GL_DeleteContext(tc_game_state_g.renderer.gl_context);
SDL_DestroyWindow(tc_game_state_g.renderer.window);
}

View File

@ -1,47 +1,75 @@
#include "state.h"
techneck_s tc_game_state_g;
#include <glad/glad.h> #include <glad/glad.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <stdbool.h>
#include <linmath.h> #include <linmath.h>
SDL_Window *window = NULL;
SDL_GLContext gl_context;
int main_shader = 0;
mat4x4 projection_matrix; mat4x4 projection_matrix;
mat4x4 view_matrix; mat4x4 view_matrix;
float triangle_vertices[9] = { float triangle_vertices[108] = {
0.0f, 0.0f, -1.0f, // Front
0.0f, 1.0f, -1.0f, -0.5f, -0.5f, -0.5f,
1.0f, 0.0f, -1.0f -0.5f, 0.5f, -0.5f,
}; 0.5f, -0.5f, -0.5f,
typedef struct tc_vec3
{
float x;
float y;
float z;
} tc_vec3_s; 0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
// Back
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
// Left
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
// Right
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
// Top
-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
// Bottom
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
};
tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f }; tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f };
tc_vec3_s tc_camera_rotation = { 0.0f, 0.0f, 0.0f }; tc_vec3_s tc_camera_rotation = { 0.0f, 0.0f, 0.0f };
typedef struct tc_object
{
int vbo;
int vao;
tc_vec3_s position;
tc_vec3_s rotation;
} tc_object_s;
typedef struct tc_block
{
tc_vec3_s position;
tc_object_s drawing;
} tc_block_s;
tc_block_s block; tc_block_s block;
void render_block(tc_block_s block) void render_block(tc_block_s block)
@ -52,13 +80,13 @@ void render_block(tc_block_s block)
// mat4x4_rotate_Y(view_matrix, model_matrix, block.rotation.y); // mat4x4_rotate_Y(view_matrix, model_matrix, block.rotation.y);
// mat4x4_rotate_Z(view_matrix, model_matrix, block.rotation.z); // mat4x4_rotate_Z(view_matrix, model_matrix, block.rotation.z);
int model_matrix_uniform_location = glGetUniformLocation(main_shader, "model_matrix"); int model_matrix_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix");
glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]); glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]);
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo); glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 36);
} }
tc_block_s tc_new_block_at_3f(float x, float y, float z) tc_block_s tc_new_block_at_3f(float x, float y, float z)
@ -69,100 +97,17 @@ tc_block_s tc_new_block_at_3f(float x, float y, float z)
block.position.z = z; block.position.z = z;
glGenBuffers(1, &block.drawing.vbo); glGenBuffers(1, &block.drawing.vbo);
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo); glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 9, &triangle_vertices[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 108, &triangle_vertices[0], GL_STATIC_DRAW);
return block; return block;
} }
char * load_file(char *path, int *out_len)
{
FILE *file = NULL;
if((file = fopen(path, "r")) != NULL)
{
fseek(file, 0, SEEK_END);
int length = ftell(file);
fseek(file, 0, SEEK_SET);
char *content = malloc(length+1);
fread(content, 1, length, file);
if(out_len != NULL)
{
*out_len = length;
}
return content;
}
return NULL;
}
int setup_shaders(char *vertex_path, char *fragment_path)
{
int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
int len_vertex_source = 0;
char *vertex_source = load_file(vertex_path, &len_vertex_source);
int len_fragment_source = 0;
char *fragment_source = load_file(fragment_path, &len_fragment_source);
glShaderSource(vertex_shader, 1, &vertex_source, &len_vertex_source);
glShaderSource(fragment_shader, 1, &fragment_source, &len_fragment_source);
glCompileShader(vertex_shader);
glCompileShader(fragment_shader);
int vertex_compile_status = 0;
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vertex_compile_status);
if(vertex_compile_status != GL_TRUE)
{
int log_length = 0;
glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &log_length);
char *info_log = malloc(log_length+1);
glGetShaderInfoLog(vertex_shader, log_length, &log_length, info_log);
printf("VERTEX SHADER FAILED COMPILING! ERROR: %s\n", info_log);
return -1;
}
int fragment_compile_status = 0;
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &fragment_compile_status);
if(fragment_compile_status != GL_TRUE)
{
int log_length = 0;
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &log_length);
char *info_log = malloc(log_length+1);
glGetShaderInfoLog(fragment_shader, log_length, &log_length, info_log);
printf("FRAGMENT SHADER FAILED COMPILING! ERROR: %s\n", info_log);
return -1;
}
int program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
return program;
}
void cleanup()
{
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
}
void render() void render()
{ {
float color[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; float color[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color); glClearBufferfv(GL_COLOR, 0, color);
float depth = 10000000.0f;
glClearBufferfv(GL_DEPTH, 0, &depth);
mat4x4_perspective(projection_matrix, 120, 1200 / 800, 0.0001f, 1000.0f); mat4x4_perspective(projection_matrix, 120, 1200 / 800, 0.0001f, 1000.0f);
mat4x4_translate(view_matrix, -tc_camera_position.x, -tc_camera_position.y, tc_camera_position.z); mat4x4_translate(view_matrix, -tc_camera_position.x, -tc_camera_position.y, tc_camera_position.z);
@ -170,18 +115,18 @@ void render()
// mat4x4_rotate_Y(view_matrix, view_matrix, tc_camera_rotation.y); // mat4x4_rotate_Y(view_matrix, view_matrix, tc_camera_rotation.y);
// mat4x4_rotate_Z(view_matrix, view_matrix, tc_camera_rotation.z); // mat4x4_rotate_Z(view_matrix, view_matrix, tc_camera_rotation.z);
glUseProgram(main_shader); glUseProgram(tc_game_state_g.renderer.draw_shader.program_id);
int projection_uniform_location = glGetUniformLocation(main_shader, "projection_matrix"); int projection_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "projection_matrix");
glUniformMatrix4fv(projection_uniform_location, 1, GL_FALSE, &projection_matrix[0][0]); glUniformMatrix4fv(projection_uniform_location, 1, GL_FALSE, &projection_matrix[0][0]);
int view_uniform_location = glGetUniformLocation(main_shader, "view_matrix"); int view_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "view_matrix");
glUniformMatrix4fv(view_uniform_location, 1, GL_FALSE, &view_matrix[0][0]); glUniformMatrix4fv(view_uniform_location, 1, GL_FALSE, &view_matrix[0][0]);
// printf("%d\n", view_uniform_location); // printf("%d\n", view_uniform_location);
render_block(block); render_block(block);
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(tc_game_state_g.renderer.window);
} }
bool shift_pressed = false; bool shift_pressed = false;
@ -200,7 +145,7 @@ bool update()
switch(event.type) switch(event.type)
{ {
case SDL_QUIT: case SDL_QUIT:
SDL_HideWindow(window); SDL_HideWindow(tc_game_state_g.renderer.window);
return false; return false;
break; break;
@ -313,24 +258,15 @@ bool update()
return true; return true;
} }
void flushput(const char *message)
{
puts(message);
fflush(stdout);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
SDL_Init(SDL_INIT_EVERYTHING); tc_game_state_g = tc_init();
window = SDL_CreateWindow(
"Techneck",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1200,
800,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
gl_context = SDL_GL_CreateContext(window);
gladLoadGLLoader(&SDL_GL_GetProcAddress);
gladLoadGL();
main_shader = setup_shaders("vertex_shader.glsl", "fragment_shader.glsl");
block = tc_new_block_at_3f(0.0, 0.0, 0.0); block = tc_new_block_at_3f(0.0, 0.0, 0.0);
@ -345,16 +281,16 @@ int main(int argc, char **argv)
if(frame_index == 1) if(frame_index == 1)
{ {
SDL_ShowWindow(window); SDL_ShowWindow(tc_game_state_g.renderer.window);
} }
block.position.x += 0.1f; block.position.x += 0.005f;
++frame_index; ++frame_index;
SDL_Delay(1000/60); SDL_Delay(1000/60);
} }
cleanup(); tc_cleanup();
SDL_Quit(); SDL_Quit();
return 0; return 0;

92
code/source-c/shaders.c Normal file
View File

@ -0,0 +1,92 @@
#include "shaders.h"
char * load_file(char *path, int *out_len)
{
FILE *file = NULL;
if((file = fopen(path, "r")) != NULL)
{
fseek(file, 0, SEEK_END);
int length = ftell(file);
fseek(file, 0, SEEK_SET);
char *content = malloc(length+1);
fread(content, 1, length, file);
if(out_len != NULL)
{
*out_len = length;
}
return content;
}
return NULL;
}
tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_path)
{
int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
int len_vertex_source = 0;
char *vertex_source = load_file(vertex_path, &len_vertex_source);
int len_fragment_source = 0;
char *fragment_source = load_file(fragment_path, &len_fragment_source);
glShaderSource(vertex_shader, 1, &vertex_source, &len_vertex_source);
glShaderSource(fragment_shader, 1, &fragment_source, &len_fragment_source);
glCompileShader(vertex_shader);
glCompileShader(fragment_shader);
int vertex_compile_status = 0;
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vertex_compile_status);
if(vertex_compile_status != GL_TRUE)
{
int log_length = 0;
glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &log_length);
char *info_log = malloc(log_length+1);
glGetShaderInfoLog(vertex_shader, log_length, &log_length, info_log);
printf("VERTEX SHADER FAILED COMPILING! ERROR: %s\n", info_log);
tc_shader_program_s invalid_program;
invalid_program.program_id = 0;
return invalid_program;
}
int fragment_compile_status = 0;
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &fragment_compile_status);
if(fragment_compile_status != GL_TRUE)
{
int log_length = 0;
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &log_length);
char *info_log = malloc(log_length+1);
glGetShaderInfoLog(fragment_shader, log_length, &log_length, info_log);
printf("FRAGMENT SHADER FAILED COMPILING! ERROR: %s\n", info_log);
tc_shader_program_s invalid_program;
invalid_program.program_id = 0;
return invalid_program;
}
int program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
tc_shader_program_s shader_program;
shader_program.program_id = program;
return shader_program;
}

21
code/source-c/shaders.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef TC_SHADERS_H
#define TC_SHADERS_H
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <glad/glad.h>
typedef struct tc_shader_program
{
uint32_t program_id;
} tc_shader_program_s;
tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_path);
#endif

65
code/source-c/state.h Normal file
View File

@ -0,0 +1,65 @@
#ifndef TC_STATE_H
#define TC_STATE_H
#include <stdbool.h>
#include <stdint.h>
#include <SDL2/SDL.h>
#include "shaders.h"
typedef struct tc_vec3
{
float x;
float y;
float z;
} tc_vec3_s;
typedef struct tc_object
{
uint32_t vbo;
uint32_t vao;
tc_vec3_s position;
tc_vec3_s rotation;
} tc_object_s;
typedef struct tc_block
{
tc_vec3_s position;
tc_object_s drawing;
} tc_block_s;
typedef struct tc_camera
{
tc_vec3_s position;
tc_vec3_s rotation;
float fov;
} tc_camera_s;
typedef struct tc_renderer
{
tc_camera_s active_camera;
tc_shader_program_s draw_shader;
SDL_Window *window;
SDL_GLContext gl_context;
} tc_renderer_s;
typedef struct
{
tc_renderer_s renderer;
} techneck_s;
extern techneck_s tc_game_state_g;
techneck_s tc_init ();
void tc_cleanup ();
#endif