noise shader

This commit is contained in:
Natalia Nowakowska 2024-01-29 00:54:18 +01:00
parent 8a7374a0fc
commit 7a738c0534
3 changed files with 172 additions and 3 deletions

View File

@ -0,0 +1,80 @@
#version 430 core
float PI = 3.14159f;
uniform sampler2D colorTexture;
uniform float exposition;
in vec3 vecNormal;
in vec3 worldPos;
in vec2 vtc;
out vec4 outColor;
in vec3 viewDirTS;
in vec3 lightDirTS;
in vec3 sunDirTS;
uniform float u_time;
float pi = 3.14159;
// 2D Random
float random (in vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))
* 43758.5453123);
}
// 2D Noise 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 randA = vec2(a *2.0 *pi);
vec2 randB = vec2(b *2.0 *pi);
vec2 randC = vec2(c *2.0 *pi);
vec2 randD = vec2(d *2.0 *pi);
vec2 offsetA = a-st;
vec2 offsetB = b-st;
vec2 offsetC = c-st;
vec2 offsetD = d-st;
offsetA = offsetA*randA;
offsetB = offsetB*randB;
offsetC = offsetC*randC;
offsetD = offsetD*randD;
float resA = smoothstep(offsetA.x,offsetA.y, f.y);
float resB = smoothstep(offsetB.x,offsetB.y, f.y);
float resC = smoothstep(offsetC.x,offsetC.y, f.y);
float resD = smoothstep(offsetD.x,offsetD.y, f.y);
vec2 u = smoothstep(0.,1.,f);
float ab= mix(a, b, u.x);
float cd = mix(c, d, u.x);
return mix(ab,cd,u.y);
}
void main()
{
vec2 st = vtc;
vec2 pos = vec2(st*3.776);
// Use the noise function
float n = 1.836 * 0.676*noise(pos*2.576);
n = smoothstep(-0.096, 1.176, n);
outColor = vec4(vec3(n), 1);
}

View File

@ -1,6 +1,80 @@
#version 430 core #version 430 core
void main() float PI = 3.14159f;
{
} uniform sampler2D colorTexture;
uniform float exposition;
in vec3 vecNormal;
in vec3 worldPos;
in vec2 vtc;
out vec4 outColor;
in vec3 viewDirTS;
in vec3 lightDirTS;
in vec3 sunDirTS;
uniform float u_time;
float pi = 3.14159;
// 2D Random
float random (in vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))
* 43758.5453123);
}
// 2D Noise 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 randA = vec2(a *2.0 *pi);
vec2 randB = vec2(b *2.0 *pi);
vec2 randC = vec2(c *2.0 *pi);
vec2 randD = vec2(d *2.0 *pi);
vec2 offsetA = a-st;
vec2 offsetB = b-st;
vec2 offsetC = c-st;
vec2 offsetD = d-st;
offsetA = offsetA*randA;
offsetB = offsetB*randB;
offsetC = offsetC*randC;
offsetD = offsetD*randD;
float resA = smoothstep(offsetA.x,offsetA.y, f.y);
float resB = smoothstep(offsetB.x,offsetB.y, f.y);
float resC = smoothstep(offsetC.x,offsetC.y, f.y);
float resD = smoothstep(offsetD.x,offsetD.y, f.y);
vec2 u = smoothstep(0.,1.,f);
float ab= mix(a, b, u.x);
float cd = mix(c, d, u.x);
return mix(ab,cd,u.y);
}
void main()
{
vec2 st = vtc;
vec2 pos = vec2(st*3.776);
// Use the noise function
float n = 1.836 * 0.676*noise(pos*2.576);
n = smoothstep(-0.096, 1.176, n);
outColor = vec4(vec3(n), 1);
}

View File

@ -62,6 +62,7 @@ GLuint programTex;
GLuint programPbr; GLuint programPbr;
GLuint programSun; GLuint programSun;
GLuint programSkyBox; GLuint programSkyBox;
GLuint programNoise;
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
@ -256,6 +257,18 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t
glUseProgram(0); glUseProgram(0);
} }
void drawObjectNoise(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, float roughness, float metallic) {
glUseProgram(programNoise);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(programNoise, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(programNoise, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
Core::DrawContext(context);
glUseProgram(0);
}
void renderScene(GLFWwindow* window) { void renderScene(GLFWwindow* window) {
glClearColor(0.5f, 0.0f, 0.25f, 1.0f); glClearColor(0.5f, 0.0f, 0.25f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -268,6 +281,7 @@ void renderScene(GLFWwindow* window) {
//drawPlanet(sphereContext, planetTranslate * planetRotate * planetScale, planetTex); //drawPlanet(sphereContext, planetTranslate * planetRotate * planetScale, planetTex);
drawObjectPBR(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal); drawObjectPBR(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal);
drawObjectNoise(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal);
//rysowanie słońca //rysowanie słońca
glm::mat4 sunScale = glm::scale(glm::vec3(sunSize)); glm::mat4 sunScale = glm::scale(glm::vec3(sunSize));
@ -316,6 +330,7 @@ void init(GLFWwindow* window) {
programPbr = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_pbr.frag"); programPbr = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_pbr.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.frag"); programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.frag");
programSkyBox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); programSkyBox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
programNoise = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_smap.frag");
loadModelToContext("./models/sphere.obj", sphereContext); loadModelToContext("./models/sphere.obj", sphereContext);
loadModelToContext("./models/cube.obj", cubeContext); loadModelToContext("./models/cube.obj", cubeContext);