optimize Skybox
This commit is contained in:
@@ -138,6 +138,7 @@ void Skybox::draw(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality) {
|
void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality) {
|
||||||
|
frameid++;
|
||||||
float dayTime = t;
|
float dayTime = t;
|
||||||
DrawContext ctx = pctx.sub();
|
DrawContext ctx = pctx.sub();
|
||||||
ctx.setDepthMask(false);
|
ctx.setDepthMask(false);
|
||||||
@@ -152,7 +153,31 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
|
|||||||
glActiveTexture(GL_TEXTURE1);
|
glActiveTexture(GL_TEXTURE1);
|
||||||
cubemap->bind();
|
cubemap->bind();
|
||||||
shader->use();
|
shader->use();
|
||||||
|
t *= M_PI*2.0f;
|
||||||
|
|
||||||
|
lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f));
|
||||||
|
shader->uniform1i("u_quality", quality);
|
||||||
|
shader->uniform1f("u_mie", mie);
|
||||||
|
shader->uniform1f("u_fog", mie - 1.0f);
|
||||||
|
shader->uniform3f("u_lightDir", lightDir);
|
||||||
|
shader->uniform1f("u_dayTime", dayTime);
|
||||||
|
|
||||||
|
if (glm::abs(mie-prevMie) + glm::abs(t-prevT) >= 0.01) {
|
||||||
|
for (uint face = 0; face < 6; face++) {
|
||||||
|
refreshFace(face, cubemap);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
uint face = frameid % 6;
|
||||||
|
refreshFace(face, cubemap);
|
||||||
|
}
|
||||||
|
prevMie = mie;
|
||||||
|
prevT = t;
|
||||||
|
|
||||||
|
cubemap->unbind();
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Skybox::refreshFace(uint face, Cubemap* cubemap) {
|
||||||
const glm::vec3 xaxs[] = {
|
const glm::vec3 xaxs[] = {
|
||||||
{0.0f, 0.0f, -1.0f},
|
{0.0f, 0.0f, -1.0f},
|
||||||
{0.0f, 0.0f, 1.0f},
|
{0.0f, 0.0f, 1.0f},
|
||||||
@@ -181,23 +206,11 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
|
|||||||
{0.0f, 0.0f, -1.0f},
|
{0.0f, 0.0f, -1.0f},
|
||||||
{0.0f, 0.0f, 1.0f},
|
{0.0f, 0.0f, 1.0f},
|
||||||
};
|
};
|
||||||
t *= M_PI*2.0f;
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, cubemap->getId(), 0);
|
||||||
|
shader->uniform3f("u_xaxis", xaxs[face]);
|
||||||
lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f));
|
shader->uniform3f("u_yaxis", yaxs[face]);
|
||||||
shader->uniform1i("u_quality", quality);
|
shader->uniform3f("u_zaxis", zaxs[face]);
|
||||||
shader->uniform1f("u_mie", mie);
|
mesh->draw();
|
||||||
shader->uniform1f("u_fog", mie - 1.0f);
|
|
||||||
shader->uniform3f("u_lightDir", lightDir);
|
|
||||||
shader->uniform1f("u_dayTime", dayTime);
|
|
||||||
for (uint face = 0; face < 6; face++) {
|
|
||||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, cubemap->getId(), 0);
|
|
||||||
shader->uniform3f("u_xaxis", xaxs[face]);
|
|
||||||
shader->uniform3f("u_yaxis", yaxs[face]);
|
|
||||||
shader->uniform3f("u_zaxis", zaxs[face]);
|
|
||||||
mesh->draw();
|
|
||||||
}
|
|
||||||
cubemap->unbind();
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Skybox::bind() const {
|
void Skybox::bind() const {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ class Shader;
|
|||||||
class Assets;
|
class Assets;
|
||||||
class Camera;
|
class Camera;
|
||||||
class Batch3D;
|
class Batch3D;
|
||||||
|
class Shader;
|
||||||
|
class Cubemap;
|
||||||
class Framebuffer;
|
class Framebuffer;
|
||||||
class DrawContext;
|
class DrawContext;
|
||||||
|
|
||||||
@@ -33,11 +35,16 @@ class Skybox {
|
|||||||
std::unique_ptr<Mesh> mesh;
|
std::unique_ptr<Mesh> mesh;
|
||||||
std::unique_ptr<Batch3D> batch3d;
|
std::unique_ptr<Batch3D> batch3d;
|
||||||
std::vector<skysprite> sprites;
|
std::vector<skysprite> sprites;
|
||||||
|
int frameid = 0;
|
||||||
|
|
||||||
|
float prevMie = -1.0f;
|
||||||
|
float prevT = -1.0f;
|
||||||
|
|
||||||
void drawStars(float angle, float opacity);
|
void drawStars(float angle, float opacity);
|
||||||
void drawBackground(
|
void drawBackground(
|
||||||
const Camera& camera, const Assets& assets, int width, int height
|
const Camera& camera, const Assets& assets, int width, int height
|
||||||
);
|
);
|
||||||
|
void refreshFace(uint face, Cubemap* cubemap);
|
||||||
public:
|
public:
|
||||||
Skybox(uint size, Shader* shader);
|
Skybox(uint size, Shader* shader);
|
||||||
~Skybox();
|
~Skybox();
|
||||||
|
|||||||
Reference in New Issue
Block a user