/* * © Copyright 2014 – Urheberrechtshinweis * Alle Rechte vorbehalten / All Rights Reserved * * Programmcode ist urheberrechtlich geschuetzt. * Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner. * Keine Verwendung ohne explizite Genehmigung. * (vgl. § 106 ff UrhG / § 97 UrhG) */ #include "Shader.h" #include Shader::Shader() { addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, R"( attribute highp vec3 a_vertex; attribute highp vec3 a_normal; attribute lowp vec4 a_color; uniform highp mat4 M; uniform highp mat4 V; uniform highp mat4 P; varying highp vec3 v_normal; varying lowp vec4 v_color; varying lowp vec4 v_vertex; void main() { gl_Position = P * V * M * vec4(a_vertex, 1.0); v_normal = (V * M * vec4(a_normal, 0.0)).xyz; v_color = a_color; v_vertex = V * M * vec4(a_vertex, 1.0); } )"); addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, R"( uniform lowp vec4 color; uniform bool useNormal; uniform bool useVertexColor; varying highp vec3 v_normal; varying lowp vec4 v_color; varying lowp vec4 v_vertex; void main() { mediump vec3 lightPos = vec3(0,0,0); // camera //vec3 lightVec = normalize(vec3(0,0,1)); mediump vec3 lightVec = normalize(lightPos - v_vertex.xyz); lowp float intensity = useNormal ? 0.3 + 0.7 * dot( normalize(v_normal), lightVec ) : 1.0; // light at camera pos lowp vec4 col = useVertexColor ? v_color : color; gl_FragColor.rgb = col.rgb * intensity; gl_FragColor.a = col.a; } )"); //bindAttributeLocation("vertices", 0); if (!link()) { std::cout << log().toStdString() << std::endl; throw std::runtime_error("shader link error"); } } void Shader::setModelMatrix(const QMatrix4x4& m) { //vao.bind();; setUniformValue(getUniform("M"), m); } void Shader::setViewMatrix(const QMatrix4x4& m) { setUniformValue(getUniform("V"), m); } void Shader::setProjectionMatrix(const QMatrix4x4& m) { setUniformValue(getUniform("P"), m); } void Shader::setUseNormals(bool use) { int loc = getUniform("useNormal"); setUniformValue(loc, use); } void Shader::setUseVertexColor(bool use) { int loc = getUniform("useVertexColor"); setUniformValue(loc, use); } int Shader::getUniform(const char* name) { int loc = uniformLocation(name); if (loc == -1) {throw std::runtime_error("error");} return loc; } int Shader::getAttribute(const char* name) { int loc = attributeLocation(name); if (loc == -1) {throw std::runtime_error("error");} return loc; } void Shader::setColor(const float r, const float g, const float b) { setUniformValue(getUniform("color"), QVector4D(r,g,b,1)); } void Shader::setColor(const float r, const float g, const float b, const float a) { setUniformValue(getUniform("color"), QVector4D(r,g,b,a)); } void Shader::setVertices(const float* values) { const int loc = getAttribute("a_vertex"); enableAttributeArray(loc); setAttributeArray(loc, GL_FLOAT, values, 3); } void Shader::unsetVertices() { const int loc = getAttribute("a_vertex"); disableAttributeArray(loc); } void Shader::setNormals(const float* values) { setUseNormals(true); const int loc = getAttribute("a_normal"); enableAttributeArray(loc); setAttributeArray(loc, GL_FLOAT, values, 3); } void Shader::unsetNormals() { setUseNormals(false); const int loc = getAttribute("a_normal"); disableAttributeArray(loc); } void Shader::setVertexColor(const float* values) { setUseVertexColor(true); const int loc = getAttribute("a_color"); enableAttributeArray(loc); setAttributeArray(loc, GL_FLOAT, values, 4); // RGBA!!! } void Shader::unsetVertexColor() { setUseVertexColor(false); const int loc = getAttribute("a_color"); disableAttributeArray(loc); }