SparrowRenderer/src/skyboxmodule.cpp
2016-01-24 16:50:20 +01:00

151 lines
4.3 KiB
C++

#include <glm/mat4x4.hpp>
#include <glm/mat3x3.hpp>
#include <glm/ext.hpp>
#include "skyboxmodule.h"
#include "shader.h"
#include "texture.h"
#include "camera.h"
#include "framebuffer.h"
#include "glassert.h"
#include "sparrowrenderer.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
SkyboxModule::SkyboxModule(Texture* myCubeMap)
{
cubeMap = myCubeMap;
glAssert(glGenVertexArrays(1, &vao));
glAssert(glBindVertexArray(vao));
glAssert(glGenBuffers(2, vbos));
glAssert(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0]));
glAssert(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLubyte), skyboxIndices, GL_STATIC_DRAW));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbos[1]));
glAssert(glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), skyboxVertices, GL_STATIC_DRAW));
if(SparrowRenderer::isModernOpenGLAvailable())
{
renderTarget = FrameBuffer::screen;
shader = new Shader(vertSource, fragSource);
mvpLocation = shader->getLocation("MVP");
}
glAssert(glBindVertexArray(0));
}
SkyboxModule::~SkyboxModule()
{
if(SparrowRenderer::isModernOpenGLAvailable())
{
glAssert(glDeleteVertexArrays(1, &vao));
glAssert(glDeleteBuffers(2, vbos));
}
}
void SkyboxModule::renderGL(Camera* myCamera, Scene* scene)
{
glm::mat4 viewMatrix = glm::mat4(glm::mat3(myCamera->getViewMatrix()));
glm::mat4 projectionMatrix = myCamera->getProjectionMatrix();
glAssert(glDisable(GL_CULL_FACE));
glAssert(glDepthMask(GL_FALSE));
if(!SparrowRenderer::isModernOpenGLAvailable())
{
glAssert(glMatrixMode(GL_MODELVIEW));
glAssert(glLoadMatrixf(glm::value_ptr(viewMatrix)));
glAssert(glMatrixMode(GL_PROJECTION));
glAssert(glLoadMatrixf(glm::value_ptr(projectionMatrix)));
}
else
{
renderTarget->bindFBO();
shader->bind();
shader->bindMat4(mvpLocation, projectionMatrix * viewMatrix);
}
cubeMap->bind(0);
glAssert(glBindVertexArray(vao));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbos[1]));
if(SparrowRenderer::isModernOpenGLAvailable())
{
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(0));
}
else
{
glAssert(glDisable(GL_LIGHTING));
glAssert(glEnable(GL_TEXTURE_CUBE_MAP));
glAssert(glEnableClientState(GL_VERTEX_ARRAY));
glAssert(glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)));
glAssert(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
glAssert(glTexCoordPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)));
}
glAssert(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0]));
glAssert(glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, NULL));
glAssert(glBindVertexArray(0));
if(!SparrowRenderer::isModernOpenGLAvailable())
{
glAssert(glEnable(GL_LIGHTING));
glAssert(glDisable(GL_TEXTURE_CUBE_MAP));
glAssert(glDisableClientState(GL_VERTEX_ARRAY));
glAssert(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
}
glAssert(glEnable(GL_CULL_FACE));
glAssert(glDepthMask(GL_TRUE));
}
void SkyboxModule::setRenderTarget(const FrameBuffer* target)
{
if(target != NULL)
renderTarget = target;
}
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
};
const GLubyte SkyboxModule::skyboxIndices[] = {
0, 6, 2,
0, 4, 6,
1, 3, 5,
3, 7, 5,
0, 3, 1,
0, 2, 3,
4, 5, 6,
5, 7, 6,
0, 5, 4,
0, 1, 5,
2, 6, 3,
6, 7, 3
};
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";