Add Texture class and basis for MC texturing

This commit is contained in:
karl 2021-04-01 21:42:20 +02:00
parent 7a3e5364e0
commit 5de6d88d37
12 changed files with 114 additions and 4 deletions

View File

@ -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);

View File

@ -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;
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

View File

@ -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'])

View File

@ -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);
}

View File

@ -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();

56
Texture.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "Texture.h"
#include <iostream>
#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);
}

34
Texture.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
// Must be the first include
#include <glad/glad.h>
// Other includes
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
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;
};