worked on 3D display

some ui changes
refactoring
new icons
This commit is contained in:
2018-02-03 23:30:55 +01:00
parent e5e19779d5
commit 3b62f23c0e
21 changed files with 1054 additions and 765 deletions

View File

@@ -6,27 +6,41 @@ 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 normal;
varying highp vec3 v_normal;
varying lowp vec4 v_color;
varying lowp vec4 v_vertex;
void main() {
gl_Position = vec4(a_vertex, 1.0);
normal = normalize( V*M*vec4(a_normal, 0.0) );
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 vec4 color;
varying highp vec3 normal;
uniform bool useNormal;
uniform bool useVertexColor;
varying highp vec3 v_normal;
varying lowp vec4 v_color;
varying lowp vec4 v_vertex;
void main() {
float intensity = dot( normal, normalize(vec3(-1,-1,-3)) );
gl_FragColor.rgb = color.rgb * intensity;
gl_FragColor.a = color.a;
vec3 lightPos = vec3(0,0,0); // camera
//vec3 lightVec = normalize(vec3(0,0,1));
vec3 lightVec = normalize(lightPos - v_vertex.xyz);
float intensity = useNormal ? 0.3 + 0.7 * dot( normalize(v_normal), lightVec ) : 1.0; // light at camera pos
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");
@@ -36,15 +50,25 @@ Shader::Shader() {
void Shader::setModelMatrix(const QMatrix4x4& m) {
//setUniformValue(getUniform("M"), m);
setUniformValue(getUniform("M"), m);
}
void Shader::setViewMatrix(const QMatrix4x4& m) {
//setUniformValue(getUniform("V"), m);
setUniformValue(getUniform("V"), m);
}
void Shader::setProjectionMatrix(const QMatrix4x4& m) {
//setUniformValue(getUniform("P"), 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) {
@@ -52,7 +76,6 @@ int Shader::getUniform(const char* 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");}
@@ -78,11 +101,25 @@ void Shader::unsetVertices() {
}
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);
}