summaryrefslogtreecommitdiff
path: root/src/gizmo/gizmo.c
blob: 3069cb45eccb4eb5c3abfbd3d7a0156f1c714fa6 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "gizmo.h"
#include "../core/device.h"
#include "../math/math.h"
#include "../shaders/common/core.h"

struct {
	bool show_grid, g_pressing;
	bool show_axis, a_pressing;
} gizmo_state;

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

void gizmo_onevent(wog_Event* e, float dt) {
	if (e == NULL) return;
	if (e->type == WOG_EKEYDOWN) {
		if (e->key == VK_A && !gizmo_state.a_pressing) {
			gizmo_state.show_axis = !gizmo_state.show_axis;
			gizmo_state.a_pressing = TRUE;
		}
		else 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;
		if(e->key == VK_A)
			gizmo_state.a_pressing = FALSE;
	}
}

void gizmo_onupdate(float dt) {
}

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

// 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() {
	const int field = 1000;
	int vCount = 80 + 4;
	Vert verts[84] = {0};//4*field/100*2
	int grid[84] = {0};
	int vertCount;
	int i = 0;
	Color color = 0xffaaaaaa;
	for (int y = -field; y <= field; y += 100)
	{
		if (y == 0 && gizmo_state.show_axis) {
			vCount -= 2;
			continue;
		}
		verts[i].index = i;
		verts[i].position = internal_vec3_make(-field, 0, y);
		verts[i].normal = vec3zero;
		verts[i].tangent = vec4zero;
		verts[i].texcoord = vec2zero;
		verts[i].color = color;
		grid[i] = i++;
		verts[i].index = i;
		verts[i].position = internal_vec3_make(field, 0, y);
		verts[i].normal = vec3zero;
		verts[i].tangent = vec4zero;
		verts[i].texcoord = vec2zero;
		verts[i].color = color;
		grid[i] = i++;
	}
	for (int x = -field; x <= field; x += 100)
	{
		if (x == 0 && gizmo_state.show_axis) {
			vCount -= 2;
			continue;
		}
		verts[i].index = i;
		verts[i].position = internal_vec3_make(x, 0, -field);
		verts[i].normal = vec3zero;
		verts[i].tangent = vec4zero;
		verts[i].texcoord = vec2zero;
		verts[i].color = color;
		grid[i] = i++;
		verts[i].index = i;
		verts[i].position = internal_vec3_make(x, 0, field);
		verts[i].normal = vec3zero;
		verts[i].tangent = vec4zero;
		verts[i].texcoord = vec2zero;
		verts[i].color = color;
		grid[i] = i++;
	}
	ssr_bindvertices(&verts, vCount, &grid, vCount/2);
	ssr_matrixmode(MATRIX_MODEL);
	ssr_loadidentity();
	ssr_useprogram(&line_shader);
	ssr_draw(PRIMITIVE_LINE);
	ssr_unuseprogram();
	ssr_enable(ENABLE_WRITEDEPTH);
}

void gizmo_axis() {
	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();
	ssr_enable(ENABLE_WRITEDEPTH);
}

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();
}