summaryrefslogtreecommitdiff
path: root/advanced_opengl/2.stencil/stencil.c
blob: 3b01f5bd9cf7f4f7d23d7599f62ed64c73ba65bd (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "GL/glew.h"
#include "GLFW/glfw3.h"

#include "pwyazh.h"
#include "pwyazh_GL.h"

#include "common.h"

int main(void)
{
	GLFWwindow *window;
	State state;
	S32 width, height;
	Input input;

	if (glfwInit() == GLFW_FALSE) {
		fprintf(stderr, "[ERROR] Failed to initialize glfw.\n");
		return(1);
	}

	width = 1024;
	height = 768;

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	window = glfwCreateWindow(width, height, "Stencil testing", 0, 0);
	if (!window) {
		fprintf(stderr, "[ERROR] Failed to create window.\n");
		glfwTerminate();
		return(1);
	}

	glfwMakeContextCurrent(window);

	if (glewInit() != GLEW_OK) {
		fprintf(stderr, "[ERROR] Failed to initialize glew.\n");
		glfwTerminate();
		return(1);
	}

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_STENCIL_TEST);
	glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

	U32 shader = create_shader_program("shaders/stencil.vert", "shaders/stencil.frag");
	U32 outline_shader = create_shader_program("shaders/stencil.vert", "shaders/outline.frag");
	U32 marble_texture = load_texture("../../data/textures/marble.jpg");
	U32 metal_texture = load_texture("../../data/textures/metal.png");

	F32 a = 0.5f;
	F32 vertices[] = {
		-a, -a, -a,  0.0f, 0.0f,
		 a, -a, -a,  1.0f, 0.0f,
		 a,  a, -a,  1.0f, 1.0f,
		 a,  a, -a,  1.0f, 1.0f,
		-a,  a, -a,  0.0f, 1.0f,
		-a, -a, -a,  0.0f, 0.0f,

		-a, -a,  a,  0.0f, 0.0f,
		 a, -a,  a,  1.0f, 0.0f,
		 a,  a,  a,  1.0f, 1.0f,
		 a,  a,  a,  1.0f, 1.0f,
		-a,  a,  a,  0.0f, 1.0f,
		-a, -a,  a,  0.0f, 0.0f,

		-a,  a,  a,  1.0f, 0.0f,
		-a,  a, -a,  1.0f, 1.0f,
		-a, -a, -a,  0.0f, 1.0f,
		-a, -a, -a,  0.0f, 1.0f,
		-a, -a,  a,  0.0f, 0.0f,
		-a,  a,  a,  1.0f, 0.0f,

		 a,  a,  a,  1.0f, 0.0f,
		 a,  a, -a,  1.0f, 1.0f,
		 a, -a, -a,  0.0f, 1.0f,
		 a, -a, -a,  0.0f, 1.0f,
		 a, -a,  a,  0.0f, 0.0f,
		 a,  a,  a,  1.0f, 0.0f,

		-a, -a, -a,  0.0f, 1.0f,
		 a, -a, -a,  1.0f, 1.0f,
		 a, -a,  a,  1.0f, 0.0f,
		 a, -a,  a,  1.0f, 0.0f,
		-a, -a,  a,  0.0f, 0.0f,
		-a, -a, -a,  0.0f, 1.0f,

		-a,  a, -a,  0.0f, 1.0f,
		 a,  a, -a,  1.0f, 1.0f,
		 a,  a,  a,  1.0f, 0.0f,
		 a,  a,  a,  1.0f, 0.0f,
		-a,  a,  a,  0.0f, 0.0f,
		-a,  a, -a,  0.0f, 1.0f
	};

	Transform cube_positions[] = {
		transform_default(),
		transform_make_translate(v3f(a, 0.0f, -4*a))
	};

	V3F platform_pos = v3f(10.0f, 0.2f, 10.0f);
	V3F platform_scale = v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f);
	Transform platform_transform = transform_make_scale_translate(platform_pos,
								      platform_scale);

	U32 VAO, VBO;
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0);
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(3*sizeof(F32)));
	glBindVertexArray(0);

	state.camera = (Camera) {
		v3f(0.0f, 0.0f, 3.0f),
		90.0f, 0.1f, 100.0f,
		0.0f, 0.0f
	};

	F32 target_fps = 60.0f;
	F32 target_spf = 1.0f/target_fps;
	F32 last_time = glfwGetTime();

	while (!glfwWindowShouldClose(window)) {
		glfwPollEvents();

		process_glfw_keyboard(window, &input);
		glfwGetFramebufferSize(window, &width, &height);

		/* INFO(pryazha): Update */
		V3F target = v3f_zero();

		if (key_first_press(input.exit))
			glfwSetWindowShouldClose(window, GLFW_TRUE);

		V3F dv = get_dv_camera_orbital(&input, state.camera.pos,
					       target, state.dt, 3.0f);
		state.camera.pos = v3f_add(state.camera.pos, dv);

		input_update_last_state(&input);

		/* INFO(pryazha): Render */
		MAT4 model, view, proj;
		view = look_at(state.camera.pos, target, v3f(0.0f, 1.0f, 0.0f));
		proj = perspective(state.camera.fovx, (F32)width/(F32)height,
				   state.camera.near, state.camera.far);

		glViewport(0, 0, width, height);
		glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

		/* NOTE(pryazha): Update stencil buffer with ones while drawing the cubes */
		glStencilFunc(GL_ALWAYS, 1, 0xFF);
		glStencilMask(0xFF);

		glUseProgram(shader);
		shader_set_mat4fv(shader, "view", view);
		shader_set_mat4fv(shader, "proj", proj);
		glBindTexture(GL_TEXTURE_2D, marble_texture);
		glBindVertexArray(VAO);
		for (S32 i = 0; i < (S32)ArrayCount(cube_positions); ++i) {
			model = transform_apply(cube_positions[i]);
			shader_set_mat4fv(shader, "model", model);
			glDrawArrays(GL_TRIANGLES, 0, 36);
		}

		/* NOTE(pryazha): Don't update stencil buffer while drawing the floor */
		glStencilMask(0x00);

		glBindTexture(GL_TEXTURE_2D, metal_texture);
		model = transform_apply(platform_transform);
		shader_set_mat4fv(shader, "model", model);
		glDrawArrays(GL_TRIANGLES, 0, 36);

		glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
		glStencilMask(0x00);
		glDisable(GL_DEPTH_TEST);

		glUseProgram(outline_shader);
		shader_set_mat4fv(outline_shader, "view", view);
		shader_set_mat4fv(outline_shader, "proj", proj);
		for (S32 i = 0; i < (S32)ArrayCount(cube_positions); ++i) {
			Transform transform = cube_positions[i];
			transform = transform_scale(transform, v3f(1.1f, 1.1f, 1.1f));
			model = transform_apply(transform);
			shader_set_mat4fv(outline_shader, "model", model);
			glDrawArrays(GL_TRIANGLES, 0, 36);
		}

		glStencilMask(0xFF);
		glStencilFunc(GL_ALWAYS, 1, 0xFF);
		glEnable(GL_DEPTH_TEST);

		glfwSwapBuffers(window);

		F32 elapsed = glfwGetTime()-last_time;
		if (elapsed < target_spf) {
			U32 sleep_time = (U32)(target_spf-elapsed);
			if (sleep_time > 0)
				sleep(sleep_time);
		}
		F32 current_time = glfwGetTime();
		state.dt = current_time-last_time;
		last_time = current_time;
	}

	glfwTerminate();
	return(0);
}