first commit
This commit is contained in:
43
src/gfx/Color.zig
Normal file
43
src/gfx/Color.zig
Normal file
@@ -0,0 +1,43 @@
|
||||
fn hue_to_rgb(p: f32, q: f32, t: f32) f32 {
|
||||
if (t < 0) {
|
||||
t += 1;
|
||||
} else if (t > 1) {
|
||||
t -= 1;
|
||||
}
|
||||
if (t < 1.0 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1.0 / 2) return q;
|
||||
if (t < 2.0 / 3) return p + (q - p) * (2.0 / 3 - t) * 6;
|
||||
return p;
|
||||
}
|
||||
|
||||
fn hsl_to_hex(h: f32, s: f32, l: f32) @Vector(3, f32) {
|
||||
h /= 360;
|
||||
s /= 100;
|
||||
l /= 100;
|
||||
const r: f32;
|
||||
const g: f32;
|
||||
const b: f32;
|
||||
if (s == 0) {
|
||||
r = l;
|
||||
g = l;
|
||||
b = l;
|
||||
} else {
|
||||
const q = if (l < 0.5) l * (1 + s) else l + s - l * s;
|
||||
const p = 2 * l - q;
|
||||
r = hue_to_rgb(p, q, h + 1.0 / 3);
|
||||
g = hue_to_rgb(p, q, h);
|
||||
b = hue_to_rgb(p, q, h - 1.0 / 3);
|
||||
}
|
||||
return @Vector(3, f32){ r, g, b };
|
||||
}
|
||||
|
||||
pub fn color_from_index(index: i32) @Vector(3, f32) {
|
||||
const color_wheel_cycle = @floor(index / 6.0);
|
||||
const darkness_cycle = @floor(index / 12.0);
|
||||
const spacing = (360.0 / 6.0);
|
||||
const offset = if (color_wheel_cycle == 0) 0 else spacing / (color_wheel_cycle + 2);
|
||||
const hue = spacing * (index % 6) + offset;
|
||||
const saturation = 100.0f;
|
||||
const lightness = 1.0f / (2 + darkness_cycle) * 100;
|
||||
return hsl_to_hex(hue, saturation, lightness);
|
||||
}
|
||||
94
src/gfx/Mesh.zig
Normal file
94
src/gfx/Mesh.zig
Normal file
@@ -0,0 +1,94 @@
|
||||
const std = @import("std");
|
||||
const c = @import("../c.zig");
|
||||
const djleddaGeom = @import("djleddaGeom.zig");
|
||||
|
||||
pub const Mesh = struct {
|
||||
vao: c_uint,
|
||||
vbo_xyz: c_uint,
|
||||
vbo_uv: c_uint,
|
||||
vbo_norm: c_uint,
|
||||
ebo: c_uint,
|
||||
num_indices: c_uint,
|
||||
|
||||
pub fn from_shape(shape: *const djleddaGeom.Shape) void {
|
||||
const mesh = Mesh{};
|
||||
mesh.num_indices = shape.indices.len;
|
||||
c.glGenVertexArrays(1, &mesh.vao);
|
||||
c.glGenBuffers(1, &mesh.vbo_xyz);
|
||||
c.glGenBuffers(1, &mesh.vbo_uv);
|
||||
c.glGenBuffers(1, &mesh.ebo);
|
||||
|
||||
c.glBindVertexArray(mesh.vao);
|
||||
|
||||
c.glBindBuffer(c.GL_ARRAY_BUFFER, mesh.vbo_xyz);
|
||||
c.glBufferData(c.GL_ARRAY_BUFFER, shape.xyz.ptr * @sizeOf(float), shape.xyz, c.GL_STATIC_DRAW);
|
||||
c.glVertexAttribPointer(0, 3, c.GL_FLOAT, c.GL_FALSE, 3 * @sizeOf(f32), @as(*void, 0));
|
||||
c.glEnableVertexAttribArray(0);
|
||||
|
||||
c.glBindBuffer(c.GL_ARRAY_BUFFER, mesh.vbo_uv);
|
||||
c.glBufferData(c.GL_ARRAY_BUFFER, shape.uv.ptr * @sizeOf(f32), shape.uv, c.GL_STATIC_DRAW);
|
||||
c.glVertexAttribPointer(1, 2, c.GL_FLOAT, c.GL_FALSE, 2 * @sizeOf(f32), @as(*void, 0));
|
||||
c.glEnableVertexAttribArray(1);
|
||||
|
||||
c.glBindBuffer(c.GL_ELEMENT_ARRAY_BUFFER, mesh.ebo);
|
||||
c.glBufferData(c.GL_ELEMENT_ARRAY_BUFFER, shape.indices.len * @sizeOf(c_uint), shape.indices.ptr, c.GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
// pub fn init(obj_file: *[]const u8) void {
|
||||
// const reader = c.tinyobj.ObjReader();
|
||||
// const success = reader.ParseFromFile(obj_file);
|
||||
// std.debug.print("{}\n", .{reader.Error()});
|
||||
//
|
||||
// const attrib = reader.GetAttrib();
|
||||
//
|
||||
// const indices_t = reader.GetShapes().at(0).mesh.indices;
|
||||
// const indices = ArrayList(c_uint)(indices_t.size());
|
||||
//
|
||||
// const vertices = ArrayList()(3*indices_t.size());
|
||||
// const normals = ArrayList()(3*indices_t.size());
|
||||
// const texcoords = ArrayList()(2*indices_t.size());
|
||||
//
|
||||
// for (int i = 0; i < indices_t.size(); i++) {
|
||||
// const vertex_data = indices_t[i];
|
||||
// vertices[3*i] = attrib.vertices[3*vertex_data.vertex_index];
|
||||
// vertices[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
|
||||
// vertices[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2];
|
||||
//
|
||||
// normals[3*i] = attrib.normals[3*vertex_data.normal_index];
|
||||
// normals[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1];
|
||||
// normals[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2];
|
||||
//
|
||||
// texcoords[2*i] = attrib.texcoords[2*vertex_data.texcoord_index];
|
||||
// texcoords[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1];
|
||||
//
|
||||
// indices[i] = i;
|
||||
// }
|
||||
//
|
||||
// num_indices = indices_t.size();
|
||||
// glGenVertexArrays(1, &vao);
|
||||
// glGenBuffers(1, &vbo_xyz);
|
||||
// glGenBuffers(1, &vbo_uv);
|
||||
// glGenBuffers(1, &vbo_norm);
|
||||
// //glGenBuffers(1, &ebo);
|
||||
//
|
||||
// glBindVertexArray(vao);
|
||||
//
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz);
|
||||
// glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
|
||||
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
// glEnableVertexAttribArray(0);
|
||||
//
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, vbo_uv);
|
||||
// glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(float), texcoords.data(), GL_STATIC_DRAW);
|
||||
// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
||||
// glEnableVertexAttribArray(1);
|
||||
//
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, vbo_norm);
|
||||
// glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW);
|
||||
// glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
// glEnableVertexAttribArray(2);
|
||||
//
|
||||
// //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
// //glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
|
||||
// }
|
||||
};
|
||||
56
src/gfx/Shader.zig
Normal file
56
src/gfx/Shader.zig
Normal file
@@ -0,0 +1,56 @@
|
||||
const c = @import("../c.zig");
|
||||
const std = @import("std");
|
||||
|
||||
const ShaderType = enum(u32) {
|
||||
fragment = c.GL_FRAGMENT_SHADER,
|
||||
vertex = c.GL_VERTEX_SHADER,
|
||||
};
|
||||
|
||||
fn create_shader(file_path: []const u8, shader_type: ShaderType, info_log: *[]const u8, allocator: *std.mem.Allocator) c_uint {
|
||||
const file = try std.fs.openFileAbsolute(file_path);
|
||||
|
||||
const file_reader = file.reader(file);
|
||||
const shader_code = std.ArrayList(u8);
|
||||
shader_code.initCapacity(allocator, 1024);
|
||||
defer allocator.free(shader_code);
|
||||
|
||||
file_reader.readAllArrayList(shader_code, 1024 * 1024);
|
||||
|
||||
const vertex_shader = c.glCreateShader(shader_type);
|
||||
c.glShaderSource(vertex_shader, 1, &shader_code.items, c.NULL);
|
||||
c.glCompileShader(vertex_shader);
|
||||
const success: i32 = undefined;
|
||||
c.glGetShaderiv(vertex_shader, c.GL_COMPILE_STATUS, &success);
|
||||
if (success != 0) {
|
||||
c.glGetShaderInfoLog(vertex_shader, 512, c.NULL, info_log);
|
||||
const shader_type_name = if (shader_type == ShaderType.fragment) "FRAGMENT" else "VERTEX";
|
||||
std.debug.print("ERROR::SHADER::{}::COMPILATION_FAILED\n{}\n", .{ shader_type_name, info_log });
|
||||
}
|
||||
|
||||
return vertex_shader;
|
||||
}
|
||||
|
||||
const Shader = struct {
|
||||
prog_id: c_uint,
|
||||
|
||||
pub fn init(self: Shader, vertex_path: *[]const u8, fragment_path: *[]const u8, allocator: *std.mem.Allocator) void {
|
||||
const info_log = [512]u8{};
|
||||
const vertex_shader = create_shader(vertex_path, ShaderType.vertex, &info_log, allocator);
|
||||
const fragment_shader = create_shader(fragment_path, ShaderType.fragment, &info_log, allocator);
|
||||
|
||||
self.prog_id = c.glCreateProgram();
|
||||
c.glAttachShader(self.prog_id, vertex_shader);
|
||||
c.glAttachShader(self.prog_id, fragment_shader);
|
||||
c.glLinkProgram(self.prog_id);
|
||||
|
||||
const success: c_uint = undefined;
|
||||
c.glGetProgramiv(self.prog_id, c.GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
c.glGetProgramInfoLog(self.prog_id, 512, c.NULL, &info_log);
|
||||
std.debug.print("ERROR::SHADER::PROGRAM::LINK_FAILED\n{}\n", .{info_log});
|
||||
}
|
||||
|
||||
c.glDeleteShader(vertex_shader);
|
||||
c.glDeleteShader(fragment_shader);
|
||||
}
|
||||
};
|
||||
57
src/gfx/djleddaGeom.zig
Normal file
57
src/gfx/djleddaGeom.zig
Normal file
@@ -0,0 +1,57 @@
|
||||
// Buffer layout:
|
||||
// X, Y, Z, U, V
|
||||
|
||||
pub const Shape = struct {
|
||||
indices: []c_uint,
|
||||
uv: []f32,
|
||||
xyz: []f32,
|
||||
};
|
||||
|
||||
const triangle_vertices = []f32{
|
||||
-0.5, -0.5, 0.0, 1.0, 1.0,
|
||||
0.5, -0.5, 0.0, 0.5, 0.5,
|
||||
0.0, 0.5, 0.0, 0.0, 0.0,
|
||||
};
|
||||
|
||||
const triangle_indices = []c_uint{ 0, 1, 2 };
|
||||
|
||||
const cube_vertices = []f32{ -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 1.0, 0.5, 0.5, -0.5, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0, 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, 0.5, 0.0, 0.0, -0.5, 0.5, 0.5, 1.0, 0.0, -0.5, 0.5, -0.5, 1.0, 1.0, -0.5, -0.5, -0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 1.0, -0.5, -0.5, 0.5, 0.0, 0.0, -0.5, 0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 1.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.5, -0.5, 0.5, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, -0.5, -0.5, -0.5, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, 1.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, -0.5, -0.5, -0.5, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.5, 0.5, -0.5, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, -0.5, 0.5, 0.5, 0.0, 0.0, -0.5, 0.5, -0.5, 0.0, 1.0 };
|
||||
|
||||
const cube_indices = []c_uint{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
|
||||
|
||||
const square_xyz = []f32{
|
||||
0.5, 0.5, 0.0,
|
||||
0.5, -0.5, 0.0,
|
||||
-0.5, -0.5, 0.0,
|
||||
-0.5, 0.5, 0.0,
|
||||
};
|
||||
|
||||
const square_uv = []f32{
|
||||
1.0, 1.0,
|
||||
1.0, 0.0,
|
||||
0.0, 0.0,
|
||||
0.0, 1.0,
|
||||
};
|
||||
|
||||
const square_indices = []c_uint{
|
||||
0, 1, 3,
|
||||
1, 2, 3,
|
||||
};
|
||||
|
||||
pub const TRIANGLE = Shape{
|
||||
.indices = triangle_indices,
|
||||
.uv = triangle_vertices,
|
||||
.xyz = triangle_vertices,
|
||||
};
|
||||
|
||||
pub const SQUARE = Shape{
|
||||
.indices = square_indices,
|
||||
.uv = square_uv,
|
||||
.xyz = square_xyz,
|
||||
};
|
||||
|
||||
pub const CUBE = Shape{
|
||||
.indices = cube_indices,
|
||||
.uv = triangle_vertices,
|
||||
.xyz = triangle_vertices,
|
||||
};
|
||||
Reference in New Issue
Block a user