skybox: added moon and stars

This commit is contained in:
MihailRis
2024-01-26 02:49:42 +03:00
parent b42a517cc6
commit a4a5aef422
22 changed files with 258 additions and 52 deletions
+9 -14
View File
@@ -5,13 +5,14 @@
#include <memory>
#include <assert.h>
#include "../content/Content.h"
#include "../graphics/ChunksRenderer.h"
#include "../window/Window.h"
#include "../window/Camera.h"
#include "../content/Content.h"
#include "../graphics/ChunksRenderer.h"
#include "../graphics/Mesh.h"
#include "../graphics/Atlas.h"
#include "../graphics/Shader.h"
#include "../graphics/Batch3D.h"
#include "../graphics/Texture.h"
#include "../graphics/LineBatch.h"
#include "../voxels/Chunks.h"
@@ -46,7 +47,8 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend)
lineBatch(new LineBatch()),
renderer(new ChunksRenderer(level,
frontend->getContentGfxCache(),
engine->getSettings())) {
engine->getSettings())),
batch3d(new Batch3D(4096)) {
auto& settings = engine->getSettings();
level->events->listen(EVT_CHUNK_HIDDEN,
@@ -129,31 +131,24 @@ void WorldRenderer::drawChunks(Chunks* chunks,
void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible){
Window::clearDepth();
EngineSettings& settings = engine->getSettings();
skybox->refresh(level->world->daytime,
1.0f+fog*2.0f, 4);
skybox->refresh(pctx, level->world->daytime, 1.0f+fog*2.0f, 4);
const Content* content = level->content;
auto indices = content->getIndices();
Assets* assets = engine->getAssets();
Atlas* atlas = assets->getAtlas("blocks");
Shader* shader = assets->getShader("main");
Shader* linesShader = assets->getShader("lines");
const Viewport& viewport = pctx.getViewport();
int displayWidth = viewport.getWidth();
int displayHeight = viewport.getHeight();
Window::clearDepth();
Window::viewport(0, 0, displayWidth, displayHeight);
// Drawing background sky plane
Shader* backShader = assets->getShader("background");
backShader->use();
backShader->uniformMatrix("u_view", camera->getView(false));
backShader->uniform1f("u_zoom", camera->zoom*camera->getFov()/(3.141592*0.5f));
backShader->uniform1f("u_ar", float(displayWidth)/float(displayHeight));
skybox->draw(backShader);
skybox->draw(pctx, camera, assets, level->getWorld()->daytime, fog);
Shader* linesShader = assets->getShader("lines");
{
GfxContext ctx = pctx.sub();
ctx.depthTest(true);
+3
View File
@@ -2,6 +2,7 @@
#define WORLD_RENDERER_CPP
#include <vector>
#include <memory>
#include <algorithm>
#include <GL/glew.h>
#include <string>
@@ -14,6 +15,7 @@
class Level;
class Camera;
class Batch3D;
class LineBatch;
class ChunksRenderer;
class Shader;
@@ -31,6 +33,7 @@ class WorldRenderer {
LineBatch* lineBatch;
ChunksRenderer* renderer;
Skybox* skybox;
std::unique_ptr<Batch3D> batch3d;
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
public:
+110 -13
View File
@@ -1,19 +1,28 @@
#include "Skybox.h"
#include <GL/glew.h>
#include <iostream>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include "../../assets/Assets.h"
#include "../../graphics/Shader.h"
#include "../../graphics/Mesh.h"
#include "../../graphics/Batch3D.h"
#include "../../window/Window.h"
#include "../../window/Camera.h"
#ifndef M_PI
#define M_PI 3.141592
#endif // M_PI
using glm::vec3;
const int STARS_COUNT = 3000;
const int STARS_SEED = 632;
Skybox::Skybox(uint size, Shader* shader) : size(size), shader(shader) {
Skybox::Skybox(uint size, Shader* shader)
: size(size),
shader(shader),
batch3d(new Batch3D(4096))
{
glGenTextures(1, &cubemap);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -31,30 +40,118 @@ Skybox::Skybox(uint size, Shader* shader) : size(size), shader(shader) {
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f
};
vattr attrs[] {2, 0};
mesh = new Mesh(vertices, 6, attrs);
mesh = std::make_unique<Mesh>(vertices, 6, attrs);
sprites.push_back(skysprite {
"misc/moon",
M_PI*0.5f,
4.0f,
false
});
sprites.push_back(skysprite {
"misc/sun",
M_PI*1.5f,
4.0f,
true
});
}
Skybox::~Skybox() {
glDeleteTextures(1, &cubemap);
glDeleteFramebuffers(1, &fbo);
delete mesh;
}
void Skybox::draw(Shader* shader) {
shader->uniform1i("u_cubemap", 1);
void Skybox::drawBackground(Camera* camera, Assets* assets, int width, int height) {
Shader* backShader = assets->getShader("background");
backShader->use();
backShader->uniformMatrix("u_view", camera->getView(false));
backShader->uniform1f("u_zoom", camera->zoom*camera->getFov()/(M_PI*0.5f));
backShader->uniform1f("u_ar", float(width)/float(height));
backShader->uniform1i("u_cubemap", 1);
bind();
mesh->draw();
unbind();
}
void Skybox::refresh(float t, float mie, uint quality) {
void Skybox::drawStars(Camera* camera, float angle, float opacity) {
batch3d->texture(nullptr);
random.setSeed(STARS_SEED);
for (int i = 0; i < STARS_COUNT; i++) {
float rx = (random.randFloat()) - 0.5f;
float ry = (random.randFloat()) - 0.5f;
float z = (random.randFloat()) - 0.5f;
float x = rx * sin(angle) + ry * -cos(angle);
float y = rx * cos(angle) + ry * sin(angle);
float sopacity = random.randFloat();
if (y < 0.0f)
continue;
sopacity *= (0.2f+sqrt(cos(angle))*0.5) - 0.05;
glm::vec4 tint (1,1,1, sopacity * opacity);
batch3d->point(camera->position + glm::vec3(x, y, z), tint);
}
batch3d->flushPoints();
}
void Skybox::draw(
const GfxContext& pctx,
Camera* camera,
Assets* assets,
float daytime,
float fog)
{
const Viewport& viewport = pctx.getViewport();
int width = viewport.getWidth();
int height = viewport.getHeight();
drawBackground(camera, assets, width, height);
GfxContext ctx = pctx.sub();
ctx.blendMode(blendmode::addition);
Shader* shader = assets->getShader("ui3d");
shader->use();
shader->uniformMatrix("u_projview", camera->getProjView());
shader->uniformMatrix("u_apply", glm::mat4(1.0f));
batch3d->begin();
float angle = daytime * M_PI * 2;
float opacity = glm::pow(1.0f-fog, 7.0f);
for (auto& sprite : sprites) {
batch3d->texture(assets->getTexture(sprite.texture));
float sangle = daytime * M_PI*2 + sprite.phase;
float distance = sprite.distance;
glm::vec3 pos(-cos(sangle)*distance, sin(sangle)*distance, 0);
glm::vec3 up(-sin(-sangle), cos(-sangle), 0.0f);
glm::vec4 tint (1,1,1, opacity);
if (!sprite.emissive) {
tint *= 0.6f+cos(angle)*0.4;
}
batch3d->sprite(camera->position+pos, up,
glm::vec3(0, 0, -1), 1, 1, UVRegion(), tint);
}
drawStars(camera, angle, opacity);
}
void Skybox::refresh(const GfxContext& pctx, float t, float mie, uint quality) {
GfxContext ctx = pctx.sub();
ctx.depthMask(false);
ctx.depthTest(false);
ready = true;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
shader->use();
Window::viewport(0,0, size,size);
const vec3 xaxs[] = {
Window::viewport(0,0, size, size);
const glm::vec3 xaxs[] = {
{0.0f, 0.0f, -1.0f},
{0.0f, 0.0f, 1.0f},
{-1.0f, 0.0f, 0.0f},
@@ -63,7 +160,7 @@ void Skybox::refresh(float t, float mie, uint quality) {
{-1.0f, 0.0f, 0.0f},
{1.0f, 0.0f, 0.0f},
};
const vec3 yaxs[] = {
const glm::vec3 yaxs[] = {
{0.0f, 1.0f, 0.0f},
{0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, -1.0f},
@@ -73,7 +170,7 @@ void Skybox::refresh(float t, float mie, uint quality) {
{0.0f, 1.0f, 0.0f},
};
const vec3 zaxs[] = {
const glm::vec3 zaxs[] = {
{1.0f, 0.0f, 0.0f},
{-1.0f, 0.0f, 0.0f},
{0.0f, -1.0f, 0.0f},
@@ -87,7 +184,7 @@ void Skybox::refresh(float t, float mie, uint quality) {
shader->uniform1i("u_quality", quality);
shader->uniform1f("u_mie", mie);
shader->uniform1f("u_fog", mie - 1.0f);
shader->uniform3f("u_lightDir", glm::normalize(vec3(sin(t), -cos(t), -0.7f)));
shader->uniform3f("u_lightDir", glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f)));
for (uint face = 0; face < 6; face++) {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, cubemap, 0);
shader->uniform3f("u_xaxis", xaxs[face]);
+30 -3
View File
@@ -1,25 +1,52 @@
#ifndef FRONTEND_GRAPHICS_SKYBOX_H_
#define FRONTEND_GRAPHICS_SKYBOX_H_
#include <memory>
#include <string>
#include <vector>
#include "../../typedefs.h"
#include "../../maths/fastmaths.h"
#include "../../graphics/GfxContext.h"
class Mesh;
class Shader;
class Assets;
class Camera;
class Batch3D;
struct skysprite {
std::string texture;
float phase;
float distance;
bool emissive;
};
class Skybox {
uint fbo;
uint cubemap;
uint size;
Mesh* mesh;
Shader* shader;
bool ready = false;
FastRandom random;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Batch3D> batch3d;
std::vector<skysprite> sprites;
void drawStars(Camera* camera, float angle, float opacity);
void drawBackground(Camera* camera, Assets* assets, int width, int height);
public:
Skybox(uint size, Shader* shader);
~Skybox();
void draw(Shader* shader);
void draw(
const GfxContext& pctx,
Camera* camera,
Assets* assets,
float daytime,
float fog);
void refresh(float t, float mie, uint quality);
void refresh(const GfxContext& pctx, float t, float mie, uint quality);
void bind() const;
void unbind() const;
bool isReady() const {