-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframe.c
190 lines (150 loc) · 6.15 KB
/
frame.c
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
#include "shader/frame.h"
// clang-format off
#include "opengl.h"
#include <GLFW/glfw3.h>
// clang-format on
#include "gen/frame_frag.h"
#include "gen/frame_vert.h"
#include "shader/error.h"
#include "window.h"
#define VBO_FRAME_VERTICES 0
#define VBO_FRAME_INDICES 1
#define ATTR_FRAME_POSITION 0
#define ATTR_FRAME_TEXCOORDS 1
#define DEPTH_RANGE 0.01, 100.0
typedef procy_frame_shader_program_t frame_shader_program_t;
typedef procy_shader_program_t shader_program_t;
typedef procy_window_t window_t;
// clang-format off
const static float FRAME_QUAD_VERTICES[] = {
// position // texture
-1.0F, 1.0F, 0.0F, 1.0F, // top-left
1.0F, 1.0F, 1.0F, 1.0F, // top-right
-1.0F, -1.0F, 0.0F, 0.0F, // bottom-left
1.0F, -1.0F, 1.0F, 0.0F // bottom-right
};
const static unsigned short FRAME_QUAD_INDICES[] = {
// first triangle
// 0--1
// | /
// 2/
0, 1, 2,
// second triangle
// /1
// / |
// 2--3
1, 3, 2
};
// clang-format on
procy_frame_shader_program_t *procy_create_frame_shader(window_t *window) {
frame_shader_program_t *shader = calloc(1, sizeof(frame_shader_program_t));
// generate + bind framebuffer
GL_CHECK(glGenFramebuffers(1, &shader->framebuffer));
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, shader->framebuffer));
// generate + bind framebuffer texture
GL_CHECK(glGenTextures(1, &shader->texture));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, shader->texture));
// set color buffer parameters and unbind
int width;
int height;
procy_get_window_size(window, &width, &height);
GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, NULL));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
// attach the texture to the framebuffer and unbind
GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, shader->texture, 0));
// set depth/stencil buffer parameters and unbind
glGenRenderbuffers(1, &shader->depth);
glBindRenderbuffer(GL_RENDERBUFFER, shader->depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, shader->depth);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// time to set up shader program stuff...
shader_program_t *program = &shader->program;
// generate VAO
glGenVertexArrays(1, &program->vao);
// generate VBOs
program->vbo_count = 2;
program->vbo = malloc(sizeof(GLuint) * program->vbo_count);
glGenBuffers(program->vbo_count, program->vbo);
procy_compile_and_link_shader(program, (char *)&embed_frame_vert[0],
(char *)&embed_frame_frag[0]);
return shader;
}
void procy_destroy_frame_shader(frame_shader_program_t *shader) {
procy_destroy_shader_program(&shader->program);
if (glIsTexture(shader->texture)) {
glDeleteTextures(1, &shader->texture);
}
if (glIsRenderbuffer(shader->depth)) {
glDeleteRenderbuffers(1, &shader->depth);
}
if (glIsFramebuffer(shader->framebuffer)) {
glDeleteFramebuffers(1, &shader->framebuffer);
}
free(shader);
}
void procy_frame_shader_begin(frame_shader_program_t *shader) {
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, shader->framebuffer));
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
GL_CHECK(glEnable(GL_DEPTH_TEST));
GL_CHECK(glDepthRange(DEPTH_RANGE));
GL_CHECK(glDepthFunc(GL_LESS));
}
void procy_frame_shader_end(frame_shader_program_t *shader) {
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT));
GL_CHECK(glDisable(GL_DEPTH_TEST));
}
void procy_frame_shader_resized(frame_shader_program_t *shader, int width,
int height) {
// regenerate the framebuffer texture at the correct size
GL_CHECK(glBindTexture(GL_TEXTURE_2D, shader->texture));
GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, NULL));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
// regenerate the depth/stencil buffer
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, shader->depth));
GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width,
height));
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, 0));
}
void procy_draw_frame_shader(frame_shader_program_t *shader) {
shader_program_t *program = &shader->program;
GL_CHECK(glUseProgram(program->program));
GL_CHECK(glBindVertexArray(program->vao));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, program->vbo[VBO_FRAME_VERTICES]));
// position
GL_CHECK(glEnableVertexAttribArray(ATTR_FRAME_POSITION));
GL_CHECK(glVertexAttribPointer(ATTR_FRAME_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(float) * 4, 0));
// texture coords
GL_CHECK(glEnableVertexAttribArray(ATTR_FRAME_TEXCOORDS));
GL_CHECK(glVertexAttribPointer(ATTR_FRAME_TEXCOORDS, 2, GL_FLOAT, GL_FALSE,
sizeof(float) * 4,
(void *)(sizeof(float) * 2))); // NOLINT
// copy vertices
GL_CHECK(glBufferData(GL_ARRAY_BUFFER, sizeof(FRAME_QUAD_VERTICES),
&FRAME_QUAD_VERTICES[0], GL_STATIC_DRAW));
// copy indices
GL_CHECK(
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, program->vbo[VBO_FRAME_INDICES]));
GL_CHECK(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(FRAME_QUAD_INDICES),
&FRAME_QUAD_INDICES, GL_STATIC_DRAW));
// draw to the screen
GL_CHECK(glActiveTexture(GL_TEXTURE0));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, shader->texture));
GL_CHECK(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL));
GL_CHECK(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0));
// clean up
GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
GL_CHECK(glBindVertexArray(0));
GL_CHECK(glUseProgram(0));
}