We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Cg Shaders (Read 1798 times)
Cg Shaders
Dec 15th, 2008, 11:36pm
 
Hello,

I've been playing around with Cg vertex and pixel/fragment shaders in OpenGL for the first time, and I was trying it in Processing since it's more fun. I thought I'd share my experiences.

After some initial luck using the CG helper class provided in another thread (which I can't find now!), I just couldn't get the lighting to stay still; when objects are rotated using the matrix, the lighting follows it round instead of staying still.

After getting a plain old JOGL app to render my simple diffuse pixel shader, I went back to see why I was still having trouble doing the same in Processing. Eventually the source code revealed that PGraphicsOpenGL falls back on PGraphics3D matrix, rather than change any OpenGL matrices. I'd been passing OpenGL's matrix states through to the shaders (as most examples do this) which I guess is why it wasn't working.
  • Grab the Processing matrices in some way, convert them into an array and pass them to the shader instead
     
  • Use/make a different OpenGL implementation of PGraphics (like proGL)
     
  • Wait and see if Processing grows its own OpenGL implementation that uses OpenGL matrices

I wondered if anybody else has got this far or further, and what they are doing? Or if anybody needs any hints getting started with shaders in OpenGL in general. I was intending to put together a simple sketch to demonstrate getting started, or at least a page in the Hacks section.

Cheers
Re: Cg Shaders
Reply #1 - Dec 15th, 2008, 11:38pm
 
Found that post with the CG helper class.

ac wrote on Oct 7th, 2006, 6:41am:
...Post with CG helper class...
Re: Cg Shaders
Reply #2 - Dec 16th, 2008, 11:31am
 
GL gl=((PGraphicsOpenGL)g).beginGL(); forces the PMatrix stuff to be exported into the GL matricies, so you can do things directly, and provides the GL object to pass direct calls to.
Just call ((PgraphicsOpenGL)g).endGL(); when you're done and you can then do normal processing stuff on top again.
Re: Cg Shaders
Reply #3 - Dec 16th, 2008, 7:20pm
 
Cool, I'll give that a go. Thanks.
Re: Cg Shaders
Reply #4 - Mar 6th, 2009, 6:18am
 
Just curious if you made any more progress with this?
Re: Cg Shaders
Reply #5 - Mar 6th, 2009, 10:29am
 
I got a little further but nothing that looks great. It doesn't help that I'm no expert in shaders! Don't have access to my sketches at the moment but I will try to post whatever I ended up with, in a few days.
Re: Cg Shaders
Reply #6 - Mar 6th, 2009, 2:41pm
 
I'm also interested in this.  I've tried ambient lighting, thinking it would fix it, but after reading this post, I think I have a better understanding of why that didn't work.
Re: Cg Shaders
Reply #7 - Mar 6th, 2009, 5:43pm
 
I have messed a bit with Cg shaders myself. Dealing with matrices can be kind of a messy situation. Most of the time when I'm doing this kind of stuff, all my transformations are usually pure GL calls (ie gl.glRotatef(blah, 0, 1, 0);

It is my understanding that when you call PGraphicsOpenGL.beginGL(), the Processing matrix transformation stack is copied into the OpenGL matrix stack, but if you are doing a lot of straight OpenGL stuff, I think for the sake of clarity, it is advisable to use only OpenGL matrix calls.

On the other hand, I've recently played around with some Cg shaders from the Nvidia shader library that have some interesting and more complex lighting models which require sending over matrices that are not typically easy to grab out of OpenGL. One reason for this is that OpenGL squishes the View and World matrices into one (MODELVIEW) and often with these lighting models, you'll need to send a ViewProjection matrix for instance. To further complicate matters, most of the Nvidia shaders want to receive matrices in row-major notation. The ones stores in the OpenGL matrices come out in column-major notation. You can just transpose a matrix to go from one to the other, but its still kind of confusing.

Anyway, with all that out of the way, here is my suggestion for dealing with matrices and Cg shaders: don't use the functions built into processing OR opengl. Use a matrix class and store your matrices yourself. The Matrix4x4 class in toxiclibs is a good place to start, but you will probably want to add functions for creating a projection matrix and a lookat matrix. Source code for these are pretty easy to find online. The rest of the stuff you'll want is already built in to the class (translate, scale, rotate). You'll also want a helper funciton to spit out the Matrix4x4 in a array of doubles (the format that Cg shaders typically take them in).

It may seem like extra work, but in the end, its more flexible... you can always throw a breakpoint and see what's happening inside of your matrices at any point in the transformation process instead of having to deal with glGets.

That's my advice, anyway.
Re: Cg Shaders
Reply #8 - Mar 6th, 2009, 10:55pm
 
when in between beginGL and endGL you just work directly with opengl.

you shading problem is well known when you want to transform everything, lets say, move the objects, the light, the camera.

you have to know each object matrix and use that for your transformations. that means its the world matrix, not the modelview matrix. the normals transformation are easily done using the INVERSE_TRANSPOSE_MATRIX.

i built my own matrix, but you can use anyone available.

you can also use opengl to create your matrices, just make sure you restore the matrix stack to where it should be.

http://www.pixelnerve.com/downloads/processing/mri_3ds_processing_2.zip

there is an example here which uses cgfx. check it out
Re: Cg Shaders
Reply #9 - Mar 7th, 2009, 12:31am
 
Sounds like the last two posters got further than I did.

I did wonder if it was just me having trouble with a combined ModelView matrix - I assumed I was just thinking about it wrong, but what adm said sounds like the same things I ran into. My housemate is into DirectX and he couldn't understand how you could do shaders without separate matrices.

Custom matrices it is then!
Re: Cg Shaders
Reply #10 - Mar 7th, 2009, 1:10am
 
basically all u need to keep track is each object's world matrix. the rest you can get from opengl. opengl rejected world matrix for some reason. its said its not important to keep track Smiley which differs from direct3d which lets you set each matrix: world, view, projection, texture seperately.

then when u render objects:

gl.perspective
gl.camera( ... );

// rendering your object
gl.loadMatrix( object.worldMatrix );
(shader) pass your shader parameters
render object
Page Index Toggle Pages: 1