SparrowRenderer/skyboxmodule.cpp
2015-07-28 13:17:14 +02:00

110 lines
2.8 KiB
C++

#include <glm/mat4x4.hpp>
#include <glm/mat3x3.hpp>
#include "skyboxmodule.h"
#include "phongentity.h"
#include "shader.h"
#include "texture.h"
#include "camera.h"
#include "glassert.h"
SkyboxModule::SkyboxModule(Texture* myCubeMap)
{
shader = new Shader(vertSource, fragSource);
cubeMap = myCubeMap;
// 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));
mvpLocation = shader->getLocation("MVP");
}
SkyboxModule::~SkyboxModule()
{
glAssert(glDeleteVertexArrays(1, &vao));
glAssert(glDeleteBuffers(1, &vbo));
}
void SkyboxModule::renderGL(Camera* myCamera)
{
shader->bind();
glm::mat4 mvp = myCamera->getProjectionMatrix() * glm::mat4(glm::mat3(myCamera->getViewMatrix()));
glAssert(glDepthMask(GL_FALSE));
cubeMap->bind(0);
shader->bindMatrix(mvpLocation, mvp);
glAssert(glBindVertexArray(vao));
glAssert(glDrawArrays(GL_TRIANGLES, 0, 36));
glAssert(glDepthMask(GL_TRUE));
}
const GLfloat SkyboxModule::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 SkyboxModule::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 SkyboxModule::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";