79 lines
2.1 KiB
C++
79 lines
2.1 KiB
C++
#include "SkyBox.h"
|
|
#include "Shader.h"
|
|
|
|
#include <glad/glad.h>
|
|
#include <stb_image.h>
|
|
|
|
using namespace std;
|
|
|
|
SkyBox::SkyBox() {
|
|
glGenTextures(1, &textureId);
|
|
}
|
|
|
|
SkyBox::~SkyBox() {
|
|
glDeleteTextures(1, &textureId);
|
|
}
|
|
|
|
bool SkyBox::Load(const std::string basename) {
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
|
|
|
|
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_X, basename + "_right1.png");
|
|
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, basename + "_left2.png");
|
|
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, basename + "_top3.png");
|
|
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, basename + "_bottom4.png");
|
|
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, basename + "_front5.png");
|
|
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, basename + "_back6.png");
|
|
|
|
programId = loadProgram("skybox");
|
|
|
|
return true;
|
|
}
|
|
|
|
bool SkyBox::Load(GLuint target, const std::string filename) {
|
|
int x, y, comp;
|
|
unsigned char *data = stbi_load(filename.c_str(), &x, &y, &comp, 4);
|
|
|
|
glTexImage2D(target, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
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;
|
|
}
|
|
|
|
void SkyBox::Render() {
|
|
GLint OldCullFaceMode;
|
|
glGetIntegerv(GL_CULL_FACE_MODE, &OldCullFaceMode);
|
|
GLint OldDepthFuncMode;
|
|
glGetIntegerv(GL_DEPTH_FUNC, &OldDepthFuncMode);
|
|
|
|
glCullFace(GL_FRONT);
|
|
glDepthFunc(GL_LEQUAL);
|
|
/*
|
|
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);
|
|
m_pMesh->Render();
|
|
*/
|
|
glCullFace(OldCullFaceMode);
|
|
glDepthFunc(OldDepthFuncMode);
|
|
}
|
|
|
|
void SkyBox::Bind(GLenum unit) {
|
|
glActiveTexture(unit);
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
|
|
}
|