diff --git a/src/shaders/basicShader.frag b/src/shaders/basicShader.frag index d22ff4d..57f4c81 100644 --- a/src/shaders/basicShader.frag +++ b/src/shaders/basicShader.frag @@ -19,20 +19,21 @@ out vec4 fColor; vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) { - // Get lighting vectors - vec3 LightDirection = normalize(lDirection); - vec3 nfNormal = normalize(fNorm); - vec3 nviewDirection = normalize(fPos); + // Get lighting vectors + vec3 LightDirection = normalize(lDirection); + vec3 nfNormal = normalize(fNorm); + vec3 nviewDirection = normalize(fPos); - // Compute diffuse component - float diff = 0.4 * max(0.0, dot(nfNormal, LightDirection)); + // Compute diffuse component + float diff = 0.4*max(0.0, dot(nfNormal, -LightDirection)); // Compute specular component - vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection)); - float spec = 0.4*pow(max(0.0, dot(Rl, nviewDirection)), 16); + //vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection)); + vec3 Rl = reflect(LightDirection, fNorm); + float spec = pow(max(0.0, dot(Rl, nviewDirection)), 64); // Compute ambient component - float amb = 0.02; + float amb = 0.2; //return vec4(0); return vec4(tex.xyz * (amb + diff + spec), tex.w); @@ -86,10 +87,10 @@ 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 = calcDirLight(texColor, fPosition, fNormal); + //+ 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 5d8da25..808aa8e 100644 --- a/src/shaders/basicShader.vert +++ b/src/shaders/basicShader.vert @@ -24,7 +24,7 @@ out vec3 fNormal; vec4 calcDirLight(vec4 eye, vec3 fPos, vec3 fNorm) { // Get lighting vectors vec3 LightDirection = normalize(lDirection); - vec3 nfNormal = normalize(fNorm); + vec3 nfNormal = normalize(fNorm - fPos); vec3 nviewDirection = normalize(fPos); // Compute diffuse component diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 6411630..783be11 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -49,20 +49,20 @@ namespace const double inc_mult = 5; const double inc_offset = 1.05; - const int numRowSphere = 20; - const int numColSphere = numRowSphere+2; - const int numVerticesSphere = numColSphere * numRowSphere + 2; - const int numTriSphere = numColSphere*(numRowSphere-1)*2 + 2*numColSphere; + const int numRowSphere = 20; + const int numColSphere = numRowSphere+2; + const int numVerticesSphere = numColSphere * numRowSphere + 2; + const int numTriSphere = numColSphere*(numRowSphere-1)*2 + 2*numColSphere; - int m_lDirectionLocation; - int m_normalMatrixLoc; + int m_lDirectionLocation; + int m_normalMatrixLoc; - QVector3D sun = QVector3D(0,-1,0); - QMatrix4x4 sunRotate; + QVector3D sun = QVector3D(0,-1,0); + QMatrix4x4 sunRotate; - SceneGroup* selection; + SceneGroup* selection; - int currentPoint = 0; // VERY lazy way of tracking light balls + int currentPoint = 0; // VERY lazy way of tracking light balls } class SkyboxCamera : public qglviewer::Camera @@ -73,10 +73,10 @@ class SkyboxCamera : public qglviewer::Camera Viewer::Viewer() { - activeColor = new QColor(255, 255, 255, 255); + activeColor = new QColor(255, 255, 255, 255); activeCell = nullptr; activeShape = 0; - angle_mult = 0.1; + angle_mult = 0.1; } Viewer::~Viewer() @@ -101,26 +101,26 @@ void Viewer::cleanup() void Viewer::drawSkybox() { - // Use the Skybox Shaders + // Use the Skybox Shaders m_program->bind(); s_texture->bind(); - // Get projection and camera transformations - QMatrix4x4 projectionMatrix; - QMatrix4x4 modelViewMatrix; - camera()->getProjectionMatrix(projectionMatrix); - camera()->getModelViewMatrix(modelViewMatrix); + // Get projection and camera transformations + QMatrix4x4 projectionMatrix; + QMatrix4x4 modelViewMatrix; + camera()->getProjectionMatrix(projectionMatrix); + camera()->getModelViewMatrix(modelViewMatrix); - // Increase size of skybox + // Increase size of skybox modelViewMatrix.scale(100); - float colorMult = 0.2 + std::fabs(0.8 * cos(std::fmod(angle_mult * frame + 300, 360) / 360 * M_PI)); + 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_isSkyLoc, true); - m_program->setUniformValue(m_drawTextLoc, true); - m_program->setUniformValue(m_isLightLoc, false); + m_program->setUniformValue(m_skyMultLoc, colorMult); + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + m_program->setUniformValue(m_isSkyLoc, true); + m_program->setUniformValue(m_drawTextLoc, true); + m_program->setUniformValue(m_isLightLoc, false); m_program->setUniformValue(m_colorLocation, *(new QColor(0, 0, 0, 0))); glCullFace( GL_FRONT ); @@ -136,7 +136,7 @@ void Viewer::draw() glClear(GL_COLOR_BUFFER_BIT); if(!isPickingActivated) - drawSkybox(); + drawSkybox(); glCullFace( GL_BACK ); @@ -146,28 +146,28 @@ void Viewer::draw() camera()->getProjectionMatrix(projectionMatrix); camera()->getModelViewMatrix(modelViewMatrix); - //modelViewMatrix.rotate(30,0,1,0); + modelViewMatrix.rotate(30,0,1,0); m_program->setUniformValue(m_projMatrixLocation, projectionMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); - // Adjust sun position + // Adjust sun position - float rotAngle = std::fmod(angle_mult * frame, 360); - sunRotate.rotate(rotAngle, 1, 0, 0); + float rotAngle = std::fmod(angle_mult * frame, 360); + sunRotate.rotate(rotAngle, 0, 0, 1); - m_program->setUniformValue(m_lDirLoc, (modelViewMatrix * sunRotate * sun)); - selection->transform.setToIdentity(); - selection->transform.rotate(rotAngle, 0, 1, 0); + m_program->setUniformValue(m_lDirLoc, (modelViewMatrix * sunRotate * sun)); + selection->transform.setToIdentity(); + selection->transform.rotate(rotAngle, 0, 1, 0); // Traverse the Scene in order to draw its components modelStack.push(modelViewMatrix); - root.accept(*this); - frame += 2; + root.accept(*this); + frame += 20; - sunRotate.setToIdentity(); - //float rotAngle = (frame * angle_mult) % 360; + sunRotate.setToIdentity(); + //float rotAngle = (frame * angle_mult) % 360; update(); } @@ -184,7 +184,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) { int y = this->parentWidget()->height() - e->y() - 23; std::cout << "--------------------------------------------------\nPicking shape at " << x << " (" << this->x() << " + " << e->x() << "), " << y << endl; - std::cout << "Window geom: " << this->window()->size().width() << "w, " << this->window()->size().height() << "h" << endl; + std::cout << "Window geom: " << this->window()->size().width() << "w, " << this->window()->size().height() << "h" << endl; QMatrix4x4 selectedPosition = pickGeom(x, y); if(!selectedPosition.isIdentity()) @@ -196,8 +196,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) { container->transform = selectedPosition; container->addChild(c); root.addChild(container); - } - QGLViewer::mousePressEvent(e); + } else QGLViewer::mousePressEvent(e); } void Viewer::mouseReleaseEvent(QMouseEvent* e) { @@ -206,20 +205,20 @@ void Viewer::mouseReleaseEvent(QMouseEvent* e) { m_program->bind(); m_program->setUniformValue(m_isPickingModeLoc, false); isPickingActivated = false; - QGLViewer::mouseReleaseEvent(e); + QGLViewer::mouseReleaseEvent(e); } void Viewer::init() { identityMatrix.setToIdentity(); - SkyboxCamera *_cam = new SkyboxCamera(); - setCamera(_cam); - //camera()->setType(qglviewer::Camera::PERSPECTIVE); - //setMouseBinding(Qt::NoModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE); - //setMouseBinding(Qt::AltModifier, Qt::LeftButton, CAMERA, NO_MOUSE_ACTION); - //setMouseBinding(Qt::NoModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); - //setMouseBinding(Qt::ControlModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); - //setMouseBinding(Qt::ShiftModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); + SkyboxCamera *_cam = new SkyboxCamera(); + setCamera(_cam); + //camera()->setType(qglviewer::Camera::PERSPECTIVE); + //setMouseBinding(Qt::NoModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE); + //setMouseBinding(Qt::AltModifier, Qt::LeftButton, CAMERA, NO_MOUSE_ACTION); + //setMouseBinding(Qt::NoModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); + //setMouseBinding(Qt::ControlModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); + //setMouseBinding(Qt::ShiftModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); // Our scene will be from -5 to 5 in X and Y (the grid will be 10x10). setSceneRadius(5); @@ -234,8 +233,8 @@ void Viewer::init() initGeometries(); initBuffers(); - sunRotate.rotate(-15,0,0,1); - sun = sunRotate * sun; + sunRotate.rotate(-15,0,0,1); + sun = sunRotate * sun; { @@ -259,15 +258,15 @@ void Viewer::init() root.addChild(selection); s1->transform.rotate(360 * 1/3,0,1,0); - s1->transform.translate(1,0,0); + s1->transform.translate(0.3,1,0); s1->transform.scale(0.05); s1->setColor(*c1); s2->transform.rotate(360 * 2/3,0,1,0); - s2->transform.translate(1,0,0); + s2->transform.translate(0.3,1,0); s2->transform.scale(0.05); s2->setColor(*c2); s3->transform.rotate(360 * 3/3,0,1,0); - s3->transform.translate(1,0,0); + s3->transform.translate(0.3,1,0); s3->transform.scale(0.05); s3->setColor(*c3); @@ -279,24 +278,10 @@ void Viewer::init() cube->setColor(*c1); cube->setType(1); sc->addChild(cube); - sc->transform.translate(-5+i, -5, -5+j); + sc->transform.translate(-5+i, 0, -5+j); root.addChild(sc); } } - /* - Shape* cube1 = new Cube(); - Shape* cube2 = new Cube(); - Shape* cube3 = new Cube(); - cube1->setColor(*c1); - cube2->setColor(*c2); - cube3->setColor(*c3); - cube2->transform.translate(3, 0, 0); - cube3->transform.translate(-3, 0, 0); - //*/ - - // uncomment this and the quaternion transformation in draw() - // to see the cube rotate. This is how we can easily make the sun. - // cube->transform.translate(3, 0, 0); } } @@ -323,68 +308,68 @@ void Viewer::initShaders() // Specify shader input paramters // The strings "vPosition", "mvMatrix", etc. have to match an attribute name in the vertex shader. - if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) - qDebug() << "Unable to find shader location for " << "vPosition"; + if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) + qDebug() << "Unable to find shader location for " << "vPosition"; - if ((m_colorLocation = m_program->uniformLocation("color")) < 0) - qDebug() << "Unable to find shader location for " << "color"; + if ((m_colorLocation = m_program->uniformLocation("color")) < 0) + qDebug() << "Unable to find shader location for " << "color"; - if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0) - qDebug() << "Unable to find shader location for " << "mvMatrix"; + if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0) + qDebug() << "Unable to find shader location for " << "mvMatrix"; - if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) - qDebug() << "Unable to find shader location for " << "projMatrix"; + if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) + qDebug() << "Unable to find shader location for " << "projMatrix"; - if ((m_lDirectionLocation = m_program->uniformLocation("lDirection")) < 0) - qDebug() << "Unable to find m_shader location for" << "lDirection"; + if ((m_lDirectionLocation = m_program->uniformLocation("lDirection")) < 0) + qDebug() << "Unable to find m_shader location for" << "lDirection"; - if ((m_normalMatrixLoc = m_program->uniformLocation("normalMatrix")) < 0) - qDebug() << "Unable to find m_shader location for" << "normalMatrix"; + if ((m_normalMatrixLoc = m_program->uniformLocation("normalMatrix")) < 0) + qDebug() << "Unable to find m_shader location for" << "normalMatrix"; - if ((m_vNormalLocation = m_program->attributeLocation("vNormal")) < 0) - qDebug() << "Unable to find m_shader location for" << "vNormal"; + if ((m_vNormalLocation = m_program->attributeLocation("vNormal")) < 0) + qDebug() << "Unable to find m_shader location for" << "vNormal"; - if ((s_vUvLocation = m_program->attributeLocation("vUv")) < 0) - qDebug() << "Unable to find shader location for " << "vUv"; + if ((s_vUvLocation = m_program->attributeLocation("vUv")) < 0) + qDebug() << "Unable to find shader location for " << "vUv"; - if ((m_isSkyLoc = m_program->uniformLocation("isSky")) < 0) - qDebug() << "Unable to find m_shader location for" << "isSky"; + if ((m_isSkyLoc = m_program->uniformLocation("isSky")) < 0) + qDebug() << "Unable to find m_shader location for" << "isSky"; - if ((m_isPhongLoc = m_program->uniformLocation("isPhong")) < 0) - qDebug() << "Unable to find m_shader location for" << "isPhong"; + if ((m_isPhongLoc = m_program->uniformLocation("isPhong")) < 0) + qDebug() << "Unable to find m_shader location for" << "isPhong"; - if ((m_lDirLoc = m_program->uniformLocation("lDirection")) < 0) - qDebug() << "Unable to find m_shader location for" << "lDirection"; + if ((m_lDirLoc = m_program->uniformLocation("lDirection")) < 0) + 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"; + if ((m_skyMultLoc = m_program->uniformLocation("skyMult")) < 0) + qDebug() << "Unable to find m_shader location for" << "skyMult"; - if ((m_drawTextLoc = m_program->uniformLocation("drawTextures")) < 0) - qDebug() << "Unable to find m_shader location for" << "drawTextures"; + if ((m_drawTextLoc = m_program->uniformLocation("drawTextures")) < 0) + qDebug() << "Unable to find m_shader location for" << "drawTextures"; - if ((m_isLightLoc = m_program->uniformLocation("isLightSource")) < 0) - qDebug() << "Unable to find m_shader location for" << "isLightSource"; + if ((m_isLightLoc = m_program->uniformLocation("isLightSource")) < 0) + qDebug() << "Unable to find m_shader location for" << "isLightSource"; - if ((m_point1Loc = m_program->uniformLocation("pointLight[0]")) < 0) - qDebug() << "Unable to find m_shader location for" << "pointLight[0]"; + if ((m_point1Loc = m_program->uniformLocation("pointLight[0]")) < 0) + qDebug() << "Unable to find m_shader location for" << "pointLight[0]"; - if ((m_point2Loc = m_program->uniformLocation("pointLight[1]")) < 0) - qDebug() << "Unable to find m_shader location for" << "pointLight[1]"; + if ((m_point2Loc = m_program->uniformLocation("pointLight[1]")) < 0) + qDebug() << "Unable to find m_shader location for" << "pointLight[1]"; - if ((m_point3Loc = m_program->uniformLocation("pointLight[2]")) < 0) - qDebug() << "Unable to find m_shader location for" << "pointLight[2]"; + if ((m_point3Loc = m_program->uniformLocation("pointLight[2]")) < 0) + qDebug() << "Unable to find m_shader location for" << "pointLight[2]"; - if ((m_c1Loc = m_program->uniformLocation("pointLightCol[0]")) < 0) - qDebug() << "Unable to find m_shader location for" << "pointLightCol[0]"; + if ((m_c1Loc = m_program->uniformLocation("pointLightCol[0]")) < 0) + qDebug() << "Unable to find m_shader location for" << "pointLightCol[0]"; - if ((m_c2Loc = m_program->uniformLocation("pointLightCol[1]")) < 0) - qDebug() << "Unable to find m_shader location for" << "pointLightCol[1]"; + if ((m_c2Loc = m_program->uniformLocation("pointLightCol[1]")) < 0) + qDebug() << "Unable to find m_shader location for" << "pointLightCol[1]"; - if ((m_c3Loc = m_program->uniformLocation("pointLightCol[2]")) < 0) - qDebug() << "Unable to find m_shader location for" << "pointLightCol[2]"; + if ((m_c3Loc = m_program->uniformLocation("pointLightCol[2]")) < 0) + qDebug() << "Unable to find m_shader location for" << "pointLightCol[2]"; - if ((m_isPickingModeLoc = m_program->uniformLocation("isPickingMode")) < 0) - qDebug() << "Unable to find m_shader location for" << "isPickingMode" << m_program->log(); + if ((m_isPickingModeLoc = m_program->uniformLocation("isPickingMode")) < 0) + qDebug() << "Unable to find m_shader location for" << "isPickingMode" << m_program->log(); m_program->setUniformValue(m_isPhongLoc, true); m_program->setUniformValue(m_drawTextLoc, false); @@ -393,8 +378,8 @@ void Viewer::initShaders() s_texture = new QOpenGLTexture(QImage("src/data/skybox.jpg"));/*/ s_texture = new QOpenGLTexture(QImage("src/data/uvLayoutGrid.png"));//*/ - s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); - s_texture->setMagnificationFilter(QOpenGLTexture::Linear); + s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); + s_texture->setMagnificationFilter(QOpenGLTexture::Linear); // load remaining textures for(int i = 0; i(numColSphere); - float phiInc = 3.14159265f / static_cast(numRowSphere+1); - for (int row=0; row(row+1) * phiInc); - for (int col=0; col(numColSphere); + float phiInc = 3.14159265f / static_cast(numRowSphere+1); + for (int row=0; row(row+1) * phiInc); + for (int col=0; colbind(); - m_program->setUniformValue(m_isPhongLoc, on); - if(on) std::cout << "Phong ON\n"; - else std::cout << "Phong OFF\n"; - this->update(); + m_program->setUniformValue(m_isPhongLoc, on); + if(on) std::cout << "Phong ON\n"; + else std::cout << "Phong OFF\n"; + this->update(); } void Viewer::changeColor(QColor c){ @@ -776,7 +784,7 @@ QMatrix4x4 Viewer::pickGeom(int x, int y){ root.accept(*this); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - // for debugging purposes + // for debugging purposes /*unsigned char pdata[512][512][4]; glReadPixels(x-256, y-256, 512, 512, GL_RGBA, GL_UNSIGNED_BYTE, pdata); QImage *im = new QImage(512, 512, QImage::Format_RGB32); @@ -789,10 +797,10 @@ QMatrix4x4 Viewer::pickGeom(int x, int y){ if(i==256 && j == 256) std::cout<<"--- Color under cursor: " << (int)pdata[j][i][0] <<" "<< (int)pdata[j][i][1] <<" "<< (int)pdata[j][i][2] << endl; pickedColor->setAlpha(255); - im->setPixelColor(i, j, pickedColor->rgb()); - } - } - im->save("./screenshot.bmp");//*/ + im->setPixelColor(i, j, pickedColor->rgb()); + } + } + im->save("./screenshot.bmp");//*/ glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);