painter_shader.cpp Example File
sensors/cubehouse/painter_shader.cpp
#include "painter.h"
#include "light.h"
#include "lightmodel.h"
#include "material.h"
#include <QtOpenGL/qglshaderprogram.h>
ShaderPainter::ShaderPainter()
{
materialProgram = new QGLShaderProgram();
materialProgram->addShaderFromSourceFile
(QGLShader::Vertex, QLatin1String(":/lighting.vsh"));
materialProgram->addShaderFromSourceFile
(QGLShader::Fragment, QLatin1String(":/material.fsh"));
materialProgram->bindAttributeLocation("vertex", 0);
materialProgram->bindAttributeLocation("normal", 1);
materialProgram->bindAttributeLocation("texcoord", 2);
materialProgram->link();
textureProgram = new QGLShaderProgram();
textureProgram->addShaderFromSourceFile
(QGLShader::Vertex, QLatin1String(":/lighting.vsh"));
textureProgram->addShaderFromSourceFile
(QGLShader::Fragment, QLatin1String(":/texture.fsh"));
textureProgram->bindAttributeLocation("vertex", 0);
textureProgram->bindAttributeLocation("normal", 1);
textureProgram->bindAttributeLocation("texcoord", 2);
textureProgram->link();
currentProgram = 0;
matricesChanged = false;
}
ShaderPainter::~ShaderPainter()
{
delete materialProgram;
delete textureProgram;
}
void ShaderPainter::setMatrices(const QMatrix4x4 &mv, const QMatrix4x4 &proj)
{
combinedMatrix = proj * mv;
modelViewMatrix = mv;
normalMatrix = mv.normalMatrix();
matricesChanged = true;
}
void ShaderPainter::selectMaterial(Material *material)
{
if (currentProgram != materialProgram) {
materialProgram->bind();
materialProgram->enableAttributeArray(0);
materialProgram->enableAttributeArray(1);
materialProgram->disableAttributeArray(2);
currentProgram = materialProgram;
matricesChanged = true;
}
if (matricesChanged) {
materialProgram->setUniformValue("matrix", combinedMatrix);
materialProgram->setUniformValue("modelView", modelViewMatrix);
materialProgram->setUniformValue("normalMatrix", normalMatrix);
matricesChanged = false;
}
updateMaterials(materialProgram, material);
}
void ShaderPainter::selectTexturedMaterial(Material *material)
{
if (currentProgram != textureProgram) {
textureProgram->bind();
textureProgram->enableAttributeArray(0);
textureProgram->enableAttributeArray(1);
textureProgram->enableAttributeArray(2);
textureProgram->setUniformValue("tex", 0);
currentProgram = textureProgram;
matricesChanged = true;
}
if (matricesChanged) {
textureProgram->setUniformValue("matrix", combinedMatrix);
textureProgram->setUniformValue("modelView", modelViewMatrix);
textureProgram->setUniformValue("normalMatrix", normalMatrix);
matricesChanged = false;
}
updateMaterials(textureProgram, material);
}
void ShaderPainter::setVertices(const float *array, int stride)
{
materialProgram->setAttributeArray(0, array, 3, stride * sizeof(float));
}
void ShaderPainter::setTexCoords(const float *array, int stride)
{
materialProgram->setAttributeArray(2, array, 2, stride * sizeof(float));
}
void ShaderPainter::setNormals(const float *array, int stride)
{
materialProgram->setAttributeArray(1, array, 3, stride * sizeof(float));
}
void ShaderPainter::updateMaterials
(QGLShaderProgram *program, Material *material)
{
QVector4D pli = light->eyePosition(QMatrix4x4());
program->setUniformValue("pli", QVector3D(pli.x(), pli.y(), pli.z()));
program->setUniformValue("pliw", GLfloat(pli.w()));
program->setUniformValue("viewerAtInfinity", (int)(lightModel->viewerPosition() == LightModel::ViewerAtInfinity));
program->setUniformValue("acs", lightModel->ambientSceneColor());
program->setUniformValue("acm", material->ambientColor());
program->setUniformValue("dcm", material->diffuseColor());
program->setUniformValue("scm", material->specularColor());
program->setUniformValue("srm", (float)(material->shininess()));
}
[+] Documentation Feedback