JOGL Shader Questions

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

JOGL Shader Questions

bgroenks96
This is sort of migrated from JGO.

I'm trying to figure out how to start using GLSL with JOGL to do pixel shading on images drawn to GLAutoDrawable via glDrawPixels.

I'm confused as to how a few things work in GLSL:
1) How can I access the color of the current pixel in the shader call?  What is the form of that pixel (RGB, BGR, etc.) so that I can unpack it?
2) Do I just assign gl_FragColor to change the pixel?
3) How do I get coordinates of the current pixel?
4) When is glSwapBuffer() necessary?

I've now been told not to use glDrawPixels, which now confuses me even further.

Which adds the new question:
5) What is the most generally reliable method of loading an image and rendering it on a GLDrawable?
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

Demoscene Passivist
Administrator
>1) How can I access the color of the current pixel in the shader call?  
>What is the form of that pixel (RGB, BGR, etc.) so that I can unpack it?

You simply can't. That's the whole idea with shaders that they run in parallel. But you can access the pixels from a previous frame with some additional work.

>2) Do I just assign gl_FragColor to change the pixel?

Exactly!

>3) How do I get coordinates of the current pixel?

I guess you mean screen coordinates: gl_FragCoord.xy

>4) When is glSwapBuffer() necessary?

Generally when you are finished rendering your frame.
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

gouessej
Administrator
In reply to this post by bgroenks96
bgroenks96, thank you for posting your question here. My answer was:
http://www.java-gaming.org/topics/jogl-shader-questions/29087/msg/266085/view.html#msg266085
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
Your answer is, unfortunately, no longer there.... I don't know why.

davedes said:
The "current pixel" is not known. If you want post-processing, then typically you would render the screen to an FBO texture, and then in your post shader sample from the texture in order to "get the screen pixel."

That makes sense... but I don't really know how to do it.  Does anyone know of any examples of that ^?

Why does the fact that shaders run in parallel mean the current pixel isn't known?  What can a frag shader do without knowing the pixel color?

Update:
Ok I found a pretty decent shader example here.

But what is going on with:
    uniform sampler2D fish_y_offset;
and why does it never get assigned to anything?  Does GLSL do that automatically?

I also don't really get what's happening with this:
    gl_FragColor = texture2D(fish_y_offset, gl_TexCoord[0].st);
And since texture2D is now deprecated, what's the now accepted way to do that?
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
In reply to this post by gouessej
gouessej -

Unrelated, but I was trying to play your game, and JWS kept hanging on: Verifying Application...

It made me sad :(
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

gouessej
Administrator
This post was updated on .
In reply to this post by bgroenks96
bgroenks96 wrote
Your answer is, unfortunately, no longer there.... I don't know why.
My answer has been moved by the moderator, I prefer posting it anew here:
gouessej wrote
Hi

Why do you want to use glDrawPixels?

1) The role of a pixel shader consists in computing the color. What do you really want to access?
2) Yes.
3) Pixel shaders have knowledge of the screen coordinate being drawn, it should be possible, use gl_FragCoord.
4) I would rather use GLSL to perform light computations, for example per pixel lighting.

I remind you that questions specific to any JogAmp APIs including JOGL should be posted on our official forum. Ok I will try to find a similar example using JOGL 2.0.

Edit.: Rather use GLOffscreenAutoDrawable or a simple texture mapped in a quad, it will be more reliable. glDrawPixels might not work very well with GLSL on some hardware.

Edit.2: This is an example using GLOffscreenAutoDrawable, this is a very simple example using GLSL.

Edit. 3: Feel free to post general OpenGL questions here of course. Sorry for the disturbance.
I advise you to read a basic course about shaders as you seem not to understand which variables are in input and in output in a shader of a particular type (vertex, pixel, geometry, ...).

bgroenks96 wrote
Unrelated, but I was trying to play your game, and JWS kept hanging on: Verifying Application...
Sometimes Java Web Start hangs on machines not fully compatible with IPV6, it's weird but it has nothing to do with my game.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
This post was updated on .
gouessej wrote
I advise you to read a basic course about shaders as you seem not to understand which variables are in input and in output in a shader of a particular type (vertex, pixel, geometry, ...).
I've been reading about shaders, but I'm more confused about JOGL and GLSL specifically.

If you have any particular article or shader-tutorial you would like me to read before continuing my inquiries, please post it, and I will be happy to do so.

Edit:
The reason this isn't entirely general OpenGL is because I'm looking for examples of doing this in JOGL.  I want to figure out how to write the shader AND interface with it properly from the Java code, which is clearly in JOGL, not LWJGL or anything else.
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
In reply to this post by gouessej
Ok I'm going to try and clarify what I'm trying to do here so that maybe it will be easier for you guys to point me in the right direction:

I want to be able to have a collection of renderable objects that contain loaded BufferedImages (sprites) that can render their content to a central frame renderer.  In my Java2D renderer, I did this by passing around a Graphics2D object generated by a VI.

I am first planning on writing class that would perform the necessary conversions and transformations to a BufferedImage on load.

I am using a FPSAnimator to control the rendering loop.

The reason I want a shader is for enabling gamma correction on rendered frames (and possibly lighting later on).

I'm stuck right now on how I should be collectively rendering the images.  Should I be loading them as Textures and drawing them as quads?  Or should I be using glDrawPixels?
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

Demoscene Passivist
Administrator
In reply to this post by bgroenks96
>The reason this isn't entirely general OpenGL is because I'm looking for examples of doing this in JOGL.
> I want to figure out how to write the shader AND interface with it properly from the Java code, which
>is clearly in JOGL, not LWJGL or anything else.

You may take a look a my blog at copypastaresearch.tumblr.com

There are a lot of examples on how to use shaders with JOGL ofc with sourcecode. E.g. take a look at this simple example on how to setup a simple fullscreen fragment shader.
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

gouessej
Administrator
In reply to this post by bgroenks96
You don't need glDrawPixels. You can use shaders with the helpers specific to JOGL or using the same methods you would call in plain C.

Personally, I love this tutorial even though it is a bit old. I gave you 2 examples in my previous post, have you understood them? I can show you how I use shaders in Ardor3D and JMonkeyEngine. Demoscene Passivist has some excellent examples too.

You can do gamma correction and lighting directly in the fragment shader.

Edit.: I like this quick GLSL reference guide too.

Edit.2: If I were you, I would not bind a whole engine to Java2D/AWT. Maybe use JOGL image loading with TextureIO. You can use JOGL under Android if and only if you don't depend on AWT.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

Xerxes Rånby
This post was updated on .
In reply to this post by bgroenks96
2013-03-25 20:37, bgroenks96 [via jogamp] skrev:
>
>     gouessej wrote
>     I advise you to read a basic course about shaders as you seem not to understand which variables are in input and in output in a shader of a particular type (vertex, pixel, geometry, ...).
>
> I've been reading about shaders, but I'm more confused about JOGL and GLSL specifically.
>
> If you have any particular article or shader-tutorial you would like me to read before continuing my inquiries, please post it, and I will be happy to do so.

Shaders are passed in clear text Strings to your GPU driver. The GPU driver compiles the shader for use by the processing units on your device.
The syntax for shaders differs slightly depending on which OpenGL version you target:

=================
Most of the JogAmp examples uses mobile and desktop compatible GLSL syntax and are using the GL2ES2 profile that is supported on both OpenGL 2 and ES 2 hardware.

OpenGL 2 up to OpenGL version 3.3. shaders can use Vertex, Geometry and Fragment shaders but work only the desktop.
http://www.khronos.org/files/opengl-quick-reference-card.pdf - quick reference card that covers GLSL for Vertex, Geometry and Fragment shaders.

OpenGL ES 2 shaders can only use Vertex and Fragment shaders and work only on mobile.
http://www.khronos.org/opengles/2_X/
I recommend you to print out the OpenGL ES 2 Quick Reference Card that also contains a summary of OpenGL ES Shading Language 1.0:
http://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf
If you need more detailed documentation for GLSL read the specification:
http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf

OpenGL 2 and ES 2 shaders uses similar keywords inside the GLSL language thus it is possible to write Vertex and Fragment shaders in a way that work on both desktop and mobile by using the GLSL GPU compilers preprocessor to hide keywords that are specific to mobile OpenGL ES 2.
The quick way to make a Vertex or Fragment shader OpenGL 2 and ES 2 compatible is by placingthe following 4 lines at the top of the Vertex and Fragment shader:

#ifdef GL_ES
precision mediump float; // Precision Qualifiers
precision mediump int; // GLSL ES section 4.5.2
#endif

the precision qualifiers are mandatory to be specified in mobile OpenGL ES 2 GLSL spec but do not exist in desktop OpenGL 2 GLSL spec.

I have written a simple introduction to OpenGL 2 and ES 2 compatible Vertex and Fragment Shaders here:
http://labb.zafena.se/?p=547
Source: https://github.com/xranby/jogl-demos/blob/master/src/demos/es2/RawGL2ES2demo.java#L44


=================

Below I will list the latest OpenGL specifications and how they differs from the above OpenGL 2 and ES 2:

OpenGL 4.3 (desktop) adds support for three new kind of shaders:
Tessellation Control Language
Tessellation Evaluation Language
Compute Language
http://www.khronos.org/files/opengl43-quick-reference-card.pdf
You also notice that some of the shader keywords have changed from the OpenGL 2 to OpenGL 4.3
The "variant" keyword is now renamed to "out" inside the vertex shader
The "variant" keyword is now renamed to "in" inside the fragment shader

OpenGL ES 3 (mobile)
GLSL version 1.1
http://www.khronos.org/files/opengles3-quick-reference-card.pdf
specificaton: http://www.khronos.org/registry/gles/specs/3.0/GLSL_ES_Specification_3.00.4.pdf
Similar to OpenGL 4.3, ES 3 GLSL also use the new "in" and "out" keywords.
ES 3 have removed all of the restrictions found inside the ES 2 Appendix A specification, these changes require new mobile hardware and drivers that have started to ship in 2013.

It looks like there will be possible to create a new desktop and mobile profile possibly called GL43ES3 that may be both OpenGL 4.3 and ES 3 compatible that can be used by the latest OpenGL hardware on both mobile and desktop.

=================

Online tutorials for OpenGL 2 up to OpenGL version 3.3:

http://dev-fjord.blogspot.se/2011/07/jogl-part-1-introduction.html
http://dev-fjord.blogspot.se/2011/08/jogl-part-2-deferred-renderer.html - this tutorial gives a good outline on how to use Deferred Rendering for multi pass advanced Lighting rendering.
this tutorial focus on how to to manage the user defined Geometry Buffer (G-Buffer).
http://en.wikipedia.org/wiki/Deferred_shading

http://www.arcsynthesis.org/gltut/ - Learning Modern 3D Graphics Programming with focus on OpenGL version 3.3.

Cheers
Xerxes
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
In reply to this post by gouessej
Thanks guys.  That's a lot of examples.  Let me look through all of these and I will let you know how I'm doing after that.

And I'm fine with using TextureIO directly rather than doing everything from AWT.  I wasn't expecting the OpenGL renderer to work with existing Java2D dependent code.  As long as I can load data from a URL, I'm all for it.
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
In reply to this post by gouessej
I read over some of Demoscene's examples as well as the tutorial gouessej pointed out.  Both were very helpful.  The tutorial explaining some of the data types and IO variables was especially useful.

I'm still a little confused about which approach I should be taking, however.

For the gamma correction and lighting, should I be rendering everything to a FBO and sampling it?  Or should I just bind/sample textures individually in the shader as they are rendered?
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

gouessej
Administrator
I would choose the second way.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
That definitely sounds easier.  But will the shader still run even when there's no texture (i.e. draw triangle vertices w/ glColor3f)?
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
In reply to this post by gouessej
Sorry, it's been a while.

Would it be possible to have the shader run with or without a bound texture?

I was thinking maybe a check in the shader the uses a manually set color variable?
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

Sven Gothel
Administrator
On 04/06/2013 01:38 AM, bgroenks96 [via jogamp] wrote:
> Sorry, it's been a while.
>
> Would it be possible to have the shader run with or without a bound texture?
>
> I was thinking maybe a check in the shader the uses a manually set color
> variable?

That is one extension I had in mind as well,
and while modularizing them .. I may have a chance to add it.

A round movie button comes to mind :)

~Sven


signature.asc (911 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Shader Questions

bgroenks96
Forgive me.... I'm not sure what you're saying....

I think I can figure out rendering images simply using Texture, glBindTexture, glTexCoord2f, and glVertex2f.

I'm just still confused as how to use a shader to apply gamma correction to each rendered fragment.

I'd prefer to be able to run the shader whether or not a texture is bound.  So if I call glColor3f and then draw a polygon, I can still have the shader run on those pixels.

I'm pretty lost on that issue.