~vladh/hare-hazel

1cf209c6a62f1a3fb689cefcec10354eaf0bf8d1 — Vlad-Stefan Harbuz 1 year, 4 months ago 419038d
move shaders into files
4 files changed, 60 insertions(+), 46 deletions(-)

M hazel/graphics/graphics.ha
M hazel/graphics/shaders.ha
A shaders/bunny.frag
A shaders/bunny.vert
M hazel/graphics/graphics.ha => hazel/graphics/graphics.ha +17 -2
@@ 2,7 2,10 @@
// SPDX-License-Identifier: MIT

use fmt;
use io;
use math;
use os;
use strings;

use gl;
use glm;


@@ 60,8 63,20 @@ export fn init_window() void = {
};

fn init_shaders() void = {
	const vert = make_shader(VERT_SHADER, gl::GL_VERTEX_SHADER)!;
	const frag = make_shader(FRAG_SHADER, gl::GL_FRAGMENT_SHADER)!;
	let source: [10000]u8 = [0...];

	const vert_f = os::open("shaders/bunny.vert")!;
	defer io::close(vert_f)!;
	io::read(vert_f, source)!;
	const vert = make_shader(&source[0], gl::GL_VERTEX_SHADER)!;

	source = [0...];

	const frag_f = os::open("shaders/bunny.frag")!;
	defer io::close(frag_f)!;
	io::read(frag_f, source)!;
	const frag = make_shader(&source[0], gl::GL_FRAGMENT_SHADER)!;

	const prog = make_shader_program(vert, frag)!;
	gl::glDeleteShader(vert);
	gl::glDeleteShader(frag);

M hazel/graphics/shaders.ha => hazel/graphics/shaders.ha +6 -44
@@ 11,51 11,13 @@ use glm::{ v3, v4, m4 };

use hazel::util;

def VERT_SHADER: str = "
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec3 color;

out vec3 vs_normal;
out vec3 vs_color;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
	gl_Position = projection * view * model * vec4(pos.x, pos.y, pos.z, 1.0);
	vs_normal = normal;
	vs_color = color;
}
\0";

def FRAG_SHADER: str = "
#version 330 core
out vec4 out_color;

in vec3 vs_normal;
in vec3 vs_color;

uniform float t;
uniform vec3 uniform_color;

void main()
{
	out_color = vec4((vs_normal + 1.0) / 2.0, 1.0);
	// out_color = vec4(
	// 	(uniform_color * 0.3) + vs_color * (((sin(t) + 1.0) / 2) + 0.3),
	// 	1.0f
	// );
}
\0";

fn make_shader(source: str, kind: uint) (uint | errors::invalid) = {
fn make_shader(
	source: nullable *const u8,
	kind: uint,
) (uint | errors::invalid) = {
	const shader = gl::glCreateShader(kind);
	const cstr = util::cstr(source);
	gl::glShaderSource(shader, 1, &cstr, null);
	gl::glShaderSource(shader, 1,
		&source: nullable *const nullable *const i8, null);
	gl::glCompileShader(shader);

	let success: i32 = 1;

A shaders/bunny.frag => shaders/bunny.frag +18 -0
@@ 0,0 1,18 @@
#version 330 core
out vec4 out_color;

in vec3 vs_normal;
in vec3 vs_color;

uniform float t;
uniform vec3 uniform_color;

void
main()
{
	out_color = vec4((vs_normal + 1.0) / 2.0, 1.0);
	// out_color = vec4(
	// 	(uniform_color * 0.3) + vs_color * (((sin(t) + 1.0) / 2) + 0.3),
	// 	1.0f
	// );
}

A shaders/bunny.vert => shaders/bunny.vert +19 -0
@@ 0,0 1,19 @@
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec3 color;

out vec3 vs_normal;
out vec3 vs_color;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void
main()
{
	gl_Position = projection * view * model * vec4(pos.x, pos.y, pos.z, 1.0);
	vs_normal = normal;
	vs_color = color;
}