diff --git a/MCRenderer.cpp b/MCRenderer.cpp index 051ad3e..1e270ca 100644 --- a/MCRenderer.cpp +++ b/MCRenderer.cpp @@ -8,7 +8,10 @@ MCRenderer::MCRenderer(int size_x, int size_y, int size_z) : size_x(size_x), size_y(size_y), size_z(size_z), render_shader(Shader("Shader/mc.vs", "Shader/mc.fs", "Shader/mc.gs")), noise_shader(Shader("Shader/noise.vs", "Shader/noise.fs")), - noise(Framebuffer3D(size_x, size_y, size_z)), camera(Camera(90, 1920, 1080, 0.1, 1000.0)) { + noise(Framebuffer3D(size_x, size_y, size_z)), camera(Camera(90, 1920, 1080, 0.1, 1000.0)), + albedo("Resources/Textures/PavingStones/PavingStones070_2K_Color.jpg", Texture::Settings()), + bump("Resources/Textures/PavingStones/PavingStones070_2K_Displacement.jpg", + Texture::Settings()) { // Each noise layer needs vertices, so we define two triangles which span a rectangle float data[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0}, {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}}; @@ -81,6 +84,8 @@ void MCRenderer::render(float delta) { // Bind the noise texture to the rendering shader noise.bind_to(0); + albedo.bind_to(1); + bump.bind_to(2); // Draw all layers as polygons glDrawArrays(GL_POINTS, 0, size_x * size_y * size_z); diff --git a/MCRenderer.h b/MCRenderer.h index 571a4d1..9440058 100644 --- a/MCRenderer.h +++ b/MCRenderer.h @@ -3,6 +3,7 @@ #include "Camera.h" #include "Framebuffer3D.h" #include "Shader.h" +#include "Texture.h" #include "VertexBuffer.h" class MCRenderer { @@ -25,4 +26,7 @@ class MCRenderer { VertexBuffer vertex_rectangle; Camera camera; + + Texture albedo; + Texture bump; }; diff --git a/Resources/Textures/PavingStones/PavingStones070_2K_AmbientOcclusion.jpg b/Resources/Textures/PavingStones/PavingStones070_2K_AmbientOcclusion.jpg new file mode 100644 index 0000000..c3b7bc6 Binary files /dev/null and b/Resources/Textures/PavingStones/PavingStones070_2K_AmbientOcclusion.jpg differ diff --git a/Resources/Textures/PavingStones/PavingStones070_2K_Color.jpg b/Resources/Textures/PavingStones/PavingStones070_2K_Color.jpg new file mode 100644 index 0000000..17cfa3f Binary files /dev/null and b/Resources/Textures/PavingStones/PavingStones070_2K_Color.jpg differ diff --git a/Resources/Textures/PavingStones/PavingStones070_2K_Displacement.jpg b/Resources/Textures/PavingStones/PavingStones070_2K_Displacement.jpg new file mode 100644 index 0000000..e1241fc Binary files /dev/null and b/Resources/Textures/PavingStones/PavingStones070_2K_Displacement.jpg differ diff --git a/Resources/Textures/PavingStones/PavingStones070_2K_Normal.jpg b/Resources/Textures/PavingStones/PavingStones070_2K_Normal.jpg new file mode 100644 index 0000000..1837857 Binary files /dev/null and b/Resources/Textures/PavingStones/PavingStones070_2K_Normal.jpg differ diff --git a/Resources/Textures/PavingStones/PavingStones070_2K_Roughness.jpg b/Resources/Textures/PavingStones/PavingStones070_2K_Roughness.jpg new file mode 100644 index 0000000..a1d5211 Binary files /dev/null and b/Resources/Textures/PavingStones/PavingStones070_2K_Roughness.jpg differ diff --git a/SConstruct b/SConstruct index 37825b9..d41b453 100644 --- a/SConstruct +++ b/SConstruct @@ -9,4 +9,4 @@ env.CompilationDatabase() env.Append(LIBS=['glfw', 'dl']) -env.Program('program.out', [Glob('*.cpp', 'Util/*.cpp'), 'Util/glad.c']) +env.Program('program.out', [Glob('*.cpp'), 'Util/glad.c']) diff --git a/Shader/mc.fs b/Shader/mc.fs index 9eb10ba..b2df4a6 100644 --- a/Shader/mc.fs +++ b/Shader/mc.fs @@ -1,12 +1,15 @@ #version 430 layout (binding = 0) uniform sampler3D densities; +layout (binding = 1) uniform sampler2D albedo; +layout (binding = 2) uniform sampler2D bump; in vec3 varTextureG; +in vec2 varUV; out vec4 color; void main(void) { - float f = texture(densities, varTextureG).r; - color = vec4(f, f, f, 1.0); + vec3 f = texture(albedo, varUV).rgb; + color = vec4(f, 1.0); } diff --git a/Shader/mc.gs b/Shader/mc.gs index efed0c1..bb7c9bf 100644 --- a/Shader/mc.gs +++ b/Shader/mc.gs @@ -8,7 +8,9 @@ uniform mat4 view; in vec3 varTexture[]; in int varIndex[]; + out vec3 varTextureG; +out vec2 varUV; vec3 vectors[13] = { vec3(0.0, 0.0, 0.0), vec3(0.5, 1.0, 1.0), vec3(0.0, 0.5, 1.0), @@ -186,9 +188,15 @@ void main(void) { vec4 base = gl_in[0].gl_Position; int index = varIndex[0] * 12; for(int i = 0; i < 12; i += 3) { + // TODO: Calculate tangents + vec3 tangent; + vec3 bitangent; + for(int k = 0; k < 3; k++) { gl_Position = proj * view * (base + vec4(vectors[table[index + i + k]], 0.0)); varTextureG = varTexture[0]; + // FIXME: These UV coordinates become stretched on one axis + varUV = vectors[table[index + i + k]].xz; EmitVertex(); } EndPrimitive(); diff --git a/Texture.cpp b/Texture.cpp new file mode 100644 index 0000000..9be63d3 --- /dev/null +++ b/Texture.cpp @@ -0,0 +1,56 @@ +#include "Texture.h" +#include + +#define STB_IMAGE_IMPLEMENTATION +#include "Util/stb_image.h" + +bool Texture::is_valid() const { + return valid; +} + +uint Texture::load_texture(const std::string &path, Settings settings) { + uint gl_id; + + glGenTextures(1, &gl_id); + glBindTexture(GL_TEXTURE_2D, gl_id); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + if (settings.mipmaps) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + } else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + } + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + int width, height, nrChannels; + unsigned char *data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0); + + if (data) { + // Check number of channels + unsigned int glChannels = GL_RED; + if (nrChannels == 3) + glChannels = GL_RGB; + else if (nrChannels == 4) + glChannels = GL_RGBA; + + glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels, GL_UNSIGNED_BYTE, + data); + + if (settings.mipmaps) { + glGenerateMipmap(GL_TEXTURE_2D); + } + + valid = true; + } + + stbi_image_free(data); + + return gl_id; +} + +void Texture::bind_to(int unit) { + glActiveTexture(GL_TEXTURE0 + unit); + glBindTexture(GL_TEXTURE_2D, id); +} \ No newline at end of file diff --git a/Texture.h b/Texture.h new file mode 100644 index 0000000..3c903c5 --- /dev/null +++ b/Texture.h @@ -0,0 +1,34 @@ +#pragma once + +// Must be the first include +#include + +// Other includes +#include +#include +#include + +class Texture { + public: + struct Settings { + Settings() : mipmaps(true), transparent(false){}; + + bool mipmaps; + bool transparent; + }; + + explicit Texture(const std::string &path, Settings settings) + : id(load_texture(path, settings)), valid(false) { + } + + bool is_valid() const; + + // Bind a texture from the given path and return its ID + uint load_texture(const std::string &path, Settings settings); + + void bind_to(int unit); + + private: + const uint id; + bool valid; +}; \ No newline at end of file