From dfb583f446300729d359b1405b8455032ebaef88 Mon Sep 17 00:00:00 2001 From: Brody Brooks Date: Tue, 18 Aug 2026 14:56:55 -0400 Subject: [PATCH] Added glib and math dependencies, incorporated custom vector3, vector2, matrix4x4 structs --- src/core/matrix4x4.vala | 28 ++++++++++++++-------------- src/core/texture2d.vala | 21 +++++++++++++++++++++ src/core/vector2.vala | 2 +- src/meson.build | 13 +++++++++++-- src/window.ui | 4 ++-- 5 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 src/core/texture2d.vala diff --git a/src/core/matrix4x4.vala b/src/core/matrix4x4.vala index ca5756c..ee9fbf5 100644 --- a/src/core/matrix4x4.vala +++ b/src/core/matrix4x4.vala @@ -1,18 +1,19 @@ /** * A 4-by-4 matrix useful in OpenGL matrix operations. */ -public struct Ch3.Mat4x4 { +public struct Worldbuilder.Matrix4x4 +{ public float data[4*4]; /** * Create a new matrix with a value along its diagonal. */ - public Mat4x4 (float diagonal_value) { + public Matrix4x4 (float diagonal_value) { for (int i = 0; i < 4; i++) this[i, i] = diagonal_value; } - public Mat4x4.identity () { + public Matrix4x4.identity () { this (1.0f); } @@ -24,7 +25,7 @@ public struct Ch3.Mat4x4 { * @param near distance to near plane along the z axis * @param far distance to far plane along the z axis */ - public Mat4x4.perspective (float fov, float aspect_ratio, float near, float far) { + public Matrix4x4.perspective (float fov, float aspect_ratio, float near, float far) { // see http://www.songho.ca/opengl/gl_projectionmatrix.html float height_2 = Math.tanf (fov / 2) * near; @@ -48,7 +49,7 @@ public struct Ch3.Mat4x4 { * @param target what the camera is looking at * @param up a direction that represents "up" */ - public Mat4x4.look_at (Vec3 position, Vec3 target, Vec3 up) { + public Matrix4x4.look_at (Vector3 position, Vector3 target, Vector3 up) { // see https://learnopengl.com/Getting-started/Camera up = up.normalized (); @@ -82,13 +83,13 @@ public struct Ch3.Mat4x4 { return data[col*4 + row]; } - public Mat4x4 write (int row, int col, float value) { + public Matrix4x4 write (int row, int col, float value) { var copy = this; copy[row, col] = value; return copy; } - public Mat4x4 rotateX (float radians) { + public Matrix4x4 rotateX (float radians) { // see https://learnopengl.com/Getting-started/Transformations var copy = this; // rotate around x axis @@ -103,7 +104,7 @@ public struct Ch3.Mat4x4 { return copy; } - public Mat4x4 rotateY (float radians) { + public Matrix4x4 rotateY (float radians) { var copy = this; // rotate around y axis float sin_y, cos_y; @@ -117,7 +118,7 @@ public struct Ch3.Mat4x4 { return copy; } - public Mat4x4 rotateZ (float radians) { + public Matrix4x4 rotateZ (float radians) { var copy = this; // rotate around z axis float sin_z, cos_z; @@ -131,7 +132,7 @@ public struct Ch3.Mat4x4 { return copy; } - public Mat4x4 scale (float x, float y, float z) { + public Matrix4x4 scale (float x, float y, float z) { var copy = this; copy[0, 0] *= x; copy[1, 1] *= y; @@ -139,7 +140,7 @@ public struct Ch3.Mat4x4 { return copy; } - public Mat4x4 translate (float x, float y, float z) { + public Matrix4x4 translate (float x, float y, float z) { var copy = this; copy[0, 3] += x; copy[1, 3] += y; @@ -147,8 +148,8 @@ public struct Ch3.Mat4x4 { return copy; } - public Mat4x4 multiply (Mat4x4 other) { - var result = Mat4x4 (0); + public Matrix4x4 multiply (Matrix4x4 other) { + var result = Matrix4x4 (0); // this x other = result for (int row = 0; row < 4; row++) @@ -188,4 +189,3 @@ public struct Ch3.Mat4x4 { return sb.str; } } - diff --git a/src/core/texture2d.vala b/src/core/texture2d.vala new file mode 100644 index 0000000..efe677c --- /dev/null +++ b/src/core/texture2d.vala @@ -0,0 +1,21 @@ +public class Worldbuilder.Texture2D : Object { + public string path { get; construct; } + public GL.uint id { get; private set; } + + /** + * Loads a 2D texture from a resource and sets up a GL texture buffer. + * + * @param filename basename + */ + public Texture2D (string filename) throws Error { + Object (path: @"/com/brodybrooks/worldbuilder/textures/$filename"); + var pixbuf = new Gdk.Pixbuf.from_resource (this.path); + debug ("loading %s ...", this.path); + id = GL.gen_texture (); + GL.bind_texture (GL.TEXTURE_2D, id); + GL.tex_image_2D (GL.TEXTURE_2D, 0, GL.RGB, pixbuf.width, pixbuf.height, 0, GL.RGB, GL.UNSIGNED_BYTE, pixbuf.get_pixels ()); + GL.generate_mipmap (GL.TEXTURE_2D); + GL.bind_texture (GL.TEXTURE_2D, 0); + } +} + diff --git a/src/core/vector2.vala b/src/core/vector2.vala index 548ea40..2b91ce6 100644 --- a/src/core/vector2.vala +++ b/src/core/vector2.vala @@ -2,7 +2,7 @@ public struct Worldbuilder.Vector2 { public float x; public float y; - public Vec2 (float x = 0, float y = 0) { + public Vector2 (float x = 0, float y = 0) { this.x = x; this.y = y; } diff --git a/src/meson.build b/src/meson.build index 94463fc..9e9c036 100644 --- a/src/meson.build +++ b/src/meson.build @@ -2,14 +2,21 @@ worldbuilder_sources = [ 'main.vala', 'application.vala', 'window.vala', + 'scene_renderer.vala', + + 'core/vector3.vala', + 'core/matrix4x4.vala', + 'core/vector2.vala', ] worldbuilder_deps = [ config_dep, + dependency('glib-2.0'), dependency('gtk4', version: '>= 4.2'), dependency('libadwaita-1', version: '>= 1.4'), cc.find_library('epoxy'), valac.find_library('gl', dirs: meson.source_root() / 'vapi'), + cc.find_library('m', required: false) # optional on Windows ] worldbuilder_sources += gnome.compile_resources('worldbuilder-resources', @@ -17,8 +24,10 @@ worldbuilder_sources += gnome.compile_resources('worldbuilder-resources', c_name: 'worldbuilder' ) -executable('worldbuilder', worldbuilder_sources, +executable( + 'worldbuilder', + worldbuilder_sources, dependencies: worldbuilder_deps, include_directories: config_inc, - install: true, + install: true, ) diff --git a/src/window.ui b/src/window.ui index 6022d18..f08c8a7 100644 --- a/src/window.ui +++ b/src/window.ui @@ -7,8 +7,8 @@ - 800 - 600 + 1280 + 720