git.haldean.org ana / 8be87ea
add ability to translate and scale the viewport Haldean Brown 1 year, 9 months ago
6 changed file(s) with 88 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
1313 src/ana.cpp
1414 src/node.cpp
1515 src/shaders.cpp
16 src/ui.cpp
1617 lib/wbogl/shader_utils.cpp)
1718
1819 target_link_libraries(ana glm SDL2 GLEW GL)
66
77 #include "node.hpp"
88 #include "shaders.hpp"
9 #include "ui.hpp"
910
1011 int main(void) {
1112 SDL_Init(SDL_INIT_VIDEO);
4243 nodes.nodes.push_back(ana::node{glm::vec2(0, 0), glm::vec2(0.1, 0.2), "hello"});
4344 nodes.nodes.push_back(ana::node{glm::vec2(-0.5, -0.5), glm::vec2(0.2, 0.2), "hello"});
4445
46 ana::ui ui;
47
4548 bool run = true;
4649 while (run) {
4750 SDL_Event ev;
4851 while (SDL_PollEvent(&ev)) {
49 if (ev.type == SDL_QUIT) {
52 switch (ev.type) {
53 case SDL_QUIT:
5054 run = false;
51 } else if (ev.type == SDL_WINDOWEVENT) {
55 break;
56
57 case SDL_WINDOWEVENT:
5258 if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
5359 glViewport(0, 0, ev.window.data1, ev.window.data2);
54 ana::shaders::set_aspect(ev.window.data1, ev.window.data2);
60 ui.set_aspect(ev.window.data1, ev.window.data2);
5561 }
62 break;
63
64 case SDL_MOUSEWHEEL:
65 if (ev.wheel.y != 0) {
66 ui.view_step(ev.wheel.y);
67 }
68 break;
69
70 case SDL_MOUSEMOTION:
71 if (ev.motion.state & SDL_BUTTON_MMASK) {
72 ui.translate(-ev.motion.xrel, ev.motion.yrel);
73 }
74 break;
5675 }
5776 }
5877 glClearColor(0.15, 0.12, 0.1, 1.0);
5978 glClear(GL_COLOR_BUFFER_BIT);
6079
61 ana::shaders::draw(nodes);
80 ana::shaders::draw(ui, nodes);
6281
6382 SDL_GL_SwapWindow(window);
6483 }
123123 return true;
124124 }
125125
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
134 void draw(const ana::nodeset &nodes) {
126 void draw(const ana::ui &ui, const ana::nodeset &nodes) {
135127 static GLfloat *node_data = nullptr;
136128 static size_t node_data_size = 0;
137129
148140 i++;
149141 }
150142
151 glBindVertexArray(node_vao);
152 glBindBuffer(GL_ARRAY_BUFFER, node_data_buf);
153 INSERT_GL_ERROR_CHECK;
154 glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), node_data, GL_DYNAMIC_DRAW);
155 INSERT_GL_ERROR_CHECK;
143 glUseProgram(node_program_id); INSERT_GL_ERROR_CHECK;
156144
157 glUseProgram(node_program_id); INSERT_GL_ERROR_CHECK;
145 glBindVertexArray(node_vao); INSERT_GL_ERROR_CHECK;
146 glBindBuffer(GL_ARRAY_BUFFER, node_data_buf); INSERT_GL_ERROR_CHECK;
147 glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), node_data, GL_DYNAMIC_DRAW); INSERT_GL_ERROR_CHECK;
148
149 glm::mat4 m = glm::ortho(
150 ui.center.x - ui.view, ui.center.x + ui.view,
151 ui.center.y - ui.view * ui.aspect, ui.center.y + ui.view * ui.aspect,
152 -1.f, 1.f);
153 glUniformMatrix4fv(node_viewport_attr, 1, GL_FALSE, glm::value_ptr(m));
154
158155 glEnableVertexAttribArray(node_data_attr); INSERT_GL_ERROR_CHECK;
159156 glBindBuffer(GL_ARRAY_BUFFER, node_data_buf); INSERT_GL_ERROR_CHECK;
160157 glVertexAttribPointer(node_data_attr, 4, GL_FLOAT, GL_FALSE, 0, 0); INSERT_GL_ERROR_CHECK;
158
161159 glDrawArrays(GL_POINTS, 0, nodes.size()); INSERT_GL_ERROR_CHECK;
162160 glDisableVertexAttribArray(node_data_attr); INSERT_GL_ERROR_CHECK;
163161 glBindVertexArray(0);
11
22 #include <GL/glew.h>
33 #include "node.hpp"
4 #include "ui.hpp"
45
56 namespace ana {
67 namespace shaders {
89 bool init(void);
910 void destroy(void);
1011 void set_aspect(int w, int h);
11 void draw(const ana::nodeset &nodes);
12 void draw(const ana::ui &ui, const ana::nodeset &nodes);
1213 }
1314 }
0 #include "ui.hpp"
1
2 namespace ana {
3
4 void ui::set_aspect(int w, int h) {
5 width = w;
6 height = h;
7 aspect = (float) h / w;
8 }
9
10 void ui::view_step(int dir) {
11 if (dir > 0) {
12 view *= 1.2;
13 if (view > 20) {
14 view = 20;
15 }
16 } else if (dir < 0) {
17 view /= 1.2;
18 if (view < 0.05) {
19 view = 0.05;
20 }
21 }
22 }
23
24 void ui::translate(int xpixels, int ypixels) {
25 float x = 2.f * view * xpixels / width;
26 float y = 2.f * view * aspect * ypixels / height;
27 center.x += x;
28 center.y += y;
29 }
30
31 }
0 #pragma once
1
2 #include <glm/glm.hpp>
3
4 namespace ana {
5 struct ui {
6 float aspect = 1.f;
7 float view = 1.f;
8 int width = 1;
9 int height = 1;
10 glm::vec2 center{0.f, 0.f};
11
12 void set_aspect(int w, int h);
13 void view_step(int dir);
14 void translate(int xpixels, int ypixels);
15 };
16 }