SparrowRenderer/skybox.cpp

107 lines
2.8 KiB
C++

#include "skybox.h"
#include "glassert.h"
#include "shader.h"
#include "material.h"
#include "skyboxmaterial.h"
#include "glm/glm.hpp"
#include "glm/ext.hpp"
SkyBox::SkyBox(const std::string filename[6]) : Entity(NULL, NULL, NULL)
{
Shader* shader = new Shader(vertSource, fragSource);
mat = new SkyBoxMaterial(shader, filename);
// set up vao
glAssert(glGenVertexArrays(1, &vao));
glAssert(glBindVertexArray(vao));
glAssert(glGenBuffers(1, &vbo));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo));
glAssert(glBufferData(GL_ARRAY_BUFFER, 108 * sizeof(GLfloat), skyboxVertices, GL_STATIC_DRAW));
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, NULL));
glAssert(glEnableVertexAttribArray(0));
glAssert(glBindVertexArray(0));
}
SkyBox::~SkyBox()
{
glAssert(glDeleteVertexArrays(1, &vao));
glAssert(glDeleteBuffers(1, &vbo));
}
void SkyBox::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
{
glm::mat4 mvp = projectionMatrix * glm::mat4(glm::mat3(viewMatrix));
glAssert(glDepthMask(GL_FALSE));
mat->bindAttributes();
Shader* shader = mat->getShader();
shader->bindMatrix(shader->getLocation("MVP"), mvp);
glAssert(glBindVertexArray(vao));
glAssert(glDrawArrays(GL_TRIANGLES, 0, 36));
glAssert(glDepthMask(GL_TRUE));
}
const GLfloat SkyBox::skyboxVertices[] = {
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f
};
const std::string SkyBox::vertSource =
"#version 330 core\n\
layout(location = 0)in vec3 inPosition;\n\
out vec3 varTexCoord;\n\
uniform mat4 MVP;\n\
void main()\n\
{\n\
gl_Position = MVP * vec4(inPosition, 1.0);\n\
varTexCoord = inPosition;\n\
}\n";
const std::string SkyBox::fragSource =
"#version 330 core\n\
in vec3 varTexCoord;\n\
out vec4 outColor;\n\
uniform samplerCube skybox;\n\
void main()\n\
{\n\
outColor = texture(skybox, varTexCoord);\n\
}\n";