Slick Forums

Discuss the Slick 2D Library
It is currently Wed Jun 19, 2013 4:07 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: how to start with GLSL
PostPosted: Mon Aug 13, 2012 7:44 pm 
Offline

Joined: Sat Jun 02, 2012 7:41 am
Posts: 68
Hi guys, I was looking for a good tutorial to start programming shaders in GLSL to use in 2d slick games. I learned how to import vertex and fragment shaders from the tutorial on slick wiki, but I'd like to find something to learn how to actually program shaders, and that gives a clear explanation of what I can do with both vertex and fragment shaders. Of course I'm not looking for something too complex, it is just because I'm curious to see what can be done with them (not going to implement anything right now, since it seems a very complex topic). Looking around, many tutorials deal with 3d graphics only, so I'm pretty confused on how to apply it in 2d. Do you know any good tutorial to start?


Top
 Profile  
 
PostPosted: Mon Aug 13, 2012 8:59 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1477
Since GLSL was built with 3D in mind, you'll probably have a hard time finding 2D-specific tutorials. However, there are plenty of code examples on the web that will translate easily to Slick.

You'll need to learn a few things about texture sampling with GLSL, as well as some basic math operations to perform certain processing. The wiki doesn't (yet) cover these things, so you're on your own.

For a 2D Slick game, shaders will mainly come in handy for "image processing." Examples:
  • Horizontal/vertical blurs: See ShaderTestAdvanced for an example. It's based on this tutorial.
  • Invert colors: See ShaderTest for an example.
  • Photoshop Blend Modes: such as screen, color burn, multiply, overlay, etc. See here for a Slick implementation.
  • "Polar Coordinates" filter: Pretty useless, but the code may help you get a better grasp on GLSL.
  • Brightness/contrast: See here

-- splitting this post in 2 since the max URL count seems to be 8!??! --


Top
 Profile  
 
PostPosted: Mon Aug 13, 2012 9:00 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1477
More advanced effects can also be achieved with GLSL, such as:
  • A shockwave effect
  • Motion blur, bloom
  • Color grading
  • Anti-aliasing hard edges, i.e. using FXAA
  • Soft shadows
  • Bump mapping and lighting to give your 2D textures more depth and dynamism
  • Anything else you can imagine...

A good place to start would be browsing through actual code, and hacking around with it.
http://www.iquilezles.org/apps/shadertoy/ (edit the code and hit Alt + Enter to recompile)
http://glsl.heroku.com/
http://webglplayground.net/

If you're stuck with a particular aspect of GLSL or the way to use it through Slick, let me know and I can update the wiki with more info. :)


P.S. For anyone who doesn't know, there is a GLSL intro on the wiki as well as some Slick tests in the dev branch.


Top
 Profile  
 
PostPosted: Mon Aug 13, 2012 9:35 pm 
Offline

Joined: Sat Jun 02, 2012 7:41 am
Posts: 68
Whoa, this was the best reply ever, thanks a lot! :o

A question I got: let's say I want to get the mouse coordinates into the fragment shader, I create a "uniform vec2 mouse" and then I have in the render method:
Code:
if (shaderWorks)
      program.bind();
      
g.fillRect(60, 60, 600, 400);
      
if (shaderWorks)
      program.unbind();

now where (I mean, where in the above code) do I pass the mouse position with the method setUniform2f(...)?


Top
 Profile  
 
PostPosted: Mon Aug 13, 2012 10:07 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1477
The shader needs to be "bound" before you can set a uniform. So it might look like this:
Code:
if (shaderWorks) {
    //first bind the shader
    program.bind();

    //now set up any uniforms
    program.setUniform2f("mouse", mouseX, mouseY);
}

... draw your stuff


Then, in the fragment shader, you would simply use the "mouse" uniform to modify the output color.

The uniform type determines which method you should use:
Code:
setUniform4f = vec4
setUniform3f = vec3
setUniform2f = vec2
setUniform1f = float
setUniform1i = int / sampler2D


(This is based on OpenGL/LWJGL naming, although in retrospect it might have been more Slick-like to simply overload setUniform.)


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 7:46 am 
Offline

Joined: Sat Jun 02, 2012 7:41 am
Posts: 68
all clear, thank you very much :) i'll play around wirh it and see if i can learn something cool! :)


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

All times are UTC


Who is online

Users browsing this forum: SquidKid 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