1 #include "AlphaSprite.hpp"
3 #include "CreatureSkin.hpp"
4 #include "PlanetSurface.hpp"
7 #include "SunSurface.hpp"
9 #include "ArrayTexture.hpp"
10 #include "Texture.hpp"
11 #include "../app/init.hpp"
19 #include <glm/gtc/type_ptr.hpp>
20 #include <glm/gtx/transform.hpp>
25 void gl_error(std::string msg) {
26 const GLubyte *errBegin = gluErrorString(glGetError());
27 if (errBegin && *errBegin != '\0') {
28 const GLubyte *errEnd = errBegin;
29 while (*errEnd != '\0') {
33 msg.append(errBegin, errEnd);
35 throw std::runtime_error(msg);
43 Shader::Shader(GLenum type)
44 : handle(glCreateShader(type)) {
46 gl_error("glCreateShader");
52 glDeleteShader(handle);
56 Shader::Shader(Shader &&other) noexcept
57 : handle(other.handle) {
61 Shader &Shader::operator =(Shader &&other) noexcept {
62 std::swap(handle, other.handle);
67 void Shader::Source(const GLchar *src) noexcept {
68 const GLchar* src_arr[] = { src };
69 glShaderSource(handle, 1, src_arr, nullptr);
72 void Shader::Compile() noexcept {
73 glCompileShader(handle);
76 bool Shader::Compiled() const noexcept {
77 GLint compiled = GL_FALSE;
78 glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
79 return compiled == GL_TRUE;
82 void Shader::Log(std::ostream &out) const {
83 int log_len = 0, max_len = 0;
84 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
85 std::unique_ptr<char[]> log(new char[max_len]);
86 glGetShaderInfoLog(handle, max_len, &log_len, log.get());
87 out.write(log.get(), log_len);
91 void Shader::AttachToProgram(GLuint id) const noexcept {
92 glAttachShader(id, handle);
97 : handle(glCreateProgram()) {
99 gl_error("glCreateProgram");
103 Program::~Program() {
105 glDeleteProgram(handle);
110 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
111 shaders.emplace_back(type);
112 Shader &shader = shaders.back();
115 if (!shader.Compiled()) {
116 shader.Log(std::cerr);
117 throw std::runtime_error("compile shader");
123 void Program::Attach(Shader &shader) noexcept {
124 shader.AttachToProgram(handle);
127 void Program::Link() noexcept {
128 glLinkProgram(handle);
131 bool Program::Linked() const noexcept {
132 GLint linked = GL_FALSE;
133 glGetProgramiv(handle, GL_LINK_STATUS, &linked);
134 return linked == GL_TRUE;
137 void Program::Log(std::ostream &out) const {
138 int log_len = 0, max_len = 0;
139 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
140 std::unique_ptr<char[]> log(new char[max_len]);
141 glGetProgramInfoLog(handle, max_len, &log_len, log.get());
142 out.write(log.get(), log_len);
146 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
147 return glGetAttribLocation(handle, name);
150 GLint Program::AttributeLocation(const std::string &name) const noexcept {
151 return AttributeLocation(name.c_str());
154 GLint Program::UniformLocation(const GLchar *name) const noexcept {
155 return glGetUniformLocation(handle, name);
158 GLint Program::UniformLocation(const std::string &name) const noexcept {
159 return UniformLocation(name.c_str());
163 void Program::Uniform(GLint loc, GLint val) noexcept {
164 glUniform1i(loc, val);
167 void Program::Uniform(GLint loc, float val) noexcept {
168 glUniform1f(loc, val);
171 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
172 glUniform3fv(loc, 1, glm::value_ptr(val));
175 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
176 glUniform4fv(loc, 1, glm::value_ptr(val));
179 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
180 glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));
184 constexpr int PlanetSurface::MAX_LIGHTS;
186 PlanetSurface::PlanetSurface()
190 "#version 330 core\n"
192 "layout(location = 0) in vec3 vtx_position;\n"
193 "layout(location = 1) in vec3 vtx_normal;\n"
194 "layout(location = 2) in vec3 vtx_tex_uv;\n"
198 "uniform mat4 MVP;\n"
200 "out vec3 vtx_viewspace;\n"
201 "out vec3 nrm_viewspace;\n"
202 "out vec3 frag_tex_uv;\n"
205 "gl_Position = MVP * vec4(vtx_position, 1.0);\n"
206 "vtx_viewspace = (MV * vec4(vtx_position, 1.0)).xyz;\n"
207 "nrm_viewspace = (MV * vec4(vtx_position, 0.0)).xyz;\n"
208 "frag_tex_uv = vtx_tex_uv;\n"
213 "#version 330 core\n"
215 "struct LightSource {\n"
221 "in vec3 vtx_viewspace;\n"
222 "in vec3 nrm_viewspace;\n"
223 "in vec3 frag_tex_uv;\n"
225 "uniform sampler2DArray tex_sampler;\n"
226 "uniform int num_lights;\n"
227 "uniform LightSource light[8];\n"
232 "vec3 normal = normalize(nrm_viewspace);\n"
233 "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
234 "vec3 total_light = tex_color * vec3(0.1, 0.1, 0.1);\n"
235 "for (int i = 0; i < num_lights; ++i) {\n"
236 "vec3 to_light = light[i].position - vtx_viewspace;\n"
237 "float distance = length(to_light) + length(vtx_viewspace);\n"
238 "vec3 light_dir = normalize(to_light);\n"
239 "float attenuation = light[i].strength / (distance * distance);\n"
240 "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * tex_color;\n"
241 "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n"
242 "vec3 specular = vec3(0.0, 0.0, 0.0);\n"
243 "if (dot(normal, light_dir) >= 0.0) {\n"
244 "attenuation * light[i].color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n"
246 "total_light = total_light + diffuse + specular;\n"
248 "color = total_light;\n"
252 if (!prog.Linked()) {
254 throw std::runtime_error("link program");
256 m_handle = prog.UniformLocation("M");
257 mv_handle = prog.UniformLocation("MV");
258 mvp_handle = prog.UniformLocation("MVP");
259 sampler_handle = prog.UniformLocation("tex_sampler");
260 num_lights_handle = prog.UniformLocation("num_lights");
261 for (int i = 0; i < MAX_LIGHTS; ++i) {
262 light_handle[3 * i + 0] = prog.UniformLocation("light[" + std::to_string(i) + "].position");
263 light_handle[3 * i + 1] = prog.UniformLocation("light[" + std::to_string(i) + "].color");
264 light_handle[3 * i + 2] = prog.UniformLocation("light[" + std::to_string(i) + "].strength");
268 PlanetSurface::~PlanetSurface() {
271 void PlanetSurface::Activate() noexcept {
273 glEnable(GL_DEPTH_TEST);
274 glDepthFunc(GL_LESS);
275 glEnable(GL_CULL_FACE);
279 void PlanetSurface::SetM(const glm::mat4 &mm) noexcept {
283 prog.Uniform(m_handle, m);
284 prog.Uniform(mv_handle, mv);
285 prog.Uniform(mvp_handle, mvp);
288 void PlanetSurface::SetV(const glm::mat4 &vv) noexcept {
292 prog.Uniform(mv_handle, mv);
293 prog.Uniform(mvp_handle, mvp);
296 void PlanetSurface::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
301 prog.Uniform(mv_handle, mv);
302 prog.Uniform(mvp_handle, mvp);
305 void PlanetSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
311 prog.Uniform(m_handle, m);
312 prog.Uniform(mv_handle, mv);
313 prog.Uniform(mvp_handle, mvp);
316 void PlanetSurface::SetTexture(ArrayTexture &tex) noexcept {
317 glActiveTexture(GL_TEXTURE0);
319 prog.Uniform(sampler_handle, GLint(0));
322 void PlanetSurface::SetLight(int n, const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept {
323 prog.Uniform(light_handle[3 * n + 0], pos);
324 prog.Uniform(light_handle[3 * n + 1], color);
325 prog.Uniform(light_handle[3 * n + 2], strength);
328 void PlanetSurface::SetNumLights(int n) noexcept {
329 prog.Uniform(num_lights_handle, std::min(MAX_LIGHTS, n));
333 SunSurface::SunSurface()
337 "#version 330 core\n"
339 "layout(location = 0) in vec3 vtx_position;\n"
343 "uniform mat4 MVP;\n"
345 "out vec3 vtx_viewspace;\n"
348 "gl_Position = MVP * vec4(vtx_position, 1);\n"
349 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
354 "#version 330 core\n"
356 "in vec3 vtx_viewspace;\n"
358 "uniform vec3 light_color;\n"
359 "uniform float light_strength;\n"
364 "vec3 to_light = -vtx_viewspace;\n"
365 "float distance = length(to_light);\n"
366 //"vec3 light_dir = normalize(to_light);\n"
367 "float attenuation = light_strength / (distance * distance);\n"
368 "color = attenuation * light_color;\n"
372 if (!prog.Linked()) {
374 throw std::runtime_error("link program");
376 m_handle = prog.UniformLocation("M");
377 mv_handle = prog.UniformLocation("MV");
378 mvp_handle = prog.UniformLocation("MVP");
379 light_color_handle = prog.UniformLocation("light_color");
380 light_strength_handle = prog.UniformLocation("light_strength");
383 vao.BindAttributes();
384 vao.EnableAttribute(0);
385 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
386 vao.ReserveAttributes(8, GL_STATIC_DRAW);
388 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
389 attrib[0].position = glm::vec3(-1.0f, -1.0f, -1.0f);
390 attrib[1].position = glm::vec3(-1.0f, -1.0f, 1.0f);
391 attrib[2].position = glm::vec3(-1.0f, 1.0f, -1.0f);
392 attrib[3].position = glm::vec3(-1.0f, 1.0f, 1.0f);
393 attrib[4].position = glm::vec3( 1.0f, -1.0f, -1.0f);
394 attrib[5].position = glm::vec3( 1.0f, -1.0f, 1.0f);
395 attrib[6].position = glm::vec3( 1.0f, 1.0f, -1.0f);
396 attrib[7].position = glm::vec3( 1.0f, 1.0f, 1.0f);
399 vao.ReserveElements(36, GL_STATIC_DRAW);
401 auto element = vao.MapElements(GL_WRITE_ONLY);
448 SunSurface::~SunSurface() {
451 void SunSurface::Activate() noexcept {
453 glEnable(GL_DEPTH_TEST);
454 glDepthFunc(GL_LESS);
455 glEnable(GL_CULL_FACE);
459 void SunSurface::SetM(const glm::mat4 &mm) noexcept {
463 prog.Uniform(m_handle, m);
464 prog.Uniform(mv_handle, mv);
465 prog.Uniform(mvp_handle, mvp);
468 void SunSurface::SetV(const glm::mat4 &vv) noexcept {
472 prog.Uniform(mv_handle, mv);
473 prog.Uniform(mvp_handle, mvp);
476 void SunSurface::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
481 prog.Uniform(mv_handle, mv);
482 prog.Uniform(mvp_handle, mvp);
485 void SunSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
491 prog.Uniform(m_handle, m);
492 prog.Uniform(mv_handle, mv);
493 prog.Uniform(mvp_handle, mvp);
496 void SunSurface::SetLight(const glm::vec3 &color, float strength) noexcept {
497 prog.Uniform(light_color_handle, color);
498 prog.Uniform(light_strength_handle, strength);
501 void SunSurface::Draw() const noexcept {
503 vao.DrawTriangles(36);
507 constexpr int CreatureSkin::MAX_LIGHTS;
509 CreatureSkin::CreatureSkin()
513 "#version 330 core\n"
515 "layout(location = 0) in vec3 vtx_position;\n"
516 "layout(location = 1) in vec3 vtx_normal;\n"
517 "layout(location = 2) in vec3 vtx_tex_uv;\n"
521 "uniform mat4 MVP;\n"
523 "out vec3 vtx_viewspace;\n"
524 "out vec3 frag_tex_uv;\n"
528 "gl_Position = MVP * vec4(vtx_position, 1);\n"
529 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
530 "normal = normalize((MV * vec4(vtx_normal, 0)).xyz);\n"
531 "frag_tex_uv = vtx_tex_uv;\n"
536 "#version 330 core\n"
538 "struct LightSource {\n"
544 "in vec3 vtx_viewspace;\n"
545 "in vec3 frag_tex_uv;\n"
548 "uniform vec3 base_color;\n"
549 "uniform vec4 highlight_color;\n"
550 "uniform sampler2DArray tex_sampler;\n"
551 "uniform int num_lights;\n"
552 "uniform LightSource light[8];\n"
557 "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
558 "vec3 mat_color = mix(base_color, highlight_color.rgb, tex_color.r * tex_color.a * highlight_color.a);\n"
559 "vec3 total_light = mat_color * vec3(0.1, 0.1, 0.1);\n"
560 "for (int i = 0; i < num_lights; ++i) {\n"
561 "vec3 to_light = light[i].position - vtx_viewspace;\n"
562 "float distance = length(to_light) + length(vtx_viewspace);\n"
563 "vec3 light_dir = normalize(to_light);\n"
564 "float attenuation = light[i].strength / (distance * distance);\n"
565 "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * mat_color;\n"
566 "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n"
567 "vec3 specular = vec3(0.0, 0.0, 0.0);\n"
568 "if (dot(normal, light_dir) >= 0.0) {\n"
569 "attenuation * light[i].color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n"
571 "total_light = total_light + diffuse + specular;\n"
573 "color = total_light;\n"
577 if (!prog.Linked()) {
579 throw std::runtime_error("link program");
581 m_handle = prog.UniformLocation("M");
582 mv_handle = prog.UniformLocation("MV");
583 mvp_handle = prog.UniformLocation("MVP");
584 base_color_handle = prog.UniformLocation("base_color");
585 highlight_color_handle = prog.UniformLocation("highlight_color");
586 sampler_handle = prog.UniformLocation("tex_sampler");
587 num_lights_handle = prog.UniformLocation("num_lights");
588 for (int i = 0; i < MAX_LIGHTS; ++i) {
589 light_handle[3 * i + 0] = prog.UniformLocation("light[" + std::to_string(i) + "].position");
590 light_handle[3 * i + 1] = prog.UniformLocation("light[" + std::to_string(i) + "].color");
591 light_handle[3 * i + 2] = prog.UniformLocation("light[" + std::to_string(i) + "].strength");
595 CreatureSkin::~CreatureSkin() {
598 void CreatureSkin::Activate() noexcept {
600 glEnable(GL_DEPTH_TEST);
601 glDepthFunc(GL_LESS);
602 glEnable(GL_CULL_FACE);
606 void CreatureSkin::SetM(const glm::mat4 &mm) noexcept {
610 prog.Uniform(m_handle, m);
611 prog.Uniform(mv_handle, mv);
612 prog.Uniform(mvp_handle, mvp);
615 void CreatureSkin::SetV(const glm::mat4 &vv) noexcept {
619 prog.Uniform(mv_handle, mv);
620 prog.Uniform(mvp_handle, mvp);
623 void CreatureSkin::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
628 prog.Uniform(mv_handle, mv);
629 prog.Uniform(mvp_handle, mvp);
632 void CreatureSkin::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
638 prog.Uniform(m_handle, m);
639 prog.Uniform(mv_handle, mv);
640 prog.Uniform(mvp_handle, mvp);
643 void CreatureSkin::SetBaseColor(const glm::vec3 &c) noexcept {
644 prog.Uniform(base_color_handle, c);
647 void CreatureSkin::SetHighlightColor(const glm::vec4 &c) noexcept {
648 prog.Uniform(highlight_color_handle, c);
651 void CreatureSkin::SetTexture(ArrayTexture &tex) noexcept {
652 glActiveTexture(GL_TEXTURE0);
654 prog.Uniform(sampler_handle, GLint(0));
657 void CreatureSkin::SetLight(int n, const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept {
658 prog.Uniform(light_handle[3 * n + 0], pos);
659 prog.Uniform(light_handle[3 * n + 1], color);
660 prog.Uniform(light_handle[3 * n + 2], strength);
663 void CreatureSkin::SetNumLights(int n) noexcept {
664 prog.Uniform(num_lights_handle, std::min(MAX_LIGHTS, n));
668 AlphaSprite::AlphaSprite()
672 "#version 330 core\n"
674 "layout(location = 0) in vec3 vtx_position;\n"
675 "layout(location = 1) in vec2 vtx_texture;\n"
679 "uniform mat4 MVP;\n"
681 "out vec2 frag_tex_uv;\n"
684 "gl_Position = MVP * vec4(vtx_position, 1);\n"
685 "frag_tex_uv = vtx_texture;\n"
690 "#version 330 core\n"
692 "in vec2 frag_tex_uv;\n"
694 "uniform sampler2D tex_sampler;\n"
695 "uniform vec4 fg_color;\n"
696 "uniform vec4 bg_color;\n"
701 "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
702 "vec4 factor = mix(bg_color, fg_color, tex_color.a);\n"
703 "color = vec4((tex_color * factor).rgb, factor.a);\n"
707 if (!prog.Linked()) {
709 throw std::runtime_error("link program");
711 m_handle = prog.UniformLocation("M");
712 mv_handle = prog.UniformLocation("MV");
713 mvp_handle = prog.UniformLocation("MVP");
714 sampler_handle = prog.UniformLocation("tex_sampler");
715 fg_color_handle = prog.UniformLocation("fg_color");
716 bg_color_handle = prog.UniformLocation("bg_color");
719 vao.BindAttributes();
720 vao.EnableAttribute(0);
721 vao.EnableAttribute(1);
722 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
723 vao.AttributePointer<glm::vec2>(1, false, offsetof(Attributes, texture));
724 vao.ReserveAttributes(4, GL_STATIC_DRAW);
726 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
727 attrib[0].position = glm::vec3(-0.5f, -0.5f, 0.0f);
728 attrib[0].texture = glm::vec2(0.0f, 0.0f);
729 attrib[1].position = glm::vec3(-0.5f, 0.5f, 0.0f);
730 attrib[1].texture = glm::vec2(0.0f, 1.0f);
731 attrib[2].position = glm::vec3( 0.5f, -0.5f, 0.0f);
732 attrib[2].texture = glm::vec2(1.0f, 0.0f);
733 attrib[3].position = glm::vec3( 0.5f, 0.5f, 0.0f);
734 attrib[3].texture = glm::vec2(1.0f, 1.0f);
737 vao.ReserveElements(7, GL_STATIC_DRAW);
739 auto element = vao.MapElements(GL_WRITE_ONLY);
748 AlphaSprite::~AlphaSprite() {
751 void AlphaSprite::Activate() noexcept {
753 glEnable(GL_DEPTH_TEST);
754 glDepthFunc(GL_LESS);
755 glEnable(GL_CULL_FACE);
757 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
760 void AlphaSprite::SetM(const glm::mat4 &mm) noexcept {
764 prog.Uniform(m_handle, m);
765 prog.Uniform(mv_handle, mv);
766 prog.Uniform(mvp_handle, mvp);
769 void AlphaSprite::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
774 prog.Uniform(mv_handle, mv);
775 prog.Uniform(mvp_handle, mvp);
778 void AlphaSprite::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
784 prog.Uniform(m_handle, m);
785 prog.Uniform(mv_handle, mv);
786 prog.Uniform(mvp_handle, mvp);
789 void AlphaSprite::SetTexture(Texture &tex) noexcept {
790 glActiveTexture(GL_TEXTURE0);
792 prog.Uniform(sampler_handle, GLint(0));
795 void AlphaSprite::SetFgColor(const glm::vec4 &color) noexcept {
796 prog.Uniform(fg_color_handle, color);
799 void AlphaSprite::SetBgColor(const glm::vec4 &color) noexcept {
800 prog.Uniform(bg_color_handle, color);
803 void AlphaSprite::DrawRect() const noexcept {
805 vao.DrawTriangleStrip(4);
814 "#version 330 core\n"
816 "layout(location = 0) in vec2 vtx_position;\n"
822 // disamond rule adjust
823 //"vec3 position = vtx_position + vec3(0.5, 0.5, 0.0);\n"
824 "gl_Position = P * vec4(vtx_position, z, 1);\n"
829 "#version 330 core\n"
840 if (!prog.Linked()) {
842 throw std::runtime_error("link program");
844 p_handle = prog.UniformLocation("P");
845 z_handle = prog.UniformLocation("z");
846 c_handle = prog.UniformLocation("c");
849 vao.BindAttributes();
850 vao.EnableAttribute(0);
851 vao.AttributePointer<glm::vec2>(0, false, offsetof(Attributes, position));
852 vao.ReserveAttributes(255, GL_DYNAMIC_DRAW);
854 vao.ReserveElements(255, GL_DYNAMIC_DRAW);
861 void Canvas::Resize(float w, float h) noexcept {
862 prog.Uniform(p_handle, glm::ortho(0.0f, w, h, 0.0f, 1.0e4f, -1.0e4f));
865 void Canvas::ZIndex(float z) noexcept {
866 prog.Uniform(z_handle, -z);
869 void Canvas::SetColor(const glm::vec4 &color) noexcept {
870 prog.Uniform(c_handle, color);
873 void Canvas::Activate() noexcept {
875 glEnable(GL_DEPTH_TEST);
876 glDepthFunc(GL_LESS);
877 glEnable(GL_CULL_FACE);
879 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
882 void Canvas::DrawLine(const glm::vec2 &p1, const glm::vec2 &p2, float width) {
883 glm::vec2 d = glm::normalize(p2 - p1) * (width * 0.5f);
884 glm::vec2 n = glm::vec2(d.y, -d.x);
886 vao.BindAttributes();
888 auto attr = vao.MapAttributes(GL_WRITE_ONLY);
889 attr[0].position = p1 - d + n;
890 attr[1].position = p1 - d - n;
891 attr[2].position = p2 + d + n;
892 attr[3].position = p2 + d - n;
896 auto elem = vao.MapElements(GL_WRITE_ONLY);
902 vao.DrawTriangleStrip(4);
906 void Canvas::DrawRect(const glm::vec2 &p1, const glm::vec2 &p2, float width) {
907 glm::vec2 min(std::min(p1.x, p2.x), std::min(p1.y, p2.y));
908 glm::vec2 max(std::max(p1.x, p2.x), std::max(p1.y, p2.y));
909 glm::vec2 dg1(min.x, max.y);
910 glm::vec2 dg2(max.x, min.y);
911 glm::vec2 d(width * 0.5f, width * 0.5f);
912 glm::vec2 n(d.y, -d.x);
914 vao.BindAttributes();
916 auto attr = vao.MapAttributes(GL_WRITE_ONLY);
917 attr[0].position = min + d;
918 attr[1].position = min - d;
919 attr[2].position = dg1 + n;
920 attr[3].position = dg1 - n;
921 attr[4].position = max - d;
922 attr[5].position = max + d;
923 attr[6].position = dg2 - n;
924 attr[7].position = dg2 + n;
928 auto elem = vao.MapElements(GL_WRITE_ONLY);
940 vao.DrawTriangleStrip(10);
944 void Canvas::FillRect(const glm::vec2 &p1, const glm::vec2 &p2) {
945 glm::vec2 min(std::min(p1.x, p2.x), std::min(p1.y, p2.y));
946 glm::vec2 max(std::max(p1.x, p2.x), std::max(p1.y, p2.y));
947 glm::vec2 dg1(min.x, max.y);
948 glm::vec2 dg2(max.x, min.y);
950 vao.BindAttributes();
952 auto attr = vao.MapAttributes(GL_WRITE_ONLY);
953 attr[0].position = min;
954 attr[1].position = dg1;
955 attr[2].position = dg2;
956 attr[3].position = max;
960 auto elem = vao.MapElements(GL_WRITE_ONLY);
966 vao.DrawTriangleStrip(4);