fucking around with lighting shaders and the speed of sound

This commit is contained in:
Dmitri K 2016-11-18 17:51:49 -05:00
parent b985770871
commit 489578ba10
3 changed files with 285 additions and 276 deletions

View File

@ -19,20 +19,21 @@ out vec4 fColor;
vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) { vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm) {
// Get lighting vectors // Get lighting vectors
vec3 LightDirection = normalize(lDirection); vec3 LightDirection = normalize(lDirection);
vec3 nfNormal = normalize(fNorm); vec3 nfNormal = normalize(fNorm);
vec3 nviewDirection = normalize(fPos); vec3 nviewDirection = normalize(fPos);
// Compute diffuse component // Compute diffuse component
float diff = 0.4 * max(0.0, dot(nfNormal, LightDirection)); float diff = 0.4*max(0.0, dot(nfNormal, -LightDirection));
// Compute specular component // Compute specular component
vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection)); //vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection));
float spec = 0.4*pow(max(0.0, dot(Rl, nviewDirection)), 16); vec3 Rl = reflect(LightDirection, fNorm);
float spec = pow(max(0.0, dot(Rl, nviewDirection)), 64);
// Compute ambient component // Compute ambient component
float amb = 0.02; float amb = 0.2;
//return vec4(0); //return vec4(0);
return vec4(tex.xyz * (amb + diff + spec), tex.w); return vec4(tex.xyz * (amb + diff + spec), tex.w);
@ -86,10 +87,10 @@ main()
vec3 nfNormal = normalize(fNormal); vec3 nfNormal = normalize(fNormal);
vec3 nviewDirection = normalize(fPosition); vec3 nviewDirection = normalize(fPosition);
fColor = calcDirLight(texColor, fPosition, fNormal) fColor = calcDirLight(texColor, fPosition, fNormal);
+ calcPointLight(texColor, fPosition, fNormal, 0)/4 //+ calcPointLight(texColor, fPosition, fNormal, 0)/4
+ calcPointLight(texColor, fPosition, fNormal, 1)/4 //+ calcPointLight(texColor, fPosition, fNormal, 1)/4
+ calcPointLight(texColor, fPosition, fNormal, 2)/4; //+ calcPointLight(texColor, fPosition, fNormal, 2)/4;
} else { } else {
fColor = texColor + ifColor; fColor = texColor + ifColor;
} }

View File

@ -24,7 +24,7 @@ out vec3 fNormal;
vec4 calcDirLight(vec4 eye, vec3 fPos, vec3 fNorm) { vec4 calcDirLight(vec4 eye, vec3 fPos, vec3 fNorm) {
// Get lighting vectors // Get lighting vectors
vec3 LightDirection = normalize(lDirection); vec3 LightDirection = normalize(lDirection);
vec3 nfNormal = normalize(fNorm); vec3 nfNormal = normalize(fNorm - fPos);
vec3 nviewDirection = normalize(fPos); vec3 nviewDirection = normalize(fPos);
// Compute diffuse component // Compute diffuse component

View File

@ -49,20 +49,20 @@ namespace
const double inc_mult = 5; const double inc_mult = 5;
const double inc_offset = 1.05; const double inc_offset = 1.05;
const int numRowSphere = 20; const int numRowSphere = 20;
const int numColSphere = numRowSphere+2; const int numColSphere = numRowSphere+2;
const int numVerticesSphere = numColSphere * numRowSphere + 2; const int numVerticesSphere = numColSphere * numRowSphere + 2;
const int numTriSphere = numColSphere*(numRowSphere-1)*2 + 2*numColSphere; const int numTriSphere = numColSphere*(numRowSphere-1)*2 + 2*numColSphere;
int m_lDirectionLocation; int m_lDirectionLocation;
int m_normalMatrixLoc; int m_normalMatrixLoc;
QVector3D sun = QVector3D(0,-1,0); QVector3D sun = QVector3D(0,-1,0);
QMatrix4x4 sunRotate; 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 class SkyboxCamera : public qglviewer::Camera
@ -73,10 +73,10 @@ class SkyboxCamera : public qglviewer::Camera
Viewer::Viewer() 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; angle_mult = 0.1;
} }
Viewer::~Viewer() Viewer::~Viewer()
@ -101,26 +101,26 @@ void Viewer::cleanup()
void Viewer::drawSkybox() void Viewer::drawSkybox()
{ {
// Use the Skybox Shaders // Use the Skybox Shaders
m_program->bind(); m_program->bind();
s_texture->bind(); s_texture->bind();
// Get projection and camera transformations // Get projection and camera transformations
QMatrix4x4 projectionMatrix; QMatrix4x4 projectionMatrix;
QMatrix4x4 modelViewMatrix; QMatrix4x4 modelViewMatrix;
camera()->getProjectionMatrix(projectionMatrix); camera()->getProjectionMatrix(projectionMatrix);
camera()->getModelViewMatrix(modelViewMatrix); camera()->getModelViewMatrix(modelViewMatrix);
// Increase size of skybox // Increase size of skybox
modelViewMatrix.scale(100); 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_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);
m_program->setUniformValue(m_drawTextLoc, true); m_program->setUniformValue(m_drawTextLoc, true);
m_program->setUniformValue(m_isLightLoc, false); m_program->setUniformValue(m_isLightLoc, false);
m_program->setUniformValue(m_colorLocation, *(new QColor(0, 0, 0, 0))); m_program->setUniformValue(m_colorLocation, *(new QColor(0, 0, 0, 0)));
glCullFace( GL_FRONT ); glCullFace( GL_FRONT );
@ -136,7 +136,7 @@ void Viewer::draw()
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
if(!isPickingActivated) if(!isPickingActivated)
drawSkybox(); drawSkybox();
glCullFace( GL_BACK ); glCullFace( GL_BACK );
@ -146,28 +146,28 @@ void Viewer::draw()
camera()->getProjectionMatrix(projectionMatrix); camera()->getProjectionMatrix(projectionMatrix);
camera()->getModelViewMatrix(modelViewMatrix); 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_projMatrixLocation, projectionMatrix);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
// Adjust sun position // Adjust sun position
float rotAngle = std::fmod(angle_mult * frame, 360); float rotAngle = std::fmod(angle_mult * frame, 360);
sunRotate.rotate(rotAngle, 1, 0, 0); sunRotate.rotate(rotAngle, 0, 0, 1);
m_program->setUniformValue(m_lDirLoc, (modelViewMatrix * sunRotate * sun)); m_program->setUniformValue(m_lDirLoc, (modelViewMatrix * sunRotate * sun));
selection->transform.setToIdentity(); selection->transform.setToIdentity();
selection->transform.rotate(rotAngle, 0, 1, 0); selection->transform.rotate(rotAngle, 0, 1, 0);
// 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 += 2; frame += 20;
sunRotate.setToIdentity(); sunRotate.setToIdentity();
//float rotAngle = (frame * angle_mult) % 360; //float rotAngle = (frame * angle_mult) % 360;
update(); update();
} }
@ -184,7 +184,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
int y = this->parentWidget()->height() - e->y() - 23; int y = this->parentWidget()->height() - e->y() - 23;
std::cout << "--------------------------------------------------\nPicking shape at " << x << " (" << this->x() << " + " << e->x() << "), " << y << endl; 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); QMatrix4x4 selectedPosition = pickGeom(x, y);
if(!selectedPosition.isIdentity()) if(!selectedPosition.isIdentity())
@ -196,8 +196,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
container->transform = selectedPosition; container->transform = selectedPosition;
container->addChild(c); container->addChild(c);
root.addChild(container); root.addChild(container);
} } else QGLViewer::mousePressEvent(e);
QGLViewer::mousePressEvent(e);
} }
void Viewer::mouseReleaseEvent(QMouseEvent* e) { void Viewer::mouseReleaseEvent(QMouseEvent* e) {
@ -206,20 +205,20 @@ void Viewer::mouseReleaseEvent(QMouseEvent* e) {
m_program->bind(); m_program->bind();
m_program->setUniformValue(m_isPickingModeLoc, false); m_program->setUniformValue(m_isPickingModeLoc, false);
isPickingActivated = false; isPickingActivated = false;
QGLViewer::mouseReleaseEvent(e); QGLViewer::mouseReleaseEvent(e);
} }
void Viewer::init() void Viewer::init()
{ {
identityMatrix.setToIdentity(); identityMatrix.setToIdentity();
SkyboxCamera *_cam = new SkyboxCamera(); SkyboxCamera *_cam = new SkyboxCamera();
setCamera(_cam); setCamera(_cam);
//camera()->setType(qglviewer::Camera::PERSPECTIVE); //camera()->setType(qglviewer::Camera::PERSPECTIVE);
//setMouseBinding(Qt::NoModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE); //setMouseBinding(Qt::NoModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE);
//setMouseBinding(Qt::AltModifier, Qt::LeftButton, CAMERA, NO_MOUSE_ACTION); //setMouseBinding(Qt::AltModifier, Qt::LeftButton, CAMERA, NO_MOUSE_ACTION);
//setMouseBinding(Qt::NoModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), 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::ControlModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION);
//setMouseBinding(Qt::ShiftModifier, 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). // Our scene will be from -5 to 5 in X and Y (the grid will be 10x10).
setSceneRadius(5); setSceneRadius(5);
@ -234,8 +233,8 @@ void Viewer::init()
initGeometries(); initGeometries();
initBuffers(); initBuffers();
sunRotate.rotate(-15,0,0,1); sunRotate.rotate(-15,0,0,1);
sun = sunRotate * sun; sun = sunRotate * sun;
{ {
@ -259,15 +258,15 @@ void Viewer::init()
root.addChild(selection); root.addChild(selection);
s1->transform.rotate(360 * 1/3,0,1,0); 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->transform.scale(0.05);
s1->setColor(*c1); s1->setColor(*c1);
s2->transform.rotate(360 * 2/3,0,1,0); 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->transform.scale(0.05);
s2->setColor(*c2); s2->setColor(*c2);
s3->transform.rotate(360 * 3/3,0,1,0); 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->transform.scale(0.05);
s3->setColor(*c3); s3->setColor(*c3);
@ -279,24 +278,10 @@ void Viewer::init()
cube->setColor(*c1); cube->setColor(*c1);
cube->setType(1); cube->setType(1);
sc->addChild(cube); sc->addChild(cube);
sc->transform.translate(-5+i, -5, -5+j); sc->transform.translate(-5+i, 0, -5+j);
root.addChild(sc); 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 // Specify shader input paramters
// The strings "vPosition", "mvMatrix", etc. have to match an attribute name in the vertex shader. // The strings "vPosition", "mvMatrix", etc. have to match an attribute name in the vertex shader.
if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0)
qDebug() << "Unable to find shader location for " << "vPosition"; qDebug() << "Unable to find shader location for " << "vPosition";
if ((m_colorLocation = m_program->uniformLocation("color")) < 0) if ((m_colorLocation = m_program->uniformLocation("color")) < 0)
qDebug() << "Unable to find shader location for " << "color"; qDebug() << "Unable to find shader location for " << "color";
if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0) if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "mvMatrix"; qDebug() << "Unable to find shader location for " << "mvMatrix";
if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "projMatrix"; qDebug() << "Unable to find shader location for " << "projMatrix";
if ((m_lDirectionLocation = m_program->uniformLocation("lDirection")) < 0) if ((m_lDirectionLocation = 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_normalMatrixLoc = m_program->uniformLocation("normalMatrix")) < 0) if ((m_normalMatrixLoc = m_program->uniformLocation("normalMatrix")) < 0)
qDebug() << "Unable to find m_shader location for" << "normalMatrix"; qDebug() << "Unable to find m_shader location for" << "normalMatrix";
if ((m_vNormalLocation = m_program->attributeLocation("vNormal")) < 0) if ((m_vNormalLocation = m_program->attributeLocation("vNormal")) < 0)
qDebug() << "Unable to find m_shader location for" << "vNormal"; qDebug() << "Unable to find m_shader location for" << "vNormal";
if ((s_vUvLocation = m_program->attributeLocation("vUv")) < 0) if ((s_vUvLocation = m_program->attributeLocation("vUv")) < 0)
qDebug() << "Unable to find shader location for " << "vUv"; qDebug() << "Unable to find shader location for " << "vUv";
if ((m_isSkyLoc = m_program->uniformLocation("isSky")) < 0) if ((m_isSkyLoc = m_program->uniformLocation("isSky")) < 0)
qDebug() << "Unable to find m_shader location for" << "isSky"; qDebug() << "Unable to find m_shader location for" << "isSky";
if ((m_isPhongLoc = m_program->uniformLocation("isPhong")) < 0) if ((m_isPhongLoc = m_program->uniformLocation("isPhong")) < 0)
qDebug() << "Unable to find m_shader location for" << "isPhong"; qDebug() << "Unable to find m_shader location for" << "isPhong";
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) if ((m_skyMultLoc = m_program->uniformLocation("skyMult")) < 0)
qDebug() << "Unable to find m_shader location for" << "skyMult"; qDebug() << "Unable to find m_shader location for" << "skyMult";
if ((m_drawTextLoc = m_program->uniformLocation("drawTextures")) < 0) if ((m_drawTextLoc = m_program->uniformLocation("drawTextures")) < 0)
qDebug() << "Unable to find m_shader location for" << "drawTextures"; qDebug() << "Unable to find m_shader location for" << "drawTextures";
if ((m_isLightLoc = m_program->uniformLocation("isLightSource")) < 0) if ((m_isLightLoc = m_program->uniformLocation("isLightSource")) < 0)
qDebug() << "Unable to find m_shader location for" << "isLightSource"; qDebug() << "Unable to find m_shader location for" << "isLightSource";
if ((m_point1Loc = m_program->uniformLocation("pointLight[0]")) < 0) if ((m_point1Loc = m_program->uniformLocation("pointLight[0]")) < 0)
qDebug() << "Unable to find m_shader location for" << "pointLight[0]"; qDebug() << "Unable to find m_shader location for" << "pointLight[0]";
if ((m_point2Loc = m_program->uniformLocation("pointLight[1]")) < 0) if ((m_point2Loc = m_program->uniformLocation("pointLight[1]")) < 0)
qDebug() << "Unable to find m_shader location for" << "pointLight[1]"; qDebug() << "Unable to find m_shader location for" << "pointLight[1]";
if ((m_point3Loc = m_program->uniformLocation("pointLight[2]")) < 0) if ((m_point3Loc = m_program->uniformLocation("pointLight[2]")) < 0)
qDebug() << "Unable to find m_shader location for" << "pointLight[2]"; qDebug() << "Unable to find m_shader location for" << "pointLight[2]";
if ((m_c1Loc = m_program->uniformLocation("pointLightCol[0]")) < 0) if ((m_c1Loc = m_program->uniformLocation("pointLightCol[0]")) < 0)
qDebug() << "Unable to find m_shader location for" << "pointLightCol[0]"; qDebug() << "Unable to find m_shader location for" << "pointLightCol[0]";
if ((m_c2Loc = m_program->uniformLocation("pointLightCol[1]")) < 0) if ((m_c2Loc = m_program->uniformLocation("pointLightCol[1]")) < 0)
qDebug() << "Unable to find m_shader location for" << "pointLightCol[1]"; qDebug() << "Unable to find m_shader location for" << "pointLightCol[1]";
if ((m_c3Loc = m_program->uniformLocation("pointLightCol[2]")) < 0) if ((m_c3Loc = m_program->uniformLocation("pointLightCol[2]")) < 0)
qDebug() << "Unable to find m_shader location for" << "pointLightCol[2]"; qDebug() << "Unable to find m_shader location for" << "pointLightCol[2]";
if ((m_isPickingModeLoc = m_program->uniformLocation("isPickingMode")) < 0) if ((m_isPickingModeLoc = m_program->uniformLocation("isPickingMode")) < 0)
qDebug() << "Unable to find m_shader location for" << "isPickingMode" << m_program->log(); qDebug() << "Unable to find m_shader location for" << "isPickingMode" << m_program->log();
m_program->setUniformValue(m_isPhongLoc, true); m_program->setUniformValue(m_isPhongLoc, true);
m_program->setUniformValue(m_drawTextLoc, false); 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/skybox.jpg"));/*/
s_texture = new QOpenGLTexture(QImage("src/data/uvLayoutGrid.png"));//*/ s_texture = new QOpenGLTexture(QImage("src/data/uvLayoutGrid.png"));//*/
s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
s_texture->setMagnificationFilter(QOpenGLTexture::Linear); s_texture->setMagnificationFilter(QOpenGLTexture::Linear);
// load remaining textures // load remaining textures
for(int i = 0; i<TEX_LENGTH; i++){ for(int i = 0; i<TEX_LENGTH; i++){
@ -422,25 +407,25 @@ void Viewer::initGeometries()
// Create our pentagone object, store its vertices on the graphic card, and // Create our pentagone object, store its vertices on the graphic card, and
// bind the data to the vPosition attribute of the shader // bind the data to the vPosition attribute of the shader
GLfloat verticesCube[numVerticesCube][3] = { // 12 triangles == 6 squares GLfloat verticesCube[numVerticesCube][3] = { // 12 triangles == 6 squares
//Front, if Z is towards us //Front, if Z is towards us
{ -0.5, 0.5, 0.5 }, { -0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, { -0.5, 0.5, 0.5 }, { -0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.5 },
{ 0.5, -0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { -0.5, 0.5, 0.5 }, { 0.5, -0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { -0.5, 0.5, 0.5 },
//Back //Back
{ -0.5, 0.5, -0.5 }, { 0.5, 0.5, -0.5 }, { 0.5, -0.5, -0.5 }, { -0.5, 0.5, -0.5 }, { 0.5, 0.5, -0.5 }, { 0.5, -0.5, -0.5 },
{ 0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, 0.5, -0.5 }, { 0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, 0.5, -0.5 },
//Right //Right
{ -0.5, 0.5, 0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, 0.5, 0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, -0.5, -0.5 },
{ -0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }, { -0.5, 0.5, 0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }, { -0.5, 0.5, 0.5 },
//Left //Left
{ 0.5, 0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, 0.5, -0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, 0.5, -0.5 },
{ 0.5, -0.5, -0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, -0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, -0.5, 0.5 },
//Top //Top
{ -0.5, 0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, 0.5, -0.5 }, { -0.5, 0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, 0.5, -0.5 },
{ 0.5, 0.5, -0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, 0.5, 0.5 }, { 0.5, 0.5, -0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, 0.5, 0.5 },
//Bottom //Bottom
{ -0.5, -0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, -0.5, 0.5 }, { -0.5, -0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, -0.5, 0.5 },
{ 0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }, { -0.5, -0.5, -0.5 } { 0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }, { -0.5, -0.5, -0.5 }
}; };
GLfloat uvs[numVerticesCube][2] = { GLfloat uvs[numVerticesCube][2] = {
@ -462,161 +447,184 @@ void Viewer::initGeometries()
{ .25, .50 }, { .49, .75 }, { .49, .50 }, { .25, .50 }, { .49, .75 }, { .49, .50 },
{ .49, .75 }, { .25, .50 }, { .25, .75 }, { .49, .75 }, { .25, .50 }, { .25, .75 },
}; };
GLfloat normals[numVerticesCube][3] = { GLfloat normals[numVerticesCube][3] = {
{ 0.0, 0.0, 1.0 }, {0.0, 0.0, 1.0}, {0.0, 0.0, 1.0}, { 0.0, 0.0, 1.0 }, {0.0, 0.0, 1.0}, {0.0, 0.0, 1.0},
{ 0.0, 0.0, 1.0 }, {0.0, 0.0, 1.0}, {0.0, 0.0, 1.0}, { 0.0, 0.0, 1.0 }, {0.0, 0.0, 1.0}, {0.0, 0.0, 1.0},
{ 0.0, 0.0, -1.0 }, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0}, { 0.0, 0.0, -1.0 }, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0},
{ 0.0, 0.0, -1.0 }, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0}, { 0.0, 0.0, -1.0 }, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0},
{ -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, { -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0},
{ -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, { -1.0, 0.0, 0.0 }, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0},
{ 1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, { 1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0},
{ 1.0, 0.0, 0.0 }, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, { 1.0, 0.0, 0.0 }, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0},
{ 0.0, 1.0, 0.0 }, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, { 0.0, 1.0, 0.0 }, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
{ 0.0, 1.0, 0.0 }, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, { 0.0, 1.0, 0.0 }, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
{ 0.0, -1.0, 0.0 }, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0}, { 0.0, -1.0, 0.0 }, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0},
{ 0.0, -1.0, 0.0 }, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0} { 0.0, -1.0, 0.0 }, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0}
}; };
GLfloat sphereVertices[numVerticesSphere][3]; GLfloat sphereVertices[numVerticesSphere][3];
GLfloat sphereNormals[numVerticesSphere][3]; GLfloat sphereNormals[numVerticesSphere][3];
GLuint sphereIndices[numTriSphere * 3][3]; GLuint sphereIndices[numTriSphere * 3][3];
// Create Sphere // Create Sphere
// Generate surrounding vertices // Generate surrounding vertices
unsigned int v = 0; unsigned int v = 0;
float thetaInc = 2.0f*3.14159265f / static_cast<float>(numColSphere); float thetaInc = 2.0f*3.14159265f / static_cast<float>(numColSphere);
float phiInc = 3.14159265f / static_cast<float>(numRowSphere+1); float phiInc = 3.14159265f / static_cast<float>(numRowSphere+1);
for (int row=0; row<numRowSphere; ++row) for (int row=0; row<numRowSphere; ++row)
{ {
float phi = 3.14159265f - (static_cast<float>(row+1) * phiInc); float phi = 3.14159265f - (static_cast<float>(row+1) * phiInc);
for (int col=0; col<numColSphere; ++col, ++v) for (int col=0; col<numColSphere; ++col, ++v)
{ {
float theta = col*thetaInc; float theta = col*thetaInc;
sphereVertices[v][0] = 0.5*sin(theta)*sin(phi); sphereVertices[v][0] = 0.5*sin(theta)*sin(phi);
sphereVertices[v][1] = 0.5*cos(phi); sphereVertices[v][1] = 0.5*cos(phi);
sphereVertices[v][2] = 0.5*cos(theta)*sin(phi); sphereVertices[v][2] = 0.5*cos(theta)*sin(phi);
sphereNormals[v][0] = sphereVertices[v][0]*2.0f; // Multiply by 2 because sphere radius is 0.5 sphereNormals[v][0] = sphereVertices[v][0]*2.0f; // Multiply by 2 because sphere radius is 0.5
sphereNormals[v][1] = sphereVertices[v][1]*2.0f; sphereNormals[v][1] = sphereVertices[v][1]*2.0f;
sphereNormals[v][2] = sphereVertices[v][2]*2.0f; sphereNormals[v][2] = sphereVertices[v][2]*2.0f;
} }
} }
// Generate cap vertices // Generate cap vertices
sphereVertices[numColSphere*numRowSphere+0][0] = 0.0f; sphereVertices[numColSphere*numRowSphere+0][0] = 0.0f;
sphereVertices[numColSphere*numRowSphere+0][1] = -0.5f; sphereVertices[numColSphere*numRowSphere+0][1] = -0.5f;
sphereVertices[numColSphere*numRowSphere+0][2] = 0.0f; sphereVertices[numColSphere*numRowSphere+0][2] = 0.0f;
sphereVertices[numColSphere*numRowSphere+1][0] = 0.0f; sphereVertices[numColSphere*numRowSphere+1][0] = 0.0f;
sphereVertices[numColSphere*numRowSphere+1][1] = 0.5f; sphereVertices[numColSphere*numRowSphere+1][1] = 0.5f;
sphereVertices[numColSphere*numRowSphere+1][2] = 0.0f; sphereVertices[numColSphere*numRowSphere+1][2] = 0.0f;
sphereNormals[numColSphere*numRowSphere+0][0] = 0.0f; sphereNormals[numColSphere*numRowSphere+0][0] = 0.0f;
sphereNormals[numColSphere*numRowSphere+0][1] = -1.0f; sphereNormals[numColSphere*numRowSphere+0][1] = -1.0f;
sphereNormals[numColSphere*numRowSphere+0][2] = 0.0f; sphereNormals[numColSphere*numRowSphere+0][2] = 0.0f;
sphereNormals[numColSphere*numRowSphere+1][0] = 0.0f; sphereNormals[numColSphere*numRowSphere+1][0] = 0.0f;
sphereNormals[numColSphere*numRowSphere+1][1] = 1.0f; sphereNormals[numColSphere*numRowSphere+1][1] = 1.0f;
sphereNormals[numColSphere*numRowSphere+1][2] = 0.0f; sphereNormals[numColSphere*numRowSphere+1][2] = 0.0f;
// Generate surrounding indices (faces) // Generate surrounding indices (faces)
unsigned int tri = 0; unsigned int tri = 0;
for (int row=0; row<numRowSphere-1; ++row) for (int row=0; row<numRowSphere-1; ++row)
{ {
unsigned int rowStart = row*numColSphere; unsigned int rowStart = row*numColSphere;
unsigned int topRowStart = rowStart + numColSphere; unsigned int topRowStart = rowStart + numColSphere;
for (int col=0; col<numColSphere; ++col, tri += 2) for (int col=0; col<numColSphere; ++col, tri += 2)
{ {
// Compute quad vertices // Compute quad vertices
unsigned int v = rowStart + col; unsigned int v = rowStart + col;
unsigned int vi = (col<numColSphere-1) ? v+1 : rowStart; unsigned int vi = (col<numColSphere-1) ? v+1 : rowStart;
unsigned int vj = topRowStart + col; unsigned int vj = topRowStart + col;
unsigned int vji = (col<numColSphere-1) ? vj+1 : topRowStart; unsigned int vji = (col<numColSphere-1) ? vj+1 : topRowStart;
// Add to indices // Add to indices
sphereIndices[tri+0][0] = v; sphereIndices[tri+0][0] = v;
sphereIndices[tri+0][1] = vi; sphereIndices[tri+0][1] = vi;
sphereIndices[tri+0][2] = vj; sphereIndices[tri+0][2] = vj;
sphereIndices[tri+1][0] = vi; sphereIndices[tri+1][0] = vi;
sphereIndices[tri+1][1] = vji; sphereIndices[tri+1][1] = vji;
sphereIndices[tri+1][2] = vj; sphereIndices[tri+1][2] = vj;
} }
} }
// Generate cap indices (faces) // Generate cap indices (faces)
for (int col=0; col<numColSphere; ++col, tri += 2) for (int col=0; col<numColSphere; ++col, tri += 2)
{ {
sphereIndices[tri+0][0] = numColSphere*numRowSphere; sphereIndices[tri+0][0] = numColSphere*numRowSphere;
sphereIndices[tri+0][1] = (col<numColSphere-1) ? col+1 : 0; sphereIndices[tri+0][1] = (col<numColSphere-1) ? col+1 : 0;
sphereIndices[tri+0][2] = col; sphereIndices[tri+0][2] = col;
unsigned int rowStart = (numRowSphere-1)*numColSphere; unsigned int rowStart = (numRowSphere-1)*numColSphere;
sphereIndices[tri+1][0] = numColSphere*numRowSphere+1; sphereIndices[tri+1][0] = numColSphere*numRowSphere+1;
sphereIndices[tri+1][1] = rowStart + col; sphereIndices[tri+1][1] = rowStart + col;
sphereIndices[tri+1][2] = (col<numColSphere-1) ? (rowStart + col + 1) : rowStart; sphereIndices[tri+1][2] = (col<numColSphere-1) ? (rowStart + col + 1) : rowStart;
} }
// Prep buffers // Prep buffers
glGenBuffers(NumBuffers, m_Buffers); glGenBuffers(NumBuffers, m_Buffers);
// Fill cube vertex VBO // Fill cube vertex VBO
GLsizeiptr offsetVertices = 0;
GLsizeiptr offsetNormals = sizeof(verticesCube);
GLsizeiptr offsetUV = offsetNormals + sizeof(normals);
GLsizeiptr dataSize = offsetUV + sizeof(uvs); GLsizeiptr offsetVertices = 0;
GLsizeiptr offsetNormals = sizeof(verticesCube);
GLsizeiptr offsetUV = offsetNormals + sizeof(normals);
GLsizeiptr dataSize = offsetUV + sizeof(uvs);
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
glBufferData(GL_ARRAY_BUFFER, dataSize, glBufferData(GL_ARRAY_BUFFER, dataSize,
NULL, GL_STATIC_DRAW); NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, offsetVertices, sizeof(verticesCube), verticesCube); glBufferSubData(GL_ARRAY_BUFFER, offsetVertices, sizeof(verticesCube), verticesCube);
glBufferSubData(GL_ARRAY_BUFFER, offsetNormals, sizeof(normals), normals); glBufferSubData(GL_ARRAY_BUFFER, offsetNormals, sizeof(normals), normals);
glBufferSubData(GL_ARRAY_BUFFER, offsetUV, sizeof(uvs), uvs); glBufferSubData(GL_ARRAY_BUFFER, offsetUV, sizeof(uvs), uvs);
glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(m_vPositionLocation); glEnableVertexAttribArray(m_vPositionLocation);
glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(offsetNormals)); glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(offsetNormals));
glEnableVertexAttribArray(m_vNormalLocation); glEnableVertexAttribArray(m_vNormalLocation);
glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offsetUV)); glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offsetUV));
glEnableVertexAttribArray(s_vUvLocation); glEnableVertexAttribArray(s_vUvLocation);
// Fill Sphere VBO /*GLsizeiptr offsetVertices = 0;
GLsizeiptr offsetNormals = sizeof(verticesCube);
GLsizeiptr offsetUV = offsetNormals + sizeof(normals);
GLsizeiptr dataSize = offsetUV + sizeof(uvs);
GLsizeiptr offsetSphereV = 0; glBindVertexArray(m_VAOs[VAO_Cube]);
GLsizeiptr offsetSphereN = offsetSphereV + sizeof(sphereVertices); glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
GLsizeiptr sphereDataSize = offsetSphereN + sizeof(sphereNormals); glBufferData(GL_ARRAY_BUFFER, dataSize, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, offsetVertices, sizeof(verticesCube), verticesCube);
glBufferSubData(GL_ARRAY_BUFFER, offsetNormals, sizeof(normals), normals);
glBufferSubData(GL_ARRAY_BUFFER, offsetUV, sizeof(uvs), uvs);
glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(m_vPositionLocation);
glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(sizeof(offsetNormals)));
glEnableVertexAttribArray(m_vNormalLocation);
glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(offsetUV)));
glEnableVertexAttribArray(s_vUvLocation);//*/
// Fill Sphere VBO
glBindVertexArray(m_VAOs[VAO_Sphere]); GLsizeiptr offsetSphereV = 0;
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Sphere]); GLsizeiptr offsetSphereN = offsetSphereV + sizeof(sphereVertices);
glBufferData(GL_ARRAY_BUFFER, sphereDataSize, GLsizeiptr sphereDataSize = offsetSphereN + sizeof(sphereNormals);
NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, offsetSphereV, sizeof(sphereVertices), sphereVertices);
glBufferSubData(GL_ARRAY_BUFFER, offsetSphereN, sizeof(sphereNormals), sphereNormals);
glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glBindVertexArray(m_VAOs[VAO_Sphere]);
glEnableVertexAttribArray(m_vPositionLocation); glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Sphere]);
glBufferData(GL_ARRAY_BUFFER, sphereDataSize,
NULL, GL_STATIC_DRAW);
glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(offsetNormals)); glBufferSubData(GL_ARRAY_BUFFER, offsetSphereV, sizeof(sphereVertices), sphereVertices);
glEnableVertexAttribArray(m_vNormalLocation); glBufferSubData(GL_ARRAY_BUFFER, offsetSphereN, sizeof(sphereNormals), sphereNormals);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Buffers[EBO_Sphere]); glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphereIndices), sphereIndices, GL_STATIC_DRAW); glEnableVertexAttribArray(m_vPositionLocation);
glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(offsetNormals));
glEnableVertexAttribArray(m_vNormalLocation);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Buffers[EBO_Sphere]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphereIndices), sphereIndices, GL_STATIC_DRAW);
} }
void Viewer::initBuffers(){ void Viewer::initBuffers(){
@ -631,7 +639,7 @@ void Viewer::initBuffers(){
void Viewer::visit(Cube &s) void Viewer::visit(Cube &s)
{ {
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
QColor* faceColor = new QColor; QColor* faceColor = new QColor;
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
@ -657,7 +665,7 @@ void Viewer::visit(Cube &s)
void Viewer::visit(Sphere &s) void Viewer::visit(Sphere &s)
{ {
// std::cout << "Sphere found"; // std::cout << "Sphere found";
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
if(!isPickingActivated){ if(!isPickingActivated){
@ -711,10 +719,10 @@ void Viewer::visit(SceneGroup &s)
void Viewer::setPhong(bool on) { void Viewer::setPhong(bool on) {
m_program->bind(); m_program->bind();
m_program->setUniformValue(m_isPhongLoc, on); m_program->setUniformValue(m_isPhongLoc, on);
if(on) std::cout << "Phong ON\n"; if(on) std::cout << "Phong ON\n";
else std::cout << "Phong OFF\n"; else std::cout << "Phong OFF\n";
this->update(); this->update();
} }
void Viewer::changeColor(QColor c){ void Viewer::changeColor(QColor c){
@ -776,7 +784,7 @@ QMatrix4x4 Viewer::pickGeom(int x, int y){
root.accept(*this); root.accept(*this);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// for debugging purposes // for debugging purposes
/*unsigned char pdata[512][512][4]; /*unsigned char pdata[512][512][4];
glReadPixels(x-256, y-256, 512, 512, GL_RGBA, GL_UNSIGNED_BYTE, pdata); glReadPixels(x-256, y-256, 512, 512, GL_RGBA, GL_UNSIGNED_BYTE, pdata);
QImage *im = new QImage(512, 512, QImage::Format_RGB32); 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) 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; std::cout<<"--- Color under cursor: " << (int)pdata[j][i][0] <<" "<< (int)pdata[j][i][1] <<" "<< (int)pdata[j][i][2] << endl;
pickedColor->setAlpha(255); pickedColor->setAlpha(255);
im->setPixelColor(i, j, pickedColor->rgb()); im->setPixelColor(i, j, pickedColor->rgb());
} }
} }
im->save("./screenshot.bmp");//*/ im->save("./screenshot.bmp");//*/
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData); glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);