dykow

ske

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