2013-05-02 11:25:45 +00:00
|
|
|
void OpenGLProgram::bind(OpenGL* instance, const Markup::Node& node, const string& pathname) {
|
2013-04-09 13:31:46 +00:00
|
|
|
filter = glrFilter(node["filter"].text());
|
|
|
|
wrap = glrWrap(node["wrap"].text());
|
|
|
|
modulo = glrModulo(node["modulo"].integer());
|
|
|
|
|
|
|
|
string w = node["width"].text(), h = node["height"].text();
|
|
|
|
if(w.endswith("%")) relativeWidth = fp(w.rtrim<1>("%")) / 100.0;
|
|
|
|
else absoluteWidth = decimal(w);
|
|
|
|
if(h.endswith("%")) relativeHeight = fp(h.rtrim<1>("%")) / 100.0;
|
|
|
|
else absoluteHeight = decimal(h);
|
|
|
|
|
|
|
|
if(node.name != "program") return;
|
|
|
|
format = glrFormat(node["format"].text());
|
|
|
|
|
|
|
|
program = glCreateProgram();
|
|
|
|
glGenFramebuffers(1, &framebuffer);
|
|
|
|
|
|
|
|
if(file::exists({pathname, node["vertex"].text()})) {
|
2013-04-14 08:52:47 +00:00
|
|
|
string source = file::read({pathname, node["vertex"].text()});
|
2013-04-09 13:31:46 +00:00
|
|
|
vertex = glrCreateShader(program, GL_VERTEX_SHADER, source);
|
|
|
|
} else {
|
|
|
|
vertex = glrCreateShader(program, GL_VERTEX_SHADER, OpenGLVertexShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(file::exists({pathname, node["geometry"].text()})) {
|
2013-04-14 08:52:47 +00:00
|
|
|
string source = file::read({pathname, node["geometry"].text()});
|
2013-04-09 13:31:46 +00:00
|
|
|
geometry = glrCreateShader(program, GL_GEOMETRY_SHADER, source);
|
|
|
|
} else {
|
|
|
|
//geometry shaders, when attached, must pass all vertex output through to the fragment shaders
|
|
|
|
//geometry = glrCreateShader(program, GL_GEOMETRY_SHADER, OpenGLGeometryShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(file::exists({pathname, node["fragment"].text()})) {
|
2013-04-14 08:52:47 +00:00
|
|
|
string source = file::read({pathname, node["fragment"].text()});
|
2013-04-09 13:31:46 +00:00
|
|
|
fragment = glrCreateShader(program, GL_FRAGMENT_SHADER, source);
|
|
|
|
} else {
|
|
|
|
fragment = glrCreateShader(program, GL_FRAGMENT_SHADER, OpenGLFragmentShader);
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
for(auto& leaf : node.find("pixmap")) {
|
2013-04-09 13:31:46 +00:00
|
|
|
nall::image image({pathname, leaf.text()});
|
|
|
|
image.transform(0, 32, 255u << 24, 255u << 16, 255u << 8, 255u << 0);
|
|
|
|
if(image.empty()) continue;
|
|
|
|
|
|
|
|
GLuint texture;
|
|
|
|
glGenTextures(1, &texture);
|
|
|
|
|
2013-04-14 08:52:47 +00:00
|
|
|
unsigned n = pixmaps.size();
|
|
|
|
pixmaps(n).texture = texture;
|
|
|
|
pixmaps(n).width = image.width;
|
|
|
|
pixmaps(n).height = image.height;
|
|
|
|
pixmaps(n).format = format;
|
|
|
|
pixmaps(n).filter = filter;
|
|
|
|
pixmaps(n).wrap = wrap;
|
|
|
|
if(leaf["format"].exists()) pixmaps(n).format = glrFormat(leaf["format"].text());
|
|
|
|
if(leaf["filter"].exists()) pixmaps(n).filter = glrFilter(leaf["filter"].text());
|
|
|
|
if(leaf["wrap"].exists()) pixmaps(n).wrap = glrWrap(leaf["wrap"].text());
|
2013-04-09 13:31:46 +00:00
|
|
|
|
|
|
|
unsigned w = glrSize(image.width), h = glrSize(image.height);
|
2013-05-02 11:25:45 +00:00
|
|
|
uint32_t* buffer = new uint32_t[w * h]();
|
2013-04-09 13:31:46 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
2013-04-14 08:52:47 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, pixmaps(n).format, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
|
2013-04-09 13:31:46 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image.width, image.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, image.data);
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
2013-04-14 08:52:47 +00:00
|
|
|
OpenGLSurface::allocate();
|
2013-04-09 13:31:46 +00:00
|
|
|
glrLinkProgram(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLProgram::release() {
|
|
|
|
OpenGLSurface::release();
|
2013-05-02 11:25:45 +00:00
|
|
|
for(auto& pixmap : pixmaps) glDeleteTextures(1, &pixmap.texture);
|
2013-04-14 08:52:47 +00:00
|
|
|
pixmaps.reset();
|
2013-04-09 13:31:46 +00:00
|
|
|
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
format = GL_RGBA8;
|
|
|
|
filter = GL_LINEAR;
|
|
|
|
wrap = GL_CLAMP_TO_BORDER;
|
|
|
|
phase = 0;
|
|
|
|
modulo = 0;
|
|
|
|
absoluteWidth = 0;
|
|
|
|
absoluteHeight = 0;
|
|
|
|
relativeWidth = 0;
|
|
|
|
relativeHeight = 0;
|
|
|
|
}
|