Slick Forums

Discuss the Slick 2D Library
It is currently Sun May 26, 2013 8:04 am

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: SlickShader
PostPosted: Sat May 14, 2011 5:20 pm 
Offline
Regular

Joined: Sun Dec 07, 2008 5:22 am
Posts: 238
Location: Vancouver, BC, Canada
So I while back I mentioned the idea of a simple extension to hide some of the boiler plate code when using Shaders http://slick.javaunlimited.net/viewtopic.php?t=3212. Well I have attempted to create just that.

Here are some of the things SlickShader tries to achieve:

A simple static fragment shader is probably the simplest and most common, this should be extremely easy to use.
usage example - Apply a shader to an Image.
Code:
Shader myShader = Shader.makeShader("data/vertexShader.vrt", "data/fragmentShader.frg");
//...
myShader.startUse();
  myImage.draw();
  myImage2.draw();
  //etc.
Shader.forceFixedShader



Make it easy to set variable for shaders that change every frame for example a heat distortion shader, or shader that makes obscuring artefacts above the player character translucent.
Code:
myShader.setUniformFloatVariable("playerPosition", xPos, yPos);


Making shaders is an art of its own and can be quite difficult. It doesn't help that the default behaviour of OpenGL when faced with an invalid shader is to either fail with no warning or do nothing. SlickShader attempts to give you as much feedback as possible when something is wrong or looks fishy. This can result in warning spam during testing however; its still way better then nothing.


MultiTexturing can be used for a number of effects such as normal mapping translucency mapping. And most of the more complicated shader effects. Since this is less common and more complex by nature it isn't quite as easy to use yet.

Code:
Shader myShader1 = Shader.makeShader("data/vertexShader1.vrt", "data/fragmentShader1.frg");
Shader myShader2 = Shader.makeShader("data/vertexShader2.vrt", "data/fragmentShade2r.frg");

MultiTex myMultiTexture = new MultiTex("data/image1.png", "data/image2.png");
//...
myMultiTexture.startShader();
myMultiTexture.setUniformIntVariable("texture1", 0)
              .setUniformIntVariable("texture2", 1);

Here texture1 and texture2 refer to variables in the shader program, 0 and 1 correspond to Shader Units and the 1st and 2nd respectively images passed to multTex



Anyway I spent the last week trying it out on various video cards to make sure it works, and while it's still very basic it is definitely usable. The source is available here:
https://code.google.com/p/slickshader/.

Disclaimer: By far the hardest part of using shaders is writing the shader program itself. In some ways it is easier since we are only working with 2D but it can also be harder to work out examples which are all aimed at 3D graphics. It may be a good idea to look at the shaders used in the Example class, though the glLight_VertShader does have bugs.

_________________
If at first quads don't succeed tri tri again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2011 3:08 pm 
Offline

Joined: Wed Apr 27, 2011 3:21 am
Posts: 7
I remember seeing a shader application demo in the forums somewhere sometime ago, but the thing that sets that apart from yours is that your method can allow multiple shaders to be applied to one image at a time. Works great.

Actually, one problem I have right now is the picture getting brighter when I apply a shader. I'm pretty much a noob at GLSL so please help me out!
(Based on http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/)
"blurHorizontal.frg"
Code:
uniform sampler2D RTScene; // the texture with the scene you want to blur
varying vec2 vTexCoord;

const float blurSize = 1.0/512.0; // I've chosen this size because this will result in that every step will be one pixel wide if the RTScene texture is of size 512x512

void main(void)
{
   vec4 sum = vec4(0.0);

   // blur in x (horizontal)
   // take nine samples, with the distance blurSize between them
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x - 4.0*blurSize, vTexCoord.y)) * 0.05;
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x - 3.0*blurSize, vTexCoord.y)) * 0.09;
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x - 2.0*blurSize, vTexCoord.y)) * 0.12;
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x - blurSize, vTexCoord.y)) * 0.15;
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x + blurSize, vTexCoord.y)) * 0.15;
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x + 2.0*blurSize, vTexCoord.y)) * 0.12;
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x + 3.0*blurSize, vTexCoord.y)) * 0.09;
   sum += texture2D(RTScene, gl_TexCoord[0].st + vec2(vTexCoord.x + 4.0*blurSize, vTexCoord.y)) * 0.05;

   gl_FragColor = sum;
}


"blurVertical.frg"
Code:
uniform sampler2D colorMap; // this should hold the texture rendered by the horizontal blur pass
varying vec2 vTexCoord;

const float blurSize = 1.0 / 512.0;

void main( void )
{
   vec4 sum = vec4( 0.0 );

   // blur in y (vertical)
   // take nine samples, with the distance blurSize between them
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y - 4.0*blurSize)) * 0.05;
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y - 3.0*blurSize)) * 0.09;
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y - 2.0*blurSize)) * 0.12;
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y - blurSize)) * 0.15;
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y + blurSize)) * 0.15;
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y + 2.0*blurSize)) * 0.12;
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y + 3.0*blurSize)) * 0.09;
   sum += texture2D(colorMap, gl_TexCoord[0].st + vec2(vTexCoord.x, vTexCoord.y + 4.0*blurSize)) * 0.05;

   gl_FragColor = sum;
}


"basicVert.vrt"
Code:
uniform float width;
uniform float height;

void main(void)
{

vec4 a = gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;

gl_FrontColor = gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * a;


}


The code:
Init
Code:
blurH = Shader.makeShader( "data/basicVert.vrt", "data/blurHorizontal.frg" );
blurV = Shader.makeShader( "data/basicVert.vrt", "data/blurVertical.frg" );

Render
Code:
blurH.startShader();
blurV.startShader();
myImage.draw( x, y );


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2011 2:15 am 
Offline
Regular

Joined: Sun Dec 07, 2008 5:22 am
Posts: 238
Location: Vancouver, BC, Canada
First glad to see someone else getting use out of this.
Second I am really not very good with shaders themselves.

So answer to you question is that I think
Code:
gl_FrontColor = gl_Color;

is causing your brightening problem though I can't be sure. It shouldn't be needed if you aren't doing lighting. So scrapping it should be harmless if nothing else.

Another thing worth noting is that I didn't realize you could multi shader something in the way described. I do have a method in place for stringing together shaders but it is not yet exposed as part the public Interface. So what you found is really cool.

Finally, something seems really weird in your vertex shader. Nowhere do you set a value for vTexCoord, so I am at a loss as to where that info is coming from in your frag shaders. it should (I think) be just plain old
Code:
vTexCoord = (gl_position * gl_ModelViewProjectionMatrix).xy;


The phrase in the article
Quote:
This shader screen align a quad with width 1. Any method to render a screen aligned quad will work. So you’re free to use other shaders.

seems to be important for the coordinate values, but I don't entirely understand what they mean.

Will try to take a closer look at it later, but I hope that helps for now.

_________________
If at first quads don't succeed tri tri again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2011 12:17 pm 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
Is this project dead? I really want this to go on :) And I now might have a use for it!

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2011 3:58 pm 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
R.D. wrote:
Is this project dead? I really want this to go on :) And I now might have a use for it!


Yea, first time I've heard of it. Sounds really cool!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2011 7:12 pm 
Offline

Joined: Thu Oct 20, 2011 7:11 pm
Posts: 1
R.D. wrote:
Is this project dead? I really want this to go on :) And I now might have a use for it!


Same here. This seems really promising! :)


Top
 Profile  
 
 Post subject: Re: SlickShader
PostPosted: Sat Nov 12, 2011 11:04 pm 
Offline
Regular

Joined: Sun Dec 07, 2008 5:22 am
Posts: 238
Location: Vancouver, BC, Canada
Wow, people are still interested. No it's not really dead just on hiatus, I guess.

I'll make some excuses for myself here:
-I'm still not really good with openGL and trying to make things work on all scratch that most video cards is really hard, especially when I only have 3 to test on and they are all ATI.
-I'm even worse at GLSL then I am at openGL this makes it hard to figure out whether it is a shader bug or part of the system.
-I really need to make a small collection of commonly used shaders (blur effects, sepia tone, translucency masks) but again I'm really not that good at making shaders.
-I got a new job, and haven't had as much free time.
-I am really lazy.

So no it's not dead, if there are things you want fixed or features you want added I will do my best.

Also sorry for taking so long to respond, I thought I had things set up to email me when someone posted but that doesn't seem to be the case.

_________________
If at first quads don't succeed tri tri again.


Top
 Profile  
 
 Post subject: Re: SlickShader
PostPosted: Sun Nov 13, 2011 12:28 pm 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
Good to hear it's not dead :)
If you want to test stuff un NVidia, give me your tests, your source is already a project in my workspace :D For default shaders (I like the idea alot, you could pack this al into the jar!) you could ask theagent back ath JGO. He will probatly help!

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Top
 Profile  
 
 Post subject: Re: SlickShader
PostPosted: Sun Nov 13, 2011 2:33 pm 
Offline

Joined: Thu Feb 10, 2011 5:21 pm
Posts: 18
Would you like two shaders I found over the internet?


Top
 Profile  
 
 Post subject: Re: SlickShader
PostPosted: Sun Nov 13, 2011 9:22 pm 
Offline
Regular

Joined: Sun Dec 07, 2008 5:22 am
Posts: 238
Location: Vancouver, BC, Canada
@R.D.
:oops: The tests are basically whatever is done in the Example class. *cough*
That is another thing that needs doing: proper test set-ups.

@Senthior
Yeah; can you pm me those along with a description of what they are supposed to do, and I will add those in.

@Everyone
This project has an open invitation, if you would like write permissions just ask and I will gladly grant them.
Chances are if you are reading this and are interested then there is something you can contribute.

_________________
If at first quads don't succeed tri tri again.


Top
 Profile  
 
 Post subject: Re: SlickShader
PostPosted: Wed Dec 14, 2011 11:12 pm 
Offline

Joined: Mon Sep 26, 2011 1:24 pm
Posts: 2
Hey, could you write a shader that adds bloom to an image?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group