noise #8

Merged
s473581 merged 6 commits from noise into master 2024-02-04 13:03:08 +01:00
3 changed files with 87 additions and 12 deletions
Showing only changes of commit 45421d3e1d - Show all commits

View File

@ -79,7 +79,7 @@ void main()
outColor = vec4(vec3(n), 1);
}
*/
/*
out vec4 outColor;
uniform float u_time;
@ -156,6 +156,8 @@ void main() {
outColor = vec4((f*f*f+.9*f*f+.9*f)*color,1.);
}
*/
/*
uniform float u_time;
out vec4 outColor;

View File

@ -7,7 +7,7 @@ uniform sampler2D depthMap;
uniform vec3 cameraPos;
uniform sampler2D colorTexture;
uniform sampler2D colorTexture; //planet texture
uniform vec3 sunDir;
uniform vec3 sunColor;
@ -15,10 +15,11 @@ uniform vec3 sunColor;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform float metallic;
uniform float roughness;
uniform float metallic; //pbr
uniform float roughness; //pbr
uniform float exposition; //pbr
uniform float exposition;
uniform float u_time;
in vec3 vecNormal;
in vec3 worldPos;
@ -30,6 +31,8 @@ in vec3 viewDirTS;
in vec3 lightDirTS;
in vec3 sunDirTS;
vec4 textureColor;
float DistributionGGX(vec3 normal, vec3 H, float roughness)
{
float a = roughness * roughness;
@ -104,8 +107,71 @@ vec3 toneMapping(vec3 color)
return mapped;
}
void main()
{
float random (in vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
// Based on Morgan McGuire @morgan3d
// https://www.shadertoy.com/view/4dS3Wd
float noise (in vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
// Four corners in 2D of a tile
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
#define OCTAVES 6
float fbm (in vec2 st) {
// Initial values
float value = 0.0;
float amplitude = .5;
float frequency = 0.;
//
// Loop of octaves
for (int i = 0; i < OCTAVES; i++) {
value += amplitude * noise(st);
st *= 2.;
amplitude *= .5;
}
return value;
}
vec4 noiseColor() {
vec2 st = vtc;
float cloudIntensity = 15.0;
// Efekt lustrzanego odbicia
vec2 mirroredSt = vec2(1.0 - st.x, st.y);
vec3 mirroredColor = vec3(0.0);
mirroredColor += fbm(mirroredSt * cloudIntensity);
// Efekt oryginalny
vec3 color = vec3(0.0);
color += fbm(st * cloudIntensity);
// Dodaj gradientowe mieszanie miêdzy orygina³em a lustrzanym odbiciem
float gradient = smoothstep(0.45, 0.55, st.x);
vec3 finalColor = mix(color, mirroredColor, gradient);
vec4 noiseColor = vec4(finalColor, 1.0);
return noiseColor;
}
void main() {
vec3 normal = normalize(vecNormal);
vec3 viewDir = normalize(cameraPos - worldPos);
@ -128,5 +194,11 @@ void main()
//sun
illumination = illumination + PBRLight(sunDir, sunColor, normal, viewDir, toneMappedColor);
outColor = vec4(vec3(1.0) - exp(-illumination * exposition), 1);
textureColor = vec4(vec3(1.0) - exp(-illumination * exposition), 1);
vec4 noiseColor = noiseColor();
vec3 mixedColor = mix(textureColor.rgb, noiseColor.rgb, noiseColor.r);
outColor = vec4(mixedColor * min(1, AMBIENT + diffuse), 1.0);
}

View File

@ -232,7 +232,7 @@ void drawSkyBox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint text
glUseProgram(0);
}
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, float roughness, float metallic) {
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, float roughness, float metallic, float time) {
glUseProgram(programPbr);
Core::SetActiveTexture(texture, "colorTexture", programPbr, 0);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
@ -252,6 +252,7 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t
glUniform3f(glGetUniformLocation(programPbr, "lightPos"), sunPos.x, sunPos.y, sunPos.z);
glUniform3f(glGetUniformLocation(programPbr, "lightColor"), lightColor.x, lightColor.y, lightColor.z);
glUniform1f(glGetUniformLocation(programNoise, "u_time"), time);
Core::DrawContext(context);
glUseProgram(0);
@ -285,8 +286,8 @@ void renderScene(GLFWwindow* window) {
glm::mat4 cloudScale = glm::scale(glm::vec3(planetSize * 1.05f));
//drawPlanet(sphereContext, planetTranslate * planetRotate * planetScale, planetTex);
drawObjectPBR(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal);
drawObjectNoise(sphereContext, planetTranslate * planetRotate * cloudScale, time);
drawObjectPBR(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal, time);
//drawObjectNoise(sphereContext, planetTranslate * planetRotate * cloudScale, time);
//rysowanie słońca
glm::mat4 sunScale = glm::scale(glm::vec3(sunSize));
@ -335,7 +336,7 @@ void init(GLFWwindow* window) {
programPbr = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_pbr.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.frag");
programSkyBox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
programNoise = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_noise.frag");
//programNoise = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_noise.frag");
loadModelToContext("./models/sphere.obj", sphereContext);
loadModelToContext("./models/cube.obj", cubeContext);