54 lines
1.6 KiB
C++
54 lines
1.6 KiB
C++
#include "programs.h"
|
|
#include <glm/glm.hpp>
|
|
|
|
//---
|
|
// 2D Texture shader
|
|
//---
|
|
|
|
ProgramTexture::ProgramTexture(): Program()
|
|
{
|
|
glBindVertexArray(this->vao);
|
|
glEnableVertexAttribArray(0);
|
|
glEnableVertexAttribArray(1);
|
|
|
|
// TODO: Better way to access Azur's internal shaders
|
|
extern char const *azur_glsl__vs_tex2d;
|
|
extern char const *azur_glsl__fs_tex2d;
|
|
|
|
this->prog = azur::gl::loadProgramSources(
|
|
GL_VERTEX_SHADER, azur_glsl__vs_tex2d,
|
|
GL_FRAGMENT_SHADER, azur_glsl__fs_tex2d,
|
|
0);
|
|
}
|
|
|
|
void ProgramTexture::set_vertex_attributes() const
|
|
{
|
|
glVertexAttribPointer(glGetAttribLocation(this->prog, "a_vertex"),
|
|
2, GL_FLOAT, GL_FALSE,
|
|
sizeof(ProgramTexture_Attributes),
|
|
(void *)offsetof(ProgramTexture_Attributes, vertex)
|
|
);
|
|
glVertexAttribPointer(glGetAttribLocation(this->prog, "a_texture_pos"),
|
|
2, GL_FLOAT, GL_FALSE,
|
|
sizeof(ProgramTexture_Attributes),
|
|
(void *)offsetof(ProgramTexture_Attributes, uv)
|
|
);
|
|
}
|
|
|
|
void ProgramTexture::add_texture(int x, int y, int width, int height)
|
|
{
|
|
ProgramTexture_Attributes attr[4] = {
|
|
{ glm::vec2(x, y), glm::vec2(0.0, 0.0) },
|
|
{ glm::vec2(x+width, y), glm::vec2(1.0, 0.0) },
|
|
{ glm::vec2(x, y+height), glm::vec2(0.0, 1.0) },
|
|
{ glm::vec2(x+width, y+height), glm::vec2(1.0, 1.0) },
|
|
};
|
|
|
|
this->vertices.push_back(attr[0]);
|
|
this->vertices.push_back(attr[1]);
|
|
this->vertices.push_back(attr[2]);
|
|
this->vertices.push_back(attr[1]);
|
|
this->vertices.push_back(attr[2]);
|
|
this->vertices.push_back(attr[3]);
|
|
}
|