first draft of SSAO

This commit is contained in:
Paolo Cignoni cignoni 2009-07-27 17:36:32 +00:00
parent 616a4096a2
commit e2f9233c41
10 changed files with 112 additions and 137 deletions

View File

@ -85,9 +85,9 @@ bool DecorateShadowPlugin::StartDecorate(QAction* action, MeshModel& m, GLArea*
}
/*if(action->text() == ST(DP_SHOW_SSAO)){
if(action->text() == ST(DP_SHOW_SSAO)){
result = this->_decorator = new SSAO();
}*/
}
result = this->_decorator->init();
return result;

View File

@ -39,7 +39,7 @@
#include "shadow_mapping.h"
#include "variance_shadow_mapping.h"
#include "variance_shadow_mapping_blur.h"
//#include "ssao.h"
#include "ssao.h"
class DecorateShadowPlugin : public QObject, public MeshDecorateInterface
{
@ -64,8 +64,8 @@ public:
typeList <<
DP_SHOW_SIMPLE_SHADOW <<
DP_SHOW_VSM_SHADOW <<
DP_SHOW_VSM_SHADOW_BLUR; /*<<
DP_SHOW_SSAO;*/
DP_SHOW_VSM_SHADOW_BLUR <<
DP_SHOW_SSAO;
FilterIDType tt;
foreach(tt , types()){

View File

@ -5,8 +5,8 @@ HEADERS = decorate_shadow.h \
../../../../vcglib/wrap/gui/trackball.h \
decorate_shader.h \
variance_shadow_mapping.h \
variance_shadow_mapping_blur.h
#ssao.h
variance_shadow_mapping_blur.h \
ssao.h
SOURCES = decorate_shadow.cpp \
shadow_mapping.cpp \
../../meshlab/filterparameter.cpp \
@ -14,6 +14,6 @@ SOURCES = decorate_shadow.cpp \
../../../../vcglib/wrap/gui/trackball.cpp \
../../../../vcglib/wrap/gui/trackmode.cpp \
variance_shadow_mapping.cpp \
variance_shadow_mapping_blur.cpp
#ssao.cpp
variance_shadow_mapping_blur.cpp \
ssao.cpp
TARGET = decorate_shadow

View File

@ -1,7 +1,7 @@
uniform sampler2D scene;
uniform vec2 scale;
void main() {
vec4 color = vec4(vec3(0.0), 1.0);
vec4 color = vec4(vec3(0.0), 0.0);
color += texture2D( scene, gl_TexCoord[0].st + vec2( -3.0 * scale.x, -3.0 * scale.y ) ) * 0.015625;
color += texture2D( scene, gl_TexCoord[0].st + vec2( -2.0 * scale.x, -2.0 * scale.y ) )*0.09375;
@ -11,7 +11,8 @@ void main() {
color += texture2D( scene, gl_TexCoord[0].st + vec2( 2.0 * scale.x, 2.0 * scale.y ) )*0.09375;
color += texture2D( scene, gl_TexCoord[0].st + vec2( 3.0 * scale.x, 3.0 * scale.y ) ) * 0.015625;
if(color.a < 0.5)
discard;
//gl_FragColor = color;//vec4(color.xyz, (0.5 - color.x));
if(scale.y == 0.0)
gl_FragColor = color;//vec4(color.xyz, 0.5);

View File

@ -1,13 +1,7 @@
varying vec3 normal;
varying vec4 point;
//varying float depth;
void main(){
float depth = point.z;// / point.w;
depth = depth * 0.5 + 0.5;
vec3 n = normalize(normal);// * 0.5 + 0.5;
gl_FragColor = vec4(vec3(n), gl_FragCoord.z);
//gl_FragColor = vec4(normal, 1.0);
//gl_FragColor = vec4(vec3(depth), 1.0);
gl_FragColor = vec4(vec3(normal), depth);
}

View File

@ -1,20 +1,7 @@
/*uniform float farPlane;
uniform float nearPlane;
uniform float diag;*/
varying vec3 normal;
varying vec4 point;
//varying float depth;
void main(){
normal = normalize(gl_NormalMatrix * gl_Normal);
//normal = gl_Normal;
normal = (gl_Normal + 1.0) / 2.0;
gl_Position = ftransform();
point = gl_Position;
//depth = gl_Position.z;
//depth = (gl_Position.z + nearPlane) / farPlane;
//depth = (gl_Position.z/ farPlane) + nearPlane;
//depth = (gl_Position.z * (nearPlane * farPlane/ nearPlane + farPlane))// + (nearPlane);// + nearPlane);
//depth = gl_Position / diag;
}

View File

@ -1,23 +1,43 @@
uniform sampler2D rnm;
uniform sampler2D normalMap;
varying vec2 uv;
varying vec4 texCoord;
const float totStrength = 1.38;
const float strength = 0.07;
const float totStrength = 5.38;
const float strength = 0.7;
const float offset = 18.0;
const float falloff = 0.000002;
const float rad = 0.006;
#define SAMPLES 10 // 10 is good
const float invSamples = -1.38/10.0;
const float rad = 0.005;
#define SAMPLES 16 // 10 is good
const float invSamples = -1.38/2.0;
void main(void)
{
vec4 texCoordPostW = texCoord / texCoord.w;
texCoordPostW = texCoordPostW * 0.5 + 0.5;
vec2 uv = texCoordPostW.xy;
// these are the random vectors inside a unit sphere
vec3 pSphere[10] = vec3[](vec3(-0.010735935, 0.01647018, 0.0062425877),vec3(-0.06533369, 0.3647007, -0.13746321),vec3(-0.6539235, -0.016726388, -0.53000957),vec3(0.40958285, 0.0052428036, -0.5591124),vec3(-0.1465366, 0.09899267, 0.15571679),vec3(-0.44122112, -0.5458797, 0.04912532),vec3(0.03755566, -0.10961345, -0.33040273),vec3(0.019100213, 0.29652783, 0.066237666),vec3(0.8765323, 0.011236004, 0.28265962),vec3(0.29264435, -0.40794238, 0.15964167));
vec3 pSphere[16] = vec3[](
vec3(-0.010735935, 0.01647018, 0.0062425877),
vec3(-0.06533369, 0.3647007, -0.13746321),
vec3(-0.6539235, -0.016726388, -0.53000957),
vec3(0.40958285, 0.0052428036, -0.5591124),
vec3(-0.1465366, 0.09899267, 0.15571679),
vec3(-0.44122112, -0.5458797, 0.04912532),
vec3(0.03755566, -0.10961345, -0.33040273),
vec3(0.019100213, 0.29652783, 0.066237666),
vec3(0.8765323, 0.011236004, 0.28265962),
vec3(0.29264435, -0.40794238, 0.15964167),
vec3(0.50958289, 0.2424578036, -0.777556324),
vec3(-0.5476366, 0.09554684, 0.157546479),
vec3(0.445354129, -0.54557427, -0.02452532),
vec3(0.274621116, -0.991025345, -0.25477073),
vec3(0.757454103, 0.00575283, -0.2236553),
vec3(0.8325432003, -0.011354804, 0.9564872)
);
// grab a normal for reflecting the sample rays later on
vec3 fres = normalize((texture2D(rnm,uv*offset).xyz*2.0) - vec3(1.0));
vec3 fres = normalize((texture2D(rnm , uv * offset).xyz * 2.0) - vec3(1.0));
vec4 currentPixelSample = texture2D(normalMap,uv);
@ -51,11 +71,15 @@ void main(void)
bl += step(falloff,depthDifference)*(1.0-dot(occluderFragment.xyz,norm))*(1.0-smoothstep(falloff,strength,depthDifference));
}
// output the result
//gl_FragColor.r = 1.0+bl*invSamples;
float ao = 1.0 + totStrength * bl * invSamples;
/*if(ao > 0.7)
discard;
gl_FragColor = vec4(vec3(0.0), 0.7 - ao);*/
float ao = 1.0+bl*invSamples;
gl_FragColor = vec4(vec3(ao), 1.0);
//gl_FragColor = 1.0;
float alpha = 1.0;
if((norm.x + norm.y + norm.z) == 0.0)
alpha = 0.0;
gl_FragColor = vec4(vec3(ao), alpha);
}

View File

@ -1,13 +1,14 @@
uniform mat4 mvpl;
varying vec2 uv;
//varying vec2 uv;
varying vec4 texCoord;
void main(void)
{
//gl_Position = ftransform();
gl_Position = mvpl * gl_Vertex;
gl_Position = sign( gl_Position );
gl_Position = ftransform();
texCoord = gl_Position;
// gl_Position = mvpl * gl_Vertex;
//gl_Position = sign( gl_Position );
// Texture coordinate for screen aligned (in correct range):
uv = (vec2( gl_Position.x, gl_Position.y ) + vec2( 1.0 ) ) * 0.5;
// uv = (vec2( gl_Position.x, gl_Position.y ) + vec2( 1.0 ) ) * 0.5;
//uv = vec2( gl_Position.x, gl_Position.y )+ vec2( 1.0 ) * 0.5;
}

View File

@ -15,7 +15,7 @@ SSAO::SSAO():DecorateShader()
this->_ssaoShaderProgram = 0;
this->_blurH = 0;
// this->_blurV = 0;
this->_blurV = 0;
this->_blurVert = 0;
this->_blurFrag = 0;
this->_blurShaderProgram = 0;
@ -49,7 +49,7 @@ SSAO::~SSAO(){
glDeleteTexturesEXT(1, &(this->_ssao));
glDeleteTexturesEXT(1, &(this->_blurH));
// glDeleteTexturesEXT(1, &(this->_blurV));
glDeleteTexturesEXT(1, &(this->_blurV));
glDeleteFramebuffersEXT(1, &_fbo);
}
@ -79,83 +79,26 @@ void SSAO::runShader(MeshModel& m, GLArea* gla){
/***********************************************************/
//GENERAZIONE SHADOW MAP
/***********************************************************/
// glEnable(GL_POLYGON_OFFSET_FILL);
// glPolygonOffset(1.0, 1.0);
GLfloat g_mModelView[16];
GLfloat g_mProjection[16];
vcg::Box3f bb = m.cm.bbox;
vcg::Point3f center;
center = bb.Center();
float diag = bb.Diag();
vcg::Matrix44f tm = gla->trackball.Matrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(-(diag/2),
diag/2,
-(diag/2),
diag/2,
-(diag/2),
diag/2);
glGetFloatv(GL_PROJECTION_MATRIX, g_mProjection);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
vcg::Matrix44f rotation;
vcg::Similarityf track = gla->trackball.track;
track.rot.ToMatrix(rotation);
glMultMatrixf(rotation.transpose().V());
//traslate the model in the center
glTranslatef(-center[0],-center[1],-center[2]);
glGetFloatv(GL_MODELVIEW_MATRIX, g_mModelView);
this->bind();
glUseProgram(this->_normalMapShaderProgram);
/*GLuint farLoc = glGetUniformLocation(this->_normalMapShaderProgram, "farPlane");
glUniform1f(farLoc, gla->farPlane);
GLuint nearLoc = glGetUniformLocation(this->_normalMapShaderProgram, "nearPlane");
glUniform1f(nearLoc, gla->nearPlane);
GLuint diagLoc = glGetUniformLocation(this->_normalMapShaderProgram, "diag");
glUniform1f(diagLoc, m.cm.bbox.Diag());
*/
RenderMode rm = gla->getCurrentRenderMode();
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m.Render(rm.drawMode, vcg::GLW::CMNone, vcg::GLW::TMNone);
// glDisable(GL_POLYGON_OFFSET_FILL);
this->printColorMap(this->_normalMap, "_normalMap.png");
//this->printColorMap(this->_normalMap, "_normalMap.png");
//this->unbind();
glUseProgram(0);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
/***********************************************************/
//SSAO PASS
/***********************************************************/
/*GLint depthFuncOld;
glGetIntegerv(GL_DEPTH_FUNC, &depthFuncOld);
glDepthFunc(GL_LEQUAL);*/
vcg::Matrix44f mvpl = (vcg::Matrix44f(g_mProjection).transpose() * vcg::Matrix44f(g_mModelView).transpose()).transpose();
//this->unbind();
glUseProgram(this->_ssaoShaderProgram);
GLuint matrixLoc = glGetUniformLocation(this->_ssaoShaderProgram, "mvpl");
glUniformMatrix4fv(matrixLoc, 1, 0, mvpl.V());
/*glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);*/
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->_noise);
@ -163,6 +106,7 @@ GLfloat g_mModelView[16];
glUniform1i(noiseloc, 0);
glActiveTexture(GL_TEXTURE1);
//glBindTexture(GL_TEXTURE_2D, this->_blurV);
glBindTexture(GL_TEXTURE_2D, this->_normalMap);
GLuint loc = glGetUniformLocation(this->_ssaoShaderProgram, "normalMap");
glUniform1i(loc, 1);
@ -170,12 +114,19 @@ GLfloat g_mModelView[16];
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m.Render(rm.drawMode, vcg::GLW::CMNone, vcg::GLW::TMNone);
m.Render(rm.drawMode, rm.colorMode, vcg::GLW::TMNone);
this->printColorMap(this->_ssao, "_ssao1.png");
//this->printColorMap(this->_ssao, "_ssao.png");
//this->unbind();
/*glEnable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
*/
glUseProgram(0);
/****************************************************************************************/
// BLURRING
/****************************************************************************************/
@ -193,15 +144,15 @@ GLfloat g_mModelView[16];
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
//vcg::Point3f t = track.tra;
//glTranslated(0,0,t[2]);
glTranslated(0,0,-1);
/***********************************************************/
//BLURRING horizontal
/***********************************************************/
glUseProgram(this->_blurShaderProgram);
GLfloat scale = 1/(this->_texSize * BLUR_COEF);// * SHADOW_COEF);
float blur_coef = 0.8;
GLfloat scale = 1/(this->_texSize * blur_coef);// * SHADOW_COEF);
//GLfloat scale = (1/(this->_texSize)) * BLUR_COEF * SHADOW_COEF;
GLuint scaleLoc = glGetUniformLocation(this->_blurShaderProgram, "scale");
glUniform2f(scaleLoc, scale, 0.0);
@ -225,23 +176,23 @@ GLfloat g_mModelView[16];
glVertex3f(-this->_texSize/2,this->_texSize/2,0);
glEnd();
this->printColorMap(this->_blurH, "./_blurOrizzontale.png");
this->unbind();
//this->printColorMap(this->_blurH, "./_blurOrizzontale.png");
//this->unbind();
/***********************************************************/
//BLURRING vertical
/***********************************************************/
this->unbind();
glUniform2f(scaleLoc, 0.0, scale);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, this->_blurH);
loc = glGetUniformLocation(this->_blurShaderProgram, "scene");
glUniform1i(loc, 0);
/*glDrawBuffer(GL_COLOR_ATTACHMENT3_EXT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);*/
glBegin(GL_QUADS);
glTexCoord2d(0,0);
glVertex3f(-this->_texSize/2,-this->_texSize/2,0);
@ -253,12 +204,12 @@ GLfloat g_mModelView[16];
glVertex3f(-this->_texSize/2,this->_texSize/2,0);
glEnd();
//glDepthFunc((GLenum)depthFuncOld);
glUseProgram(0);
//this->printColorMap(this->_blurV, "_blurVericale.png");
//this->unbind();
glEnable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glUseProgram(0);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
@ -268,6 +219,8 @@ GLfloat g_mModelView[16];
// BLURRING END
/****************************************************************************************/
int error = glGetError();
}
@ -311,7 +264,6 @@ bool SSAO::setup()
//attacco al FBO la texture di colore
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, this->_ssao, 0);
//genero la texture di blur orizzontale.
glGenTextures(1, &this->_blurH);
glBindTexture(GL_TEXTURE_2D, this->_blurH);
@ -326,6 +278,22 @@ bool SSAO::setup()
//e la texture per il blur orizzontale
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, this->_blurH, 0);
//genero la texture di blur verticale.
glGenTextures(1, &this->_blurV);
glBindTexture(GL_TEXTURE_2D, this->_blurV);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, this->_texSize, this->_texSize, 0, GL_RGB, GL_FLOAT, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this->_texSize, this->_texSize, 0, GL_RGBA, GL_FLOAT, NULL);
//e la texture per il blur orizzontale
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT, GL_TEXTURE_2D, this->_blurV, 0);
//genero il render buffer per il depth buffer
glGenRenderbuffersEXT(1, &(this->_depth));
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, this->_depth);
@ -335,7 +303,7 @@ bool SSAO::setup()
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, this->_depth);
//GLenum drawBuffers[] = {this->_normalMap, this->_ssao, this->_blurH};
GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3};
glDrawBuffersARB(3, drawBuffers);
this->loadNoiseTxt();

View File

@ -58,7 +58,7 @@ private:
GLuint _ssao;
GLuint _noise;
GLuint _blurH;
//GLuint _blurV;
GLuint _blurV;
GLuint _depth;