Skip to content

Commit

Permalink
Much better AO and working screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMoroz committed Aug 17, 2019
1 parent a0b9426 commit afc39f6
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
2 changes: 1 addition & 1 deletion game_folder/shaders/compute/MRRM3.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ void main() {
//color = vec4(rad);

//save the DE sphere
imageStore(DE_output, global_pos, HDRmapping(color.xyz, Camera.exposure, 0.5));
imageStore(DE_output, global_pos, HDRmapping(color.xyz, Camera.exposure, 2.2));
}
2 changes: 1 addition & 1 deletion game_folder/shaders/compute/ray_marching.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define MAX_DIST 50
#define MIN_DIST 1e-6
#define MAX_MARCHES 150
#define MAX_MARCHES 200
#define NORMARCHES 2


Expand Down
19 changes: 14 additions & 5 deletions game_folder/shaders/compute/shading.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include<ray_marching.glsl>

#define PI 3.14159265
#define AMBIENT_MARCHES 3
#define BACKGROUND_COLOR 30*vec4(0.2,0.3,1,1)
#define AMBIENT_MARCHES 12
#define BACKGROUND_COLOR 6*vec4(1,1,1,1)

//better to use a sampler though
vec4 interp(layout (rgba32f) image2D text, vec2 coord)
Expand Down Expand Up @@ -65,21 +65,30 @@ vec4 shading(in vec4 pos, in vec4 dir, float fov)
float error = fov*dir.w;
vec4 norm = calcNormal(pos.xyz, error/8);
norm.xyz = normalize(norm.xyz);
pos.w = 30*error;
pos.w = iMarbleRad/2 + error;

float dcoef = 0.01/iMarbleRad;

vec3 albedo = COL(pos.xyz - norm.w*norm.xyz).xyz;

float occlusion_angle = 1;
float occlusion_angle = 0;
float integral = 0;
float i_coef = 0;

//march in the direction of the normal
#pragma unroll
for(int i = 0; i < AMBIENT_MARCHES; i++)
{
norm.w += pos.w;
pos.xyz += pos.w*norm.xyz;
pos.w = DE(pos.xyz);
occlusion_angle = occlusion_angle*0.5 + 0.5*min(occlusion_angle, pos.w/norm.w);
i_coef = 1/(dcoef*norm.w+1);//importance
occlusion_angle += i_coef*pos.w/norm.w;
integral += i_coef;
}

occlusion_angle /= integral; // average weighted by importance

//square because its a surface
vec4 ambient_color = BACKGROUND_COLOR*pow(occlusion_angle,2);

Expand Down
8 changes: 7 additions & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,22 @@ int main(int argc, char *argv[]) {
screenshot_i++;
//Update the shader values
scene.SetResolution(shader, sres_res.x, sres_res.y);
rend.ReInitialize(sres_res.x, sres_res.y);
scene.WriteRenderer(rend);
shader.setUniform("render_texture", screenshot_txt);
rend.SetOutputTexture(screenshot_txt);
scene.Write(shader);

//Setup full-screen shader
sf::RenderStates states = sf::RenderStates::Default;
states.shader = &shader;
shader.setUniform("render_texture", screenshot_txt);
//shader.setUniform("render_texture", screenshot_txt);

window.setActive(false);
//Draw the fractal
//Draw to the render texture
screenshotTexture.setActive(true);
rend.Render();
screenshotTexture.draw(rect_scrshot, states);
screenshotTexture.display();
screenshotTexture.getTexture().copyToImage().saveToFile((std::string)"screenshots/screenshot"+(std::string)num2str(time(NULL))+".jpg");
Expand All @@ -371,6 +376,7 @@ int main(int argc, char *argv[]) {
window.setActive(true);

scene.SetResolution(shader, window_res.x, window_res.y);
rend.ReInitialize(window_res.x, window_res.y);
scene.Write(shader);
} else if (keycode == sf::Keyboard::F4) {
overlays.TWBAR_ENABLED = !overlays.TWBAR_ENABLED;
Expand Down
71 changes: 71 additions & 0 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,77 @@ void Renderer::Initialize(int w, int h, std::string compute_folder)
config.close();
}

void Renderer::ReInitialize(int w, int h)
{
shader_textures.clear();
global_size.clear();

width = w;
height = h;
camera.SetResolution(vec2(w, h));
camera.SetAspectRatio((float)w / (float)h);

ExprParser parser;

std::ifstream config(shader_folder + "/pipeline.cfg");
if (config.fail())
{
ERROR_MSG("Error opening pipeline configuration");
return;
}
std::string line;

int element = 0;
int cur_shader = 0;

std::map<std::string, float> variables;
variables["width"] = width;
variables["height"] = height;

std::vector<GLuint> stage_textures;
std::string shader_file;
vec2 global, tex_resolution;
while (std::getline(config, line))
{
if (line.substr(0, 1) != "#")
{
parser.Parse(line);
switch (element++)
{
case 0:
//shader_file = compute_folder + "/" + line;
// LoadShader(shader_file);
break;
case 1:
global.x = ceil(parser.Evaluate(variables));
break;
case 2:
global.y = ceil(parser.Evaluate(variables));
break;
case 3:
tex_resolution.x = ceil(parser.Evaluate(variables));
break;
case 4:
tex_resolution.y = ceil(parser.Evaluate(variables));
break;
case 5:
int tnum = parser.Evaluate(variables);
for (int i = 0; i < tnum; i++)
{
stage_textures.push_back(GenerateTexture(tex_resolution.x, tex_resolution.y));
}
shader_textures.push_back(stage_textures);
stage_textures.clear();
global_size.push_back(global);
element = 0;
break;
}
}
}

config.close();
}

void Renderer::SetOutputTexture(sf::Texture & tex)
{
shader_textures[shader_textures.size()-1][0] = tex.getNativeHandle();
Expand Down
1 change: 1 addition & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Renderer
Renderer();

void Initialize(int w, int h, std::string compute_folder);
void ReInitialize(int w, int h);

void SetOutputTexture(sf::Texture& tex);
void LoadShader(std::string shader_file);
Expand Down

0 comments on commit afc39f6

Please sign in to comment.