diff --git a/log750-lab.pro b/log750-lab.pro index 6d7ba5b..6fa8823 100644 --- a/log750-lab.pro +++ b/log750-lab.pro @@ -52,7 +52,10 @@ HEADERS += QGLViewer/camera.h \ src/glnodes/scenegroup.h DISTFILES += src/shaders/basicShader.vert \ - src/shaders/basicShader.frag + src/shaders/basicShader.frag \ + src/shaders/skyboxshader.frag \ + src/shaders/skyboxshader.vert \ + src/data/skybox.jpg FORMS += QGLViewer/ImageInterface.ui mainwindow.ui diff --git a/src/data/skybox.jpg b/src/data/skybox.jpg new file mode 100644 index 0000000..bc58273 Binary files /dev/null and b/src/data/skybox.jpg differ diff --git a/src/shaders/skyboxshader.frag b/src/shaders/skyboxshader.frag new file mode 100644 index 0000000..3637651 --- /dev/null +++ b/src/shaders/skyboxshader.frag @@ -0,0 +1,10 @@ +#version 400 core +in vec2 texCoords; +out vec4 fColor; + +uniform sampler2D skybox; + +void main() +{ + fColor = textureCube(skybox, texCoords); +} diff --git a/src/shaders/skyboxshader.vert b/src/shaders/skyboxshader.vert new file mode 100644 index 0000000..50e7021 --- /dev/null +++ b/src/shaders/skyboxshader.vert @@ -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; +} + diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 1e25414..2fae4b9 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -74,8 +74,38 @@ void Viewer::cleanup() doneCurrent(); } +void Viewer::drawSkybox() +{ + // Use the Skybox Shaders + skyboxRenderShaderProgram->bind(); + s_texture->bind(); + + // Get projection and camera transformations + QMatrix4x4 projectionMatrix; + QMatrix4x4 modelViewMatrix; + camera()->getProjectionMatrix(projectionMatrix); + camera()->getModelViewMatrix(modelViewMatrix); + + // Increase size of skybox + + modelViewMatrix.scale(100); + + skyboxRenderShaderProgram->setUniformValue(s_projMatrixLocation, projectionMatrix); + skyboxRenderShaderProgram->setUniformValue(s_mvMatrixLocation, modelViewMatrix); +// skyboxRenderShaderProgram->setAttributeValue(s_colorLocation, ); + + + int faces = floor(numVerticesCube/6); + for(int i = 0; i < faces; i++){ // 6 vertexes par face + glBindVertexArray(m_VAOs[VAO_Cube]); + + glDrawArrays(GL_TRIANGLES, i*6, 6); + } +} + void Viewer::draw() { + drawSkybox(); // Bind our vertex/fragment shaders m_program->bind(); @@ -217,32 +247,60 @@ void Viewer::initShaders() qDebug() << "Unable to find shader location for " << "projMatrix"; - /* - * Adding Texture Shader - * - */ - textureRenderShaderprogram = new QOpenGLShaderProgram; - if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/textureShader.vert")) { - cerr << "Unable to load Shader" << endl - << "Log file:" << endl; - qDebug() << textureRenderShaderprogram->log(); - } - if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Fragment, "src/shaders/textureShader.frag")) { - cerr << "Unable to load Shader" << endl - << "Log file:" << endl; - qDebug() << textureRenderShaderprogram->log(); - } + /* + * Adding Texture Shader + * + */ + textureRenderShaderprogram = new QOpenGLShaderProgram; + if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/textureShader.vert")) { + cerr << "Unable to load Shader" << endl + << "Log file:" << endl; + qDebug() << textureRenderShaderprogram->log(); + } + if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Fragment, "src/shaders/textureShader.frag")) { + cerr << "Unable to load Shader" << endl + << "Log file:" << endl; + qDebug() << textureRenderShaderprogram->log(); + } - textureRenderShaderprogram->link(); + textureRenderShaderprogram->link(); - if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) - qDebug() << "Unable to find shader location for " << "vPosition"; + /* + * Adding Skybox Shaders + * + */ + skyboxRenderShaderProgram = new QOpenGLShaderProgram; + if (!skyboxRenderShaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/skyboxshader.vert")) { + cerr << "Unable to load Shader" << endl + << "Log file:" << endl; + qDebug() << skyboxRenderShaderProgram->log(); + } + if (!skyboxRenderShaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, "src/shaders/skyboxshader.frag")) { + cerr << "Unable to load Shader" << endl + << "Log file:" << endl; + qDebug() << skyboxRenderShaderProgram->log(); + } - if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0) - qDebug() << "Unable to find shader location for " << "mvMatrix"; + skyboxRenderShaderProgram->link(); - if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) - qDebug() << "Unable to find shader location for " << "projMatrix"; + if ((s_texCoordsLocation = m_program->attributeLocation("texCoords")) < 0) + qDebug() << "Unable to find shader location for " << "vPosition"; + + if ((s_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) + 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) + 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()); + s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); + s_texture->setMagnificationFilter(QOpenGLTexture::Linear); } // Creates the basic shapes in memory. We only have 3, so we just prep them all in advance. diff --git a/src/viewer/simpleViewer.h b/src/viewer/simpleViewer.h index 3933e09..a1c95dd 100644 --- a/src/viewer/simpleViewer.h +++ b/src/viewer/simpleViewer.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include "../interfaces/visitor.h" @@ -56,7 +57,8 @@ signals: int shapeSelected(int); protected : - virtual void draw(); + virtual void draw(); + virtual void drawSkybox(); virtual void init(); virtual void mouseMoveEvent(QMouseEvent* e); @@ -73,14 +75,24 @@ private: Shape* pickGeom(int, int); // shader switching variables and constants; - QOpenGLShaderProgram *colorPickerShaderProgram; - QOpenGLShaderProgram *textureRenderShaderprogram; + QOpenGLShaderProgram *colorPickerShaderProgram; + QOpenGLShaderProgram *textureRenderShaderprogram; + QOpenGLShaderProgram *skyboxRenderShaderProgram; QOpenGLShaderProgram *m_program; int m_vPositionLocation; int m_colorLocation; int m_projMatrixLocation; int m_mvMatrixLocation; + int s_texCoordsLocation; + int s_vPositionLocation; + int s_mvMatrixLocation; + int s_colorLocation; + int s_projMatrixLocation; + int s_skyboxCubemapLocation; + + QOpenGLTexture *s_texture; + SceneGroup* activeCell; QColor* activeColor; int activeShape;