diff --git a/shaders/DrawGrains.fs b/shaders/DrawGrains.fs index 90d0b49..97a6545 100644 --- a/shaders/DrawGrains.fs +++ b/shaders/DrawGrains.fs @@ -1,6 +1,52 @@ -uniform vec4 color; +#version 330 -void main(void) +// uniforms +uniform vec4 lightPosition; // should be in the eye space +uniform vec4 lightAmbient; // light ambient color +uniform vec4 lightDiffuse; // light diffuse color +uniform vec4 lightSpecular; // light specular color +uniform vec4 materialAmbient; // material ambient color +uniform vec4 materialDiffuse; // material diffuse color +uniform vec4 materialSpecular; // material specular color +uniform float materialShininess; // material specular shininess +uniform bool drawLines; // + +// in +in vec3 esVertex; +in vec3 esNormal; +in vec2 texCoord0; + +// out +out vec4 fragColor; + +void main() { - gl_FragColor = color; + vec3 normal = normalize(esNormal); + vec3 light; + if(lightPosition.w == 0.0) + { + light = normalize(lightPosition.xyz); + } + else + { + light = normalize(lightPosition.xyz - esVertex); + } + vec3 view = normalize(-esVertex); + vec3 reflectVec = reflect(-light, normal); // 2 * N * (N dot L) - L + + vec3 color = lightAmbient.rgb * materialAmbient.rgb; // begin with ambient + float dotNL = max(dot(normal, light), 0.0); + color += lightDiffuse.rgb * materialDiffuse.rgb * dotNL; // add diffuse + float dotVR = max(dot(view, reflectVec), 0.0); + color += pow(dotVR, materialShininess) * lightSpecular.rgb * materialSpecular.rgb; // add specular + fragColor = vec4(color, materialDiffuse.a); // set frag color + + if (drawLines) + { + fragColor = vec4(materialAmbient.rgb * vec3(0.8, 0.8, 0.8), materialDiffuse.a); + } + else + { + fragColor = vec4(materialAmbient.rgb, materialDiffuse.a); + } } diff --git a/shaders/DrawGrains.gs b/shaders/DrawGrains.gs deleted file mode 100644 index ad7ef93..0000000 --- a/shaders/DrawGrains.gs +++ /dev/null @@ -1,26 +0,0 @@ -#version 330 core -layout (points) in; -layout (line_strip, max_vertices = 2) out; - -in Vertex -{ - vec3 semiAxisLengths; -} vertex[]; - -uniform float normalLength; -uniform mat4 modelViewProjectionMatrix; - - -void main(void) -{ - vec3 P = gl_in[0].gl_Position.xyz; - vec3 N = vec3(0., 0., 1.); - - gl_Position = modelViewProjectionMatrix * vec4(P, 1.0); - EmitVertex(); - - gl_Position = modelViewProjectionMatrix * vec4(P + N * 1., 1); - EmitVertex(); - - EndPrimitive(); -} \ No newline at end of file diff --git a/shaders/DrawGrains.gs.old b/shaders/DrawGrains.gs.old deleted file mode 100644 index be9014d..0000000 --- a/shaders/DrawGrains.gs.old +++ /dev/null @@ -1,86 +0,0 @@ -#version 330 core -layout (points) in; -layout (line_strip, max_vertices = 200) out; - -in Vertex -{ - vec3 semiAxisLengths; -} vertex[]; - -uniform float normalLength; -uniform mat4 modelViewProjectionMatrix; - -uniform float cosTheta[21]; -uniform float sinTheta[21]; -uniform float cosPhi[21]; -uniform float sinPhi[21]; - - -void main(void) -{ - vec3 P = gl_in[0].gl_Position.xyz; - //vec3 semiAxisLengths = vertex[0].semiAxisLengths; - vec3 semiAxisLengths = vec3(0.1, 0.1, 0.1); - - float x; - float y; - float z; - - - - int count = 0; - - for (int phi_i = 0; phi_i < 21; phi_i++) - { - if (phi_i == 0) - { - x = P.x; - y = P.y; - z = P.z + semiAxisLengths.z; - gl_Position = modelViewProjectionMatrix * vec4(x, y, z, 1.0); - EmitVertex(); - count++; - if (count == 200) - { - EndPrimitive(); - count = 0; - } - } - else if (phi_i == 20) - { - x = P.x; - y = P.y; - z = P.z - 1.; - gl_Position = modelViewProjectionMatrix * vec4(x, y, z, 1.0); - EmitVertex(); - count++; - if (count == 200) - { - EndPrimitive(); - count = 0; - } - } - else - { - for (int theta_i = 0; theta_i < 21; theta_i++) - { - //x = P.x + semiAxisLengths.x * sinTheta[theta_i] * cosPhi[phi_i]; - //y = P.y + semiAxisLengths.y * sinTheta[theta_i] * sinPhi[phi_i]; - //z = P.z + semiAxisLengths.z * cosTheta[theta_i]; - x = P.x + semiAxisLengths.x * sinTheta[theta_i] * cosPhi[phi_i]; - y = P.y + semiAxisLengths.y * sinTheta[theta_i] * sinPhi[phi_i]; - z = P.z + semiAxisLengths.z * cosTheta[theta_i]; - gl_Position = modelViewProjectionMatrix * vec4(x, y, z, 1.0); - EmitVertex(); - count++; - if (count == 200) - { - EndPrimitive(); - count = 0; - } - } - } - } - - EndPrimitive(); -} \ No newline at end of file diff --git a/shaders/DrawGrains.vs b/shaders/DrawGrains.vs index 7f87732..3078d7b 100644 --- a/shaders/DrawGrains.vs +++ b/shaders/DrawGrains.vs @@ -1,17 +1,26 @@ -attribute vec3 vertexIn; -attribute vec3 semiAxisLengths; - -/*out Vertex -{ - vec3 semiAxisLengths; -} vertex;*/ +#version 330 +// uniforms +uniform mat4 modelViewMatrix; +uniform mat4 normalMatrix; uniform mat4 modelViewProjectionMatrix; uniform vec3 center; +uniform int instanceId; -void main(void) +// in +in vec3 vertexPosition; +in vec3 vertexNormal; +in vec2 vertexTexCoord; + +// out +out vec3 esVertex; +out vec3 esNormal; +out vec2 texCoord0; + +void main() { - gl_PointSize = 5; - gl_Position = modelViewProjectionMatrix * vec4(vertexIn + center, 1.0); - //vertex.semiAxisLengths = semiAxisLengths; + esVertex = vec3(modelViewMatrix * vec4(vertexPosition, 1.0)); + esNormal = vec3(normalMatrix * vec4(vertexNormal, 1.0)); + texCoord0 = vertexTexCoord; + gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0); } diff --git a/shaders/ellipsoid.fs b/shaders/ellipsoid.fs deleted file mode 100644 index 97a6545..0000000 --- a/shaders/ellipsoid.fs +++ /dev/null @@ -1,52 +0,0 @@ -#version 330 - -// uniforms -uniform vec4 lightPosition; // should be in the eye space -uniform vec4 lightAmbient; // light ambient color -uniform vec4 lightDiffuse; // light diffuse color -uniform vec4 lightSpecular; // light specular color -uniform vec4 materialAmbient; // material ambient color -uniform vec4 materialDiffuse; // material diffuse color -uniform vec4 materialSpecular; // material specular color -uniform float materialShininess; // material specular shininess -uniform bool drawLines; // - -// in -in vec3 esVertex; -in vec3 esNormal; -in vec2 texCoord0; - -// out -out vec4 fragColor; - -void main() -{ - vec3 normal = normalize(esNormal); - vec3 light; - if(lightPosition.w == 0.0) - { - light = normalize(lightPosition.xyz); - } - else - { - light = normalize(lightPosition.xyz - esVertex); - } - vec3 view = normalize(-esVertex); - vec3 reflectVec = reflect(-light, normal); // 2 * N * (N dot L) - L - - vec3 color = lightAmbient.rgb * materialAmbient.rgb; // begin with ambient - float dotNL = max(dot(normal, light), 0.0); - color += lightDiffuse.rgb * materialDiffuse.rgb * dotNL; // add diffuse - float dotVR = max(dot(view, reflectVec), 0.0); - color += pow(dotVR, materialShininess) * lightSpecular.rgb * materialSpecular.rgb; // add specular - fragColor = vec4(color, materialDiffuse.a); // set frag color - - if (drawLines) - { - fragColor = vec4(materialAmbient.rgb * vec3(0.8, 0.8, 0.8), materialDiffuse.a); - } - else - { - fragColor = vec4(materialAmbient.rgb, materialDiffuse.a); - } -} diff --git a/shaders/ellipsoid.vs b/shaders/ellipsoid.vs deleted file mode 100644 index 3078d7b..0000000 --- a/shaders/ellipsoid.vs +++ /dev/null @@ -1,26 +0,0 @@ -#version 330 - -// uniforms -uniform mat4 modelViewMatrix; -uniform mat4 normalMatrix; -uniform mat4 modelViewProjectionMatrix; -uniform vec3 center; -uniform int instanceId; - -// in -in vec3 vertexPosition; -in vec3 vertexNormal; -in vec2 vertexTexCoord; - -// out -out vec3 esVertex; -out vec3 esNormal; -out vec2 texCoord0; - -void main() -{ - esVertex = vec3(modelViewMatrix * vec4(vertexPosition, 1.0)); - esNormal = vec3(normalMatrix * vec4(vertexNormal, 1.0)); - texCoord0 = vertexTexCoord; - gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0); -} diff --git a/src/GrainsAsEllipsoids.cpp b/src/GrainsAsEllipsoids.cpp index 3ecb94d..5232f5f 100644 --- a/src/GrainsAsEllipsoids.cpp +++ b/src/GrainsAsEllipsoids.cpp @@ -39,8 +39,7 @@ bool GrainsAsEllipsoids::initProgram(QOpenGLContext* context) m_program.reset(new QOpenGLShaderProgram(context)); // create vertex shader -// QString vertexShaderFile(m_shaderPath + "/DrawGrains.vs"); - QString vertexShaderFile(m_shaderPath + "/ellipsoid.vs"); + QString vertexShaderFile(m_shaderPath + "/DrawGrains.vs"); if (!m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, vertexShaderFile)) { error = m_program->log(); @@ -58,8 +57,7 @@ bool GrainsAsEllipsoids::initProgram(QOpenGLContext* context) // } // create fragment shader -// QString fragmentShaderFile(m_shaderPath + "/DrawGrains.fs"); - QString fragmentShaderFile(m_shaderPath + "/ellipsoid.fs"); + QString fragmentShaderFile(m_shaderPath + "/DrawGrains.fs"); if (!m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, fragmentShaderFile)) { error = m_program->log();