taste the rainbow

This commit is contained in:
maelstrom 2024-10-09 18:19:37 +02:00
parent 2e47b379fc
commit c0e6669045
13 changed files with 68 additions and 133 deletions

View file

@ -1,108 +1,15 @@
#version 330 core
// Implements the Phong lighting model with respect to materials and lighting materials
// Structs
struct Material {
vec3 diffuse;
vec3 specular;
float shininess;
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
// vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float quadratic;
};
// I/O
uniform sampler2D uTexture;
in vec3 vPos;
in vec3 vNormal;
in vec2 vTexCoords;
out vec4 FragColor;
#define NR_POINT_LIGHTS 4
uniform vec3 viewPos;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform int numPointLights;
uniform DirLight sunLight;
uniform Material material;
// Functions
vec3 calculateDirectionalLight(DirLight light);
vec3 calculatePointLight(PointLight light);
out vec4 fColor;
// Main
void main() {
vec3 result = vec3(0.0);
result += calculateDirectionalLight(sunLight);
for (int i = 0; i < numPointLights; i++) {
result += calculatePointLight(pointLights[i]);
}
FragColor = vec4(result, 1.0);
fColor = vec4(vTexCoords, 1.0, 1.0);
}
vec3 calculateDirectionalLight(DirLight light) {
// Calculate diffuse
vec3 norm = normalize(vNormal);
vec3 lightDir = normalize(-light.direction);
float diff = max(dot(norm, lightDir), 0.0);
// Calculate specular
vec3 viewDir = normalize(viewPos - vPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 ambient = light.ambient * material.diffuse;
vec3 diffuse = light.diffuse * diff * material.diffuse;
vec3 specular = light.specular * spec * material.specular;
return (ambient + diffuse + specular);
}
vec3 calculatePointLight(PointLight light) {
// Calculate ambient light
// Calculate diffuse light
vec3 norm = normalize(vNormal);
vec3 lightDir = normalize(light.position - vPos);
float diff = max(dot(norm, lightDir), 0.0);
// Calculate specular
vec3 viewDir = normalize(viewPos - vPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// Calculate attenuation
float distance = length(light.position - vPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
vec3 ambient = light.ambient * material.diffuse;
vec3 diffuse = light.diffuse * diff * material.diffuse;
vec3 specular = light.specular * spec * material.specular;
return (ambient + diffuse + specular) * attenuation;
}

View file

@ -7,8 +7,6 @@ out vec3 vPos;
out vec3 vNormal;
out vec2 vTexCoords;
uniform mat4 model;
uniform mat3 normalMatrix;
uniform mat4 view;
uniform mat4 projection;
@ -17,4 +15,5 @@ void main()
gl_Position = projection * vec4(vec3(view * vec4(aPos, 0.0)), 1.0);
vPos = vec3(vec4(aPos, 1.0));
vNormal = aNormal;
vTexCoords = aTexCoords;
}

View file

@ -0,0 +1 @@
null_plainsky is copyright of Jauhn Dabz, jauhn@yahoo.com, https://nullpoint.fragland.net

Binary file not shown.

After

(image error) Size: 48 KiB

Binary file not shown.

After

(image error) Size: 33 KiB

Binary file not shown.

After

(image error) Size: 47 KiB

Binary file not shown.

After

(image error) Size: 43 KiB

Binary file not shown.

After

(image error) Size: 49 KiB

Binary file not shown.

After

(image error) Size: 35 KiB

View file

@ -13,6 +13,7 @@
#include "defaultmeshes.h"
#include "../camera.h"
#include "../part.h"
#include "texture.h"
#include "renderer.h"
@ -20,6 +21,7 @@ Shader *shader = NULL;
Shader *skyboxShader = NULL;
extern Camera camera;
extern std::vector<Part> parts;
Texture* skyboxTexture = NULL;
void renderInit(GLFWwindow* window) {
glViewport(0, 0, 1200, 900);
@ -28,6 +30,8 @@ void renderInit(GLFWwindow* window) {
glEnable(GL_DEPTH_TEST);
skyboxTexture = new Texture("assets/textures/skybox/null_plainsky512_ft.jpg", GL_RGB);
// Compile shader
shader = new Shader("assets/shaders/phong.vs", "assets/shaders/phong.fs");
skyboxShader = new Shader("assets/shaders/skybox.vs", "assets/shaders/skybox.fs");
@ -90,46 +94,12 @@ void renderParts() {
void renderSkyBox() {
skyboxShader->use();
// glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)1200 / (float)900, 0.1f, 100.0f);
// glm::mat4 view = camera.getLookAt();
// shader->set("projection", projection);
// shader->set("view", view);
// shader->set("material", Material {
// // .ambient = glm::vec3(1.0f, 0.5f, 0.31f),
// .diffuse = glm::vec3(1.0f, 0.5f, 0.31f),
// .specular = glm::vec3(0.5f, 0.5f, 0.5f),
// .shininess = 32.0f,
// });
// shader->set("viewPos", camera.cameraPos);
// view/projection transformations
glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)1200 / (float)900, 0.1f, 100.0f);
glm::mat4 view = camera.getLookAt();
skyboxShader->set("projection", projection);
skyboxShader->set("view", view);
skyboxShader->set("material", Material {
// .ambient = glm::vec3(1.0f, 0.5f, 0.31f),
.diffuse = glm::vec3(1.0f, 0.5f, 0.31f),
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 32.0f,
});
skyboxShader->set("sunLight", DirLight {
.direction = glm::vec3(-0.2f, -1.0f, -0.3f),
.ambient = glm::vec3(0.2f, 0.2f, 0.2f),
.diffuse = glm::vec3(0.5f, 0.5f, 0.5f),
.specular = glm::vec3(1.0f, 1.0f, 1.0f),
});
skyboxShader->set("numPointLights", 0);
glm::mat4 model = glm::mat4(1.0f);
// model = glm::translate(model, part.position);
// model = model * glm::mat4_cast(part.rotation);
// model = glm::scale(model, part.scale);
shader->set("model", model);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
shader->set("normalMatrix", normalMatrix);
skyboxShader->set("uTexture", 0);
CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);

43
src/rendering/texture.cpp Normal file
View file

@ -0,0 +1,43 @@
#include "texture.h"
#include <GL/glew.h>
#include <GL/gl.h>
#include <stb/stb_image.h>
Texture::Texture(const char* texturePath, unsigned int format) {
glGenTextures(1, &this->ID);
glBindTexture(GL_TEXTURE_2D, this->ID);
// Wrapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Interpolation
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, 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();
}
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
Texture::~Texture() {
glDeleteTextures(1, &this->ID);
}
void Texture::activate(unsigned int textureIdx) {
glActiveTexture(GL_TEXTURE0 + textureIdx);
glBindTexture(GL_TEXTURE_2D, this->ID);
}

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

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

2
src/stb.cpp Normal file
View file

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>