diff --git a/src/shaders/objShader.frag b/src/shaders/objShader.frag index 6df19bb..e0a632f 100644 --- a/src/shaders/objShader.frag +++ b/src/shaders/objShader.frag @@ -15,50 +15,48 @@ in vec3 fPosition; out vec4 fColor; -vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) { +vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm, vec3 lightDir) { // Get lighting vectors - vec3 LightDirection = normalize(lDirection); + vec3 LightDirection = normalize(lightDir); vec3 nviewDirection = normalize(fPos); - vec3 nfNormal; + vec3 nfNormal = normalize(fNorm); // Compute diffuse component - float diff = 0.2*max(0.0, dot(nfNormal, -LightDirection)); + float diff = max(0.0, dot(nfNormal, -LightDirection)); + vec3 diffV = diff * Kd; // Compute specular component vec3 Rl = reflect(LightDirection, nfNormal); - float spec = 0.2*pow(max(0.0, dot(/*normalMatrix */ Rl, nviewDirection)), 64); + float spec = pow(max(0.0, dot(/*normalMatrix */ Rl, nviewDirection)), Kn); + vec3 specV = spec * Ks; - // Compute ambient component - float amb = 0.2; - - float mult = 1;//max(0.0, -LightDirection.y+1.5); - - //return vec4(0); - return vec4(tex.xyz * (diff + amb + spec) * mult, tex.w); + vec3 totalLight = diffV + specV; + //return vec4(Kd + Ks, 1) + return vec4(totalLight, 1); } vec4 calcPointLight(vec4 tex, vec3 fPos, vec3 fNorm, int i) { // Get lighting vectors vec3 LightDirection = normalize(pointLight[i] + fPos); - // vec3 nfNormal = normalize(fNorm); + vec3 nfNormal = normalize(fNorm); vec3 nviewDirection = normalize(fPos); - vec3 nfNormal; // Attenuation float distance = length(nviewDirection - pointLight[i] - fPos) / 3; float attenuation = 0.5 + 1 / max(0.25, distance * distance); // Compute diffuse component - float diff = 0.3 * max(0.0, dot(nfNormal, LightDirection)); + float diff = max(0.0, dot(nfNormal, LightDirection)); + vec3 diffV = diff * Kd; // Compute specular component vec3 Rl = reflect(-LightDirection, /*normalMatrix */ nfNormal); - float spec = 0.5 * pow(max(0.0, dot(Rl, nviewDirection)), 32); + float spec = pow(max(0.0, dot(Rl, nviewDirection)), Kn); + vec3 specV = spec * Ks; // Compute ambient component - float amb = 0.2; - - return vec4(pointLightCol[i].xyz * attenuation * (amb + diff + spec) * tex.xyz, pointLightCol[i].w); + vec3 totalLight = attenuation * (diffV + specV); + return vec4(totalLight, 1); } void @@ -71,9 +69,7 @@ main() vec3 nfNormal = normalize(fNormal); vec3 nviewDirection = normalize(fPosition); - fColor = calcDirLight(texColor, fPosition, fNormal) - + calcPointLight(texColor, fPosition, fNormal, 0)/4 - + calcPointLight(texColor, fPosition, fNormal, 1)/4 - + calcPointLight(texColor, fPosition, fNormal, 2)/4; + fColor = 0.3 * calcDirLight(texColor, fPosition, fNormal, lDirection) + + 0.6 * calcDirLight(texColor, fPosition, fNormal, vec3(0.0)-fPosition); fColor.a = 1.0; } diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 85783bc..3195649 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -885,11 +885,14 @@ void Viewer::drawTool() { // Get projection and camera transformations QMatrix4x4 modelViewMatrix; - camera()->getModelViewMatrix(modelViewMatrix); + //camera()->getModelViewMatrix(modelViewMatrix); QMatrix4x4 scale = QMatrix4x4(); - // scale.translate(QVector3D(0,1.5,0)); - scale.scale(2.0); + //scale.translate(QVector3D(0,1.5,0)); + scale.translate(0.5, -0.25, -2); + scale.rotate(100, 1, 0, 0); + scale.rotate(-90, 0, 1, 0); + scale.scale(0.1); objShader->setUniformValue(o_u_mvMatrix, modelViewMatrix * scale); objShader->setUniformValue(o_u_nmMatrix, modelViewMatrix.normalMatrix());