From 2cc2285eb12f9af73456bea8ee31fa603f4afc19 Mon Sep 17 00:00:00 2001 From: psperl Date: Wed, 16 Jul 2008 01:14:53 +0000 Subject: [PATCH] Blur implemented (mostly) properly git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@1101 6778bc44-b910-0410-a7a0-be141de4315d --- src/projectM-engine/CMakeLists.txt | 2 +- src/projectM-engine/Renderer.cpp | 223 ++++++++++++++++++++++++++++- src/projectM-engine/Renderer.hpp | 28 ++-- src/projectM-engine/blur.cg | 46 ++++++ src/projectM-engine/projectM.cg | 50 ++----- 5 files changed, 300 insertions(+), 49 deletions(-) create mode 100644 src/projectM-engine/blur.cg diff --git a/src/projectM-engine/CMakeLists.txt b/src/projectM-engine/CMakeLists.txt index 0b155a289..1027637a1 100644 --- a/src/projectM-engine/CMakeLists.txt +++ b/src/projectM-engine/CMakeLists.txt @@ -149,7 +149,7 @@ FILE(GLOB presets "presets/*.milk" "presets/*.prjm" "presets/*.tga") INSTALL(FILES ${presets} DESTINATION ${CMAKE_INSTALL_PREFIX}/share/projectM/presets) FILE(GLOB fonts "fonts/*.ttf") INSTALL(FILES ${fonts} DESTINATION ${CMAKE_INSTALL_PREFIX}/share/projectM/fonts) -INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/projectM.cg DESTINATION ${CMAKE_INSTALL_PREFIX}/share/projectM/shaders) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/projectM.cg ${CMAKE_CURRENT_SOURCE_DIR}/blur.cg DESTINATION ${CMAKE_INSTALL_PREFIX}/share/projectM/shaders) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/libprojectM.pc DESTINATION ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/pkgconfig) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/config.inp DESTINATION ${CMAKE_INSTALL_PREFIX}/share/projectM) INSTALL(FILES projectM.hpp event.h dlldefs.h fatal.h PresetFrameIO.hpp PCM.hpp DESTINATION include/libprojectM) diff --git a/src/projectM-engine/Renderer.cpp b/src/projectM-engine/Renderer.cpp index f56a94bc2..a06efb854 100644 --- a/src/projectM-engine/Renderer.cpp +++ b/src/projectM-engine/Renderer.cpp @@ -112,7 +112,34 @@ SetupCg(); std::cout<<"Generating Noise Textures"<texsize, renderTarget->texsize, 1/(float)renderTarget->texsize,1/(float)renderTarget->texsize); cgGLSetParameter4f(cgGetNamedParameter(program, "aspect"), 1/aspect,1,aspect,1); + cgGLSetTextureParameter(cgGetNamedParameter(program, "sampler_main"),renderTarget->textureID[1]); + cgGLEnableTextureParameter(cgGetNamedParameter(program, "sampler_main")); + cgGLSetTextureParameter(cgGetNamedParameter(program, "sampler_noise_lq_lite"),noise_texture_lq_lite); cgGLEnableTextureParameter(cgGetNamedParameter(program, "sampler_noise_lq_lite")); @@ -378,7 +465,25 @@ void Renderer::SetupCgVariables(CGprogram program, const Pipeline &pipeline, con cgGLSetTextureParameter(cgGetNamedParameter(program, "sampler_noisevol_lq"),noise_texture_lq_vol); cgGLEnableTextureParameter(cgGetNamedParameter(program, "sampler_noisevol_lq")); + if (blur1_enabled) + { + cgGLSetTextureParameter(cgGetNamedParameter(program, "sampler_blur1"),blur1_tex); + cgGLEnableTextureParameter(cgGetNamedParameter(program, "sampler_blur1")); + } + if (blur2_enabled) + { + cgGLSetTextureParameter(cgGetNamedParameter(program, "sampler_blur2"),blur2_tex); + cgGLEnableTextureParameter(cgGetNamedParameter(program, "sampler_blur2")); + } + if (blur3_enabled) + { + cgGLSetTextureParameter(cgGetNamedParameter(program, "sampler_blur3"),blur3_tex); + cgGLEnableTextureParameter(cgGetNamedParameter(program, "sampler_blur3")); + } + cgGLSetParameter4f(cgGetNamedParameter(program, "texsize_noise_lq"), 256, 256,1.0/(float)256,1.0/(float)256); + cgGLSetParameter4f(cgGetNamedParameter(program, "texsize_noise_mq"), 64, 64,1.0/(float)64,1.0/(float)64); + cgGLSetParameter4f(cgGetNamedParameter(program, "texsize_noise_hq"), 32, 32,1.0/(float)32,1.0/(float)32); cgGLSetParameter4f(cgGetNamedParameter(program, "texsize_noise_lq_lite"), 32, 32,1.0/(float)32,1.0/(float)32); } @@ -393,6 +498,117 @@ void Renderer::SetupCgQVariables(CGprogram program, const PresetOutputs &q) cgGLSetParameter4f(cgGetNamedParameter(program, "_qg"), q.q25, q.q26, q.q27, q.q28); cgGLSetParameter4f(cgGetNamedParameter(program, "_qh"), q.q29, q.q30, q.q31, q.q32); } + +void Renderer::RenderBlurTextures(const Pipeline *pipeline, const PipelineContext &pipelineContext) +{ +if(blur1_enabled || blur2_enabled || blur3_enabled) +{ + float tex[4][2] = {{0, 1}, + {0, 0}, + {1, 0}, + {1, 1}}; + + glBlendFunc(GL_ONE, GL_ZERO); + glColor4f(1.0, 1.0, 1.0, 1.0f); + + glEnable(GL_TEXTURE_2D); + + glEnableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2,GL_FLOAT,0,tex); + + cgGLEnableProfile(myCgProfile); + checkForCgError("enabling profile"); + + if (blur1_enabled) + { + cgGLSetParameter4f(cgGetNamedParameter(blur1Program, "srctexsize"), renderTarget->texsize, renderTarget->texsize, 1/(float)renderTarget->texsize,1/(float)renderTarget->texsize); + + + cgGLBindProgram(blur1Program); + checkForCgError("binding blur1 program"); + + float points[4][2] = {{0, 0}, + {0, 1}, + { 1, 1}, + { 1, 0}}; + + + glVertexPointer(2,GL_FLOAT,0,points); + + glDrawArrays(GL_TRIANGLE_FAN,0,4); + + + glBindTexture(GL_TEXTURE_2D, blur1_tex); + glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,renderTarget->texsize, renderTarget->texsize); + + cgGLUnbindProgram(myCgProfile); + checkForCgError("unbinding blur1 program"); + } + + + if (blur2_enabled) + { + cgGLSetParameter4f(cgGetNamedParameter(blur2Program, "srctexsize"), renderTarget->texsize, renderTarget->texsize, 1/(float)renderTarget->texsize,1/(float)renderTarget->texsize); + + cgGLBindProgram(blur2Program); + checkForCgError("binding blur2 program"); + + float points[4][2] = {{0, 0}, + {0, 0.5}, + { 0.5, 0.5}, + { 0.5, 0}}; + + + glVertexPointer(2,GL_FLOAT,0,points); + + glDrawArrays(GL_TRIANGLE_FAN,0,4); + + + glBindTexture(GL_TEXTURE_2D, blur2_tex); + glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,renderTarget->texsize/2, renderTarget->texsize/2); + + + } + + + if (blur3_enabled) + { + cgGLSetParameter4f(cgGetNamedParameter(blur2Program, "srctexsize"), renderTarget->texsize/2, renderTarget->texsize/2, 2/(float)renderTarget->texsize,2/(float)renderTarget->texsize); + + float points[4][2] = {{0, 0}, + {0, 0.25}, + { 0.25, 0.25}, + { 0.25, 0}}; + + + glVertexPointer(2,GL_FLOAT,0,points); + + glDrawArrays(GL_TRIANGLE_FAN,0,4); + + + glBindTexture(GL_TEXTURE_2D, blur3_tex); + glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,renderTarget->texsize/4, renderTarget->texsize/4); + } + + if (blur2_enabled || blur3_enabled) + { + cgGLUnbindProgram(myCgProfile); + checkForCgError("unbinding blur2 program"); + } + + + + cgGLDisableProfile(myCgProfile); + checkForCgError("disabling blur profile"); + + glDisable(GL_TEXTURE_2D); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + +} +} + #endif void Renderer::ResetTextures() @@ -417,8 +633,6 @@ void Renderer::SetupPass1(const Pipeline* pipeline, const PipelineContext &pipel renderTarget->lock(); glViewport( 0, 0, renderTarget->texsize, renderTarget->texsize ); - - glEnable( GL_TEXTURE_2D ); //If using FBO, switch to FBO texture @@ -442,6 +656,9 @@ void Renderer::SetupPass1(const Pipeline* pipeline, const PipelineContext &pipel glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); +#ifdef USE_CG + RenderBlurTextures(pipeline,pipelineContext); +#endif } void Renderer::RenderItems(const Pipeline* pipeline, const PipelineContext &pipelineContext) diff --git a/src/projectM-engine/Renderer.hpp b/src/projectM-engine/Renderer.hpp index 056926f0f..0dd675596 100644 --- a/src/projectM-engine/Renderer.hpp +++ b/src/projectM-engine/Renderer.hpp @@ -120,13 +120,22 @@ private: #ifdef USE_CG std::string cgTemplate; + std::string blurProgram; - unsigned int noise_texture_lq_lite; - unsigned int noise_texture_lq; - unsigned int noise_texture_mq; - unsigned int noise_texture_hq; - unsigned int noise_texture_lq_vol; - unsigned int noise_texture_hq_vol; + GLuint noise_texture_lq_lite; + GLuint noise_texture_lq; + GLuint noise_texture_mq; + GLuint noise_texture_hq; + GLuint noise_texture_lq_vol; + GLuint noise_texture_hq_vol; + + + bool blur1_enabled; + bool blur2_enabled; + bool blur3_enabled; + GLuint blur1_tex; + GLuint blur2_tex; + GLuint blur3_tex; float rand_preset[4]; @@ -135,8 +144,10 @@ private: CGcontext myCgContext; CGprofile myCgProfile; - CGprogram myCgWarpProgram, - myCgCompositeProgram; + CGprogram myCgWarpProgram; + CGprogram myCgCompositeProgram; + CGprogram blur1Program; + CGprogram blur2Program; bool LoadCgProgram(std::string program, CGprogram &p); bool checkForCgCompileError(const char *situation); @@ -144,6 +155,7 @@ private: void SetupCg(); void SetupCgVariables(CGprogram program, const Pipeline &pipeline, const PipelineContext &pipelineContext); void SetupCgQVariables(CGprogram program, const PresetOutputs &presetOutputs); + void RenderBlurTextures(const Pipeline* pipeline, const PipelineContext &pipelineContext); #endif #ifdef USE_FTGL diff --git a/src/projectM-engine/blur.cg b/src/projectM-engine/blur.cg new file mode 100644 index 000000000..1417d4e59 --- /dev/null +++ b/src/projectM-engine/blur.cg @@ -0,0 +1,46 @@ +struct outtype {float4 color : COLOR;}; + +uniform float4 srctexsize; + +outtype blur1(float2 uv : TEXCOORD0, uniform sampler2D sampler_blur : TEX0) +{ +float2 uv2 = uv.xy + srctexsize.zw*float2(1,1); + +float3 val = tex2D(sampler_blur, float2(uv.x-.0015, uv.y)).xyz; +val += tex2D(sampler_blur, float2(uv.x+.0015, uv.y)).xyz; +val += tex2D(sampler_blur, float2(uv.x, uv.y + .0015)).xyz; +val += tex2D(sampler_blur, float2(uv.x, uv.y - .0015)).xyz; + +outtype OUT; +OUT.color.xyz = val* 0.25; +OUT.color.w = 1; +return OUT; +} + +outtype blur2(float2 uv : TEXCOORD0, uniform sampler2D sampler_blur : TEX0) +{ +float2 uv2 = uv.xy + srctexsize.zw*float2(1,0); + +float3 val = tex2D(sampler_blur, float2(uv.x-.0015, uv.y)).xyz; +val += tex2D(sampler_blur, float2(uv.x+.0015, uv.y)).xyz; +val += tex2D(sampler_blur, float2(uv.x, uv.y + .0015)).xyz; +val += tex2D(sampler_blur, float2(uv.x, uv.y - .0015)).xyz; + +val *= 0.25; + +float t = min( min(uv.x, uv.y), 1-max(uv.x,uv.y) ); +t = sqrt(t); + +float minimum = 0.5; +float variance = 0.5; +float size = 50; + +t = minimum + variance*saturate(t*size); +t = 1; +val.xyz *= t; + +outtype OUT; +OUT.color.xyz = val; +OUT.color.w = 1; +return OUT; +} diff --git a/src/projectM-engine/projectM.cg b/src/projectM-engine/projectM.cg index 50f285f52..437ed03c1 100644 --- a/src/projectM-engine/projectM.cg +++ b/src/projectM-engine/projectM.cg @@ -39,7 +39,6 @@ #define tex2d tex2D #define tex3d tex3D - #define getrad sqrt((uv.x-0.5)*2*(uv.x-0.5)*2+(uv.y-0.5)*2*(uv.y-0.5)*2)*.7071067 #define getang atan2(((uv.y-0.5)*2),((uv.x-0.5)*2)) @@ -49,14 +48,22 @@ #define uv_orig uv uniform sampler2D sampler_main; + uniform sampler2D sampler_noise_lq; uniform sampler2D sampler_noise_lq_lite; uniform sampler2D sampler_noise_mq; uniform sampler2D sampler_noise_hq; uniform sampler3D sampler_noisevol_lq; uniform sampler3D sampler_noisevol_hq; -uniform float4 texsize_noise_lq; -uniform float4 texsize_noise_lq_lite; + +uniform sampler2D sampler_blur1; +uniform sampler2D sampler_blur2; +uniform sampler2D sampler_blur3; + +float4 texsize_noise_lq; +float4 texsize_noise_mq; +float4 texsize_noise_hq; +float4 texsize_noise_lq_lite; float4 _qa; float4 _qb; @@ -74,14 +81,9 @@ float blur2_max; float blur3_min; float blur3_max; -#define GetBlur1(uv) blur(uv)*blur1_max+blur1_min -#define GetBlur2(uv) blur2(uv)*blur2_max+blur2_min -#define GetBlur3(uv) blur2(uv)*blur2_max+blur3_min - -//#define GetBlur1(uv) (tex2D(sampler_blur1,uv).xyz*blur1_max+blur1_min) -//#define GetBlur2(uv) (tex2D(sampler_blur2,uv).xyz*blur2_max+blur2_min) -//#define GetBlur3(uv) (tex2D(sampler_blur3,uv).xyz*blur3_max+blur3_min) - +#define GetBlur1(uv) (tex2D(sampler_blur1,uv).xyz*blur1_max+blur1_min) +#define GetBlur2(uv) (tex2D(sampler_blur2,uv).xyz*blur2_max+blur2_min) +#define GetBlur3(uv) (tex2D(sampler_blur3,uv).xyz*blur3_max+blur3_min) float4 slow_roam_cos; float4 roam_cos; @@ -110,32 +112,6 @@ float4 aspect; #define sampler_fw_main sampler_main #define sampler_fc_main sampler_main -#define texsize_noise_mq texsize_noise_lq -#define texsize_noise_hq texsize_noise_lq -#define texsize_noisevol_lq texsize_noise_lq_lite -#define texsize_noisevol_hq texsize_noise_lq_lite - -float3 blur(float2 uv) -{ -float3 val = tex2D(sampler_main, uv).xyz; -val += tex2D(sampler_main, float2(uv.x-.0015, uv.y)).xyz; -val += tex2D(sampler_main, float2(uv.x+.0015, uv.y)).xyz; -val += tex2D(sampler_main, float2(uv.x, uv.y + .0015)).xyz; -val += tex2D(sampler_main, float2(uv.x, uv.y - .0015)).xyz; - -return val * 0.2; -} -float3 blur2(float2 uv) -{ -float3 val = blur(uv); -val += blur( float2(uv.x-.0015, uv.y)); -val += blur( float2(uv.x+.0015, uv.y)); -val += blur( float2(uv.x, uv.y + .0015)); -val += blur( float2(uv.x, uv.y - .0015)); - -return val * 0.2; -} - struct outtype {float4 color : COLOR;}; outtype OUT; float3 ret;