mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-28 02:58:35 +02:00
~ | Weird random, needs to be upgrade
This commit is contained in:
@ -29,7 +29,7 @@ class Shader
|
|||||||
|
|
||||||
// void setBool(const std::string &name, bool value) const;
|
// void setBool(const std::string &name, bool value) const;
|
||||||
void set_int(const std::string &name, int value) const;
|
void set_int(const std::string &name, int value) const;
|
||||||
// void setFloat(const std::string &name, float value) const;
|
void set_float(const std::string &name, float value) const;
|
||||||
void set_vec2(const std::string &name, const glm::vec2 &value) const;
|
void set_vec2(const std::string &name, const glm::vec2 &value) const;
|
||||||
void set_vec3(const std::string &name, const glm::vec3 &value) const;
|
void set_vec3(const std::string &name, const glm::vec3 &value) const;
|
||||||
// void setVec4(const std::string &name, const RT::Vec4f &value) const;
|
// void setVec4(const std::string &name, const RT::Vec4f &value) const;
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
|
|
||||||
MAT 255 255 255 0.0 1.0 2.0
|
MAT 255 255 255 0.0 1.0 2.0
|
||||||
MAT 255 255 255 1.0 1.0 2.0
|
MAT 255 255 255 3.0 1.0 2.0
|
||||||
MAT 255 0 0 0.0 1.0 2.0
|
MAT 255 0 0 0.0 1.0 2.0
|
||||||
|
|
||||||
sp 0 -50 -6 50.0 0
|
sp 0 -50 -6 50.0 0
|
||||||
sp 50 30 -6 30.0 1
|
sp 50 30 -6 30.0 1
|
||||||
sp 0 1 -2 1.5 2
|
|
||||||
|
sp 0 1 -2 1.5 0
|
||||||
|
sp 0 2 -5 1.5 2
|
||||||
|
sp 0 4 -1 1.5 2
|
||||||
|
|
||||||
|
|
||||||
R 1.0 -2.0 10
|
R 1.0 -2.0 10
|
||||||
|
@ -26,6 +26,7 @@ uniform vec2 u_resolution;
|
|||||||
uniform vec3 u_cameraPosition;
|
uniform vec3 u_cameraPosition;
|
||||||
uniform mat4 u_viewMatrix;
|
uniform mat4 u_viewMatrix;
|
||||||
uniform int u_frameCount;
|
uniform int u_frameCount;
|
||||||
|
uniform float u_time;
|
||||||
|
|
||||||
vec3 lightPos = vec3(5.0, 5.0, 5.0);
|
vec3 lightPos = vec3(5.0, 5.0, 5.0);
|
||||||
vec3 lightColor = vec3(1.0, 1.0, 1.0);
|
vec3 lightColor = vec3(1.0, 1.0, 1.0);
|
||||||
@ -118,7 +119,7 @@ vec3 pathtrace(Ray ray, vec2 random)
|
|||||||
|
|
||||||
ray.origin = hit.position + hit.normal * 0.001;
|
ray.origin = hit.position + hit.normal * 0.001;
|
||||||
//cosine weighted importance sampling
|
//cosine weighted importance sampling
|
||||||
vec3 unit_sphere = normalize(randomVec3Mixed(random, u_frameCount, -1.0, 1.0));
|
vec3 unit_sphere = normalize(randomVec3(random, u_time));
|
||||||
if (dot(unit_sphere, hit.normal) < 0.0)
|
if (dot(unit_sphere, hit.normal) < 0.0)
|
||||||
unit_sphere = -unit_sphere;
|
unit_sphere = -unit_sphere;
|
||||||
ray.direction = normalize(hit.normal + unit_sphere);
|
ray.direction = normalize(hit.normal + unit_sphere);
|
||||||
|
@ -1,28 +1,44 @@
|
|||||||
|
float rand_seed = 0;
|
||||||
|
|
||||||
float seed = 1.0;
|
uint hash( uint x ) {
|
||||||
float getRandom(vec2 uv, int frameCount)
|
x += ( x << 10u );
|
||||||
{
|
x ^= ( x >> 6u );
|
||||||
float seed = dot(uv, vec2(12.9898, 78.233)) + float(frameCount);
|
x += ( x << 3u );
|
||||||
return fract(sin(seed) * 43758.5453);
|
x ^= ( x >> 11u );
|
||||||
}
|
x += ( x << 15u );
|
||||||
vec3 randomVec3(vec2 uv, int frameCount)
|
return x;
|
||||||
{
|
|
||||||
return vec3(
|
|
||||||
getRandom(uv + vec2(0.1, 0.1), frameCount),
|
|
||||||
getRandom(uv + vec2(0.2, 0.2), frameCount),
|
|
||||||
getRandom(uv + vec2(0.3, 0.3), frameCount)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 randomVec3Mixed(vec2 uv, int frameCount, float min_val, float max_val)
|
uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y) ); }
|
||||||
{
|
uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ); }
|
||||||
float randomX = getRandom(uv + vec2(0.1, 0.1), frameCount);
|
uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }
|
||||||
float randomY = getRandom(uv + vec2(0.2, 0.2), frameCount);
|
|
||||||
float randomZ = getRandom(uv + vec2(0.3, 0.3), frameCount);
|
|
||||||
|
|
||||||
return vec3(
|
float floatConstruct( uint m ) {
|
||||||
mix(min_val, max_val, randomX),
|
const uint ieeeMantissa = 0x007FFFFFu;
|
||||||
mix(min_val, max_val, randomY),
|
const uint ieeeOne = 0x3F800000u;
|
||||||
mix(min_val, max_val, randomZ)
|
|
||||||
);
|
m &= ieeeMantissa;
|
||||||
|
m |= ieeeOne;
|
||||||
|
|
||||||
|
float f = uintBitsToFloat( m );
|
||||||
|
return f - 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float randombis( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }
|
||||||
|
float randombis( vec2 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
|
||||||
|
float randombis( vec3 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
|
||||||
|
float randombis( vec4 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
|
||||||
|
|
||||||
|
float random(vec2 uv, float time)
|
||||||
|
{
|
||||||
|
if (rand_seed == 0)
|
||||||
|
rand_seed = time;
|
||||||
|
rand_seed *= 2;
|
||||||
|
return randombis(vec3(uv.xy, time * rand_seed));
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 randomVec3(vec2 uv, float time)
|
||||||
|
{
|
||||||
|
return (vec3((random(uv, time) - 0.5) * 2, (random(uv, time) - 0.5) * 2, (random(uv, time) - 0.5) * 2));
|
||||||
}
|
}
|
@ -44,6 +44,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
shader.set_int("u_frameCount", window.getFrameCount());
|
shader.set_int("u_frameCount", window.getFrameCount());
|
||||||
shader.set_int("u_objectsNum", gpu_data.size());
|
shader.set_int("u_objectsNum", gpu_data.size());
|
||||||
|
shader.set_float("u_time", (float)(glfwGetTime()));
|
||||||
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
||||||
shader.set_vec3("u_cameraPosition", scene.getCamera()->get_position());
|
shader.set_vec3("u_cameraPosition", scene.getCamera()->get_position());
|
||||||
shader.set_mat4("u_viewMatrix", scene.getCamera()->get_view_matrix());
|
shader.set_mat4("u_viewMatrix", scene.getCamera()->get_view_matrix());
|
||||||
|
@ -167,6 +167,10 @@ void Shader::set_int(const std::string &name, int value) const
|
|||||||
{
|
{
|
||||||
glUniform1i(glGetUniformLocation(_program_compute, name.c_str()), value);
|
glUniform1i(glGetUniformLocation(_program_compute, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
void Shader::set_float(const std::string &name, float value) const
|
||||||
|
{
|
||||||
|
glUniform1f(glGetUniformLocation(_program_compute, name.c_str()), value);
|
||||||
|
}
|
||||||
void Shader::set_vec2(const std::string &name, const glm::vec2 &value) const
|
void Shader::set_vec2(const std::string &name, const glm::vec2 &value) const
|
||||||
{
|
{
|
||||||
glUniform2fv(glGetUniformLocation(_program_compute, name.c_str()), 1, glm::value_ptr(value));
|
glUniform2fv(glGetUniformLocation(_program_compute, name.c_str()), 1, glm::value_ptr(value));
|
||||||
|
@ -90,7 +90,7 @@ void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
|
|||||||
{
|
{
|
||||||
win->_scene->getCamera()->process_mouse(xoffset, yoffset, true);
|
win->_scene->getCamera()->process_mouse(xoffset, yoffset, true);
|
||||||
|
|
||||||
// scene.frameCount = 0;
|
win->_frameCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
lastX = xpos;
|
lastX = xpos;
|
||||||
|
Reference in New Issue
Block a user