Monday, 29 October 2012

Engines 0.7: Ogre Shaders

UGH ...Ogre is being difficult. I don't know if it's just me but I find its shaders pretty convoluted. And on top of that, for some reason uncommenting the Cg Shader Plugin still has my Ogre log screaming at me, and I quote: "* Supported Shader Profiles: arbfp1 arbvp1 glsl. (Yeah, have fun using cg when you can't! HA!)" Ok, maybe I added in that last bit, but I truly feel like Ogre is holding a grudge against me..or maybe it's just way too early in the morning to be writing a blog. I don't see myself having time to do it later though :/

So GlSL it is then, Ogre. I somehow cobbled together a vertex and fragment shader in that language (it's not that complicated or different than cg. In fact it might be simpler. I'm not sure yet though). Oh, and I forgot to say, the reason I'm working out how to use shaders is that I want to do the "morph animations" question. The one where you have to morph one object into another (there's a bit more to it than that).

Now anyone with half a brain would assume that Ogre would do keyframe, morphing animations for you. But I really don't know if it does. Form what I have found and read, I think you need to keyframe things in Maya and then do some witchcraft and summon demons to write code for you in a mysterious language called XML.. Here's the link if you can figure it out. http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Morph+animation. I saw shaders and though, "oh well if I have to animate stuff through a shader, I might as well just pass vertices in the good 'ol fashioned way and save myself the trouble of learning another language.

How exactly do you make shaders in Ogre though? The answer is "not very easily". So you have a material file, which you apply to an object. That part is straightforward. Then either inside that material file, or in a separate file with the ".program" extension, you write a "shader program". This isn't where you put the shader code itself, this is just a program that passes parameters to the shader program. You also have to call this program in your material's "pass" section, while adding in any parameters that change.

There's an example. It's pretty confusing, but this helped. http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Getting+Started+With+Ogre+CG+Materials&structure=Cookbook The only problem is that it was in cg instead of glsl (and cg is the shader language I know).  Also, a bit of advice: separate your vertex and fragment shaders into different files. Glsl will give you errors of you don't. Here's a look at glsl:


By the way, that's a vertex shader. And gl_Position and gl_Vertex are variables that are built into glsl. gl_Position is the vertex position the program outputs, while gl_Vertex, I'm guessing, is the vertex position put into the shader. Also note that "uniform" is used for variables passed to the shader form Ogre and "varying" is used for variables that will be passed to the fragment program afterwards. 

I guess none of this has actually used anything other than scripts so far. So here's how to pass variables from Ogre: 
Yup...that part is probably the easiest.

So that's what I worked on this weekend (I would've done more but my parents convinced me to go home one last time before the Christmas break and that was kindof a hassle considering how far away I live). There wasn't much time and I'm probably doing morph the complicated way, but hey, at least I now know how to use shaders and can maybe put some into our game someday when I'm not swamped with work and have a few free hours. a

No comments:

Post a Comment