Working camera

This commit is contained in:
Riku Avelar 2016-11-09 16:52:31 -05:00
parent 99d5fb4e24
commit b1a838d170
5 changed files with 55 additions and 31 deletions

View File

@ -53,9 +53,9 @@ HEADERS += QGLViewer/camera.h \
DISTFILES += src/shaders/basicShader.vert \ DISTFILES += src/shaders/basicShader.vert \
src/shaders/basicShader.frag \ src/shaders/basicShader.frag \
src/shaders/skyboxshader.frag \ src/data/skybox.jpg \
src/shaders/skyboxshader.vert \ src/shaders/skyboxShader.frag \
src/data/skybox.jpg src/shaders/skyboxShader.vert
FORMS += QGLViewer/ImageInterface.ui mainwindow.ui FORMS += QGLViewer/ImageInterface.ui mainwindow.ui

View File

@ -0,0 +1,10 @@
#version 400 core
in vec3 texCoords;
out vec4 fColor;
uniform sampler2D skybox;
void main()
{
fColor = texture(skybox, texCoords.xy);
}

View File

@ -0,0 +1,15 @@
#version 400 core
layout (location = 0) in vec3 position;
uniform mat4 mvMatrix;
uniform mat4 projMatrix;
out vec3 texCoords;
in vec4 vPosition;
void
main()
{
gl_Position = projMatrix * mvMatrix * vPosition;
texCoords = position;
}

View File

@ -1,10 +1,10 @@
#version 400 core #version 400 core
in vec2 texCoords; in vec3 texCoords;
out vec4 fColor; out vec4 fColor;
uniform sampler2D skybox; uniform sampler2D skybox;
void main() void main()
{ {
fColor = textureCube(skybox, texCoords); fColor = texture(skybox, texCoords.xy);
} }

View File

@ -88,13 +88,12 @@ void Viewer::drawSkybox()
// Increase size of skybox // Increase size of skybox
modelViewMatrix.scale(100); //modelViewMatrix.scale(3);
skyboxRenderShaderProgram->setUniformValue(s_projMatrixLocation, projectionMatrix); skyboxRenderShaderProgram->setUniformValue(s_projMatrixLocation, projectionMatrix);
skyboxRenderShaderProgram->setUniformValue(s_mvMatrixLocation, modelViewMatrix); skyboxRenderShaderProgram->setUniformValue(s_mvMatrixLocation, modelViewMatrix);
// skyboxRenderShaderProgram->setAttributeValue(s_colorLocation, ); // skyboxRenderShaderProgram->setAttributeValue(s_colorLocation, );
int faces = floor(numVerticesCube/6); int faces = floor(numVerticesCube/6);
for(int i = 0; i < faces; i++){ // 6 vertexes par face for(int i = 0; i < faces; i++){ // 6 vertexes par face
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
@ -106,6 +105,7 @@ void Viewer::drawSkybox()
void Viewer::draw() void Viewer::draw()
{ {
drawSkybox(); drawSkybox();
// Bind our vertex/fragment shaders // Bind our vertex/fragment shaders
m_program->bind(); m_program->bind();
@ -117,8 +117,7 @@ void Viewer::draw()
// Prepare a transformation stack // Prepare a transformation stack
// stack<QMatrix4x4> modelStack; // stack<QMatrix4x4> modelStack;
modelViewMatrix.scale(0.95);
//modelViewMatrix.rotate(45, 0, 1, 0); //modelViewMatrix.rotate(45, 0, 1, 0);
// uncomment this and the cube translation in init() // uncomment this and the cube translation in init()
@ -134,15 +133,15 @@ void Viewer::draw()
// 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++;
update(); update();
} }
void Viewer::mouseMoveEvent(QMouseEvent* e) { void Viewer::mouseMoveEvent(QMouseEvent* e) {
//cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl; //cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl;
// Normal QGLViewer behavior. // Normal QGLViewer behavior.
//QGLViewer::mouseMoveEvent(e); QGLViewer::mouseMoveEvent(e);
} }
void Viewer::mousePressEvent(QMouseEvent* e) { void Viewer::mousePressEvent(QMouseEvent* e) {
@ -156,6 +155,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
Shape* selectedShape = pickGeom(x, y); Shape* selectedShape = pickGeom(x, y);
// OpenGL coords start at the window's bottom-left, not the frame's. ugh. // OpenGL coords start at the window's bottom-left, not the frame's. ugh.
QGLViewer::mousePressEvent(e);
} }
void Viewer::deselect(){ void Viewer::deselect(){
@ -175,18 +175,17 @@ void Viewer::deselect(){
void Viewer::mouseReleaseEvent(QMouseEvent* e) { void Viewer::mouseReleaseEvent(QMouseEvent* e) {
//cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl; //cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl;
//QGLViewer::mouseReleaseEvent(e); QGLViewer::mouseReleaseEvent(e);
} }
void Viewer::init() void Viewer::init()
{ {
// We want to restrict ourselves to a 2D viewer. //camera()->setType(qglviewer::Camera::PERSPECTIVE);
//camera()->setType(qglviewer::Camera::ORTHOGRAPHIC); //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);
@ -249,8 +248,8 @@ void Viewer::initShaders()
/* /*
* Adding Texture Shader * Adding Texture Shader
*
*/ */
/*
textureRenderShaderprogram = new QOpenGLShaderProgram; textureRenderShaderprogram = new QOpenGLShaderProgram;
if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/textureShader.vert")) { if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/textureShader.vert")) {
cerr << "Unable to load Shader" << endl cerr << "Unable to load Shader" << endl
@ -264,41 +263,41 @@ void Viewer::initShaders()
} }
textureRenderShaderprogram->link(); textureRenderShaderprogram->link();
*/
/* /*
* Adding Skybox Shaders * Adding Skybox Shaders
* *
*/ */
skyboxRenderShaderProgram = new QOpenGLShaderProgram; skyboxRenderShaderProgram = new QOpenGLShaderProgram;
if (!skyboxRenderShaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/skyboxshader.vert")) { if (!skyboxRenderShaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/skyboxShader.vert")) {
cerr << "Unable to load Shader" << endl cerr << "Unable to load Shader" << endl
<< "Log file:" << endl; << "Log file:" << endl;
qDebug() << skyboxRenderShaderProgram->log(); qDebug() << skyboxRenderShaderProgram->log();
} }
if (!skyboxRenderShaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, "src/shaders/skyboxshader.frag")) { if (!skyboxRenderShaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, "src/shaders/skyboxShader.frag")) {
cerr << "Unable to load Shader" << endl cerr << "Unable to load Shader" << endl
<< "Log file:" << endl; << "Log file:" << endl;
qDebug() << skyboxRenderShaderProgram->log(); qDebug() << skyboxRenderShaderProgram->log();
} }
skyboxRenderShaderProgram->link(); skyboxRenderShaderProgram->link();
skyboxRenderShaderProgram->bind();
if ((s_texCoordsLocation = m_program->attributeLocation("texCoords")) < 0) // if ((s_texCoordsLocation = m_program->attributeLocation("texCoords")) < 0)
qDebug() << "Unable to find shader location for " << "vPosition"; // qDebug() << "Unable to find shader location for " << "vPosition";
if ((s_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) if ((s_vPositionLocation = m_program->attributeLocation("vPosition")) < 0)
qDebug() << "Unable to find shader location for " << "vPosition"; qDebug() << "Unable to find shader location for " << "vPosition";
if ((s_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "mvMatrix";
if ((s_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) if ((s_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "projMatrix"; qDebug() << "Unable to find shader location for " << "projMatrix";
if ((s_skyboxCubemapLocation = m_program->uniformLocation("cubemap")) < 0)
qDebug() << "Unable to find shader location for " << "projMatrix";
s_texture = new QOpenGLTexture(QImage("../data/skybox.jpg").mirrored()); if ((s_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "mvMatrix";
s_texture = new QOpenGLTexture(QImage("src/data/skybox.jpg").mirrored());
s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
s_texture->setMagnificationFilter(QOpenGLTexture::Linear); s_texture->setMagnificationFilter(QOpenGLTexture::Linear);
} }