177 lines
5.2 KiB
C++
177 lines
5.2 KiB
C++
#include <glm/mat4x4.hpp>
|
|
#include <glm/mat3x3.hpp>
|
|
#include <glm/ext.hpp>
|
|
#include "skyboxmodule.h"
|
|
#include "phongentity.h"
|
|
#include "shader.h"
|
|
#include "texture.h"
|
|
#include "camera.h"
|
|
#include "glassert.h"
|
|
#include "sparrowrenderer.h"
|
|
|
|
SkyboxModule::SkyboxModule(Texture* myCubeMap)
|
|
{
|
|
if(SparrowRenderer::isModernOpenGLAvailable())
|
|
{
|
|
shader = new Shader(vertSource, fragSource);
|
|
mvpLocation = shader->getLocation("MVP");
|
|
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));
|
|
}
|
|
else
|
|
{
|
|
displayList = glAssert(glGenLists(1));
|
|
glAssert(glNewList(displayList, GL_COMPILE));
|
|
glAssert(glDisable(GL_LIGHTING));
|
|
glAssert(glEnable(GL_TEXTURE_CUBE_MAP));
|
|
myCubeMap->bind(0);
|
|
drawCube();
|
|
glAssert(glDisable(GL_TEXTURE_CUBE_MAP));
|
|
glAssert(glEnable(GL_LIGHTING));
|
|
glAssert(glEndList());
|
|
}
|
|
}
|
|
|
|
SkyboxModule::~SkyboxModule()
|
|
{
|
|
if(SparrowRenderer::isModernOpenGLAvailable())
|
|
{
|
|
glAssert(glDeleteVertexArrays(1, &vao));
|
|
glAssert(glDeleteBuffers(1, &vbo));
|
|
}
|
|
}
|
|
|
|
void SkyboxModule::renderGL(Camera* myCamera, Scene* scene)
|
|
{
|
|
glm::mat4 viewMatrix = glm::mat4(glm::mat3(myCamera->getViewMatrix()));
|
|
glm::mat4 projectionMatrix = myCamera->getProjectionMatrix();
|
|
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)));
|
|
glAssert(glCallList(displayList));
|
|
}
|
|
else
|
|
{
|
|
shader->bind();
|
|
shader->bindMatrix(mvpLocation, projectionMatrix * viewMatrix);
|
|
}
|
|
cubeMap->bind(0);
|
|
glAssert(glBindVertexArray(vao));
|
|
glAssert(glDrawArrays(GL_TRIANGLES, 0, 36));
|
|
glAssert(glDepthMask(GL_TRUE));
|
|
}
|
|
|
|
void SkyboxModule::drawCube()
|
|
{
|
|
const float t = 1;
|
|
glAssert(glBegin(GL_QUADS));
|
|
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
|
|
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
|
|
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
|
|
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
|
|
|
|
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
|
|
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
|
|
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
|
|
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
|
|
|
|
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
|
|
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
|
|
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
|
|
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
|
|
|
|
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
|
|
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
|
|
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
|
|
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
|
|
|
|
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
|
|
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
|
|
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
|
|
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
|
|
|
|
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
|
|
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
|
|
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
|
|
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
|
|
glAssert(glEnd());
|
|
}
|
|
|
|
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";
|