Shader for MOAIDraw?

Discussion about using Moai SDK - post questions, bugs and issues here.

Moderators: seebs, franciscotufro

Shader for MOAIDraw?

Postby joarb » Mon May 07, 2012 11:34 pm

Would it be possible to implement support for applying shaders to MOAIDraw? Rationale: I'm drawing some primitives that could benefit a lot from having a gradient applied to them.
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby adam » Tue May 08, 2012 11:29 am

Yeah, its not too hard to add, we won't be getting to that for quite awhile though.

If you go to MOAIDraw in the source, and look at the Bind function, thats where the shader gets set. You would just need to get a shader reference that could set set from LUA there.

You can look an MOAIProp to see how it retains the shader reference.
User avatar
adam
 
Posts: 262
Joined: Wed Sep 14, 2011 11:50 am

Re: Shader for MOAIDraw?

Postby joarb » Tue May 08, 2012 11:39 am

Thanks adam! I'll definitely have a look at it. :)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby joarb » Fri May 18, 2012 11:33 am

I used adams great answer as a starting point and checked out MOAIDraw::Bind and MOAIProp for guidance as to how I could implement shader support for MOAIDraw.

MOAIProp and MOAIShader both inherit from MOAINode. In contrast, MOAIDraw doesn't. The question, then, was wether I should go through the possible hassel of letting MOAIDraw inherit from MOAINode, or if a simpler approach would suffice? MOAIDraw, begin a singleton, is packed with static member methods, and since it doesn't inherit from MOAINode and since MOAIDraw::Bind is a static method, it was tempting to simply add a private static member MOAIShader* mShader. So I did. (Considering the fact that MOAIProp declares it's shader as MOAILuaSharedPtr < MOAIShader > mShader, this naive approach will quite possibly ruin any attempt of re-using a particular shader for other objects - I don't really know. There may be other, less-than-optimal aspects of this design as well. That's really not the question, although any comments in that regard are welcome. For the moment, it's simply a conceptual test.)

In MOAIDraw::Bind I did the following:
Code: Select all
  1. if ( mShader ) {       

  2.         gfxDevice.SetShader ( mShader );

  3. } else {

  4.         gfxDevice.SetShaderPreset ( MOAIShaderMgr::LINE_SHADER );

  5. }


Nothing fancy. Should work, right? Wrong.

Here's the problem. When I try to build the libmoai-ios scheme, everything is fine. However, when I try to build the moai scheme (the one containing MoaiSample project), XCode is complaining that mShader is an undefined symbol. More precisely, the linker says the following:
Undefined symbols for architecture i386:
"MOAIDraw::mShader", referenced from:
MOAIDraw::_setShader(lua_State*) in libmoai-ios.a(MOAIDraw.o)
MOAIDraw::Bind() in libmoai-ios.a(MOAIDraw.o)

Now, the field mShader is definetly there. I've cleaned everything - the error message persists. Can anyone please shed some light on this? Thanks! :)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby adam » Fri May 18, 2012 5:36 pm

Somtimes xcode is... xcode.

Try closing it down, make sure no other projects are open besides the one you are trying to compile.
User avatar
adam
 
Posts: 262
Joined: Wed Sep 14, 2011 11:50 am

Re: Shader for MOAIDraw?

Postby joarb » Sat May 19, 2012 12:38 am

Well, it feels like that's the only thing I didn't check. Unfortunately, it didn't compile even after restarting XCode.
I'm posting my changes here in full detail, hoping that someone can reproduce this ... I'm new to XCode, so any help would be greatly appreciated. First from MOAIDraw.h:
Code: Select all
  1. class MOAIDraw :

  2.         public MOAIGlobalClass < MOAIDraw, MOAILuaObject > {

  3. private:

  4.         static MOAIShader*      mShader; // <- This is new

  5.         //----------------------------------------------------------------//

  6.         // Existing lua methods goes here

  7.         static int      _setShader       ( lua_State* L ); // <- This is new

  8. public:

  9.         DECL_LUA_SINGLETON ( MOAIDraw )

  10.         //----------------------------------------------------------------//

  11.         // Public methods goes here

  12. };


The changes in MOAIDraw.cpp are as follows. The defintion of the newly introduced MOAIDraw::_setShader method:
Code: Select all
  1. int MOAIDraw::_setShader ( lua_State* L ) {

  2.         MOAI_LUA_SETUP ( MOAIDraw, "U" )

  3.         MOAIShader* shader = state.GetLuaObject < MOAIShader >( 2 );

  4.         MOAIDraw::mShader = shader;

  5.         return 0;

  6. }

The modified MOAIDraw::Bind method:
Code: Select all
  1. void MOAIDraw::Bind () {

  2.         MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get ();

  3.         gfxDevice.SetTexture ();

  4.         if ( MOAIDraw::mShader ) {     

  5.                 gfxDevice.SetShader ( MOAIDraw::mShader );

  6.         } else {

  7.                 gfxDevice.SetShaderPreset ( MOAIShaderMgr::LINE_SHADER );

  8.         }

  9.         gfxDevice.SetVertexPreset ( MOAIVertexFormatMgr::XYZWC );

  10. }


As mentioned before, the libmoai.xcodeproj compiles fine, whereas MoaiSample.xcodeproj doesn't. I've started wondering if there are some configuration changes that I have to do to make this work(?).

If someone would like to reproduce this, but don't feel like messing with their source, I'm happy to provide a tarball/zip on request. :)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby ibisum » Sat May 19, 2012 5:39 am

No idea about the compile issue, but I'm sure waiting with baited breath to hear about your results .. seems like a quick and easy way to add anti-aliasing ..
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Shader for MOAIDraw?

Postby thebullno1 » Sat May 19, 2012 6:44 am

When you declare a static variable in the header, you need to define it in the source too.
Try adding
Code: Select all
  1. MOAIShader*      MOAIDraw::mShader;


to MOAIShader.cpp.

It's a C++ thing, not an xcode problem.
User avatar
thebullno1
 
Posts: 258
Joined: Sat Jul 02, 2011 8:53 am

Re: Shader for MOAIDraw?

Postby joarb » Sun May 20, 2012 10:32 am

Thank you very much, thebullno1! As you probably have guessed, I'm not exactly a C++ veteran, so this would've taken me days and days of googling. I guess XCode got a little too much negative press in this thread, but since the library-project compiled, I was dead certain that it had to be an XCode thing. :)

@ibisum: yeah, I think it would be really great for a lot of stuff - at the moment I'm rolling my own gui and I'm hoping it will give my buttons a sorely needed facelift. I'll keep you posted in this thread when any progress is being made.
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby ibisum » Sun May 20, 2012 11:26 am

@joarb, I'm using moaiguihttp://code.google.com/p/moaigui/ for GUI stuff, its really quite nice .. I'd also use the shader for the purposes of prettying up the buttons ..
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Shader for MOAIDraw?

Postby joarb » Mon May 21, 2012 11:45 am

@ibisum: that does seem like a fairly mature project. Nice link! At the moment, my gui needs are pretty basic and I've ported my app to use a variant of the state manager as shown in the Wolf Clicker tutorial. At first glance, it seems like that another rewrite in order for the moaigui components and state manager to play along nicely, will cause more problems than it solves for me. But I'll be sure to keep moaigui in mind when I get going with my next project. :)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby ibisum » Mon May 21, 2012 12:28 pm

Well just FYI, we're about to ship an app that uses MOAIGUI, and have done a lot of work in it lately, so if you decide to pick it up and have another look be sure I'll be happy to help ..
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Shader for MOAIDraw?

Postby joarb » Tue May 22, 2012 2:26 am

Thanks ibisum, that's super nice of you. :)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby joarb » Thu May 24, 2012 12:32 pm

It seems like I just tried to hard with this one ... When my first attempt didn't quite work out. In the midst of what I suspected to be a horribly long debugging session, it dawned on me that MOAIScriptDeck is a MOAIDeck descendant, as all the other MOAI-decks. So what's one of the cool features of MOAIDeck? Well, it supports shaders using MOAIDeck:setShader. So I did a MOAIScriptDeck:setShader, and so far it looks really promising. Happy coding! ;)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby ibisum » Fri May 25, 2012 1:50 am

Ah so does this mean we could easily add anti-aliasing shaders to a Deck somehow, without needing to modify the host? If so I'd love to know the details ..
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Shader for MOAIDraw?

Postby joarb » Fri May 25, 2012 2:49 am

Well, I'm not nearly experienced enough in OpenGL SL to answer that, but I have achieved drawing primitives and having them shaded. At the moment, I'm trying to figure out if it's possible to pass in attributes to the vertex shader using MOAIVertexBuffer/MOAIShader:setVertexAttribute (see upcoming post for details).

I'll post a working sample tonight (GMT +1) if possible. :)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby joarb » Fri May 25, 2012 10:36 am

I promised a working example, and so here it is. Main.lua goes like this:
Code: Select all
  1. width = 512

  2. height = 512

  3.  

  4. MOAISim.openWindow ( "test", width, height )

  5.  

  6. viewport = MOAIViewport.new ()

  7. viewport:setSize ( width, height )

  8. viewport:setScale ( width, height )

  9.  

  10. layer = MOAILayer2D.new ()

  11. layer:setViewport ( viewport )

  12. MOAISim.pushRenderPass ( layer )

  13.  

  14. scriptDeck = MOAIScriptDeck.new ( )

  15. scriptDeck:setRect ( - width / 2, - height / 2, width / 2, height / 2 )

  16.  

  17. prop = MOAIProp2D.new ()

  18. prop:setDeck ( scriptDeck )

  19. layer:insertProp ( prop )

  20.  

  21. onDraw = function ( )

  22.         MOAIGfxDevice.setPenColor ( 1, 1, 1, 1 )

  23.         vertices = { - 100.0, 0.0, 0.0, 200.0, 100.0, 0.0 }

  24.         MOAIDraw.fillFan ( vertices )

  25. end

  26.  

  27. file = assert ( io.open ( 'shader.vsh', mode ))

  28. vsh = file:read ( '*all' )

  29. file:close ()

  30.  

  31. file = assert ( io.open ( 'shader.fsh', mode ))

  32. fsh = file:read ( '*all' )

  33. file:close ()

  34.  

  35. shader = MOAIShader.new ()

  36. shader:setVertexAttribute ( 1, 'position' )

  37. shader:setVertexAttribute ( 2, 'color' )

  38.  

  39. shader:load ( vsh, fsh )

  40.  

  41. scriptDeck:setShader ( shader )

  42. scriptDeck:setDrawCallback ( onDraw )

  43.  

I'm still not sure how the content of the vertex buffers are filled, but at least they end up in the vertex shader as expected. Here's the vertex shader (shader.vsh):
Code: Select all
  1. attribute vec4 position;

  2. attribute vec4 color;

  3.  

  4. varying vec4 colorVarying;

  5.  

  6. void main () {

  7.         gl_Position = position;

  8.     colorVarying = color * position;

  9. }

and finally the fragment shader (shader.fsh):
Code: Select all
  1. varying LOWP vec4 colorVarying;

  2.  

  3. void main() {

  4.         gl_FragColor = colorVarying;

  5. }

  6.  

For me, the output looks like this:
Image
As I wrote in the post regarding custom vertex attributes (http://getmoai.com/forums/need-assistance-using-moaishader-setvertexattribute-t827/), I need to be able to pass in custom vertex attributes to get my desired effects. If there is a way to do that, I suspect that this method can take you anywhere.
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: Shader for MOAIDraw?

Postby joarb » Mon May 28, 2012 1:32 pm

Finally got it to work, see http://getmoai.com/forums/post4226.html#p4226 for the way I did it. @ibisum: try googling for opengl shader antialiasing to see if you can find any good tutorials. If nothing turns up, I believe it's pretty easy to set up OpenGL to draw antialiased lines, but I'm guessing you'd have to modify the Moai SDK (and rebuild the host) to do that.
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm


Return to Moai SDK

Who is online

Users browsing this forum: ezraanderson and 0 guests

x