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) {
// 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;
}

View File

@ -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

View File

@ -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<TEX_LENGTH; i++){
@ -422,25 +407,25 @@ void Viewer::initGeometries()
// Create our pentagone object, store its vertices on the graphic card, and
// bind the data to the vPosition attribute of the shader
GLfloat verticesCube[numVerticesCube][3] = { // 12 triangles == 6 squares
//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 },
//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 },
//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 },
//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 },
//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 },
//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 }
};
//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 },
//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 },
//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 },
//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 },
//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 },
//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 }
};
GLfloat uvs[numVerticesCube][2] = {
@ -462,161 +447,184 @@ void Viewer::initGeometries()
{ .25, .50 }, { .49, .75 }, { .49, .50 },
{ .49, .75 }, { .25, .50 }, { .25, .75 },
};
};
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},
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},
{ -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 sphereNormals[numVerticesSphere][3];
GLuint sphereIndices[numTriSphere * 3][3];
GLfloat sphereVertices[numVerticesSphere][3];
GLfloat sphereNormals[numVerticesSphere][3];
GLuint sphereIndices[numTriSphere * 3][3];
// Create Sphere
// Generate surrounding vertices
unsigned int v = 0;
float thetaInc = 2.0f*3.14159265f / static_cast<float>(numColSphere);
float phiInc = 3.14159265f / static_cast<float>(numRowSphere+1);
for (int row=0; row<numRowSphere; ++row)
{
float phi = 3.14159265f - (static_cast<float>(row+1) * phiInc);
for (int col=0; col<numColSphere; ++col, ++v)
{
float theta = col*thetaInc;
sphereVertices[v][0] = 0.5*sin(theta)*sin(phi);
sphereVertices[v][1] = 0.5*cos(phi);
sphereVertices[v][2] = 0.5*cos(theta)*sin(phi);
// Create Sphere
// Generate surrounding vertices
unsigned int v = 0;
float thetaInc = 2.0f*3.14159265f / static_cast<float>(numColSphere);
float phiInc = 3.14159265f / static_cast<float>(numRowSphere+1);
for (int row=0; row<numRowSphere; ++row)
{
float phi = 3.14159265f - (static_cast<float>(row+1) * phiInc);
for (int col=0; col<numColSphere; ++col, ++v)
{
float theta = col*thetaInc;
sphereVertices[v][0] = 0.5*sin(theta)*sin(phi);
sphereVertices[v][1] = 0.5*cos(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][1] = sphereVertices[v][1]*2.0f;
sphereNormals[v][2] = sphereVertices[v][2]*2.0f;
}
}
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][2] = sphereVertices[v][2]*2.0f;
}
}
// Generate cap vertices
sphereVertices[numColSphere*numRowSphere+0][0] = 0.0f;
sphereVertices[numColSphere*numRowSphere+0][1] = -0.5f;
sphereVertices[numColSphere*numRowSphere+0][2] = 0.0f;
// Generate cap vertices
sphereVertices[numColSphere*numRowSphere+0][0] = 0.0f;
sphereVertices[numColSphere*numRowSphere+0][1] = -0.5f;
sphereVertices[numColSphere*numRowSphere+0][2] = 0.0f;
sphereVertices[numColSphere*numRowSphere+1][0] = 0.0f;
sphereVertices[numColSphere*numRowSphere+1][1] = 0.5f;
sphereVertices[numColSphere*numRowSphere+1][2] = 0.0f;
sphereVertices[numColSphere*numRowSphere+1][0] = 0.0f;
sphereVertices[numColSphere*numRowSphere+1][1] = 0.5f;
sphereVertices[numColSphere*numRowSphere+1][2] = 0.0f;
sphereNormals[numColSphere*numRowSphere+0][0] = 0.0f;
sphereNormals[numColSphere*numRowSphere+0][1] = -1.0f;
sphereNormals[numColSphere*numRowSphere+0][2] = 0.0f;
sphereNormals[numColSphere*numRowSphere+0][0] = 0.0f;
sphereNormals[numColSphere*numRowSphere+0][1] = -1.0f;
sphereNormals[numColSphere*numRowSphere+0][2] = 0.0f;
sphereNormals[numColSphere*numRowSphere+1][0] = 0.0f;
sphereNormals[numColSphere*numRowSphere+1][1] = 1.0f;
sphereNormals[numColSphere*numRowSphere+1][2] = 0.0f;
sphereNormals[numColSphere*numRowSphere+1][0] = 0.0f;
sphereNormals[numColSphere*numRowSphere+1][1] = 1.0f;
sphereNormals[numColSphere*numRowSphere+1][2] = 0.0f;
// Generate surrounding indices (faces)
unsigned int tri = 0;
for (int row=0; row<numRowSphere-1; ++row)
{
unsigned int rowStart = row*numColSphere;
unsigned int topRowStart = rowStart + numColSphere;
// Generate surrounding indices (faces)
unsigned int tri = 0;
for (int row=0; row<numRowSphere-1; ++row)
{
unsigned int rowStart = row*numColSphere;
unsigned int topRowStart = rowStart + numColSphere;
for (int col=0; col<numColSphere; ++col, tri += 2)
{
// Compute quad vertices
unsigned int v = rowStart + col;
unsigned int vi = (col<numColSphere-1) ? v+1 : rowStart;
unsigned int vj = topRowStart + col;
unsigned int vji = (col<numColSphere-1) ? vj+1 : topRowStart;
for (int col=0; col<numColSphere; ++col, tri += 2)
{
// Compute quad vertices
unsigned int v = rowStart + col;
unsigned int vi = (col<numColSphere-1) ? v+1 : rowStart;
unsigned int vj = topRowStart + col;
unsigned int vji = (col<numColSphere-1) ? vj+1 : topRowStart;
// Add to indices
sphereIndices[tri+0][0] = v;
sphereIndices[tri+0][1] = vi;
sphereIndices[tri+0][2] = vj;
sphereIndices[tri+1][0] = vi;
sphereIndices[tri+1][1] = vji;
sphereIndices[tri+1][2] = vj;
}
}
// Add to indices
sphereIndices[tri+0][0] = v;
sphereIndices[tri+0][1] = vi;
sphereIndices[tri+0][2] = vj;
sphereIndices[tri+1][0] = vi;
sphereIndices[tri+1][1] = vji;
sphereIndices[tri+1][2] = vj;
}
}
// Generate cap indices (faces)
for (int col=0; col<numColSphere; ++col, tri += 2)
{
sphereIndices[tri+0][0] = numColSphere*numRowSphere;
sphereIndices[tri+0][1] = (col<numColSphere-1) ? col+1 : 0;
sphereIndices[tri+0][2] = col;
// Generate cap indices (faces)
for (int col=0; col<numColSphere; ++col, tri += 2)
{
sphereIndices[tri+0][0] = numColSphere*numRowSphere;
sphereIndices[tri+0][1] = (col<numColSphere-1) ? col+1 : 0;
sphereIndices[tri+0][2] = col;
unsigned int rowStart = (numRowSphere-1)*numColSphere;
sphereIndices[tri+1][0] = numColSphere*numRowSphere+1;
sphereIndices[tri+1][1] = rowStart + col;
sphereIndices[tri+1][2] = (col<numColSphere-1) ? (rowStart + col + 1) : rowStart;
}
unsigned int rowStart = (numRowSphere-1)*numColSphere;
sphereIndices[tri+1][0] = numColSphere*numRowSphere+1;
sphereIndices[tri+1][1] = rowStart + col;
sphereIndices[tri+1][2] = (col<numColSphere-1) ? (rowStart + col + 1) : rowStart;
}
// Prep buffers
glGenBuffers(NumBuffers, m_Buffers);
// Prep buffers
glGenBuffers(NumBuffers, m_Buffers);
// Fill cube vertex VBO
GLsizeiptr offsetVertices = 0;
GLsizeiptr offsetNormals = sizeof(verticesCube);
GLsizeiptr offsetUV = offsetNormals + sizeof(normals);
// Fill cube vertex VBO
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]);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
glBufferData(GL_ARRAY_BUFFER, dataSize,
NULL, GL_STATIC_DRAW);
glBindVertexArray(m_VAOs[VAO_Cube]);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
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);
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_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(m_vPositionLocation);
glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(offsetNormals));
glEnableVertexAttribArray(m_vNormalLocation);
glVertexAttribPointer(m_vNormalLocation, 3, GL_FLOAT, GL_TRUE, 0, BUFFER_OFFSET(offsetNormals));
glEnableVertexAttribArray(m_vNormalLocation);
glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offsetUV));
glEnableVertexAttribArray(s_vUvLocation);
glVertexAttribPointer(s_vUvLocation, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offsetUV));
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;
GLsizeiptr offsetSphereN = offsetSphereV + sizeof(sphereVertices);
GLsizeiptr sphereDataSize = offsetSphereN + sizeof(sphereNormals);
glBindVertexArray(m_VAOs[VAO_Cube]);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
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]);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Sphere]);
glBufferData(GL_ARRAY_BUFFER, sphereDataSize,
NULL, GL_STATIC_DRAW);
GLsizeiptr offsetSphereV = 0;
GLsizeiptr offsetSphereN = offsetSphereV + sizeof(sphereVertices);
GLsizeiptr sphereDataSize = offsetSphereN + sizeof(sphereNormals);
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));
glEnableVertexAttribArray(m_vPositionLocation);
glBindVertexArray(m_VAOs[VAO_Sphere]);
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));
glEnableVertexAttribArray(m_vNormalLocation);
glBufferSubData(GL_ARRAY_BUFFER, offsetSphereV, sizeof(sphereVertices), sphereVertices);
glBufferSubData(GL_ARRAY_BUFFER, offsetSphereN, sizeof(sphereNormals), sphereNormals);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Buffers[EBO_Sphere]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphereIndices), sphereIndices, GL_STATIC_DRAW);
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(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(){
@ -631,7 +639,7 @@ void Viewer::initBuffers(){
void Viewer::visit(Cube &s)
{
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
QColor* faceColor = new QColor;
glBindVertexArray(m_VAOs[VAO_Cube]);
@ -657,7 +665,7 @@ void Viewer::visit(Cube &s)
void Viewer::visit(Sphere &s)
{
// std::cout << "Sphere found";
// std::cout << "Sphere found";
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
if(!isPickingActivated){
@ -711,10 +719,10 @@ void Viewer::visit(SceneGroup &s)
void Viewer::setPhong(bool on) {
m_program->bind();
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);