Guest User

My3dRotationTest

a guest
Jun 23rd, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <SDL2/SDL.h>
  4. #include <SDL2/SDL_opengl.h>
  5. #include <GL/glu.h>
  6. #include <GL/gl.h>
  7.  
  8. //Declare Game Engine Variables and Functions
  9. int width,height;
  10. float delta, currentTime, lastTime, ratio;
  11. const Uint8* state = SDL_GetKeyboardState(NULL);
  12.  
  13. struct point {
  14.     float x;
  15.     float y;
  16.     float z;
  17. };
  18.  
  19. class translation {
  20.         point returnPoint;
  21.         float rotationMatrix[4][4];
  22.         float inputMatrix[4][1];
  23.         float outputMatrix[4][1];
  24.         float L;
  25.         float axisVectorx2;
  26.         float axisVectory2;
  27.         float axisVectorz2;
  28.     public:
  29.         point rotatePoint(point axisVector, point pointToRotate, point pointOfRotation, float angle) {
  30.             // Get distance of X coords, Y coords, and Z coords then rotate that point about the normalized arbituary axis vector
  31.             inputMatrix[0][0] = pointOfRotation.x - pointToRotate.x;
  32.             inputMatrix[0][0] = pointOfRotation.y - pointToRotate.y;
  33.             inputMatrix[0][0] = pointOfRotation.z - pointToRotate.z;
  34.             inputMatrix[0][0] = 1.0;
  35.  
  36.             L = (axisVector.x*axisVector.x+ axisVector.y* axisVector.y+ axisVector.z* axisVector.z);
  37.             angle = angle * 0.0174532925; //converting to radian value
  38.             axisVectorx2 = axisVector.x* axisVector.x;
  39.             axisVectory2 = axisVector.y* axisVector.y;
  40.             axisVectorz2 = axisVector.z* axisVector.z;
  41.             rotationMatrix[0][0] = (axisVectorx2 + (axisVectory2 + axisVectorz2) * cos(angle)) / L;
  42.             rotationMatrix[0][1] = (axisVector.x* axisVector.y* (1 - cos(angle)) - axisVector.z* sqrt(L) * sin(angle)) / L;
  43.             rotationMatrix[0][2] = (axisVector.x* axisVector.z* (1 - cos(angle)) + axisVector.y* sqrt(L) * sin(angle)) / L;
  44.             rotationMatrix[0][3] = 0.0;
  45.            
  46.             rotationMatrix[1][0] = (axisVector.x* axisVector.y* (1 - cos(angle)) + axisVector.z* sqrt(L) * sin(angle)) / L;
  47.             rotationMatrix[1][1] = (axisVectory2 + (axisVectorx2 + axisVectorz2) * cos(angle)) / L;
  48.             rotationMatrix[1][2] = (axisVector.y* axisVector.z* (1 - cos(angle)) - axisVector.x* sqrt(L) * sin(angle)) / L;
  49.             rotationMatrix[1][3] = 0.0;
  50.            
  51.             rotationMatrix[2][0] = (axisVector.x* axisVector.z* (1 - cos(angle)) - axisVector.y* sqrt(L) * sin(angle)) / L;
  52.             rotationMatrix[2][1] = (axisVector.y* axisVector.z* (1 - cos(angle)) + axisVector.x* sqrt(L) * sin(angle)) / L;
  53.             rotationMatrix[2][2] = (axisVectorz2 + (axisVectorx2 + axisVectory2) * cos(angle)) / L;
  54.             rotationMatrix[2][3] = 0.0;
  55.             rotationMatrix[3][0] = 0.0;
  56.             rotationMatrix[3][1] = 0.0;
  57.             rotationMatrix[3][2] = 0.0;
  58.             rotationMatrix[3][3] = 1.0;
  59.            
  60.             for(int i = 0; i < 4; i++ ){
  61.                 for(int j = 0; j < 1; j++){
  62.                     outputMatrix[i][j] = 0;
  63.                     for(int k = 0; k < 4; k++){
  64.                         outputMatrix[i][j] += rotationMatrix[i][k] * inputMatrix[k][j];
  65.                     }
  66.                 }
  67.             }
  68.  
  69.             returnPoint.x = outputMatrix[0][0];
  70.             returnPoint.y = outputMatrix[1][0];
  71.             returnPoint.z = outputMatrix[2][0];
  72.             return returnPoint;
  73.         }
  74. } translation;
  75.  
  76.  
  77. point myPoint1;
  78. point myAxisVector1;
  79. point myPointOfRotation1;
  80. float myAngle1;
  81. //Declare Game Engine Variables and Functions
  82.  
  83.  
  84. int main(int argc, char*argv[]) {
  85.     std::cout << __FILE__ << " was last compiled on " << __DATE__ << " at " << __TIME__ << ".\n";
  86.    
  87.     bool running = true;
  88.    
  89.     SDL_Init(SDL_INIT_VIDEO);
  90.     SDL_Window* window = NULL;
  91.     window = SDL_CreateWindow("Game",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480,SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
  92.    
  93.     if(window == NULL) {
  94.         std::cout << "ERROR:Window could not be created.\n";
  95.         return 0;
  96.     }
  97.    
  98.     SDL_GLContext ctx = SDL_GL_CreateContext(window);
  99.     SDL_Event mainEvent;
  100.     //Init GL
  101.     glClearColor( 0.f, 0.f, 0.f, 1.f );
  102.     glFrontFace(GL_CCW);
  103.     glCullFace(GL_BACK);
  104.     glEnable(GL_CULL_FACE);
  105.     glEnable(GL_DEPTH_TEST);
  106.     //Init GL
  107.  
  108.     //Init Variables and game data
  109.     myPoint1.x = 0.5;
  110.     myPoint1.y = 0.5;
  111.     myPoint1.z = 0.0;
  112.     myAngle1 = 10.0;
  113.     myPointOfRotation1.x = 0.0;
  114.     myPointOfRotation1.y = 0.0;
  115.     myPointOfRotation1.z = 0.0;
  116.     myAxisVector1.x = 1.0;
  117.     myAxisVector1.y = 1.0;
  118.     myAxisVector1.z = 1.0;
  119.     //Init Variables and game data
  120.    
  121.     while(running) {
  122.         while(SDL_PollEvent( &mainEvent)) {
  123.             switch( mainEvent.type ) {
  124.                 case SDL_QUIT:
  125.                     running = false;
  126.                     break;
  127.                 default:
  128.                     break;
  129.             }
  130.         }
  131.         if(state[SDL_SCANCODE_ESCAPE]) {
  132.             running = false;
  133.         }
  134.         currentTime = SDL_GetTicks();
  135.         delta = (currentTime-lastTime);
  136.         lastTime = SDL_GetTicks();
  137.         SDL_GetWindowSize(window,&width,&height);
  138.         ratio = width / (float) height;
  139.         glViewport(0, 0, width, height);
  140.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  141.         glMatrixMode(GL_PROJECTION);
  142.         glLoadIdentity();
  143.         glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  144.         glMatrixMode(GL_MODELVIEW);
  145.         glLoadIdentity();
  146.        
  147.         //Update
  148.         myPoint1 = translation.rotatePoint(myAxisVector1,myPoint1,myPointOfRotation1,myAngle1 * delta);
  149.         //Update
  150.        
  151.         //Render
  152.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  153.  
  154.         glBegin(GL_LINES);
  155.         glColor3f(0.0,1.0,0.0);
  156.         glVertex3f(0.0,0.0,0.0);
  157.         glVertex3f(myPoint1.x,myPoint1.y,myPoint1.z);
  158.         glEnd();
  159.        
  160.         SDL_GL_SwapWindow(window);
  161.         //Render
  162.     }
  163.     SDL_DestroyWindow(window);
  164.     SDL_GL_DeleteContext(ctx);
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment