Techneck/code/source-c/main.c

364 lines
11 KiB
C

#include "state.h"
#include <glad/glad.h>
#include <SDL2/SDL.h>
#include <linmath.h>
mat4x4 projection_matrix;
mat4x4 view_matrix;
float triangle_vertices[108] =
{
// Front
-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,
// 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_rotation = { 0.0f, 0.0f, 0.0f };
void render_block(tc_block_s block)
{
mat4x4 model_matrix;
mat4x4_identity(model_matrix);
mat4x4_rotate_X(view_matrix, model_matrix, block.rotation.x);
mat4x4_rotate_Y(view_matrix, model_matrix, block.rotation.y);
mat4x4_rotate_Z(view_matrix, model_matrix, block.rotation.z);
mat4x4_translate_in_place(model_matrix, block.position.x, block.position.y, block.position.z);
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]);
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
tc_block_s tc_new_block_at_3f(float x, float y, float z)
{
tc_block_s block;
block.position.x = x;
block.position.y = y;
block.position.z = z;
block.rotation.x = 0.0f;
block.rotation.y = 0.0f;
block.rotation.z = 0.0f;
glGenBuffers(1, &block.drawing.vbo);
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 108, &triangle_vertices[0], GL_STATIC_DRAW);
return block;
}
void render()
{
float color[4] = { 0.12f, 0.5f, 0.8f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color);
float depth = 10000000.0f;
glClearBufferfv(GL_DEPTH, 0, &depth);
int fb_width = 0;
int fb_height = 0;
SDL_GetWindowSize(tc_game_state_g.renderer.window, &fb_width, &fb_height);
glViewport(0, 0, fb_width, fb_height);
mat4x4_perspective(projection_matrix, 120, ((float) fb_width) / ((float) fb_height), 0.0001f, 1000.0f);
tc_vec3_s camera_rotation = tc_game_state_g.renderer.active_camera.rotation;
mat4x4_identity(view_matrix);
mat4x4_rotate_X(view_matrix, view_matrix, camera_rotation.x);
mat4x4_rotate_Y(view_matrix, view_matrix, camera_rotation.y);
mat4x4_rotate_Z(view_matrix, view_matrix, camera_rotation.z);
mat4x4_translate_in_place(view_matrix, -tc_camera_position.x, -tc_camera_position.y, tc_camera_position.z);
glUseProgram(tc_game_state_g.renderer.draw_shader.program_id);
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]);
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]);
// printf("%d\n", view_uniform_location);
tc_draw_world(tc_game_state_g.main_world);
SDL_GL_SwapWindow(tc_game_state_g.renderer.window);
}
bool shift_pressed = false;
bool go_up = false;
bool go_left = false;
bool go_right = false;
bool go_forward = false;
bool go_backwards = false;
bool rotate_left = false;
bool rotate_right = false;
bool update()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
SDL_HideWindow(tc_game_state_g.renderer.window);
return false;
break;
case SDL_WINDOWEVENT:
{
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
{
}
} break;
case SDL_KEYDOWN:
if(event.key.keysym.scancode == SDL_SCANCODE_Q)
{
rotate_left = true;
}
if(event.key.keysym.scancode == SDL_SCANCODE_E)
{
rotate_right = true;
}
if(event.key.keysym.scancode == SDL_SCANCODE_W)
{
go_forward = true;
}
if(event.key.keysym.scancode == SDL_SCANCODE_S)
{
go_backwards = true;
}
if(event.key.keysym.scancode == SDL_SCANCODE_A)
{
go_left = true;
}
if(event.key.keysym.scancode == SDL_SCANCODE_D)
{
go_right = true;;
}
if(event.key.keysym.scancode == SDL_SCANCODE_SPACE)
{
go_up = true;
}
if(event.key.keysym.scancode == SDL_SCANCODE_LSHIFT)
{
shift_pressed = true;
}
break;
case SDL_KEYUP:
if(event.key.keysym.scancode == SDL_SCANCODE_Q)
{
rotate_left = false;
}
if(event.key.keysym.scancode == SDL_SCANCODE_E)
{
rotate_right = false;
}
if(event.key.keysym.scancode == SDL_SCANCODE_W)
{
go_forward = false;
}
if(event.key.keysym.scancode == SDL_SCANCODE_S)
{
go_backwards = false;
}
if(event.key.keysym.scancode == SDL_SCANCODE_A)
{
go_left = false;
}
if(event.key.keysym.scancode == SDL_SCANCODE_D)
{
go_right = false;;
}
if(event.key.keysym.scancode == SDL_SCANCODE_SPACE)
{
go_up = false;
}
if(event.key.keysym.scancode == SDL_SCANCODE_LSHIFT)
{
shift_pressed = false;
}
break;
case SDL_MOUSEMOTION:
{
if(!(event.motion.state & SDL_BUTTON_LMASK)) break;
tc_game_state_g.renderer.active_camera.rotation.y += event.motion.xrel / 50.0f;
tc_game_state_g.renderer.active_camera.rotation.x += event.motion.yrel / 50.0f;
if(tc_game_state_g.renderer.active_camera.rotation.x > (3.1415f/2.0f))
tc_game_state_g.renderer.active_camera.rotation.x = (3.1415f/2.0f);
if(tc_game_state_g.renderer.active_camera.rotation.x < -(3.1415f/2.0f))
tc_game_state_g.renderer.active_camera.rotation.x = -(3.1415f/2.0f);
} break;
}
}
if(shift_pressed)
{
tc_camera_position.y -= 0.25;
}
if(go_up)
{
tc_camera_position.y += 0.25;
}
tc_vec3_s difference_in_perspective;
difference_in_perspective.x = 0.0f;
difference_in_perspective.y = 0.0f;
difference_in_perspective.z = 0.0f;
if(go_left)
{
difference_in_perspective.x -= 0.45f;
}
if(go_right)
{
difference_in_perspective.x += 0.45f;
}
if(go_forward)
{
difference_in_perspective.z += 0.4f;
}
if(go_backwards)
{
difference_in_perspective.z -= 0.4f;
}
mat4x4 position_matrix;
mat4x4_identity(position_matrix);
mat4x4_translate(position_matrix, difference_in_perspective.x, 0.0f, difference_in_perspective.z);
mat4x4_rotate_X(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.x);
mat4x4_rotate_Y(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.y);
mat4x4_rotate_Z(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.z);
vec4 viewed_difference = { difference_in_perspective.x, 0.0f, difference_in_perspective.z };
vec4 raw_difference = { 0.0f, 0.0f, 0.0f };
mat4x4_mul_vec4(raw_difference, position_matrix, viewed_difference);
tc_camera_position.x += raw_difference[0];
tc_camera_position.z += raw_difference[2];
tc_game_state_g.main_world->spawn_loader->center.x = ((int32_t) tc_camera_position.x) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.y = ((int32_t) tc_camera_position.y) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.z = ((int32_t) tc_camera_position.z) / 32 - UPDATE_DISTANCE;
tc_update_world(tc_game_state_g.main_world);
return true;
}
int main(int argc, char **argv)
{
tc_init();
int64_t frame_index = 1;
while(frame_index > 0)
{
if(!update())
{
frame_index = -1;
}
render();
if(frame_index == 1)
{
SDL_ShowWindow(tc_game_state_g.renderer.window);
}
++frame_index;
SDL_Delay(1000/60);
}
tc_cleanup();
SDL_Quit();
return 0;
}