Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 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 <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.  
  39. void AnimateBone(Mesh& mesh, char* boneName, float phiX, float phiY, float phiZ) {
  40. //TO DO
  41. matrix33 r_x = { {1,0,0},{0,cos(phiX),-sin(phiX)},{0,sin(phiX),cos(phiX)} };
  42. matrix33 r_y = { {cos(phiY),0,sin(phiY)},{0,1,0},{-sin(phiY),0,cos(phiY) } };
  43. matrix33 r_z = { {cos(phiZ), -sin(phiZ),0},{sin(phiZ), cos(phiZ), 0}, {0,0,1} };
  44.  
  45. matrix33 r = r_x * r_y*r_z;
  46. int id = mesh.animation.GetBoneIndexOf(boneName);
  47. mesh.animation.bones[id].matrix.x_component() = r.x_component();
  48. mesh.animation.bones[id].matrix.y_component() = r.y_component();
  49. mesh.animation.bones[id].matrix.z_component() = r.z_component();
  50.  
  51. }
  52.  
  53. void updateChilds(Mesh& mesh, int index){
  54.  
  55. MeshAnimation::TBone b = mesh.animation.bones[index];
  56. matrix44 m;
  57.  
  58. // bind pose : default
  59. vec3f pos(b.pos[0], b.pos[1], b.pos[2]);
  60. m.set(b.rot[0], b.rot[1], b.rot[2], b.rot[3]);
  61. m.set_translation(pos);
  62. if (mesh.animation.bones[index].parent >= 0)
  63. mesh.animation.bones[index].matrix = m*mesh.animation.bones[b.parent].matrix;
  64. else
  65. mesh.animation.bones[index].matrix = m;
  66. //std::cout << b.name << ", ";
  67. loopi(0, b.childs.size())
  68. {
  69. int childIndex = b.childs[i];
  70. updateChilds(mesh, childIndex);
  71. }
  72. }
  73.  
  74. /////////////////////////////////////////////////////////
  75.  
  76. float rotation = 5.5;
  77. bool rise = true;
  78.  
  79. void AnimateArms(Mesh& mesh){
  80. //TO DO
  81. char* boneName[] = { "joint2", "joint5" };
  82. int id[4];
  83.  
  84.  
  85.  
  86. if (rise) rotation += 0.0003;
  87. else rotation -= 0.0003;
  88.  
  89. if (rotation < 5.5) rise = true;
  90. else if (rotation > 7) rise = false;
  91.  
  92.  
  93.  
  94. AnimateBone(mesh, boneName[0], rotation, rotation, 0.02*rotation);
  95. id[0] = mesh.animation.GetBoneIndexOf(boneName[0]);
  96. updateChilds(mesh, id[0] + 1);
  97.  
  98. AnimateBone(mesh, boneName[1], -rotation, -rotation, -0.02*rotation);
  99.  
  100. id[1] = mesh.animation.GetBoneIndexOf(boneName[1]);
  101. updateChilds(mesh, id[1] + 1);
  102.  
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109. /////////////////////////////////////////////////////////
  110. //
  111. // END
  112. //
  113. /////////////////////////////////////////////////////////
  114.  
  115. void DrawScene()
  116. {
  117. if ( GetAsyncKeyState(VK_ESCAPE) ) exit(0);
  118.  
  119. // mouse pointer position
  120. POINT cursor;
  121. GetCursorPos(&cursor);
  122.  
  123. // camera orientation
  124. float viewangle_x = float(cursor.x-1280/2)/4.0;
  125. float viewangle_y = float(cursor.y-768/2)/4.0;
  126.  
  127. //time
  128. static uint t0=timeGetTime();
  129. double time_elapsed=double(timeGetTime()-t0)/1000;
  130.  
  131. // clear and basic
  132. glClearDepth(1);
  133. glClearColor(0,0,0,1);
  134. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  135. glEnable(GL_DEPTH_TEST);
  136. glEnable(GL_CULL_FACE);
  137. glCullFace(GL_BACK);
  138.  
  139. // projection
  140. int vp[4];
  141. glMatrixMode( GL_PROJECTION);
  142. glLoadIdentity();
  143. glGetIntegerv(GL_VIEWPORT, vp);
  144. gluPerspective(90.0,float(vp[2])/float(vp[3]) , 0.01, 100);
  145.  
  146. // modelview
  147. glMatrixMode( GL_MODELVIEW);
  148. glLoadIdentity();
  149. glRotatef( viewangle_y,1,0,0); // set rotation
  150. glRotatef( viewangle_x,0,1,0); // set rotation
  151.  
  152. // select level of detail for rendering
  153. // (lods are generated automatically by the ogre xml converter )
  154.  
  155. int lod=0;
  156. if(GetAsyncKeyState(VK_F1)) lod=1;
  157. if(GetAsyncKeyState(VK_F2)) lod=2;
  158. if(GetAsyncKeyState(VK_F3)) lod=3;
  159. if(GetAsyncKeyState(VK_F4)) lod=4;
  160. if(GetAsyncKeyState(VK_F5)) lod=5;
  161.  
  162. // Test 1 : Halo character (animated mesh)
  163.  
  164. static Mesh halo ("../data/halo/halo.material", // required material file)
  165. "../data/halo/halo.mesh.xml", // required mesh file
  166. "../data/halo/halo.skeleton.xml"); // optional skeleton file
  167. //halo.skinning = false;
  168.  
  169. loopi(0,2)
  170. {
  171. // Set the skeleton to an animation at a given time
  172.  
  173. //int idle = halo.animation.GetAnimationIndexOf("idle"); // <- in case we dont know the animation id
  174. halo.animation.SetPose(
  175. i/2, // animation id (2 animations, 0 and 1, are available)
  176. time_elapsed ); // time in seconds
  177.  
  178. // F6 : example to manually set the shoulder - for shooting a weapon e.g.
  179. if (GetAsyncKeyState(VK_F7)){
  180. printBone(halo, 0, 0);
  181. }
  182. if(GetAsyncKeyState(VK_F6))
  183. {
  184. AnimateArms(halo);
  185. }
  186.  
  187. // Draw the model
  188.  
  189. halo.Draw(
  190. vec3f(i*7,-5,7), // position
  191. vec3f(0,/*time_elapsed*0.3*/ 1.57F, 0), // rotation
  192. lod, // LOD level
  193. (i&1)==1 ); // draw skeleton ?
  194. }
  195.  
  196. // Test 2 : moon (static mesh)
  197.  
  198. //static Mesh moon ("../data/moon/moon.material", // required material file)
  199. // "../data/moon/moon.mesh.xml"); // required mesh file
  200. // moon.Draw(
  201. // vec3f( 1.1,-0,-1), // position
  202. // vec3f(
  203. // time_elapsed*0.2, // rotation
  204. // time_elapsed*0.1,
  205. // time_elapsed*0.4),
  206. // lod // LOD level
  207. // );
  208.  
  209. // Swap
  210. glutSwapBuffers();
  211. }
  212. ///////////////////////////////////////////
  213. int main(int argc, char **argv)
  214. {
  215. glutInit(&argc, argv);
  216. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
  217. glutInitWindowSize(1280, 768);
  218. glutInitWindowPosition(0, 0);
  219. glutCreateWindow("Skeletal Animation");
  220. glutDisplayFunc(DrawScene);
  221. glutIdleFunc(DrawScene);
  222. glewInit();
  223. wglSwapIntervalEXT(0);
  224. glutMainLoop();
  225. return 0;
  226. }
  227. ///////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement