This commit is contained in:
maelstrom 2024-11-19 12:46:43 +01:00
parent d10606dca5
commit 9b06763cfb
9 changed files with 127 additions and 7 deletions

View file

@ -29,11 +29,19 @@ struct PointLight {
float quadratic; float quadratic;
}; };
const int FaceRight = 0;
const int FaceTop = 1;
const int FaceBack = 2;
const int FaceLeft = 3;
const int FaceBottom = 4;
const int FaceFront = 5;
// I/O // I/O
in vec3 vPos; in vec3 vPos;
in vec3 vNormal; in vec3 vNormal;
in vec2 vTexCoords; in vec2 vTexCoords;
flat in int vSurfaceZ;
out vec4 FragColor; out vec4 FragColor;
@ -44,6 +52,7 @@ uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform int numPointLights; uniform int numPointLights;
uniform DirLight sunLight; uniform DirLight sunLight;
uniform Material material; uniform Material material;
uniform sampler2DArray studs;
// Functions // Functions
@ -61,8 +70,9 @@ void main() {
for (int i = 0; i < numPointLights; i++) { for (int i = 0; i < numPointLights; i++) {
result += calculatePointLight(pointLights[i]); result += calculatePointLight(pointLights[i]);
} }
FragColor = vec4(result, 1.0); vec4 studPx = texture(studs, vec3(vTexCoords, vSurfaceZ));
FragColor = vec4(mix(result, vec3(studPx), studPx.w), 1);
} }
vec3 calculateDirectionalLight(DirLight light) { vec3 calculateDirectionalLight(DirLight light) {

View file

@ -3,18 +3,60 @@ layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords; layout (location = 2) in vec2 aTexCoords;
const int FaceRight = 0;
const int FaceTop = 1;
const int FaceBack = 2;
const int FaceLeft = 3;
const int FaceBottom = 4;
const int FaceFront = 5;
const int SurfaceSmooth = 0;
const int SurfaceGlue = 1;
const int SurfaceWeld = 2;
const int SurfaceStuds = 3;
const int SurfaceInlets = 4;
const int SurfaceUniversal = 5;
out vec3 vPos; out vec3 vPos;
out vec3 vNormal; out vec3 vNormal;
out vec2 vTexCoords; out vec2 vTexCoords;
flat out int vSurfaceZ;
uniform mat4 model; uniform mat4 model;
uniform mat3 normalMatrix; uniform mat3 normalMatrix;
uniform mat4 view; uniform mat4 view;
uniform mat4 projection; uniform mat4 projection;
uniform int surfaces[6];
uniform vec3 texScale;
void main() void main()
{ {
gl_Position = projection * view * model * vec4(aPos, 1.0); gl_Position = projection * view * model * vec4(aPos, 1.0);
vPos = vec3(model * vec4(aPos, 1.0)); vPos = vec3(model * vec4(aPos, 1.0));
vNormal = normalMatrix * aNormal; vNormal = normalMatrix * aNormal;
int vFace = aNormal == vec3(0,1,0) ? FaceTop :
aNormal == vec3(0, -1, 0) ? FaceBottom :
aNormal == vec3(1, 0, 0) ? FaceRight :
aNormal == vec3(-1, 0, 0) ? FaceLeft :
aNormal == vec3(0, 0, 1) ? FaceFront :
aNormal == vec3(0, 0, -1) ? FaceBack : -1;
vSurfaceZ = surfaces[vFace];
// if (surfaces[vFace] > SurfaceUniversal) vSurfaceZ = 0;
switch (vFace) {
case FaceTop:
case FaceBottom:
// vTexCoords = aTexCoords * vec2(texScale.x / 2, fract(surfaceOffset + texScale.z / 12));
vTexCoords = aTexCoords * vec2(texScale.x, texScale.z) / 2;
break;
case FaceLeft:
case FaceRight:
vTexCoords = aTexCoords * vec2(texScale.y, texScale.z) / 2;
break;
case FaceFront:
case FaceBack:
vTexCoords = aTexCoords * vec2(texScale.x, texScale.y) / 2;
break;
};
} }

BIN
assets/textures/studs.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

View file

@ -5,7 +5,7 @@ Mesh* CUBE_MESH;
void initMeshes() { void initMeshes() {
static float vertices[] = { static float vertices[] = {
// positions // normals // texture coords // positions // normals // texture coords
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,

View file

@ -15,7 +15,7 @@
#include "../camera.h" #include "../camera.h"
#include "../part.h" #include "../part.h"
#include "skybox.h" #include "skybox.h"
#include "texture.h" #include "texture3d.h"
#include "renderer.h" #include "renderer.h"
@ -24,6 +24,7 @@ Shader *skyboxShader = NULL;
extern Camera camera; extern Camera camera;
extern std::vector<Part> parts; extern std::vector<Part> parts;
Skybox* skyboxTexture = NULL; Skybox* skyboxTexture = NULL;
Texture3D* studsTexture = NULL;
void renderInit(GLFWwindow* window) { void renderInit(GLFWwindow* window) {
glViewport(0, 0, 1200, 900); glViewport(0, 0, 1200, 900);
@ -31,6 +32,10 @@ void renderInit(GLFWwindow* window) {
initMeshes(); initMeshes();
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
skyboxTexture = new Skybox({ skyboxTexture = new Skybox({
"assets/textures/skybox/null_plainsky512_lf.jpg", "assets/textures/skybox/null_plainsky512_lf.jpg",
@ -41,6 +46,8 @@ void renderInit(GLFWwindow* window) {
"assets/textures/skybox/null_plainsky512_bk.jpg", "assets/textures/skybox/null_plainsky512_bk.jpg",
}, GL_RGB); }, GL_RGB);
studsTexture = new Texture3D("assets/textures/studs.png", 64, 64, 6, GL_RGBA);
// Compile shader // Compile shader
shader = new Shader("assets/shaders/phong.vs", "assets/shaders/phong.fs"); shader = new Shader("assets/shaders/phong.vs", "assets/shaders/phong.fs");
skyboxShader = new Shader("assets/shaders/skybox.vs", "assets/shaders/skybox.fs"); skyboxShader = new Shader("assets/shaders/skybox.vs", "assets/shaders/skybox.fs");
@ -81,6 +88,10 @@ void renderParts() {
// .linear = 0.9, // .linear = 0.9,
// .quadratic = 0.32, // .quadratic = 0.32,
// }); // });
studsTexture->activate(0);
shader->set("studs", 0);
shader->set("surfaces[1]", 3);
shader->set("surfaces[4]", 4);
// Pre-calculate the normal matrix for the shader // Pre-calculate the normal matrix for the shader
@ -96,6 +107,7 @@ void renderParts() {
shader->set("material", part.material); shader->set("material", part.material);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model))); glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
shader->set("normalMatrix", normalMatrix); shader->set("normalMatrix", normalMatrix);
shader->set("texScale", part.scale);
CUBE_MESH->bind(); CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);

View file

@ -4,7 +4,7 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <stb/stb_image.h> #include <stb/stb_image.h>
Texture::Texture(const char* texturePath, unsigned int format) { Texture::Texture(const char* texturePath, unsigned int format, bool noMipMaps) {
glGenTextures(1, &this->ID); glGenTextures(1, &this->ID);
glBindTexture(GL_TEXTURE_2D, this->ID); glBindTexture(GL_TEXTURE_2D, this->ID);
@ -28,7 +28,7 @@ Texture::Texture(const char* texturePath, unsigned int format) {
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
GL_UNSIGNED_BYTE, data); GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); if (!noMipMaps) glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data); stbi_image_free(data);
} }

View file

@ -6,7 +6,7 @@ class Texture {
private: private:
unsigned int ID; unsigned int ID;
public: public:
Texture(const char* texturePath, unsigned format = GL_RGB); Texture(const char* texturePath, unsigned format = GL_RGB, bool noMipMaps = false);
~Texture(); ~Texture();
void activate(unsigned int textureIdx); void activate(unsigned int textureIdx);

View file

@ -0,0 +1,43 @@
#include "texture3d.h"
#include <GL/glew.h>
#include <GL/gl.h>
#include <stb/stb_image.h>
Texture3D::Texture3D(const char* texturePath, unsigned int tileWidth, unsigned int tileHeight, unsigned int tileCount, unsigned int format) {
glGenTextures(1, &this->ID);
glBindTexture(GL_TEXTURE_2D_ARRAY, this->ID);
// Wrapping
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_R, GL_REPEAT);
// Interpolation
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// stbi_set_flip_vertically_on_load(true);
int width, height, nrChannels;
unsigned char *data = stbi_load(texturePath, &width, &height,
&nrChannels, 0);
if (!data) {
printf("Failed to load texture '%s'\n", texturePath);
abort();
}
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, format, tileWidth, tileHeight, /* no of layers= */ tileCount, 0, format,
GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
Texture3D::~Texture3D() {
glDeleteTextures(1, &this->ID);
}
void Texture3D::activate(unsigned int textureIdx) {
glActiveTexture(GL_TEXTURE0 + textureIdx);
glBindTexture(GL_TEXTURE_2D, this->ID);
}

13
src/rendering/texture3d.h Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#define GL_RGB 0x1907
class Texture3D {
private:
unsigned int ID;
public:
Texture3D(const char* texturePath, unsigned int tileWidth, unsigned int tileHeight, unsigned int tileCount, unsigned format = GL_RGB);
~Texture3D();
void activate(unsigned int textureIdx);
};