Added glib and math dependencies, incorporated custom vector3, vector2, matrix4x4 structs

This commit is contained in:
2026-08-18 14:56:55 -04:00
parent b394340223
commit dfb583f446
5 changed files with 49 additions and 19 deletions
+14 -14
View File
@@ -1,18 +1,19 @@
/** /**
* A 4-by-4 matrix useful in OpenGL matrix operations. * A 4-by-4 matrix useful in OpenGL matrix operations.
*/ */
public struct Ch3.Mat4x4 { public struct Worldbuilder.Matrix4x4
{
public float data[4*4]; public float data[4*4];
/** /**
* Create a new matrix with a value along its diagonal. * 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++) for (int i = 0; i < 4; i++)
this[i, i] = diagonal_value; this[i, i] = diagonal_value;
} }
public Mat4x4.identity () { public Matrix4x4.identity () {
this (1.0f); this (1.0f);
} }
@@ -24,7 +25,7 @@ public struct Ch3.Mat4x4 {
* @param near distance to near plane along the z axis * @param near distance to near plane along the z axis
* @param far distance to far 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 // see http://www.songho.ca/opengl/gl_projectionmatrix.html
float height_2 = Math.tanf (fov / 2) * near; 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 target what the camera is looking at
* @param up a direction that represents "up" * @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 // see https://learnopengl.com/Getting-started/Camera
up = up.normalized (); up = up.normalized ();
@@ -82,13 +83,13 @@ public struct Ch3.Mat4x4 {
return data[col*4 + row]; 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; var copy = this;
copy[row, col] = value; copy[row, col] = value;
return copy; return copy;
} }
public Mat4x4 rotateX (float radians) { public Matrix4x4 rotateX (float radians) {
// see https://learnopengl.com/Getting-started/Transformations // see https://learnopengl.com/Getting-started/Transformations
var copy = this; var copy = this;
// rotate around x axis // rotate around x axis
@@ -103,7 +104,7 @@ public struct Ch3.Mat4x4 {
return copy; return copy;
} }
public Mat4x4 rotateY (float radians) { public Matrix4x4 rotateY (float radians) {
var copy = this; var copy = this;
// rotate around y axis // rotate around y axis
float sin_y, cos_y; float sin_y, cos_y;
@@ -117,7 +118,7 @@ public struct Ch3.Mat4x4 {
return copy; return copy;
} }
public Mat4x4 rotateZ (float radians) { public Matrix4x4 rotateZ (float radians) {
var copy = this; var copy = this;
// rotate around z axis // rotate around z axis
float sin_z, cos_z; float sin_z, cos_z;
@@ -131,7 +132,7 @@ public struct Ch3.Mat4x4 {
return copy; return copy;
} }
public Mat4x4 scale (float x, float y, float z) { public Matrix4x4 scale (float x, float y, float z) {
var copy = this; var copy = this;
copy[0, 0] *= x; copy[0, 0] *= x;
copy[1, 1] *= y; copy[1, 1] *= y;
@@ -139,7 +140,7 @@ public struct Ch3.Mat4x4 {
return copy; return copy;
} }
public Mat4x4 translate (float x, float y, float z) { public Matrix4x4 translate (float x, float y, float z) {
var copy = this; var copy = this;
copy[0, 3] += x; copy[0, 3] += x;
copy[1, 3] += y; copy[1, 3] += y;
@@ -147,8 +148,8 @@ public struct Ch3.Mat4x4 {
return copy; return copy;
} }
public Mat4x4 multiply (Mat4x4 other) { public Matrix4x4 multiply (Matrix4x4 other) {
var result = Mat4x4 (0); var result = Matrix4x4 (0);
// this x other = result // this x other = result
for (int row = 0; row < 4; row++) for (int row = 0; row < 4; row++)
@@ -188,4 +189,3 @@ public struct Ch3.Mat4x4 {
return sb.str; return sb.str;
} }
} }
+21
View File
@@ -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);
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ public struct Worldbuilder.Vector2 {
public float x; public float x;
public float y; public float y;
public Vec2 (float x = 0, float y = 0) { public Vector2 (float x = 0, float y = 0) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
+11 -2
View File
@@ -2,14 +2,21 @@ worldbuilder_sources = [
'main.vala', 'main.vala',
'application.vala', 'application.vala',
'window.vala', 'window.vala',
'scene_renderer.vala',
'core/vector3.vala',
'core/matrix4x4.vala',
'core/vector2.vala',
] ]
worldbuilder_deps = [ worldbuilder_deps = [
config_dep, config_dep,
dependency('glib-2.0'),
dependency('gtk4', version: '>= 4.2'), dependency('gtk4', version: '>= 4.2'),
dependency('libadwaita-1', version: '>= 1.4'), dependency('libadwaita-1', version: '>= 1.4'),
cc.find_library('epoxy'), cc.find_library('epoxy'),
valac.find_library('gl', dirs: meson.source_root() / 'vapi'), 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', worldbuilder_sources += gnome.compile_resources('worldbuilder-resources',
@@ -17,8 +24,10 @@ worldbuilder_sources += gnome.compile_resources('worldbuilder-resources',
c_name: 'worldbuilder' c_name: 'worldbuilder'
) )
executable('worldbuilder', worldbuilder_sources, executable(
'worldbuilder',
worldbuilder_sources,
dependencies: worldbuilder_deps, dependencies: worldbuilder_deps,
include_directories: config_inc, include_directories: config_inc,
install: true, install: true,
) )
+2 -2
View File
@@ -7,8 +7,8 @@
<style> <style>
<class name="devel"/> <class name="devel"/>
</style> </style>
<property name="default-width">800</property> <property name="default-width">1280</property>
<property name="default-height">600</property> <property name="default-height">720</property>
<property name="content"> <property name="content">
<object class="AdwToolbarView"> <object class="AdwToolbarView">