{"id":71,"date":"2013-06-14T19:14:01","date_gmt":"2013-06-14T17:14:01","guid":{"rendered":"http:\/\/dagger.se\/?p=71"},"modified":"2013-06-14T21:08:25","modified_gmt":"2013-06-14T19:08:25","slug":"gpu-skinned-animations-in-libgdx","status":"publish","type":"post","link":"http:\/\/dagger.se\/?p=71","title":{"rendered":"GPU skinned animations in libgdx"},"content":{"rendered":"<p>(Without using modelbatch)<\/p>\n<p>We&#8217;ve been using the old libgdx 3d api for about 7 months now, in order to develop our game. We recently (last week or so) decided to make the transition to the new 3d API to keep our libgdx versions up to date with the nightlies. The transition itself was rather simple, and we&#8217;re still keeping things at a level very near what we used in the old API. For example, we&#8217;re not using the modelBatch class nor any of the shaders provided to us.<\/p>\n<p>The main thing that we&#8217;ve been missing graphically has been animated 3d objects. We did have a keyframed animation system that we developed ourself to animate between two static states of an obj-model, but the downsides were just too many for us to bother adding it to the actual game.<\/p>\n<p>So, we finally sat down trying to implement animations today. After a few hours of reading through the source files of modelbatch, skeletontest and defaultshader &#8211; we finally understood what was going on and could start implementing our own version.<\/p>\n<p>This blogpost will go through how we handle and render our animations, and hopefully others that would like to stick to a lower level of opengl can benefit from it.<\/p>\n<p>The first thing we do is ofcourse to load the file. We do this using the libgdx class AssetManager:<\/p>\n<pre>assets.load(\"data\/model\/charModelAnim.g3dj\",Model.class);<\/pre>\n<p>The file can be loaded either as g3db (binary) or as g3dj (json). You can convert the most used file formats to either of these two using the fbx-converter (https:\/\/github.com\/libgdx\/fbx-conv), we used fbx files that our artist created for us.<\/p>\n<p>When the file has been loaded, its time to create the actual objects. We create modelInstances in the constructor of our characters:<\/p>\n<pre>Model characterModel = assets.get(\"data\/model\/charModelAnim.g3dj\");\r\ncharInstance = new ModelInstance(characterModel);\r\nanimationController = new AnimationController(charInstance);\r\nanimationController.animate(charInstance.animations.get(0).id, -1, 1f, null, 0.2f); \/\/ Starts the animaton<\/pre>\n<p>Now we have our animation set up, and all that remains is for the character to call:<\/p>\n<pre> animationController.update(deltaTime);<\/pre>\n<p>during its update method.<\/p>\n<p>This is all the logic that we need to do the actual updating of the animation, and it&#8217;s time to start thinking about how to render it. In order to render our animated objects, we do the following:<\/p>\n<pre>charShader.begin();\r\n\/\/ Bind whatever uniforms \/ textures you need\r\nfor (GameCharacter ch : g.characters){\r\n\u00a0\u00a0\u00a0 Array&lt;Renderable&gt; renderables = new Array&lt;Renderable&gt;();\r\n\u00a0\u00a0\u00a0 final Pool&lt;Renderable&gt; pool = new Pool&lt;Renderable&gt;() {\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 @Override\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 protected Renderable newObject () {\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 return new Renderable();\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 @Override\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 public Renderable obtain () {\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 Renderable renderable = super.obtain();\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 renderable.lights = null;\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 renderable.material = null;\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 renderable.mesh = null;\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 renderable.shader = null;\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 return renderable;\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 };\r\n\u00a0\u00a0\u00a0 ch.charInstance.getRenderables(renderables, pool);\r\n\u00a0\u00a0\u00a0 Matrix4 idtMatrix = new Matrix().idt();\r\n\u00a0\u00a0\u00a0 float[] bones = new float[12*16];\r\n\u00a0\u00a0\u00a0 for (int i = 0; i &lt; bones.length; i++)\r\n\u00a0\u00a0\u00a0 bones[i] = idtMatrix.val[i%16];\r\n\u00a0\u00a0\u00a0 for (Renderable render : renderables) {\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 mvpMatrix.set(g.cam.combined);\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 mvpMatrix.mul(render.worldTransform);\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 charShader.setUniformMatrix(\"u_mvpMatrix\", mvpMatrix);\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 nMatrix.set(g.cam.view);\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 nMatrix.mul(render.worldTransform);\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 charShader.setUniformMatrix(\"u_modelViewMatrix\", nMatrix);\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 nMatrix.inv();\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 nMatrix.tra();\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 charShader.setUniformMatrix(\"u_normalMatrix\", nMatrix);\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 StaticVariables.tempMatrix.idt();\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 for (int i = 0; i &lt; bones.length; i++) {\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 final int idx = i\/16;\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 bones[i] = (render.bones == null || idx &gt;= render.bones.length || render.bones[idx] == null) ?\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 idtMatrix.val[i%16] : render.bones[idx].val[i%16];\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 charShader.setUniformMatrix4fv(\"u_bones\", bones, 0, bones.length);\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 render.mesh.render(charShader, render.primitiveType, render.meshPartOffset, render.meshPartSize);\r\n\u00a0\u00a0\u00a0 }\r\n}\r\ncharShader.end();<\/pre>\n<p>So basically what happends is, we loop all characters that we want to render. We create an array of renderables, aswell as a pool of renderables. These are needed in order to collect the renderables from the modelInstance (via instance.getRenderables(array, pool)). We also need to create and initiate an array of floats to represent each bone matrix. We have 12 bone matrixes, and each matrix contains 16 floats. Now, we loop all renderables that we have collected, and set the matrixes that we need as usual. We also need to loop and fill the float array with the data from the matrices, if there is such data to be found.<\/p>\n<p>(Also, please note that you should not be creating new objects each rendercall, as in the code above, but instead create them once and reuse them.)<\/p>\n<p>So, there&#8217;s all the code we call at the CPU in order to initiate the rendering. Now all that is left is to show our shader code! All the skinning occurs in the vertex shader, and that is why we will only share that this time (Our fragment shader can be found in the normalmap source as linked from another blogpost).<\/p>\n<pre>\/\/Firstly, we need to define loads of new attributes, one for each bone.\r\nattribute vec2 a_boneWeight0;\r\nattribute vec2 a_boneWeight1;\r\n....\r\nattribute vec2 a_boneWeight11;\r\n\/\/We also need to take the bonematrices\r\nuniform mat4 u_bones[12];\r\nvoid main() {\r\n\u00a0\u00a0\u00a0 \/\/ Calculate skinning for each vertex\r\n\u00a0\u00a0\u00a0 mat4 skinning = mat4(0.0);\r\n\u00a0\u00a0\u00a0 skinning += (a_boneWeight0.y) * u_bones[int(a_boneWeight0.x)];\r\n\u00a0\u00a0\u00a0 skinning += (a_boneWeight1.y) * u_bones[int(a_boneWeight1.x)];\r\n\u00a0\u00a0\u00a0 ...\r\n\u00a0\u00a0\u00a0 skinning += (a_boneWeight11.y) * u_bones[int(a_boneWeight11.x)];\r\n\u00a0\u00a0\u00a0 \/\/Include skinning into the modelspace position\r\n\u00a0\u00a0\u00a0 vec4 pos = skinning * vec4(a_position,1.0);\r\n\u00a0\u00a0\u00a0 \/\/ Rest of code is justlike usual\r\n\u00a0\u00a0\u00a0 v = vec3((u_modelViewMatrix * pos).xyz);\r\n\u00a0\u00a0\u00a0 vsN = normalize(vec3(u_normalMatrix * skinning * vec4(a_normal, 0.0)).xyz); \/\/viewspaceNormal\r\n\u00a0\u00a0\u00a0 gl_Position = u_mvpMatrix * pos;\r\n\u00a0\u00a0\u00a0 v_texCoord = a_texCoord0;\r\n}<\/pre>\n<p>So thats basically it, and we got away without having to rewrite our entire render engine to be able to use the new 3d API!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" src=\"http:\/\/www.youtube.com\/embed\/iqFSAm3ubu0\" height=\"315\" width=\"560\" allowfullscreen=\"\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Without using modelbatch) We&#8217;ve been using the old libgdx 3d api for about 7 months now, in order to develop our game. We recently (last week or so) decided to make the transition to the new 3d API to keep our libgdx versions up to date with the nightlies. The transition itself was rather simple, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-tech","entry"],"_links":{"self":[{"href":"http:\/\/dagger.se\/index.php?rest_route=\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/dagger.se\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dagger.se\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dagger.se\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/dagger.se\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71"}],"version-history":[{"count":15,"href":"http:\/\/dagger.se\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":110,"href":"http:\/\/dagger.se\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions\/110"}],"wp:attachment":[{"href":"http:\/\/dagger.se\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dagger.se\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dagger.se\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}