Beginnings of skybox

This commit is contained in:
Riku Avelar 2016-11-09 16:14:18 -05:00
parent c5fbaac3fd
commit 99d5fb4e24
6 changed files with 124 additions and 26 deletions

View File

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

BIN
src/data/skybox.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 352 KiB

View File

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

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

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

View File

@ -26,6 +26,7 @@
#include <QOpenGLFunctions_4_0_Core>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLTexture>
#include <QMatrix4x4>
#include <QGLViewer/qglviewer.h>
#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;