1
0
Fork 0

Galaxybox (cubemap)

This commit is contained in:
May B. 2020-07-13 18:45:08 +02:00
parent c0e84d67e7
commit 5563ae4082
74 changed files with 388 additions and 14 deletions

View File

@ -36,7 +36,8 @@
- Eye adaptation
- [ ] Post processing
- https://learnopengl.com/Advanced-OpenGL/Anti-Aliasing
- [ ] Skybox
- [x] Skybox
- [ ] Environment mapping
- [ ] Cascaded shadow maps
- [ ] RayCast

12
content/shaders/Sky.fs Normal file
View File

@ -0,0 +1,12 @@
#version 330 core
// Ouput data
layout(location = 0) out vec4 color;
uniform samplerCube Texture;
in vec3 UV;
void main(){
color = texture(Texture, UV);
}

15
content/shaders/Sky.vs Normal file
View File

@ -0,0 +1,15 @@
#version 330 core
layout (location = 0) in vec3 Position_modelspace;
out vec3 UV;
uniform mat4 Projection;
uniform mat4 View;
void main()
{
UV = Position_modelspace;
gl_Position = (Projection * View * vec4(Position_modelspace, 1.0)).xyww;
}

BIN
content/textures-src/1024-realistic/Dirt_003_COLOR.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
content/textures-src/1024-realistic/Dirt_003_DISP.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
content/textures-src/1024-realistic/Dirt_003_HOS.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
content/textures-src/1024-realistic/Dirt_003_NRM.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
content/textures-src/1024-realistic/Dirt_003_OCC.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
content/textures-src/1024-realistic/Dirt_003_SPEC.png (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
content/textures-src/1024-realistic/Space_tray.cube.back.png (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
content/textures-src/1024-realistic/Space_tray.cube.left.png (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
content/textures-src/1024-realistic/Space_tray.cube.top.png (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
content/textures/1024-realistic/sky/Space_tray.cube.back.dds (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
content/textures/1024-realistic/sky/Space_tray.cube.left.dds (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
content/textures/1024-realistic/sky/Space_tray.cube.top.dds (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -1 +1,2 @@
CC0 - https://www.patreon.com/gendo - https://3dtextures.me
(c) - LemonSpawn - http://www.lemonspawn.com

BIN
content/textures/1024-realistic/terrain/Dirt.dds (Stored with Git LFS) Normal file

Binary file not shown.

BIN
content/textures/1024-realistic/terrain/Dirt.hos.dds (Stored with Git LFS) Normal file

Binary file not shown.

BIN
content/textures/1024-realistic/terrain/Dirt.nrm.dds (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -40,8 +40,9 @@ struct options {
renderer.main.pbr = config["render"]["pbr"].value_or(renderer.main.pbr);
renderer.main.triplanar = config["render"]["triplanar"].value_or(renderer.main.triplanar);
renderer.main.fog = config["render"]["fog"].value_or(renderer.main.fog);
const std::string fog = config["render"]["fog_color"].value_or(std::string{"#553868"});
const std::string fog = config["render"]["fog_color"].value_or(std::string{"#000000"});
renderer.clear_color = fromHex(fog);
renderer.skybox = config["render"]["skybox"].value_or(renderer.skybox);
world.loadDistance = config["world"]["load_distance"].value_or(world.loadDistance);
world.keepDistance = config["world"]["keep_distance"].value_or(world.keepDistance);
@ -79,6 +80,7 @@ struct options {
{"triplanar", renderer.main.triplanar},
{"fog", renderer.main.fog},
{"fog_color", toHexa(renderer.clear_color)},
{"skybox", renderer.skybox}
}));
config.insert_or_assign("world", toml::table({
{"load_distance", world.loadDistance},

View File

@ -102,6 +102,7 @@ int main(int, char *[]){
if(actions && UI::Actions::Camera) {
camera.setOptions(options.camera);
}
renderer->SkyEnable = options.renderer.skybox;
}
{ // Rendering
const double partTime = glfwGetTime();
@ -120,6 +121,7 @@ int main(int, char *[]){
reports.main.models_count++;
reports.main.tris_count += buffer->draw(pass.setup(model));
}
renderer->postProcess();
UI::render();
reports.main.render.push((glfwGetTime() - partTime) * 1000);

View File

@ -8,6 +8,8 @@ Renderer::Renderer(const Renderer::options& options) {
glBindVertexArray(VertexArrayID);
MainPass = new MainProgram(options.main);
SkyPass = new SkyProgram();
SkyEnable = options.skybox;
FogColor = glm::vec3(options.clear_color.x, options.clear_color.y, options.clear_color.z);
loadTextures(options.textures, options.mipMapLOD);
@ -16,12 +18,19 @@ Renderer::Renderer(const Renderer::options& options) {
Renderer::~Renderer() {
unloadTextures();
delete MainPass;
delete SkyPass;
glDeleteVertexArrays(1, &VertexArrayID);
}
PassContext Renderer::getPass() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
return PassContext(this, MainPass);
}
void Renderer::postProcess() {
if(SkyEnable) {
SkyPass->draw(this);
}
}
void Renderer::reloadShaders(const MainProgram::options& options) {
delete MainPass;
@ -38,13 +47,15 @@ void Renderer::unloadTextures() {
glDeleteTextures(1, &TextureAtlas);
}
void Renderer::loadTextures(const std::string& texturePath, float mipMapLOD) {
std::vector<std::string> textures;
std::vector<std::string> terrainTextures;
for(const auto texture: materials::textures) {
textures.push_back("terrain/" + texturePath + "/" + texture);
terrainTextures.push_back(texturePath + "/terrain/" + texture);
}
TextureAtlas = Program::loadTextureArray(textures, "", mipMapLOD);
NormalAtlas = Program::loadTextureArray(textures, ".nrm", mipMapLOD);
HOSAtlas = Program::loadTextureArray(textures, ".hos", mipMapLOD);
TextureAtlas = Program::loadTextureArray(terrainTextures, "", mipMapLOD);
NormalAtlas = Program::loadTextureArray(terrainTextures, ".nrm", mipMapLOD);
HOSAtlas = Program::loadTextureArray(terrainTextures, ".hos", mipMapLOD);
Skybox = Program::loadTextureCube(texturePath + "/sky/Space_tray");
}
void Renderer::lookFrom(const Camera& camera) {

View File

@ -3,6 +3,7 @@
#include <GL/glew.h>
#include <imgui.h>
#include "pass/MainProgram.hpp"
#include "pass/SkyProgram.hpp"
#include "pass/PassContext.hpp"
class Camera;
@ -11,6 +12,7 @@ class Renderer {
public:
struct options {
MainProgram::options main;
bool skybox = false;
bool wireframe = false;
std::string textures = "1024-realistic";
float mipMapLOD = -.5;
@ -28,6 +30,8 @@ public:
glm::vec3 FogColor;
GLfloat FogDepth;
bool SkyEnable;
glm::mat4 getProjectionMatrix() const {
return ProjectionMatrix;
}
@ -44,8 +48,12 @@ public:
GLuint getHOSAtlas() const {
return HOSAtlas;
}
GLuint getSkyTexture() const {
return Skybox;
}
PassContext getPass();
void postProcess();
void lookFrom(const Camera&);
void reloadShaders(const MainProgram::options&);
@ -55,6 +63,7 @@ private:
GLuint VertexArrayID;
MainProgram *MainPass;
SkyProgram *SkyPass;
glm::mat4 ProjectionMatrix;
glm::mat4 ViewMatrix;
@ -62,6 +71,7 @@ private:
GLuint TextureAtlas;
GLuint NormalAtlas;
GLuint HOSAtlas;
GLuint Skybox;
void loadTextures(const std::string &, float mipMapLOD = 0);
void unloadTextures();

View File

@ -29,6 +29,12 @@ UI::Actions UI::draw(options &options, state &state, const reports &reports, GLu
const ImGuiIO &io = ImGui::GetIO();
if(state.capture_mouse) {
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x / 2, io.DisplaySize.y / 2), ImGuiCond_Always, ImVec2(.5f, .5f));
ImGui::Begin("Aim", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoInputs);
ImGui::Image((void *)(intptr_t)aim, ImVec2(32, 32));
}
if (options.show_debug_menu) {
ImGui::BeginMainMenuBar();
ImGui::Checkbox("Render", &options.show_debug_render);
@ -68,8 +74,10 @@ UI::Actions UI::draw(options &options, state &state, const reports &reports, GLu
changeRenderer |= ImGui::Checkbox("PBR", &options.renderer.main.pbr);
ImGui::SameLine();
changeRenderer |= ImGui::Checkbox("Triplanar", &options.renderer.main.triplanar);
ImGui::SameLine();
changeRenderer |= ImGui::Checkbox("Fog", &options.renderer.main.fog);
ImGui::SameLine();
ImGui::Checkbox("Skybox", &options.renderer.skybox);
if (changeRenderer) {
actions = actions | Actions::RendererSharders;
}
@ -196,9 +204,6 @@ UI::Actions UI::draw(options &options, state &state, const reports &reports, GLu
}
ImGui::End();
}
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x / 2, io.DisplaySize.y / 2), ImGuiCond_Always, ImVec2(.5f, .5f));
ImGui::Begin("Aim", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoInputs);
ImGui::Image((void *)(intptr_t)aim, ImVec2(32, 32));
ImGui::Render();
return actions;
}

View File

@ -5,7 +5,6 @@ PassContext::PassContext(Renderer* Renderer, Program *Program):
renderer(Renderer), program(Program) { }
void PassContext::start() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program->useIt();
program->start(renderer);
}

View File

@ -53,6 +53,21 @@ GLuint Program::loadTextureArray(const std::vector<std::string> &names, const st
[suffix](const std::string &name) -> std::string { return TEXTURES_DIR + name + suffix + ".dds"; });
return loadDDSArray(paths, mipMapLOD);
}
GLuint Program::loadTextureCube(const std::string &name) {
std::array<std::string, 6> faces {
"right",
"left",
"top",
"bottom",
"front",
"back"
};
std::array<std::string, 6> paths;
std::transform(faces.begin(), faces.end(), paths.begin(),
[name](const std::string &face) -> std::string { return TEXTURES_DIR + name + ".cube." + face + ".dds"; });
return loadDDSCube(paths);
}
GLuint Program::loadRawTexture(const std::string& name) {
return loadBMP_custom(name.c_str());
}

View File

@ -23,6 +23,7 @@ public:
static GLuint loadTexture(const std::string &name, bool linear = true);
static GLuint loadRawTexture(const std::string &name);
static GLuint loadTextureArray(const std::vector<std::string> &names, const std::string &suffix = "", float mipMapLOD = 0);
static GLuint loadTextureCube(const std::string &name);
protected:
void load(const std::vector<Shader *> &shaders);

View File

@ -0,0 +1,93 @@
#include "SkyProgram.hpp"
#include "../Renderer.hpp"
const GLfloat g_cubemap_vertices[] = {
-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
};
SkyProgram::SkyProgram(): Program(), CubeBuffer(GL_TRIANGLES, 36, sizeof(g_cubemap_vertices), g_cubemap_vertices) {
std::vector<std::string> flags;
std::vector<Shader*> shaders;
shaders.push_back(loadShader(GL_VERTEX_SHADER, flags));
shaders.push_back(loadShader(GL_FRAGMENT_SHADER, flags));
load(shaders);
ViewMatrixID = glGetUniformLocation(ProgramID, "View");
ProjectionMatrixID = glGetUniformLocation(ProgramID, "Projection");
TextureID = glGetUniformLocation(ProgramID, "Texture");
}
SkyProgram::~SkyProgram() { }
std::string SkyProgram::getName() const {
return "Sky";
}
void SkyProgram::start(Renderer *renderer) {
const auto fixedView = glm::mat4(glm::mat3(renderer->getViewMatrix()));
setView(&fixedView[0][0]);
setProjection(&renderer->getProjectionMatrix()[0][0]);
bindTexture(renderer->getSkyTexture());
}
Buffer::params SkyProgram::setup(Renderer *, glm::mat4) {
return Buffer::params{.vertexOnly = true};
}
void SkyProgram::draw(Renderer *renderer) {
useIt();
start(renderer);
CubeBuffer.draw(setup(renderer, glm::mat4(1)));
}
void SkyProgram::setView(const GLfloat *matrix) {
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, matrix);
}
void SkyProgram::setProjection(const GLfloat *matrix) {
glUniformMatrix4fv(ProjectionMatrixID, 1, GL_FALSE, matrix);
}
void SkyProgram::bindTexture(GLuint textureID) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
glUniform1i(TextureID, 0);
}

View File

@ -0,0 +1,29 @@
#pragma once
#include "Program.hpp"
#include "../buffer/VertexBuffer.hpp"
/// Skybox pass
class SkyProgram: public Program {
public:
SkyProgram();
~SkyProgram();
std::string getName() const override;
void start(Renderer *) override;
Buffer::params setup(Renderer *, glm::mat4 modelMatrix) override;
void draw(Renderer *);
void setView(const GLfloat *matrix);
void setProjection(const GLfloat *matrix);
void bindTexture(const GLuint textureID);
private:
GLuint ViewMatrixID;
GLuint ProjectionMatrixID;
GLuint TextureID;
VertexBuffer CubeBuffer;
};

View File

@ -6,6 +6,7 @@
#include <cmath>
#include <vector>
#include <iostream>
#include <array>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@ -295,4 +296,79 @@ GLuint loadDDSArray(const std::vector<std::string>& imagepaths, float mipMapLOD)
return textureID;
}
GLuint loadDDSCube(const std::array<std::string, 6>& imagepaths) {
unsigned char header[124];
auto imagepath = imagepaths.begin();
FILE *fp;
/* try to open the file */
fp = fopen(imagepath->c_str(), "rb");
if (fp == NULL)
{
printf("%s could not be opened.\n", imagepath->c_str());
getchar();
return 0;
}
/* verify the type of file */
char filecode[4];
fread(filecode, 1, 4, fp);
if (strncmp(filecode, "DDS ", 4) != 0)
{
fclose(fp);
return 0;
}
/* get the surface desc */
fread(&header, 124, 1, fp);
unsigned int mainHeight = *(unsigned int *)&(header[8]);
unsigned int mainWidth = *(unsigned int *)&(header[12]);
unsigned int mainFourCC = *(unsigned int *)&(header[80]);
fclose(fp);
unsigned int mainFormat;
switch (mainFourCC)
{
case FOURCC_DXT1:
mainFormat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
break;
case FOURCC_DXT3:
mainFormat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
break;
case FOURCC_DXT5:
mainFormat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
break;
default:
return 0;
}
// Create one OpenGL texture array
GLuint textureID;
glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &textureID);
glTextureStorage2D(textureID, 1, mainFormat, mainWidth, mainHeight);
ushort layer = 0;
for (imagepath = imagepaths.begin(); imagepath != imagepaths.end(); ++imagepath, ++layer)
{
GLuint subTextureID = loadDDS(*imagepath, false);
glCopyImageSubData(subTextureID, GL_TEXTURE_2D, 0, 0, 0, 0, textureID, GL_TEXTURE_CUBE_MAP, 0, 0, 0, layer, mainWidth, mainHeight, 1);
glDeleteTextures(1, &subTextureID);
}
glTextureParameteri(textureID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(textureID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(textureID, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(textureID, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTextureParameteri(textureID, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glGenerateTextureMipmap(textureID);
return textureID;
}

View File

@ -3,6 +3,7 @@
#include <string.h>
#include <vector>
#include <array>
// Load a .BMP file using our custom loader
GLuint loadBMP_custom(const char * imagepath);
@ -11,5 +12,7 @@ GLuint loadBMP_custom(const char * imagepath);
GLuint loadDDS(const std::string& imagepath, bool linear = true);
// Load a list of .DDS files
GLuint loadDDSArray(const std::vector<std::string> &imagepaths, float mipMapLOD = 0);
// Load a .DDS cubemap
GLuint loadDDSCube(const std::array<std::string, 6> &imagepaths);
#endif

View File

@ -4,6 +4,6 @@
#include <string>
namespace materials {
static const auto count = 8;
static const std::array<std::string, count> textures = {{"Air", "Sand", "Stone_path", "Mapl", "Seaside_rock", "Stone_wall", "Rough_rock", "Alien"}};
static const auto count = 9;
static const std::array<std::string, count> textures = {{"Air", "Sand", "Dirt", "Stone_path", "Mapl", "Seaside_rock", "Stone_wall", "Rough_rock", "Alien"}};
}