Factor out constructors

This commit is contained in:
Nathan Chapman 2025-07-06 14:43:59 -06:00
parent d398f3a1d9
commit 908232c31e
13 changed files with 60 additions and 84 deletions

View File

@ -1,10 +1,6 @@
#include "camera.h"
Camera::Camera(Vector3 position, Vector3 world_up, f32 yaw, f32 pitch, f32 movement_speed, f32 rotation_speed) : position(position), world_up(world_up), yaw(yaw), pitch(pitch), movement_speed(movement_speed), rotation_speed(rotation_speed) {
update_camera(this);
}
void update_camera(Camera *camera) {
Vector3 new_front = Vector3(
cos(glm::radians(camera->yaw)) * cos(glm::radians(camera->pitch)),

View File

@ -7,19 +7,16 @@
#include "math.h"
struct Camera {
Vector3 position;
Vector3 position = Vector3(0.0f, 0.0f, 0.0f);
Vector3 front = Vector3(0.0f, 0.0f, -1.0f);
Vector3 up;
Vector3 right;
Vector3 world_up;
Vector3 up = Vector3(0.0f, 0.0f, 0.0f);
Vector3 right = Vector3(0.0f, 0.0f, 0.0f);
Vector3 world_up = Vector3(0.0f, 1.0f, 0.0f);
f32 yaw;
f32 pitch;
f32 movement_speed;
f32 rotation_speed;
Camera(Vector3 position, Vector3 up, f32 yaw, f32 pitch, f32 movement_speed, f32 rotation_speed);
~Camera() = default;
f32 yaw = 90.0f;
f32 pitch = 0.0f;
f32 movement_speed = 5.0f;
f32 rotation_speed = 1.0f;
};
void update_camera(Camera *camera);

View File

@ -1,8 +1,6 @@
#include "light.h"
Light::Light(Vector3 color, f32 ambient_intensity, Vector3 direction, f32 diffuse_intensity) : color(color), ambient_intensity(ambient_intensity), direction(direction), diffuse_intensity(diffuse_intensity) {}
void use_light(Light *light, f32 color_location, f32 ambient_intensity_location, f32 direction_location, f32 diffuse_intensity_location) {
void use_light(Directional_Light *light, f32 color_location, f32 ambient_intensity_location, f32 diffuse_intensity_location, f32 direction_location) {
glUniform3f(color_location, light->color.x, light->color.y, light->color.z);
glUniform1f(ambient_intensity_location, light->ambient_intensity);

View File

@ -7,14 +7,12 @@
struct Light {
Vector3 color = Vector3(1.0f, 1.0f, 1.0f);
f32 ambient_intensity = 1.0f;
// For a directional light
Vector3 direction = Vector3(0.0f, -1.0f, 0.0f); // Default to down
f32 diffuse_intensity = 0.0f;
};
Light(Vector3 color, f32 ambient_intensity, Vector3 direction, f32 diffuse_intensity);
~Light() = default;
struct Directional_Light : public Light {
Vector3 direction = Vector3(0.0f, -1.0f, 0.0f); // Default to down
};
// Some of these should be u32
void use_light(Light *light, f32 color_location, f32 ambient_intensity_location, f32 direction_location, f32 diffuse_intensity_location);
void use_light(Directional_Light *light, f32 color_location, f32 ambient_intensity_location, f32 diffuse_intensity_location, f32 direction_location);

View File

@ -16,9 +16,16 @@ int main() {
Mesh *meshes[10] = {nullptr};
Shader shaders[10];
Window window(1920, 1080);
Window window;
window.width = 1920;
window.height = 1080;
bool success = setup_window(&window);
if (!success) {
printf("Failed to setup window");
return 1;
}
// Delta
f32 dt = 0.0f;
f32 last_dt = 0.f;
@ -58,20 +65,26 @@ int main() {
shaders[0] = *shader;
}
Texture clay_texture = Texture((char *)"assets/textures/clay_texture_1k.png");
Texture clay_texture;
clay_texture.file_path = (char *)"assets/textures/clay_texture_1k.png";
load_texture(&clay_texture);
Texture plaster_texture = Texture((char *)"assets/textures/plaster_texture_1k.png");
Texture plaster_texture;
plaster_texture.file_path = (char *)"assets/textures/plaster_texture_1k.png";
load_texture(&plaster_texture);
Material shiny_material(1.0f, 32.0f);
Material dull_material(0.3f, 4.0f);
Material shiny_material{32.0f, 1.0f};
Material dull_material{4.0f, 0.3f};
Light sun_light(Vector3(1.0f, 1.0f, 1.0f), 0.2f, Vector3(2.0f, -1.0, -2.0f), 1.0f);
Directional_Light sun_light;
sun_light.ambient_intensity = 0.2f;
sun_light.diffuse_intensity = 1.0f;
sun_light.direction = Vector3(2.0f, -1.0, -2.0f);
Matrix4 projection = glm::perspective(45.0f, (f32)window.buffer_width / (f32)window.buffer_height, 0.1f, 100.0f);
Camera camera(Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f), 90.0f, 0.0f, 5.0f, 1.0f);
Camera camera;
update_camera(&camera);
// Loop until window is closed
@ -97,7 +110,7 @@ int main() {
use_light(
&sun_light,
shaders[0].uniform_ambient_color, shaders[0].uniform_ambient_intensity,
shaders[0].uniform_diffuse_direction, shaders[0].uniform_diffuse_intensity
shaders[0].uniform_diffuse_intensity, shaders[0].uniform_diffuse_direction
);
glUniformMatrix4fv(shaders[0].uniform_projection, 1, GL_FALSE, glm::value_ptr(projection));

View File

@ -1,8 +1,6 @@
#include "material.h"
Material::Material(f32 specular_intensity, f32 shininess) : specular_intensity(specular_intensity), shininess(shininess) {}
void use_material(Material *material, u32 shininess_location, u32 specular_intensity_location) {
glUniform1f(shininess_location, material->shininess);
glUniform1f(specular_intensity_location, material->specular_intensity);

View File

@ -7,11 +7,8 @@
The kind of a thing
*/
struct Material {
f32 shininess;
f32 specular_intensity;
Material(f32 specular_intensity, f32 shininess);
~Material() = default;
f32 shininess = 2.0f;
f32 specular_intensity = 0.2f;
};
void use_material(Material *material, u32 shininess_location, u32 specular_intensity_location);

View File

@ -1,10 +1,5 @@
#include "shader.h"
Shader::Shader() {
id = 0;
uniform_projection = 0;
uniform_model = 0;
}
Shader::~Shader() {
clear_shader(this);
@ -47,7 +42,7 @@ bool create_shader(Shader *shader, const char *vertex_shader_path, const char *f
glGetProgramiv(shader->id, GL_LINK_STATUS, &result);
if (!result) {
glGetProgramInfoLog(shader->id, sizeof(errors), NULL, errors);
glGetProgramInfoLog(shader->id, sizeof(errors), nullptr, errors);
printf("Error linking shader: '%s'\n", errors);
return false;
}
@ -56,7 +51,7 @@ bool create_shader(Shader *shader, const char *vertex_shader_path, const char *f
glGetProgramiv(shader->id, GL_VALIDATE_STATUS, &result);
if (!result) {
glGetProgramInfoLog(shader->id, sizeof(errors), NULL, errors);
glGetProgramInfoLog(shader->id, sizeof(errors), nullptr, errors);
printf("Error validating shader: '%s'\n", errors);
return false;
}
@ -94,7 +89,7 @@ bool add_shader(Shader *shader, const char *shader_code, Shader_Type shader_type
glGetShaderiv(_shader, GL_COMPILE_STATUS, &result);
if (!result) {
glGetShaderInfoLog(_shader, sizeof(errors), NULL, errors);
glGetShaderInfoLog(_shader, sizeof(errors), nullptr, errors);
printf("Error compiling the %d shader->id: '%s'\n", shader_type, errors);
return false;
}

View File

@ -11,22 +11,21 @@ enum Shader_Type : u32 {
};
struct Shader {
u32 id;
u32 uniform_projection;
u32 uniform_model;
u32 uniform_view;
u32 uniform_eye_position;
u32 id = 0;
u32 uniform_projection = 0;
u32 uniform_model = 0;
u32 uniform_view = 0;
u32 uniform_eye_position = 0;
u32 uniform_ambient_color;
u32 uniform_ambient_intensity;
u32 uniform_ambient_color = 0;
u32 uniform_ambient_intensity = 0;
u32 uniform_diffuse_direction;
u32 uniform_diffuse_intensity;
u32 uniform_diffuse_direction = 0;
u32 uniform_diffuse_intensity = 0;
u32 uniform_shininess;
u32 uniform_specular_intensity;
u32 uniform_shininess = 0;
u32 uniform_specular_intensity = 0;
Shader();
~Shader();
};

View File

@ -1,6 +1,5 @@
#include "texture.h"
Texture::Texture(char *file_path) : file_path(file_path) {}
Texture::~Texture() {
clear_texture(this);
@ -44,6 +43,6 @@ void clear_texture(Texture *texture) {
texture->width = 0;
texture->height = 0;
texture->bit_depth = 0;
texture->file_path = NULL;
texture->file_path = nullptr;
}

View File

@ -9,9 +9,8 @@ struct Texture {
int width = 0;
int height = 0;
int bit_depth = 0;
char *file_path;
char *file_path = nullptr;
Texture(char *file_path);
~Texture();
};

View File

@ -1,18 +1,6 @@
#include "window.h"
Window::Window(u32 window_width, u32 window_height) {
width = window_width;
height = window_height;
x_change_position = 0.0f;
y_change_position = 0.0f;
for (size_t i = 0; i < 1024; i++) {
ascii_keys[i] = 0;
}
}
Window::~Window() {
glfwDestroyWindow(gl_window);
glfwTerminate();
@ -36,7 +24,7 @@ bool setup_window(Window *window) {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
window->gl_window = glfwCreateWindow(window->width, window->height, "Test Window", NULL, NULL);
window->gl_window = glfwCreateWindow(window->width, window->height, "Test Window", nullptr, nullptr);
if (!window->gl_window) {
const char *desc;

View File

@ -10,18 +10,17 @@
struct Window {
u32 width = 1024, height = 768;
s32 buffer_width, buffer_height;
s32 buffer_width = 0, buffer_height = 0;
GLFWwindow *gl_window = nullptr;
bool ascii_keys[1024];
bool ascii_keys[1024] = {false};
// Camera coords
f32 last_x_position;
f32 last_y_position;
f32 x_change_position;
f32 y_change_position;
bool first_mouse_movement;
f32 last_x_position = 0.0f;
f32 last_y_position = 0.0f;
f32 x_change_position = 0.0f;
f32 y_change_position = 0.0f;
bool first_mouse_movement = true;
Window(u32 window_width, u32 window_height);
~Window();
};