Weapon.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Created by janco on 25-5-16.
  3. //
  4. #include "Weapon.h"
  5. #include "Model.h"
  6. #include <iostream>
  7. #include <string>
  8. #include <cmath>
  9. Weapon::Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rotation,
  10. Vec3f offsetPlayer, Vec3f ankerPoint, Vec3f collisionPoint,
  11. Vec2f maxRotation, Vec2f minRotation){
  12. weaponmodel = Model::load(modelFilename);
  13. rotate(rotation);
  14. move(location);
  15. this->scale = scale;
  16. this->offsetPlayer = offsetPlayer;
  17. this->ankerPoint = ankerPoint;
  18. this->maxRotation = maxRotation;
  19. this->minRotation = minRotation;
  20. this->collisionPoint = collisionPoint;
  21. };
  22. Weapon::~Weapon(){
  23. }
  24. void Weapon::rotateWeapon(Vec3f rotation){
  25. if(rotation.x < maxRotation.x && rotation.x > minRotation.x){
  26. rotationWeapon.x = rotation.x;
  27. }
  28. if(rotation.z < maxRotation.y && rotation.z > minRotation.y){
  29. rotationWeapon.z = rotation.z;
  30. }
  31. rotationWeapon.y = rotation.y;
  32. }
  33. void Weapon::rotate(Vec2f rotation){
  34. this->rotation.y = -rotation.y;
  35. }
  36. void Weapon::move(Vec3f location){
  37. position = location;
  38. }
  39. void Weapon::draw(){
  40. if (weaponmodel != nullptr)
  41. {
  42. glPushMatrix();
  43. //Player position and rotation
  44. glTranslatef(position.x, position.y, position.z);
  45. glRotatef(rotation.x, 1, 0, 0);
  46. glRotatef(rotation.y, 0, 1, 0);
  47. glRotatef(rotation.z, 0, 0, 1);
  48. //offset from player
  49. glTranslatef(offsetPlayer.x, offsetPlayer.y, offsetPlayer.z);
  50. //Rotate weapon itself, from specific anker point
  51. glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
  52. glRotatef(rotationWeapon.z, 0, 0, 1);
  53. glRotatef(rotationWeapon.y, 0, 1, 0);
  54. glRotatef(rotationWeapon.x, 1, 0, 0);
  55. glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z);
  56. glScalef(scale, scale, scale);
  57. weaponmodel->draw();
  58. //Test code for finding anker point
  59. glColor3ub(255, 255, 0);
  60. glTranslatef(collisionPoint.x, collisionPoint.y, collisionPoint.z);
  61. glBegin(GL_LINES);
  62. glVertex2f(0, 4);
  63. glVertex2f(0, -4);
  64. glVertex2f(4, 0);
  65. glVertex2f(-4, 0);
  66. glEnd();
  67. glPopMatrix();
  68. }
  69. }