This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
IndoorMap/mapview/3D/misc/Shader.cpp
root e5e19779d5 worked on android port
opengl1 -> es
2018-01-31 17:15:11 +01:00

89 lines
2.2 KiB
C++

#include "Shader.h"
#include <iostream>
Shader::Shader() {
addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, R"(
attribute highp vec3 a_vertex;
attribute highp vec3 a_normal;
uniform highp mat4 M;
uniform highp mat4 V;
uniform highp mat4 P;
varying highp vec3 normal;
void main() {
gl_Position = vec4(a_vertex, 1.0);
normal = normalize( V*M*vec4(a_normal, 0.0) );
}
)");
addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, R"(
uniform vec4 color;
varying highp vec3 normal;
void main() {
float intensity = dot( normal, normalize(vec3(-1,-1,-3)) );
gl_FragColor.rgb = color.rgb * intensity;
gl_FragColor.a = color.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) {
//setUniformValue(getUniform("M"), m);
}
void Shader::setViewMatrix(const QMatrix4x4& m) {
//setUniformValue(getUniform("V"), m);
}
void Shader::setProjectionMatrix(const QMatrix4x4& m) {
//setUniformValue(getUniform("P"), m);
}
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) {
const int loc = getAttribute("a_normal");
enableAttributeArray(loc);
setAttributeArray(loc, GL_FLOAT, values, 3);
}
void Shader::unsetNormals() {
const int loc = getAttribute("a_normal");
disableAttributeArray(loc);
}