Weapon.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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,
  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. };
  21. Weapon::~Weapon(){
  22. }
  23. void Weapon::rotateWeapon(Vec3f rotation){
  24. if(rotation.x < maxRotation.x && rotation.x > minRotation.x){
  25. rotationWeapon.x = rotation.x;
  26. }
  27. if(rotation.z < maxRotation.y && rotation.z > minRotation.y){
  28. rotationWeapon.z = rotation.z;
  29. }
  30. rotationWeapon.y = rotation.y;
  31. }
  32. void Weapon::rotate(Vec2f rotation){
  33. this->rotation.y = -rotation.y;
  34. }
  35. void Weapon::move(Vec3f location){
  36. position = location;
  37. }
  38. void Weapon::draw(){
  39. if (weaponmodel != nullptr)
  40. {
  41. glPushMatrix();
  42. //Player position and rotation
  43. glTranslatef(position.x, position.y, position.z);
  44. glRotatef(rotation.x, 1, 0, 0);
  45. glRotatef(rotation.y, 0, 1, 0);
  46. glRotatef(rotation.z, 0, 0, 1);
  47. //offset from player
  48. glTranslatef(offsetPlayer.x, offsetPlayer.y, offsetPlayer.z);
  49. //Rotate weapon itself, from specific anker point
  50. glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
  51. glRotatef(rotationWeapon.z, 0, 0, 1);
  52. glRotatef(rotationWeapon.y, 0, 1, 0);
  53. glRotatef(rotationWeapon.x, 1, 0, 0);
  54. glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z);
  55. glScalef(scale, scale, scale);
  56. weaponmodel->draw();
  57. //Test code for finding anker point
  58. /* glColor3ub(255, 255, 0);
  59. glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
  60. glBegin(GL_LINES);
  61. glVertex2f(0, 4);
  62. glVertex2f(0, -4);
  63. glVertex2f(4, 0);
  64. glVertex2f(-4, 0);
  65. glEnd();*/
  66. glPopMatrix();
  67. }
  68. }