Weapon.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. this->damage = 1;
  22. };
  23. Weapon::~Weapon(){
  24. }
  25. void Weapon::rotateWeapon(Vec3f rotation){
  26. if(rotation.x < maxRotation.x && rotation.x > minRotation.x){
  27. rotationWeapon.x = rotation.x;
  28. }
  29. if(rotation.z < maxRotation.y && rotation.z > minRotation.y){
  30. rotationWeapon.z = rotation.z;
  31. }
  32. rotationWeapon.y = rotation.y;
  33. }
  34. void Weapon::rotate(Vec2f rotation){
  35. this->rotation.y = -rotation.y;
  36. }
  37. void Weapon::move(Vec3f location){
  38. position = location;
  39. }
  40. Vec3f multiply(float matrix[16], Vec3f vec)
  41. {
  42. Vec3f result;
  43. for(int i = 0; i < 4; i++)
  44. {
  45. for(int p = 0; p < 4; p++)
  46. {
  47. result[i] += matrix[i * p] * vec[p];
  48. }
  49. }
  50. return result;
  51. }
  52. void Weapon::draw(){
  53. if (weaponmodel != nullptr)
  54. {
  55. glPushMatrix();
  56. //Player position and rotation
  57. glTranslatef(position.x, position.y, position.z);
  58. glRotatef(rotation.x, 1, 0, 0);
  59. glRotatef(rotation.y, 0, 1, 0);
  60. glRotatef(rotation.z, 0, 0, 1);
  61. //offset from player
  62. glTranslatef(offsetPlayer.x, offsetPlayer.y, offsetPlayer.z);
  63. //Rotate weapon itself, from specific anker point
  64. glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
  65. glRotatef(rotationWeapon.z, 0, 0, 1);
  66. glRotatef(rotationWeapon.y, 0, 1, 0);
  67. glRotatef(rotationWeapon.x, 1, 0, 0);
  68. glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z);
  69. weaponmodel->draw();
  70. //Test code for finding anchor point
  71. glTranslatef(collisionPoint.x, collisionPoint.y, collisionPoint.z);
  72. float matrix[16];
  73. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  74. Vec3f point = multiply(matrix, Vec3f(1,1,1));
  75. glScalef(scale, scale, scale);
  76. glPopMatrix();
  77. glPushMatrix();
  78. glTranslatef(point.x, point.y, point.z);
  79. glColor3ub(255, 255, 0);
  80. glBegin(GL_LINES);
  81. glVertex2f(0, 4);
  82. glVertex2f(0, -4);
  83. glVertex2f(4, 0);
  84. glVertex2f(-4, 0);
  85. glEnd();
  86. glPopMatrix();
  87. }
  88. }