Better phase shift and ambient light

This commit is contained in:
Riku Avelar 2016-11-10 00:59:04 -05:00
parent ba769e6ed3
commit e7c2dc3e63
4 changed files with 33 additions and 5 deletions

View File

@ -3,6 +3,7 @@ uniform vec3 lDirection;
uniform bool isSky; uniform bool isSky;
uniform bool isPhong; uniform bool isPhong;
uniform sampler2D tex; uniform sampler2D tex;
uniform float skyMult;
in vec3 fNormal; in vec3 fNormal;
in vec3 fPosition; in vec3 fPosition;
@ -16,10 +17,10 @@ main()
{ {
vec4 texColor = texture(tex, texCoords); vec4 texColor = texture(tex, texCoords);
if(isSky) { if(isSky) {
fColor = normalize(texColor*texColor*texColor*2/1.41)*2; fColor = skyMult * normalize(texColor*texColor*texColor*2/1.41)*2;
} else if(isPhong) { } else if(isPhong) {
// Get lighting vectors // Get lighting vectors
vec3 LightDirection = normalize(vec3(1,3,1)); vec3 LightDirection = normalize(lDirection);
vec3 nfNormal = normalize(fNormal); vec3 nfNormal = normalize(fNormal);
vec3 nviewDirection = normalize(fPosition); vec3 nviewDirection = normalize(fPosition);
@ -30,8 +31,8 @@ main()
vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection)); vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection));
float specular = pow(max(0.0, dot(Rl, nviewDirection)), 16); float specular = pow(max(0.0, dot(Rl, nviewDirection)), 16);
// Fragment color // Fragment color : diffused + specular + ambient
vec4 color = texColor * diffuse * 0.6 + vec4(1) * specular * 0.6; vec4 color = texColor * diffuse * 0.6 + vec4(1) * specular * 0.6 + texColor * 0.1;
// Compute final color // Compute final color
fColor = vec4(color.xyz, 1); fColor = vec4(color.xyz, 1);

View File

@ -32,7 +32,7 @@ main()
if(!isPhong && !isSky) { if(!isPhong && !isSky) {
// Get lighting vectors // Get lighting vectors
vec3 LightDirection = normalize(vec3(1,3,1)); vec3 LightDirection = normalize(lDirection);
vec3 nfNormal = normalize(fNormal); vec3 nfNormal = normalize(fNormal);
vec3 nviewDirection = normalize(fPosition); vec3 nviewDirection = normalize(fPosition);

View File

@ -24,6 +24,7 @@
#include "../interfaces/visitor.h" #include "../interfaces/visitor.h"
#include "../glnodes/scenegroup.h" #include "../glnodes/scenegroup.h"
#include "../glnodes/shapes.h" #include "../glnodes/shapes.h"
#include <math.h>
#include <qopengl.h> #include <qopengl.h>
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
@ -48,6 +49,9 @@ namespace
int m_lDirectionLocation; int m_lDirectionLocation;
int m_normalMatrixLoc; int m_normalMatrixLoc;
QVector3D sun = QVector3D(0,-1,0);
QMatrix4x4 sunRotate;
} }
class SkyboxCamera : public qglviewer::Camera class SkyboxCamera : public qglviewer::Camera
@ -61,6 +65,7 @@ Viewer::Viewer()
activeColor = new QColor(255, 255, 255, 255); activeColor = new QColor(255, 255, 255, 255);
activeCell = nullptr; activeCell = nullptr;
activeShape = 0; activeShape = 0;
angle_mult = 0.1;
} }
Viewer::~Viewer() Viewer::~Viewer()
@ -99,7 +104,9 @@ void Viewer::drawSkybox()
modelViewMatrix.scale(101); modelViewMatrix.scale(101);
float colorMult = 0.2 + std::fabs(0.8 * cos(std::fmod(angle_mult * frame + 300, 360) / 360 * M_PI));
m_program->setUniformValue(m_skyMultLoc, colorMult);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
m_program->setUniformValue(m_isSkyLoc, true); m_program->setUniformValue(m_isSkyLoc, true);
@ -141,11 +148,22 @@ void Viewer::draw()
m_program->setUniformValue(m_projMatrixLocation, projectionMatrix); m_program->setUniformValue(m_projMatrixLocation, projectionMatrix);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
// Adjust sun position
float rotAngle = std::fmod(angle_mult * frame, 360);
sunRotate.rotate(rotAngle, 1, 0, 0);
m_program->setUniformValue(m_lDirLoc, (sun * sunRotate));
// Traverse the Scene in order to draw its components // Traverse the Scene in order to draw its components
modelStack.push(modelViewMatrix); modelStack.push(modelViewMatrix);
root.accept(*this); root.accept(*this);
frame++; frame++;
sunRotate.setToIdentity();
//float rotAngle = (frame * angle_mult) % 360;
update(); update();
} }
@ -212,6 +230,9 @@ void Viewer::init()
initShaders(); initShaders();
initGeometries(); initGeometries();
sunRotate.rotate(-15,0,0,1);
sun = sun * sunRotate;
{ {
Shape* cube = new Cube(); Shape* cube = new Cube();
cube->setColor(*activeColor); cube->setColor(*activeColor);
@ -281,6 +302,9 @@ void Viewer::initShaders()
if ((m_lDirLoc = m_program->uniformLocation("lDirection")) < 0) if ((m_lDirLoc = m_program->uniformLocation("lDirection")) < 0)
qDebug() << "Unable to find m_shader location for" << "lDirection"; qDebug() << "Unable to find m_shader location for" << "lDirection";
if ((m_skyMultLoc = m_program->uniformLocation("skyMult")) < 0)
qDebug() << "Unable to find m_shader location for" << "skyMult";
/* /*
* Adding Texture Shader * Adding Texture Shader

View File

@ -96,6 +96,9 @@ private:
int m_isPhongLoc; int m_isPhongLoc;
int m_isSkyLoc; int m_isSkyLoc;
int m_lDirLoc; int m_lDirLoc;
int m_skyMultLoc;
float angle_mult;
QOpenGLTexture *s_texture; QOpenGLTexture *s_texture;