Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (800)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (866)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [libgdx] SSAO shader black screen  (Read 301 times)
0 Members and 1 Guest are viewing this topic.
Offline fvs

Innocent Bystander





« Posted 2019-12-26 03:23:54 »

Hello everyone, I've been trying to implement the SSAO shader for several days without success. Maybe someone can help me.


I am using this fragment shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
uniform sampler2D texture0;
uniform sampler2D texture1;

uniform vec2 camerarange;
uniform vec2 screensize;

float readDepth( in vec2 coord ) {
    return (2.0 * camerarange.x) / (camerarange.y + camerarange.x - texture2D( texture0, coord ).x * (camerarange.y - camerarange.x));
}


void main(void)
{
    vec2 texCoord = gl_TexCoord[0].st;
    //vec2 texCoord = texture2D(texture0, gl_TexCoord[0].st).xy;
    //vec3 texColor = texture2D(texture1, gl_TexCoord[0].st).rgb;

    float depth = readDepth( texCoord );
    float d;

    float pw = 1.0 / screensize.x;
    float ph = 1.0 / screensize.y;

    float aoCap = 0.45;

    float ao = 0.0;

    float aoMultiplier=1000.0;

    float depthTolerance = 0.00001;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    ao/=16.0;

    gl_FragColor = vec4(1.2-ao) * texture2D(texture1, texCoord);
}


And this vertex :
1  
2  
3  
4  
5  
6  
#version 110

void main(){
   gl_Position = ftransform();
   gl_TexCoord[0] = gl_MultiTexCoord0;
}


RENDER:

First I get the depthmap and scene textures from a framebuffer. And this is the result
1  
2  
3  
4  
5  
6  
7  
8  
9  
        fbo.begin();
        Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        modelBatch.begin(cam);
        modelBatch.render(models);
        modelBatch.end();
        fbo.end();
        Texture diffuseText = fbo.getTextureAttachments().get(DIFFUSE_ATTACHMENT);
        Texture depthText = fbo.getTextureAttachments().get(DEPTH_ATTACHMENT);


diffuse : https://i.imgur.com/fj2oPmT.png
depthMap: https://i.imgur.com/1Q9Zf3n.png

Next step, I apply the ssao shader  and render it in a full screen quad
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        depthText .bind(0);
        diffuseText .bind(1);
        shaderProgramSSAO.begin();
        shaderProgramSSAO.setUniformi("texture0", 0);
        shaderProgramSSAO.setUniformi("texture1", 1);
        shaderProgramSSAO.setUniformf("camerarange", new Vector2(cam.near, cam.far)); // near=1, far=20
        shaderProgramSSAO.setUniformf("screensize", new Vector2(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
        mesh.render(shaderProgramSSAO, GL20.GL_TRIANGLES);
        shaderProgramSSAO.end();


But the only result I get is a black screen.

Any suggestions? What am I doing wrong?
Thanks in advance
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (337 views)
2019-09-04 15:33:17

hadezbladez (5118 views)
2018-11-16 13:46:03

hadezbladez (2000 views)
2018-11-16 13:41:33

hadezbladez (5388 views)
2018-11-16 13:35:35

hadezbladez (1113 views)
2018-11-16 13:32:03

EgonOlsen (4527 views)
2018-06-10 19:43:48

EgonOlsen (5383 views)
2018-06-10 19:43:44

EgonOlsen (3075 views)
2018-06-10 19:43:20

DesertCoockie (3969 views)
2018-05-13 18:23:11

nelsongames (4519 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!