SparrowRenderer/src/textureredux.cpp

62 lines
1.7 KiB
C++

#include "textureredux.h"
#include "texture.h"
#include "glassert.h"
#include "shader.h"
#include <glm/ext.hpp>
TextureRedux::TextureRedux(FrameBuffer* input, int textureId) :
m_shader(NULL)
{
Texture* inputTexture = input->getTexture(textureId);
Texture* tex = new Texture(inputTexture, true);
ping.addTexture(tex, GL_COLOR_ATTACHMENT0);
pong.addTexture(inputTexture, GL_COLOR_ATTACHMENT0);
ping.initColorAttachments();
pong.initColorAttachments();
}
TextureRedux::~TextureRedux()
{
delete(ping.getTexture(0));
}
void TextureRedux::setShader(Shader* shader)
{
m_shader = shader;
uniformLocations[SAMPLER] = shader->getLocation("colorSampler");
uniformLocations[WIDTH] = shader->getLocation("width");
uniformLocations[HEIGTH] = shader->getLocation("height");
}
glm::vec3 TextureRedux::redux()
{
bool inverted = false;
int tempWidth = pong.getTexture(0)->getWidth();
int tempHeight = pong.getTexture(0)->getHeight();
while(tempWidth > 1 && tempHeight > 1)
{
m_shader->bind();
m_shader->bindInteger(uniformLocations[SAMPLER], 0);
m_shader->bindInteger(uniformLocations[WIDTH], tempWidth);
m_shader->bindInteger(uniformLocations[HEIGTH], tempHeight);
if(inverted)
{
ping.getTexture(0)->bind(0);
pong.bindFBO();
}
else
{
ping.bindFBO();
pong.getTexture(0)->bind(0);
}
tempWidth /= 2;
tempHeight /= 2;
glViewport(0, 0, tempWidth, tempHeight);
glAssert(glDrawArrays(GL_TRIANGLES, 0, 3));
}
glm::vec3 minMaxMean;
glAssert(glReadPixels(0, 0, 0, 0, GL_RGB, GL_FLOAT, glm::value_ptr(minMaxMean)));
return minMaxMean;
}