Facebook
From Coral Bongo, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 244
  1. /////////////////////////////////////////////
  2. //
  3. // Skeletal Animation
  4. // Sylwester Feduk
  5. /////////////////////////////////////////////
  6. #include <iostream>
  7. #include <vector>
  8. #include <string>
  9. #include <stdio.h>
  10. #include <glew.h>
  11. #include <wglew.h>
  12. #include <windows.h>
  13. #include <mmsystem.h>
  14. #include <GL/glut.h>
  15. using namespace std;
  16. #pragma comment(lib,"winmm.lib")
  17. ///////////////////////////////////////////
  18. #include "core.h"
  19. #include "Bmp.h"
  20. #include "ogl.h"
  21. #include "glsl.h"
  22. ///////////////////////////////////////////
  23. vec4f lightvec(-1, 0, -1, 0);
  24. #include "Mesh.h"
  25.  
  26. /////////////////////////////////////////////////////////
  27. //
  28. // EXERCISES
  29. //
  30. /////////////////////////////////////////////////////////
  31.  
  32. ///////////////////////////////////////////
  33. void printBone(Mesh halo, int index, int level) {
  34.         //TO DO
  35. }
  36. ///////////////////////////////////////////
  37.  
  38. void updateChilds(Mesh& mesh, int index);
  39. void AnimateBone(Mesh& mesh, char* boneName, float phiX, float phiY, float phiZ) {
  40.         matrix33 rx(1, 0, 0, 0, cos(phiX), -sin(phiX), 0, sin(phiX), cos(phiX));
  41.         matrix33 ry(cos(phiY), 0, sin(phiY), 0, 1, 0, -sin(phiY), 0, cos(phiY));
  42.         matrix33 rz(cos(phiZ), -sin(phiZ), 0, sin(phiZ), cos(phiZ), 0, 0, 0, 1);
  43.  
  44.         int id = mesh.animation.GetBoneIndexOf(boneName);
  45.  
  46.         matrix33 result = rx * ry * rz;
  47.  
  48.         mesh.animation.bones[id].matrix.x_component() = result.x_component();
  49.         mesh.animation.bones[id].matrix.y_component() = result.y_component();
  50.         mesh.animation.bones[id].matrix.z_component() = result.z_component();
  51.  
  52.         updateChilds(mesh, id + 1);
  53. }
  54.  
  55. void updateChilds(Mesh& mesh, int index) {
  56.  
  57.         MeshAnimation::TBone b = mesh.animation.bones[index];
  58.         matrix44 m;
  59.  
  60.         // bind pose : default
  61.         vec3f pos(b.pos[0], b.pos[1], b.pos[2]);
  62.         m.set(b.rot[0], b.rot[1], b.rot[2], b.rot[3]);
  63.         m.set_translation(pos);
  64.         if (mesh.animation.bones[index].parent >= 0)
  65.                 mesh.animation.bones[index].matrix = m*mesh.animation.bones[b.parent].matrix;
  66.         else
  67.                 mesh.animation.bones[index].matrix = m;
  68.         //std::cout << b.name << ", ";
  69.         loopi(0, b.childs.size()) {
  70.                 int childIndex = b.childs[i];
  71.                 updateChilds(mesh, childIndex);
  72.         }
  73. }
  74.  
  75. /////////////////////////////////////////////////////////
  76. bool direction = true;
  77. float x = -5;
  78. float y = 0;
  79. float z = 0;
  80.  
  81. void AnimateArms(Mesh& mesh) {
  82.  
  83.         if (direction) {
  84.                 z += 0.0001;
  85.                 AnimateBone(mesh, "joint3", x, y, z);
  86.                 AnimateBone(mesh, "joint6", -x, y, z);
  87.                 if (z >= 0.6) {
  88.                         direction = false;
  89.                 }
  90.         } else {
  91.                 z -= 0.0001;
  92.                 AnimateBone(mesh, "joint3", x, y, z);
  93.                 AnimateBone(mesh, "joint6", -x, y, z);
  94.                 if (z <= 0.0) {
  95.                         direction = true;
  96.                 }
  97.  
  98.         }
  99.  
  100.        
  101. }
  102.  
  103.  
  104. /////////////////////////////////////////////////////////
  105. //
  106. // END
  107. //
  108. /////////////////////////////////////////////////////////
  109.  
  110. void DrawScene() {
  111.         if (GetAsyncKeyState(VK_ESCAPE))  exit(0);
  112.  
  113.         // mouse pointer position
  114.         POINT cursor;
  115.         GetCursorPos(&cursor);
  116.  
  117.         // camera orientation
  118.         float   viewangle_x = float(cursor.x - 1280 / 2) / 4.0;
  119.         float   viewangle_y = float(cursor.y - 768 / 2) / 4.0;
  120.  
  121.         //time
  122.         static uint t0 = timeGetTime();
  123.         double time_elapsed = double(timeGetTime() - t0) / 1000;
  124.  
  125.         // clear and basic
  126.         glClearDepth(1);
  127.         glClearColor(0, 0, 0, 1);
  128.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  129.         glEnable(GL_DEPTH_TEST);
  130.         glEnable(GL_CULL_FACE);
  131.         glCullFace(GL_BACK);
  132.  
  133.         // projection
  134.         int vp[4];
  135.         glMatrixMode(GL_PROJECTION);
  136.         glLoadIdentity();
  137.         glGetIntegerv(GL_VIEWPORT, vp);
  138.         gluPerspective(90.0, float(vp[2]) / float(vp[3]), 0.01, 100);
  139.  
  140.         // modelview
  141.         glMatrixMode(GL_MODELVIEW);
  142.         glLoadIdentity();
  143.         glRotatef(viewangle_y, 1, 0, 0);                // set rotation
  144.         glRotatef(viewangle_x, 0, 1, 0);                // set rotation
  145.  
  146.                                                                                         // select level of detail for rendering
  147.                                                                                         // (lods are generated automatically by the ogre xml converter )
  148.  
  149.         int lod = 0;
  150.         if (GetAsyncKeyState(VK_F1)) lod = 1;
  151.         if (GetAsyncKeyState(VK_F2)) lod = 2;
  152.         if (GetAsyncKeyState(VK_F3)) lod = 3;
  153.         if (GetAsyncKeyState(VK_F4)) lod = 4;
  154.         if (GetAsyncKeyState(VK_F5)) lod = 5;
  155.  
  156.         // Test 1 : Halo character (animated mesh)
  157.  
  158.         static Mesh halo("../data/halo/halo.material",          //      required material file)
  159.                 "../data/halo/halo.mesh.xml",           //      required mesh file
  160.                 "../data/halo/halo.skeleton.xml");      //      optional skeleton file
  161.                                                                                         //halo.skinning = false;
  162.  
  163.         loopi(0, 2) {
  164.                 // Set the skeleton to an animation at a given time
  165.  
  166.                 //int idle = halo.animation.GetAnimationIndexOf("idle"); // <- in case we dont know the animation id
  167.                 halo.animation.SetPose(
  168.                         i / 2,                  // animation id (2 animations, 0 and 1, are available)
  169.                         time_elapsed);  // time in seconds
  170.  
  171.                                                         // F6 : example to manually set the shoulder - for shooting a weapon e.g.
  172.                 if (GetAsyncKeyState(VK_F7)) {
  173.                         printBone(halo, 0, 0);
  174.                 }
  175.                 if (GetAsyncKeyState(VK_F6)) {
  176.                         AnimateArms(halo);
  177.                 }
  178.  
  179.                 // Draw the model
  180.  
  181.                 halo.Draw(
  182.                         vec3f(i * 7, -5, 7),                    // position
  183.                         vec3f(0,/*time_elapsed*0.3*/ 1.57F, 0), // rotation
  184.                         lod,                                                    // LOD level
  185.                         (i & 1) == 1);                                          // draw skeleton ?
  186.         }
  187.  
  188.         // Test 2 : moon (static mesh)
  189.  
  190.         //static Mesh moon   ("../data/moon/moon.material",     //      required material file)
  191.         //                                      "../data/moon/moon.mesh.xml");  //      required mesh file
  192.         //      moon.Draw(
  193.         //              vec3f( 1.1,-0,-1),                              // position
  194.         //              vec3f(
  195.         //                      time_elapsed*0.2,                       // rotation
  196.         //                      time_elapsed*0.1,
  197.         //                      time_elapsed*0.4),     
  198.         //              lod                                                             // LOD level
  199.         //      );
  200.  
  201.         // Swap
  202.         glutSwapBuffers();
  203. }
  204. ///////////////////////////////////////////
  205. int main(int argc, char **argv) {
  206.         glutInit(&argc, argv);
  207.         glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
  208.         glutInitWindowSize(1280, 768);
  209.         glutInitWindowPosition(0, 0);
  210.         glutCreateWindow("Skeletal Animation");
  211.         glutDisplayFunc(DrawScene);
  212.         glutIdleFunc(DrawScene);
  213.         glewInit();
  214.         wglSwapIntervalEXT(0);
  215.         glutMainLoop();
  216.         return 0;
  217. }
  218. ///////////////////////////////////////////
  219.