git.haldean.org ana / d20cb5e
viewport fixes Haldean Brown 1 year, 9 months ago
3 changed file(s) with 48 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
5151 } else if (ev.type == SDL_WINDOWEVENT) {
5252 if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
5353 glViewport(0, 0, ev.window.data1, ev.window.data2);
54 ana::shaders::set_aspect(ev.window.data1, ev.window.data2);
5455 }
5556 }
5657 }
22 #include "node.hpp"
33 #include "wbogl/shader_utils.hpp"
44
5 #include <glm/glm.hpp>
6 #include <glm/gtc/matrix_transform.hpp>
7 #include <glm/gtc/type_ptr.hpp>
58 #include <iostream>
69 #include <string>
710
1114 static const std::string node_vert_src = R"(
1215 #version 330 core
1316 layout (location = 0) in vec4 data;
17
1418 out NODE_DATA {
1519 vec2 size;
1620 } node_data;
1721
22 uniform mat4 viewport;
23
1824 void main(void) {
19 gl_Position = vec4(data[0], data[1], 0.0, 1.0);
20 node_data.size = vec2(data[2], data[3]);
25 gl_Position = viewport * vec4(data[0], data[1], 0.0, 1.0);
26 vec4 s = viewport * vec4(data[2], data[3], 0.0, 0.0);
27 node_data.size = vec2(s[0], s[1]);
2128 }
2229 )";
2330
5461 }
5562 )";
5663
57 static GLuint node_program_id = 0;
58 static GLint node_data_attr = -1;
59 static GLuint node_data_buf = 0;
64 static GLuint node_vao;
65 static GLuint node_program_id;
66
67 static GLint node_data_attr;
68 static GLint node_viewport_attr;
69
70 static GLuint node_data_buf;
6071
6172 bool init(void) {
6273 GLuint vs, fs, gs;
95106 return false;
96107 }
97108
98 GLuint vao;
99 glGenVertexArrays(1, &vao);
109 node_viewport_attr = glGetUniformLocation(node_program_id, "viewport");
110 if (node_viewport_attr == -1) {
111 std::cerr << "[E] glGetAttribLocation failed for viewport matrix" << std::endl;
112 return false;
113 }
114
115 glGenVertexArrays(1, &node_vao);
100116 INSERT_GL_ERROR_CHECK;
101 glBindVertexArray(vao);
117 glBindVertexArray(node_vao);
102118 INSERT_GL_ERROR_CHECK;
103
104119 glGenBuffers(1, &node_data_buf);
105120 INSERT_GL_ERROR_CHECK;
121 glBindVertexArray(0);
106122
107123 return true;
108124 }
109125
126 void set_aspect(int w, int h) {
127 float aspect = (float) h / w;
128 float scale = 1;
129 glm::mat4 m = glm::ortho(
130 -scale, scale, -scale * aspect, scale * aspect, -1.f, 1.f);
131 glUniformMatrix4fv(node_viewport_attr, 1, GL_FALSE, glm::value_ptr(m));
132 }
133
110134 void draw(const ana::nodeset &nodes) {
135 static GLfloat *node_data = nullptr;
136 static size_t node_data_size = 0;
137
111138 size_t elems = nodes.size() * 4;
112 GLfloat *data = (GLfloat*) calloc(sizeof(GLfloat), elems);
139 if (elems != node_data_size) {
140 node_data = (GLfloat*) reallocarray(node_data, elems, sizeof(GLfloat));
141 }
113142 int i = 0;
114143 for (const ana::node &n : nodes.nodes) {
115 data[4 * i + 0] = n.loc.x;
116 data[4 * i + 1] = n.loc.y;
117 data[4 * i + 2] = n.size.x;
118 data[4 * i + 3] = n.size.y;
144 node_data[4 * i + 0] = n.loc.x;
145 node_data[4 * i + 1] = n.loc.y;
146 node_data[4 * i + 2] = n.size.x;
147 node_data[4 * i + 3] = n.size.y;
119148 i++;
120149 }
121150
151 glBindVertexArray(node_vao);
122152 glBindBuffer(GL_ARRAY_BUFFER, node_data_buf);
123153 INSERT_GL_ERROR_CHECK;
124 glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), data, GL_DYNAMIC_DRAW);
154 glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), node_data, GL_DYNAMIC_DRAW);
125155 INSERT_GL_ERROR_CHECK;
126
127 free(data);
128156
129157 glUseProgram(node_program_id); INSERT_GL_ERROR_CHECK;
130158 glEnableVertexAttribArray(node_data_attr); INSERT_GL_ERROR_CHECK;
132160 glVertexAttribPointer(node_data_attr, 4, GL_FLOAT, GL_FALSE, 0, 0); INSERT_GL_ERROR_CHECK;
133161 glDrawArrays(GL_POINTS, 0, nodes.size()); INSERT_GL_ERROR_CHECK;
134162 glDisableVertexAttribArray(node_data_attr); INSERT_GL_ERROR_CHECK;
163 glBindVertexArray(0);
135164 }
136165
137166 void destroy(void) {
77
88 bool init(void);
99 void destroy(void);
10 void set_aspect(int w, int h);
1011 void draw(const ana::nodeset &nodes);
1112 }
1213 }