From b0169b94a590c990944710dd13a72db711a3f8bc Mon Sep 17 00:00:00 2001 From: Riku Avelar Date: Fri, 25 Nov 2016 19:51:03 -0500 Subject: [PATCH] Rotation --- src/shaders/basicShader.frag | 20 ++++++++++---------- src/shaders/basicShader.vert | 2 +- src/viewer/simpleViewer.cpp | 31 ++++++++++++++++++++++++++++--- src/viewer/simpleViewer.h | 12 ++++++++++++ 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/shaders/basicShader.frag b/src/shaders/basicShader.frag index f4c656c..ee6f271 100644 --- a/src/shaders/basicShader.frag +++ b/src/shaders/basicShader.frag @@ -22,20 +22,20 @@ out vec4 fColor; vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) { // Get lighting vectors vec3 LightDirection = normalize(lDirection); - vec3 nfNormal = normalize(fNorm); + vec3 nfNormal = normalize(fNorm); vec3 nviewDirection = normalize(fPos); // Compute diffuse component - float diff = 0.2*max(0.0, dot(nfNormal, -LightDirection)); + float diff = 0.2*max(0.0, dot(nfNormal, -LightDirection)); // Compute specular component - vec3 Rl = reflect(LightDirection, fNorm); - float spec = 0.2*pow(max(0.0, dot(normalMatrix * Rl, nviewDirection)), 64); + vec3 Rl = reflect(LightDirection, nfNormal); + float spec = 0.2*pow(max(0.0, dot(/*normalMatrix */ Rl, nviewDirection)), 64); // Compute ambient component - float amb = 0.2; + float amb = 0.2; - float mult = max(0.0, -LightDirection.y+1.5); + float mult = 1;//max(0.0, -LightDirection.y+1.5); //return vec4(0); return vec4(tex.xyz * (diff + amb + spec) * mult, tex.w); @@ -55,7 +55,7 @@ vec4 calcPointLight(vec4 tex, vec3 fPos, vec3 fNorm, int i) { float diff = 0.3 * max(0.0, dot(nfNormal, LightDirection)); // Compute specular component - vec3 Rl = reflect(-LightDirection, normalMatrix * fNorm); + vec3 Rl = reflect(-LightDirection, /*normalMatrix */ nfNormal); float spec = 0.5 * pow(max(0.0, dot(Rl, nviewDirection)), 32); // Compute ambient component @@ -90,9 +90,9 @@ main() 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; + + calcPointLight(texColor, fPosition, fNormal, 0)/4 + + calcPointLight(texColor, fPosition, fNormal, 1)/4 + + calcPointLight(texColor, fPosition, fNormal, 2)/4; } else { fColor = texColor + ifColor; } diff --git a/src/shaders/basicShader.vert b/src/shaders/basicShader.vert index f56face..ec89d4d 100644 --- a/src/shaders/basicShader.vert +++ b/src/shaders/basicShader.vert @@ -78,7 +78,7 @@ main() vec4 vEyeCoord = mvMatrix * vPosition; gl_Position = projMatrix * vEyeCoord; fPosition = -vEyeCoord.xyz; - fNormal = /*normalMatrix **/ vNormal; + fNormal = normalMatrix * vNormal; texCoords = vUv; diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 825080b..d9ff0df 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -154,11 +154,11 @@ void Viewer::draw() // Adjust sun position - float rotAngle = std::fmod(angle_mult * frame, 360); - sunRotate.rotate(rotAngle, 0, 0, 1); + float rotAngle = std::fmod(angle_mult * 25 * frame, 360); + sunRotate.rotate(rotAngle, 0, 0, 1); //sunRotate.rotate(15, 1, 0, 0); - m_program->setUniformValue(m_lDirLoc, (sunRotate * sun)); + m_program->setUniformValue(m_lDirLoc, (QMatrix4x4(modelViewMatrix.normalMatrix())*sunRotate * sun)); selection->transform.setToIdentity(); selection->transform.rotate(rotAngle, 0, 1, 0); @@ -233,6 +233,8 @@ void Viewer::mousePressEvent(QMouseEvent* e) { selectedObj.shape = selectedGeom.shape; selectedObj.shape->getParent()->addChild(selection); cubeSelected(true); + + rotateSelected(X_CCW); } if(!selectedGeom.position.isIdentity() && e->modifiers().testFlag(Qt::ShiftModifier)) @@ -857,7 +859,30 @@ void Viewer::startAniumation(){} void Viewer::stopAnimation(){} +void Viewer::rotateSelected(RotateDirection dir) { + if(selectedObj.shape == nullptr || selectedObj.shape->getParent() == nullptr) return; + switch(dir) { + case X_CCW: + selectedObj.shape->getParent()->transform.rotate(90, 1, 0, 0); + break; + case X_CW: + selectedObj.shape->getParent()->transform.rotate(-90, 1, 0, 0); + break; + case Y_CCW: + selectedObj.shape->getParent()->transform.rotate(90, 0, 1, 0); + break; + case Y_CW: + selectedObj.shape->getParent()->transform.rotate(-90, 0, 1, 0); + break; + case Z_CCW: + selectedObj.shape->getParent()->transform.rotate(90, 0, 0, 1); + break; + case Z_CW: + selectedObj.shape->getParent()->transform.rotate(-90, 0, 0, 1); + break; + } +} PickedGeom Viewer::pickGeom(int x, int y){ diff --git a/src/viewer/simpleViewer.h b/src/viewer/simpleViewer.h index 5ed413e..c4ba9f5 100644 --- a/src/viewer/simpleViewer.h +++ b/src/viewer/simpleViewer.h @@ -152,6 +152,15 @@ private: Shape* generateShapeFromIndex(int); + enum RotateDirection { + X_CCW, + X_CW, + Y_CCW, + Y_CW, + Z_CCW, + Z_CW + }; + enum Textures{ TEX_DRYGOUND, TEX_GRANITEFLOOR, @@ -162,6 +171,7 @@ private: TEX_LENGTH }; + unsigned int selectedTexture = TEX_WOODFLOOR; QString TexturePaths[TEX_LENGTH] = { @@ -187,6 +197,8 @@ private: QQuaternion rot; double frame; + + void rotateSelected(RotateDirection); }; #endif // SIMPLEVIEWER_H