summaryrefslogtreecommitdiff
path: root/src/gizmo/gizmo.c
blob: e3eaaf146eb0412a6311ed35749a6a3c7bdbf313 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "gizmo.h"
#include "../core/device.h"
#include "../math/math.h"
#include "../shaders/common.h"

struct {
	bool show_grid, g_pressing;
} gizmo_state;

void gizmo_init() {
	gizmo_state.show_grid = FALSE;
	gizmo_state.g_pressing = FALSE;
}

void gizmo_onevent(wog_Event* e, float dt) {
	if (e == NULL) return;
	if (e->type == WOG_EKEYDOWN) {
		if (e->key == VK_G && !gizmo_state.g_pressing) {
			gizmo_state.show_grid = !gizmo_state.show_grid;
			gizmo_state.g_pressing = TRUE;
		}
	}
	else if (e->type == WOG_EKEYUP) {
		if(e->key == VK_G)
			gizmo_state.g_pressing = FALSE;
	}
}

void gizmo_onupdate(float dt) {
}

void gizmo_ondraw() {
	if (gizmo_state.show_grid)
		gizmo_grid();
}

// varying
#define _color reg_v4_00
static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) {
	static Vec4 p; p.xyz = in->vertex->position; p.w = 1;
	object2clip(&p, clipcoord);
	color_tocolor32(in->vertex->color, _color);
}
static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) {
	*color = *_color;
	return 1;
}
static Program line_shader = {
	vert, frag,
	VARYING_V4_00
};

void gizmo_grid() {
	Vert verts[] = {
		{0, {-10000, 0, 0}, vec3zero, vec4zero, vec2zero, 0xffff0000},
		{1, {10000, 0, 0}, vec3zero, vec4zero, vec2zero, 0xffff0000},
		{2, {0, -10000, 0}, vec3zero, vec4zero, vec2zero, 0xff00ff00},
		{3, {0, 10000, 0}, vec3zero, vec4zero, vec2zero, 0xff00ff00},
		{4, {0, 0, -10000}, vec3zero, vec4zero, vec2zero, 0xff0000ff},
		{5, {0, 0, 10000}, vec3zero, vec4zero, vec2zero, 0xff0000ff},
	};
	int grid[] = { 0, 1, 2, 3, 4, 5 };
	ssr_bindvertices(&verts, 6, &grid, 3);
	ssr_matrixmode(MATRIX_MODEL);
	ssr_loadidentity();
	ssr_useprogram(&line_shader);
	ssr_draw(PRIMITIVE_LINE);
	ssr_unuseprogram();
}

void gizmo_line(Vec3 start, Vec3 end, Color32 color) {
	Vert verts[] = {
		{0, start, vec3zero, vec4zero, vec2zero, color32_tocolor(&color)},
		{1, end, vec3zero, vec4zero, vec2zero, color32_tocolor(&color)},
	};
	int line[] = { 0, 1};
	ssr_bindvertices(&verts, 2, &line, 1);
	ssr_matrixmode(MATRIX_MODEL);
	ssr_loadidentity();
	ssr_useprogram(&line_shader);
	ssr_draw(PRIMITIVE_LINE);
	ssr_unuseprogram();
}