use package gl as gl
use package gl_utils as gl_utils
+use package main { window_width, window_height }
+
Vec2 :: struct {
x : f32;
y : f32;
}
QuadRenderer :: struct {
- is_initialized : bool = false;
-
quad_data : [..] Quad;
vertexArray : gl.GLVertexArrayObject;
program : gl.GLProgram;
+ u_proj_loc : gl.GLUniformLocation;
+
is_data_dirty : bool = false;
}
data = cast(^void) quad_data.data,
}, gl.DYNAMIC_DRAW);
- gl.enableVertexAttribArray(1);
- gl.enableVertexAttribArray(2);
- gl.enableVertexAttribArray(3);
- gl.enableVertexAttribArray(4);
- gl.enableVertexAttribArray(5);
- gl.vertexAttribDivisor(1, 1);
- gl.vertexAttribDivisor(2, 1);
- gl.vertexAttribDivisor(3, 1);
- gl.vertexAttribDivisor(4, 1);
- gl.vertexAttribDivisor(5, 1);
+ for i: 1 .. 6 {
+ gl.enableVertexAttribArray(i);
+ gl.vertexAttribDivisor(i, 1);
+ }
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, sizeof Quad, 0);
gl.vertexAttribPointer(2, 2, gl.FLOAT, false, sizeof Quad, 2 * sizeof f32);
gl.vertexAttribPointer(3, 2, gl.FLOAT, false, sizeof Quad, 4 * sizeof f32);
program = gl_utils.link_program(vertex_shader, fragment_shader);
gl.useProgram(program);
- u_proj_loc := gl.getUniformLocation(program, "u_proj");
- proj_mat : [9] gl.GLfloat;
- proj_mat[3 * 0 + 0] = 2.0f;
- proj_mat[3 * 0 + 1] = 0.0f;
- proj_mat[3 * 0 + 2] = 0.0f;
- proj_mat[3 * 1 + 0] = 0.0f;
- proj_mat[3 * 1 + 1] = -2.0f;
- proj_mat[3 * 1 + 2] = 0.0f;
- proj_mat[3 * 2 + 0] = -1.0f;
- proj_mat[3 * 2 + 1] = 1.0f;
- proj_mat[3 * 2 + 2] = 1.0f;
-
- gl.uniformMatrix3(u_proj_loc, false, proj_mat);
+ u_proj_loc = gl.getUniformLocation(program, "u_proj");
+ quad_renderer_update_view(qr);
}
quad_renderer_draw :: proc (use qr: ^QuadRenderer) {
gl.bindVertexArray(-1);
}
+quad_renderer_update_view :: proc (use qr: ^QuadRenderer) {
+ proj_mat : [4 * 4] gl.GLfloat;
+ for ^it: proj_mat do *it = 0.0f;
+
+ // Orthographic projection matrix samelessly stolen from:
+ // https://en.wikipedia.org/wiki/Orthographic_projection
+ r :: cast(f32) window_width;
+ l :: 0.0f;
+ t :: 0.0f;
+ b :: cast(f32) window_height;
+
+ proj_mat[0 * 4 + 0] = 2.0f / (r - l);
+ proj_mat[0 * 4 + 3] = -(r + l) / (r - l);
+ proj_mat[1 * 4 + 1] = 2.0f / (t - b);
+ proj_mat[1 * 4 + 3] = -(t + b) / (t - b);
+ proj_mat[2 * 4 + 2] = -2.0f;
+ proj_mat[2 * 4 + 3] = -1.0f;
+ proj_mat[3 * 4 + 3] = 1.0f;
+
+ // This matrix uses the wrong major axis so a transpose is necessary.
+ gl.uniformMatrix4(u_proj_loc, true, proj_mat);
+}
+
quad_update_at_index :: proc (use qr: ^QuadRenderer, idx: i32, quad: Quad) {
quad_data[idx] = quad;
is_data_dirty = true;
GameState :: struct {
quad_renderer : QuadRenderer;
- input_state : input.InputState;
+ input_state : input.InputState;
player : Player;
}
gl.canvasSize(window_width, window_height);
gl.viewport(0, 0, window_width, window_height);
+
+ quad_renderer_update_view(^quad_renderer);
}
}
}
defer input.postupdate(^input_state);
poll_events(gs);
- player_speed :: 0.004f;
+ player_speed :: 4f;
if input.key_down(^input_state, Key.ArrowUp) do player.y -= player_speed;
if input.key_down(^input_state, Key.ArrowDown) do player.y += player_speed;
if input.key_down(^input_state, Key.ArrowLeft) do player.x -= player_speed;
if input.key_down(^input_state, Key.ArrowRight) do player.x += player_speed;
- /* for i: 0 .. NUM_QUADS {
- t := random_float();
- d := random_float(0.8f, 1.0f);
-
- quad_update_at_index(^quad_renderer, i, Quad.{
- x = d * cos(t * 2.0f * PI) / 2.2f + 0.5f,
- y = d * sin(t * 2.0f * PI) / 2.2f + 0.5f,
-
- w = random_float(0.006f, 0.01f),
- h = random_float(0.006f, 0.01f),
-
- r = random_float(),
- g = random_float(0.6f, 1.0f),
- b = random_float(0.6f, 1.0f),
- a = 1.0f
- });
- } */
-
quad_update_at_index(^quad_renderer, NUM_QUADS - 1, Quad.{
- x = cast(f32) input_state.mouse.x / cast(f32) window_width - 0.025f,
- y = cast(f32) input_state.mouse.y / cast(f32) window_height - 0.025f,
+ x = input_state.mouse.x,
+ y = cast(f32) input_state.mouse.y,
- w = 0.05f, h = 0.05f,
+ w = 32.0f, h = 32.0f,
u = 0.25f, v = 0.25f, tw = 0.25f, th = 0.25f,
x = player.x,
y = player.y,
- w = 0.1f, h = 0.1f,
+ w = 100.0f, h = 100.0f,
u = 0.0f, v = 0.0f, tw = 1.0f, th = 1.0f,
layout(location = 4) in vec2 a_tex_size;
layout(location = 5) in vec4 a_col;
-uniform mat3 u_proj;
+uniform mat4 u_proj;
out vec2 v_tex_pos;
out vec4 v_col;
void main() {
- gl_Position = vec4(u_proj * vec3(a_vert_pos * a_size + a_pos, 1), 1);
+ gl_Position = u_proj * vec4(a_vert_pos * a_size + a_pos, 0, 1);
v_col = a_col;
v_tex_pos = a_tex_pos + a_vert_pos * a_tex_size;
NONE /usr/share/onyx/core/js/webgl.onyx /^NONE :: 0$/
NOTEQUAL /usr/share/onyx/core/js/webgl.onyx /^NOTEQUAL :: 0x0205$/
NO_ERROR /usr/share/onyx/core/js/webgl.onyx /^NO_ERROR :: 0$/
-NUM_QUADS src/main.onyx /^NUM_QUADS :: 100$/
+NUM_QUADS src/main.onyx /^NUM_QUADS :: 1 << 7$/
OBJECT_TYPE /usr/share/onyx/core/js/webgl.onyx /^OBJECT_TYPE :: 0x9112$/
ONE /usr/share/onyx/core/js/webgl.onyx /^ONE :: 1$/
ONE_MINUS_CONSTANT_ALPHA /usr/share/onyx/core/js/webgl.onyx /^ONE_MINUS_CONSTANT_ALPHA :: 0x8004$/
POLYGON_OFFSET_FACTOR /usr/share/onyx/core/js/webgl.onyx /^POLYGON_OFFSET_FACTOR :: 0x8038$/
POLYGON_OFFSET_FILL /usr/share/onyx/core/js/webgl.onyx /^POLYGON_OFFSET_FILL :: 0x8037$/
POLYGON_OFFSET_UNITS /usr/share/onyx/core/js/webgl.onyx /^POLYGON_OFFSET_UNITS :: 0x2A00$/
+Player src/main.onyx /^Player :: struct {$/
PtrMap /usr/share/onyx/core/ptrmap.onyx /^PtrMap :: struct {$/
PtrMapEntry /usr/share/onyx/core/ptrmap.onyx /^PtrMapEntry :: struct {$/
PtrMapLookupResult /usr/share/onyx/core/ptrmap.onyx /^PtrMapLookupResult :: struct {$/
bufferDataWithData /usr/share/onyx/core/js/webgl.onyx /^bufferDataWithData :: proc (target: GLenum, buffer: Buffer, usage: GLenum) #foreign "gl" "bufferDataWithData" ---$/
bufferSubData /usr/share/onyx/core/js/webgl.onyx /^bufferSubData :: proc (target: GLenum, offset: GLsizei, data: Buffer) #foreign "gl" "bufferSubData" ---$/
calloc /usr/share/onyx/core/builtin.onyx /^calloc :: proc (size: u32) -> rawptr do return alloc(context.allocator, size);$/
+canvasSize /usr/share/onyx/core/js/webgl.onyx /^canvasSize :: proc (width: GLsizei, height: GLsizei) #foreign "gl" "canvasSize" ---$/
ceil_f32 /usr/share/onyx/core/intrinsics.onyx /^ceil_f32 :: proc (val: f32) -> f32 #intrinsic ---$/
ceil_f64 /usr/share/onyx/core/intrinsics.onyx /^ceil_f64 :: proc (val: f64) -> f64 #intrinsic ---$/
cfree /usr/share/onyx/core/builtin.onyx /^cfree :: proc (ptr: rawptr) do free(context.allocator, ptr);$/
vertexAttribDivisor /usr/share/onyx/core/js/webgl.onyx /^vertexAttribDivisor :: proc (idx: GLuint, divisor: GLuint) #foreign "gl" "vertexAttribDivisor" ---$/
vertexAttribPointer /usr/share/onyx/core/js/webgl.onyx /^vertexAttribPointer :: proc (idx: GLuint, size: GLint, type: GLenum, normalized: GLboolean, stride: GLsizei, offset: GLint) #foreign "gl" "vertexAttribPointer" ---$/
viewport /usr/share/onyx/core/js/webgl.onyx /^viewport :: proc (x: GLint, y: GLint, width: GLsizei, height: GLsizei) #foreign "gl" "viewport" --- $/
-window_width src/main.onyx /^window_width := 0$/
+window_height src/main.onyx /^window_height := 0$/
+window_width src/main.onyx /^window_width := 0$/
xor_i32 /usr/share/onyx/core/intrinsics.onyx /^xor_i32 :: proc (lhs: i32, rhs: i32) -> i32 #intrinsic ---$/
xor_i64 /usr/share/onyx/core/intrinsics.onyx /^xor_i64 :: proc (lhs: i64, rhs: i64) -> i64 #intrinsic ---$/