2017-11-02 03:44:44 +01:00
|
|
|
#include "SkyBox.h"
|
|
|
|
#include "Shader.h"
|
|
|
|
|
|
|
|
#include <glad/glad.h>
|
|
|
|
#include <stb_image.h>
|
2017-11-03 03:00:32 +01:00
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
2017-11-02 03:44:44 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2017-11-03 00:22:38 +01:00
|
|
|
SkyBox::SkyBox() :
|
2017-11-03 03:00:32 +01:00
|
|
|
textureId(GL_INVALID_INDEX), programId(GL_INVALID_INDEX), textureLoc(-1) {
|
|
|
|
|
2017-11-02 03:44:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SkyBox::~SkyBox() {
|
|
|
|
glDeleteTextures(1, &textureId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkyBox::Load(const std::string basename) {
|
2017-11-03 03:00:32 +01:00
|
|
|
glGenTextures(1, &textureId);
|
|
|
|
|
|
|
|
float extent = 10;
|
|
|
|
mesh.positions.clear();
|
|
|
|
mesh.positions.push_back(glm::vec3(-extent, -extent, -extent));
|
|
|
|
mesh.positions.push_back(glm::vec3(-extent, -extent, extent));
|
|
|
|
mesh.positions.push_back(glm::vec3(extent, -extent, extent));
|
|
|
|
mesh.positions.push_back(glm::vec3(extent, -extent, -extent));
|
|
|
|
|
|
|
|
mesh.positions.push_back(glm::vec3(-extent, extent, -extent));
|
|
|
|
mesh.positions.push_back(glm::vec3(-extent, extent, extent));
|
|
|
|
mesh.positions.push_back(glm::vec3(extent, extent, extent));
|
|
|
|
mesh.positions.push_back(glm::vec3(extent, extent, -extent));
|
|
|
|
|
|
|
|
mesh.indices.clear();
|
|
|
|
// bottom
|
|
|
|
mesh.indices.push_back(glm::uvec3(1, 0, 2));
|
|
|
|
mesh.indices.push_back(glm::uvec3(2, 0, 3));
|
|
|
|
// back
|
|
|
|
mesh.indices.push_back(glm::uvec3(5, 2, 6));
|
|
|
|
mesh.indices.push_back(glm::uvec3(1, 2, 5));
|
|
|
|
// front
|
|
|
|
mesh.indices.push_back(glm::uvec3(3, 4, 7));
|
|
|
|
mesh.indices.push_back(glm::uvec3(3, 0, 4));
|
|
|
|
// left
|
|
|
|
mesh.indices.push_back(glm::uvec3(6, 3, 7));
|
|
|
|
mesh.indices.push_back(glm::uvec3(2, 3, 6));
|
|
|
|
// right
|
|
|
|
mesh.indices.push_back(glm::uvec3(0, 1, 4));
|
|
|
|
mesh.indices.push_back(glm::uvec3(4, 1, 5));
|
|
|
|
// top
|
|
|
|
mesh.indices.push_back(glm::uvec3(5, 6, 4));
|
|
|
|
mesh.indices.push_back(glm::uvec3(6, 7, 4));
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-02 03:44:44 +01:00
|
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
|
|
|
|
|
2017-11-03 03:00:32 +01:00
|
|
|
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_X, "assets/textures/" + basename + "_right1.jpg");
|
|
|
|
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, "assets/textures/" + basename + "_left2.jpg");
|
|
|
|
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, "assets/textures/" + basename + "_top3.jpg");
|
|
|
|
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, "assets/textures/" + basename + "_bottom4.jpg");
|
|
|
|
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, "assets/textures/" + basename + "_front5.jpg");
|
|
|
|
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, "assets/textures/" + basename + "_back6.jpg");
|
2017-11-02 03:44:44 +01:00
|
|
|
|
|
|
|
programId = loadProgram("skybox");
|
|
|
|
|
2017-11-03 03:00:32 +01:00
|
|
|
GLCK(textureLoc = glGetAttribLocation(programId, "tCubemap"))
|
|
|
|
GLCK(MVPloc = glGetUniformLocation(programId, "MVP"))
|
|
|
|
|
|
|
|
glMesh.create();
|
|
|
|
glMesh.update(programId);
|
|
|
|
glMesh.upload(mesh);
|
|
|
|
|
2017-11-02 03:44:44 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkyBox::Load(GLuint target, const std::string filename) {
|
|
|
|
int x, y, comp;
|
2017-11-03 03:00:32 +01:00
|
|
|
printf ("start\n");
|
|
|
|
unsigned char *data = stbi_load(filename.c_str(), &x, &y, &comp, 0);
|
|
|
|
printf ("end\n");
|
2017-11-02 03:44:44 +01:00
|
|
|
|
2017-11-03 03:00:32 +01:00
|
|
|
glTexImage2D(target, 0, GL_RGB, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
2017-11-02 03:44:44 +01:00
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
stbi_image_free(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-03 03:00:32 +01:00
|
|
|
void SkyBox::Render(glm::mat4 mvp) {
|
2017-11-02 03:44:44 +01:00
|
|
|
GLint OldCullFaceMode;
|
|
|
|
glGetIntegerv(GL_CULL_FACE_MODE, &OldCullFaceMode);
|
|
|
|
GLint OldDepthFuncMode;
|
|
|
|
glGetIntegerv(GL_DEPTH_FUNC, &OldDepthFuncMode);
|
|
|
|
|
|
|
|
glCullFace(GL_FRONT);
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
2017-11-03 00:22:38 +01:00
|
|
|
/*
|
|
|
|
Pipeline p;
|
|
|
|
p.Scale(20.0f, 20.0f, 20.0f);
|
|
|
|
p.Rotate(0.0f, 0.0f, 0.0f);
|
|
|
|
p.WorldPos(m_pCamera->GetPos().x, m_pCamera->GetPos().y,
|
|
|
|
m_pCamera->GetPos().z);
|
|
|
|
p.SetCamera(m_pCamera->GetPos(), m_pCamera->GetTarget(),
|
|
|
|
m_pCamera->GetUp());
|
|
|
|
p.SetPerspectiveProj(m_persProjInfo);
|
|
|
|
m_pSkyboxTechnique->SetWVP(p.GetWVPTrans());
|
|
|
|
m_pCubemapTex->Bind(GL_TEXTURE0);
|
|
|
|
*/
|
2017-11-03 03:00:32 +01:00
|
|
|
|
|
|
|
GLCK(glUseProgram(programId));
|
|
|
|
mvp[3] = glm::vec4(0, 0, 0, 1);
|
|
|
|
GLCK(glUniformMatrix4fv(MVPloc, 1, GL_FALSE, glm::value_ptr(mvp)))
|
|
|
|
|
|
|
|
GLCK(glUniform1i(textureLoc, 0))
|
|
|
|
GLCK(glActiveTexture(GL_TEXTURE0))
|
|
|
|
GLCK(glBindTexture(GL_TEXTURE_CUBE_MAP, textureId))
|
|
|
|
|
|
|
|
glMesh.render();
|
2017-11-02 03:44:44 +01:00
|
|
|
glCullFace(OldCullFaceMode);
|
|
|
|
glDepthFunc(OldDepthFuncMode);
|
|
|
|
}
|