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_tex_uv;\n"
197 "uniform mat4 MVP;\n"
199 "out vec3 frag_tex_uv;\n"
200 "out vec3 vtx_viewspace;\n"
203 "gl_Position = MVP * vec4(vtx_position, 1);\n"
204 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
205 "frag_tex_uv = vtx_tex_uv;\n"
210 "#version 330 core\n"
212 "struct LightSource {\n"
218 "in vec3 vtx_viewspace;\n"
219 "in vec3 frag_tex_uv;\n"
221 "uniform sampler2DArray tex_sampler;\n"
222 "uniform vec3 normal;\n"
223 "uniform int num_lights;\n"
224 "uniform LightSource light[8];\n"
229 "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
230 "vec3 total_light = tex_color * vec3(0.1, 0.1, 0.1);\n"
231 "for (int i = 0; i < num_lights; ++i) {\n"
232 "vec3 to_light = light[i].position - vtx_viewspace;\n"
233 "float distance = length(to_light) + length(vtx_viewspace);\n"
234 "vec3 light_dir = normalize(to_light);\n"
235 "float attenuation = light[i].strength / (distance * distance);\n"
236 "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * tex_color;\n"
237 "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n"
238 "vec3 specular = vec3(0.0, 0.0, 0.0);\n"
239 "if (dot(normal, light_dir) >= 0.0) {\n"
240 "attenuation * light[i].color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n"
242 "total_light = total_light + diffuse + specular;\n"
244 "color = total_light;\n"
248 if (!prog.Linked()) {
250 throw std::runtime_error("link program");
252 m_handle = prog.UniformLocation("M");
253 mv_handle = prog.UniformLocation("MV");
254 mvp_handle = prog.UniformLocation("MVP");
255 sampler_handle = prog.UniformLocation("tex_sampler");
256 normal_handle = prog.UniformLocation("normal");
257 num_lights_handle = prog.UniformLocation("num_lights");
258 for (int i = 0; i < MAX_LIGHTS; ++i) {
259 light_handle[3 * i + 0] = prog.UniformLocation("light[" + std::to_string(i) + "].position");
260 light_handle[3 * i + 1] = prog.UniformLocation("light[" + std::to_string(i) + "].color");
261 light_handle[3 * i + 2] = prog.UniformLocation("light[" + std::to_string(i) + "].strength");
265 PlanetSurface::~PlanetSurface() {
268 void PlanetSurface::Activate() noexcept {
270 glEnable(GL_DEPTH_TEST);
271 glDepthFunc(GL_LESS);
272 glEnable(GL_CULL_FACE);
276 void PlanetSurface::SetM(const glm::mat4 &mm) noexcept {
280 prog.Uniform(m_handle, m);
281 prog.Uniform(mv_handle, mv);
282 prog.Uniform(mvp_handle, mvp);
285 void PlanetSurface::SetV(const glm::mat4 &vv) noexcept {
289 prog.Uniform(mv_handle, mv);
290 prog.Uniform(mvp_handle, mvp);
293 void PlanetSurface::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
298 prog.Uniform(mv_handle, mv);
299 prog.Uniform(mvp_handle, mvp);
302 void PlanetSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
308 prog.Uniform(m_handle, m);
309 prog.Uniform(mv_handle, mv);
310 prog.Uniform(mvp_handle, mvp);
313 void PlanetSurface::SetNormal(const glm::vec3 &n) noexcept {
314 prog.Uniform(normal_handle, n);
317 void PlanetSurface::SetTexture(ArrayTexture &tex) noexcept {
318 glActiveTexture(GL_TEXTURE0);
320 prog.Uniform(sampler_handle, GLint(0));
323 void PlanetSurface::SetLight(int n, const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept {
324 prog.Uniform(light_handle[3 * n + 0], pos);
325 prog.Uniform(light_handle[3 * n + 1], color);
326 prog.Uniform(light_handle[3 * n + 2], strength);
329 void PlanetSurface::SetNumLights(int n) noexcept {
330 prog.Uniform(num_lights_handle, std::min(MAX_LIGHTS, n));
334 SunSurface::SunSurface()
338 "#version 330 core\n"
340 "layout(location = 0) in vec3 vtx_position;\n"
344 "uniform mat4 MVP;\n"
346 "out vec3 vtx_viewspace;\n"
349 "gl_Position = MVP * vec4(vtx_position, 1);\n"
350 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
355 "#version 330 core\n"
357 "in vec3 vtx_viewspace;\n"
359 "uniform vec3 light_color;\n"
360 "uniform float light_strength;\n"
365 "vec3 to_light = -vtx_viewspace;\n"
366 "float distance = length(to_light);\n"
367 //"vec3 light_dir = normalize(to_light);\n"
368 "float attenuation = light_strength / (distance * distance);\n"
369 "color = attenuation * light_color;\n"
373 if (!prog.Linked()) {
375 throw std::runtime_error("link program");
377 m_handle = prog.UniformLocation("M");
378 mv_handle = prog.UniformLocation("MV");
379 mvp_handle = prog.UniformLocation("MVP");
380 light_color_handle = prog.UniformLocation("light_color");
381 light_strength_handle = prog.UniformLocation("light_strength");
384 vao.BindAttributes();
385 vao.EnableAttribute(0);
386 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
387 vao.ReserveAttributes(8, GL_STATIC_DRAW);
389 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
390 attrib[0].position = glm::vec3(-1.0f, -1.0f, -1.0f);
391 attrib[1].position = glm::vec3(-1.0f, -1.0f, 1.0f);
392 attrib[2].position = glm::vec3(-1.0f, 1.0f, -1.0f);
393 attrib[3].position = glm::vec3(-1.0f, 1.0f, 1.0f);
394 attrib[4].position = glm::vec3( 1.0f, -1.0f, -1.0f);
395 attrib[5].position = glm::vec3( 1.0f, -1.0f, 1.0f);
396 attrib[6].position = glm::vec3( 1.0f, 1.0f, -1.0f);
397 attrib[7].position = glm::vec3( 1.0f, 1.0f, 1.0f);
400 vao.ReserveElements(36, GL_STATIC_DRAW);
402 auto element = vao.MapElements(GL_WRITE_ONLY);
449 SunSurface::~SunSurface() {
452 void SunSurface::Activate() noexcept {
454 glEnable(GL_DEPTH_TEST);
455 glDepthFunc(GL_LESS);
456 glEnable(GL_CULL_FACE);
460 void SunSurface::SetM(const glm::mat4 &mm) noexcept {
464 prog.Uniform(m_handle, m);
465 prog.Uniform(mv_handle, mv);
466 prog.Uniform(mvp_handle, mvp);
469 void SunSurface::SetV(const glm::mat4 &vv) noexcept {
473 prog.Uniform(mv_handle, mv);
474 prog.Uniform(mvp_handle, mvp);
477 void SunSurface::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
482 prog.Uniform(mv_handle, mv);
483 prog.Uniform(mvp_handle, mvp);
486 void SunSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
492 prog.Uniform(m_handle, m);
493 prog.Uniform(mv_handle, mv);
494 prog.Uniform(mvp_handle, mvp);
497 void SunSurface::SetLight(const glm::vec3 &color, float strength) noexcept {
498 prog.Uniform(light_color_handle, color);
499 prog.Uniform(light_strength_handle, strength);
502 void SunSurface::Draw() const noexcept {
504 vao.DrawTriangles(36);
508 constexpr int CreatureSkin::MAX_LIGHTS;
510 CreatureSkin::CreatureSkin()
514 "#version 330 core\n"
516 "layout(location = 0) in vec3 vtx_position;\n"
517 "layout(location = 1) in vec3 vtx_normal;\n"
518 "layout(location = 2) in vec3 vtx_tex_uv;\n"
522 "uniform mat4 MVP;\n"
524 "out vec3 vtx_viewspace;\n"
525 "out vec3 frag_tex_uv;\n"
529 "gl_Position = MVP * vec4(vtx_position, 1);\n"
530 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
531 "normal = normalize((MV * vec4(vtx_normal, 0)).xyz);\n"
532 "frag_tex_uv = vtx_tex_uv;\n"
537 "#version 330 core\n"
539 "struct LightSource {\n"
545 "in vec3 vtx_viewspace;\n"
546 "in vec3 frag_tex_uv;\n"
549 "uniform vec3 base_color;\n"
550 "uniform vec4 highlight_color;\n"
551 "uniform sampler2DArray tex_sampler;\n"
552 "uniform int num_lights;\n"
553 "uniform LightSource light[8];\n"
558 "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
559 "vec3 mat_color = mix(base_color, highlight_color.rgb, tex_color.r * tex_color.a * highlight_color.a);\n"
560 "vec3 total_light = mat_color * vec3(0.1, 0.1, 0.1);\n"
561 "for (int i = 0; i < num_lights; ++i) {\n"
562 "vec3 to_light = light[i].position - vtx_viewspace;\n"
563 "float distance = length(to_light) + length(vtx_viewspace);\n"
564 "vec3 light_dir = normalize(to_light);\n"
565 "float attenuation = light[i].strength / (distance * distance);\n"
566 "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * mat_color;\n"
567 "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n"
568 "vec3 specular = vec3(0.0, 0.0, 0.0);\n"
569 "if (dot(normal, light_dir) >= 0.0) {\n"
570 "attenuation * light[i].color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n"
572 "total_light = total_light + diffuse + specular;\n"
574 "color = total_light;\n"
578 if (!prog.Linked()) {
580 throw std::runtime_error("link program");
582 m_handle = prog.UniformLocation("M");
583 mv_handle = prog.UniformLocation("MV");
584 mvp_handle = prog.UniformLocation("MVP");
585 base_color_handle = prog.UniformLocation("base_color");
586 highlight_color_handle = prog.UniformLocation("highlight_color");
587 sampler_handle = prog.UniformLocation("tex_sampler");
588 num_lights_handle = prog.UniformLocation("num_lights");
589 for (int i = 0; i < MAX_LIGHTS; ++i) {
590 light_handle[3 * i + 0] = prog.UniformLocation("light[" + std::to_string(i) + "].position");
591 light_handle[3 * i + 1] = prog.UniformLocation("light[" + std::to_string(i) + "].color");
592 light_handle[3 * i + 2] = prog.UniformLocation("light[" + std::to_string(i) + "].strength");
596 CreatureSkin::~CreatureSkin() {
599 void CreatureSkin::Activate() noexcept {
601 glEnable(GL_DEPTH_TEST);
602 glDepthFunc(GL_LESS);
603 glEnable(GL_CULL_FACE);
607 void CreatureSkin::SetM(const glm::mat4 &mm) noexcept {
611 prog.Uniform(m_handle, m);
612 prog.Uniform(mv_handle, mv);
613 prog.Uniform(mvp_handle, mvp);
616 void CreatureSkin::SetV(const glm::mat4 &vv) noexcept {
620 prog.Uniform(mv_handle, mv);
621 prog.Uniform(mvp_handle, mvp);
624 void CreatureSkin::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
629 prog.Uniform(mv_handle, mv);
630 prog.Uniform(mvp_handle, mvp);
633 void CreatureSkin::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
639 prog.Uniform(m_handle, m);
640 prog.Uniform(mv_handle, mv);
641 prog.Uniform(mvp_handle, mvp);
644 void CreatureSkin::SetBaseColor(const glm::vec3 &c) noexcept {
645 prog.Uniform(base_color_handle, c);
648 void CreatureSkin::SetHighlightColor(const glm::vec4 &c) noexcept {
649 prog.Uniform(highlight_color_handle, c);
652 void CreatureSkin::SetTexture(ArrayTexture &tex) noexcept {
653 glActiveTexture(GL_TEXTURE0);
655 prog.Uniform(sampler_handle, GLint(0));
658 void CreatureSkin::SetLight(int n, const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept {
659 prog.Uniform(light_handle[3 * n + 0], pos);
660 prog.Uniform(light_handle[3 * n + 1], color);
661 prog.Uniform(light_handle[3 * n + 2], strength);
664 void CreatureSkin::SetNumLights(int n) noexcept {
665 prog.Uniform(num_lights_handle, std::min(MAX_LIGHTS, n));
669 AlphaSprite::AlphaSprite()
673 "#version 330 core\n"
675 "layout(location = 0) in vec3 vtx_position;\n"
676 "layout(location = 1) in vec2 vtx_texture;\n"
680 "uniform mat4 MVP;\n"
682 "out vec2 frag_tex_uv;\n"
685 "gl_Position = MVP * vec4(vtx_position, 1);\n"
686 "frag_tex_uv = vtx_texture;\n"
691 "#version 330 core\n"
693 "in vec2 frag_tex_uv;\n"
695 "uniform sampler2D tex_sampler;\n"
696 "uniform vec4 fg_color;\n"
697 "uniform vec4 bg_color;\n"
702 "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
703 "vec4 factor = mix(bg_color, fg_color, tex_color.a);\n"
704 "color = vec4((tex_color * factor).rgb, factor.a);\n"
708 if (!prog.Linked()) {
710 throw std::runtime_error("link program");
712 m_handle = prog.UniformLocation("M");
713 mv_handle = prog.UniformLocation("MV");
714 mvp_handle = prog.UniformLocation("MVP");
715 sampler_handle = prog.UniformLocation("tex_sampler");
716 fg_color_handle = prog.UniformLocation("fg_color");
717 bg_color_handle = prog.UniformLocation("bg_color");
720 vao.BindAttributes();
721 vao.EnableAttribute(0);
722 vao.EnableAttribute(1);
723 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
724 vao.AttributePointer<glm::vec2>(1, false, offsetof(Attributes, texture));
725 vao.ReserveAttributes(4, GL_STATIC_DRAW);
727 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
728 attrib[0].position = glm::vec3(-0.5f, -0.5f, 0.0f);
729 attrib[0].texture = glm::vec2(0.0f, 0.0f);
730 attrib[1].position = glm::vec3(-0.5f, 0.5f, 0.0f);
731 attrib[1].texture = glm::vec2(0.0f, 1.0f);
732 attrib[2].position = glm::vec3( 0.5f, -0.5f, 0.0f);
733 attrib[2].texture = glm::vec2(1.0f, 0.0f);
734 attrib[3].position = glm::vec3( 0.5f, 0.5f, 0.0f);
735 attrib[3].texture = glm::vec2(1.0f, 1.0f);
738 vao.ReserveElements(7, GL_STATIC_DRAW);
740 auto element = vao.MapElements(GL_WRITE_ONLY);
749 AlphaSprite::~AlphaSprite() {
752 void AlphaSprite::Activate() noexcept {
754 glEnable(GL_DEPTH_TEST);
755 glDepthFunc(GL_LESS);
756 glEnable(GL_CULL_FACE);
758 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
761 void AlphaSprite::SetM(const glm::mat4 &mm) noexcept {
765 prog.Uniform(m_handle, m);
766 prog.Uniform(mv_handle, mv);
767 prog.Uniform(mvp_handle, mvp);
770 void AlphaSprite::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
775 prog.Uniform(mv_handle, mv);
776 prog.Uniform(mvp_handle, mvp);
779 void AlphaSprite::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
785 prog.Uniform(m_handle, m);
786 prog.Uniform(mv_handle, mv);
787 prog.Uniform(mvp_handle, mvp);
790 void AlphaSprite::SetTexture(Texture &tex) noexcept {
791 glActiveTexture(GL_TEXTURE0);
793 prog.Uniform(sampler_handle, GLint(0));
796 void AlphaSprite::SetFgColor(const glm::vec4 &color) noexcept {
797 prog.Uniform(fg_color_handle, color);
800 void AlphaSprite::SetBgColor(const glm::vec4 &color) noexcept {
801 prog.Uniform(bg_color_handle, color);
804 void AlphaSprite::DrawRect() const noexcept {
806 vao.DrawTriangleStrip(4);
815 "#version 330 core\n"
817 "layout(location = 0) in vec2 vtx_position;\n"
823 // disamond rule adjust
824 //"vec3 position = vtx_position + vec3(0.5, 0.5, 0.0);\n"
825 "gl_Position = P * vec4(vtx_position, z, 1);\n"
830 "#version 330 core\n"
841 if (!prog.Linked()) {
843 throw std::runtime_error("link program");
845 p_handle = prog.UniformLocation("P");
846 z_handle = prog.UniformLocation("z");
847 c_handle = prog.UniformLocation("c");
850 vao.BindAttributes();
851 vao.EnableAttribute(0);
852 vao.AttributePointer<glm::vec2>(0, false, offsetof(Attributes, position));
853 vao.ReserveAttributes(255, GL_DYNAMIC_DRAW);
855 vao.ReserveElements(255, GL_DYNAMIC_DRAW);
862 void Canvas::Resize(float w, float h) noexcept {
863 prog.Uniform(p_handle, glm::ortho(0.0f, w, h, 0.0f, 1.0e4f, -1.0e4f));
866 void Canvas::ZIndex(float z) noexcept {
867 prog.Uniform(z_handle, -z);
870 void Canvas::SetColor(const glm::vec4 &color) noexcept {
871 prog.Uniform(c_handle, color);
874 void Canvas::Activate() noexcept {
876 glEnable(GL_DEPTH_TEST);
877 glDepthFunc(GL_LESS);
878 glEnable(GL_CULL_FACE);
880 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
883 void Canvas::DrawLine(const glm::vec2 &p1, const glm::vec2 &p2, float width) {
884 glm::vec2 d = glm::normalize(p2 - p1) * (width * 0.5f);
885 glm::vec2 n = glm::vec2(d.y, -d.x);
887 vao.BindAttributes();
889 auto attr = vao.MapAttributes(GL_WRITE_ONLY);
890 attr[0].position = p1 - d + n;
891 attr[1].position = p1 - d - n;
892 attr[2].position = p2 + d + n;
893 attr[3].position = p2 + d - n;
897 auto elem = vao.MapElements(GL_WRITE_ONLY);
903 vao.DrawTriangleStrip(4);
907 void Canvas::DrawRect(const glm::vec2 &p1, const glm::vec2 &p2, float width) {
908 glm::vec2 min(std::min(p1.x, p2.x), std::min(p1.y, p2.y));
909 glm::vec2 max(std::max(p1.x, p2.x), std::max(p1.y, p2.y));
910 glm::vec2 dg1(min.x, max.y);
911 glm::vec2 dg2(max.x, min.y);
912 glm::vec2 d(width * 0.5f, width * 0.5f);
913 glm::vec2 n(d.y, -d.x);
915 vao.BindAttributes();
917 auto attr = vao.MapAttributes(GL_WRITE_ONLY);
918 attr[0].position = min + d;
919 attr[1].position = min - d;
920 attr[2].position = dg1 + n;
921 attr[3].position = dg1 - n;
922 attr[4].position = max - d;
923 attr[5].position = max + d;
924 attr[6].position = dg2 - n;
925 attr[7].position = dg2 + n;
929 auto elem = vao.MapElements(GL_WRITE_ONLY);
941 vao.DrawTriangleStrip(10);
945 void Canvas::FillRect(const glm::vec2 &p1, const glm::vec2 &p2) {
946 glm::vec2 min(std::min(p1.x, p2.x), std::min(p1.y, p2.y));
947 glm::vec2 max(std::max(p1.x, p2.x), std::max(p1.y, p2.y));
948 glm::vec2 dg1(min.x, max.y);
949 glm::vec2 dg2(max.x, min.y);
951 vao.BindAttributes();
953 auto attr = vao.MapAttributes(GL_WRITE_ONLY);
954 attr[0].position = min;
955 attr[1].position = dg1;
956 attr[2].position = dg2;
957 attr[3].position = max;
961 auto elem = vao.MapElements(GL_WRITE_ONLY);
967 vao.DrawTriangleStrip(4);