adm
YaBB Newbies
Offline
Posts: 34
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.