-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl.c
202 lines (156 loc) · 4.18 KB
/
gl.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
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "gl.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <tgmath.h>
struct {
const GLchar *srcs_vert[SCALE_MAX];
const GLchar *src_geom;
const GLchar *src_frag;
GLuint progs[SCALE_MAX];
GLuint vao;
GLuint vbo;
} gl = {
.srcs_vert = {
[SCALE_LIN] =
"#version 330 core\n"
"#extension GL_ARB_explicit_uniform_location : require\n"
"layout(location = 0) uniform float in_width;\n"
"layout(location = 0) in float pos;\n"
"out float width;\n"
"void main() {\n"
" width = in_width;\n"
" gl_Position = vec4(width * gl_VertexID - 1.0, pos, 0.0, 1.0);\n"
"}\n",
[SCALE_LOG] =
"#version 330 core\n"
"#extension GL_ARB_explicit_uniform_location : require\n"
"layout(location = 0) uniform float scale;\n"
"layout(location = 0) in float pos;\n"
"out float width;\n"
"void main() {\n"
" int i = (gl_VertexID == 0) ? 1 : gl_VertexID;\n"
" float x = log(i) * scale;\n"
" width = log(i + 1) * scale - x;\n"
" gl_Position = vec4(x - 1.0, pos, 0.0, 1.0);\n"
"}\n",
},
.src_geom =
"#version 330 core\n"
"layout(points) in;\n"
"layout(triangle_strip, max_vertices = 4) out;\n"
"in float width[];\n"
"void main() {\n"
" vec4 v = gl_in[0].gl_Position;\n"
" gl_Position = v;\n"
" EmitVertex();\n"
" gl_Position = vec4(v.x + width[0], v.yzw);\n"
" EmitVertex();\n"
" gl_Position = vec4(v.x, -1.0, v.zw);\n"
" EmitVertex();\n"
" gl_Position = vec4(v.x + width[0], -1.0, v.zw);\n"
" EmitVertex();\n"
" EndPrimitive();\n"
"}",
.src_frag =
"#version 330 core\n"
"out vec4 color;\n"
"void main() {\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n",
};
static GLuint create_shader(GLenum type, const GLchar *src)
{
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
GLint ret;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ret);
if (ret == GL_FALSE) {
GLint loglen;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &loglen);
if (loglen != 0) {
GLchar msg[loglen];
glGetShaderInfoLog(shader, loglen, &loglen, msg);
puts(msg);
}
exit(EXIT_FAILURE);
}
return shader;
}
static GLuint create_prog(const GLchar *src_vert, GLuint geom, GLuint frag)
{
GLuint prog = glCreateProgram();
GLuint vert = create_shader(GL_VERTEX_SHADER, src_vert);
glAttachShader(prog, vert);
glAttachShader(prog, geom);
glAttachShader(prog, frag);
glLinkProgram(prog);
GLint ret;
glGetProgramiv(prog, GL_LINK_STATUS, &ret);
if (ret == GL_FALSE) {
GLint loglen;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &loglen);
if (loglen != 0) {
GLchar msg[loglen];
glGetProgramInfoLog(prog, loglen, &loglen, msg);
puts(msg);
}
exit(EXIT_FAILURE);
}
glDetachShader(prog, vert);
glDetachShader(prog, geom);
glDetachShader(prog, frag);
glDeleteShader(vert);
return prog;
}
void gl_init(void)
{
GLuint geom = create_shader(GL_GEOMETRY_SHADER, gl.src_geom);
GLuint frag = create_shader(GL_FRAGMENT_SHADER, gl.src_frag);
for (int i = 0; i < SCALE_MAX; ++i) {
gl.progs[i] = create_prog(gl.srcs_vert[i], geom, frag);
}
glDeleteShader(geom);
glDeleteShader(frag);
glGenVertexArrays(1, &gl.vao);
glGenBuffers(1, &gl.vbo);
glBindVertexArray(gl.vao);
glBindBuffer(GL_ARRAY_BUFFER, gl.vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, sizeof(GLfloat), NULL);
}
void gl_destroy(void)
{
for (int i = 0; i < SCALE_MAX; ++i)
glDeleteProgram(gl.progs[i]);
glDeleteVertexArrays(1, &gl.vao);
glDeleteBuffers(1, &gl.vbo);
}
void gl_render(GLFWwindow *win, size_t n, float arr[static restrict n], enum scale s)
{
int width, height;
glfwGetFramebufferSize(win, &width, &height);
glViewport(0, 0, width, height);
switch (s) {
case SCALE_LIN:
// in_width
glUniform1f(0, 4.0 / n);
break;
case SCALE_LOG:
// scale
glUniform1f(0, 2.0 / log(n));
break;
default:
exit(EXIT_FAILURE);
}
glBindBuffer(GL_ARRAY_BUFFER, gl.vbo);
glBufferData(GL_ARRAY_BUFFER, n * sizeof arr[0], arr, GL_STATIC_DRAW);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindVertexArray(gl.vao);
glUseProgram(gl.progs[s]);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, n);
glfwSwapBuffers(win);
}