Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_opengl.h>
- #include <GL/glu.h>
- #include <GL/gl.h>
- //Declare Game Engine Variables and Functions
- int width,height;
- float delta, currentTime, lastTime, ratio;
- const Uint8* state = SDL_GetKeyboardState(NULL);
- struct point {
- float x;
- float y;
- float z;
- };
- class translation {
- point returnPoint;
- float rotationMatrix[4][4];
- float inputMatrix[4][1];
- float outputMatrix[4][1];
- float L;
- float axisVectorx2;
- float axisVectory2;
- float axisVectorz2;
- public:
- point rotatePoint(point axisVector, point pointToRotate, point pointOfRotation, float angle) {
- // Get distance of X coords, Y coords, and Z coords then rotate that point about the normalized arbituary axis vector
- inputMatrix[0][0] = pointOfRotation.x - pointToRotate.x;
- inputMatrix[0][0] = pointOfRotation.y - pointToRotate.y;
- inputMatrix[0][0] = pointOfRotation.z - pointToRotate.z;
- inputMatrix[0][0] = 1.0;
- L = (axisVector.x*axisVector.x+ axisVector.y* axisVector.y+ axisVector.z* axisVector.z);
- angle = angle * 0.0174532925; //converting to radian value
- axisVectorx2 = axisVector.x* axisVector.x;
- axisVectory2 = axisVector.y* axisVector.y;
- axisVectorz2 = axisVector.z* axisVector.z;
- rotationMatrix[0][0] = (axisVectorx2 + (axisVectory2 + axisVectorz2) * cos(angle)) / L;
- rotationMatrix[0][1] = (axisVector.x* axisVector.y* (1 - cos(angle)) - axisVector.z* sqrt(L) * sin(angle)) / L;
- rotationMatrix[0][2] = (axisVector.x* axisVector.z* (1 - cos(angle)) + axisVector.y* sqrt(L) * sin(angle)) / L;
- rotationMatrix[0][3] = 0.0;
- rotationMatrix[1][0] = (axisVector.x* axisVector.y* (1 - cos(angle)) + axisVector.z* sqrt(L) * sin(angle)) / L;
- rotationMatrix[1][1] = (axisVectory2 + (axisVectorx2 + axisVectorz2) * cos(angle)) / L;
- rotationMatrix[1][2] = (axisVector.y* axisVector.z* (1 - cos(angle)) - axisVector.x* sqrt(L) * sin(angle)) / L;
- rotationMatrix[1][3] = 0.0;
- rotationMatrix[2][0] = (axisVector.x* axisVector.z* (1 - cos(angle)) - axisVector.y* sqrt(L) * sin(angle)) / L;
- rotationMatrix[2][1] = (axisVector.y* axisVector.z* (1 - cos(angle)) + axisVector.x* sqrt(L) * sin(angle)) / L;
- rotationMatrix[2][2] = (axisVectorz2 + (axisVectorx2 + axisVectory2) * cos(angle)) / L;
- rotationMatrix[2][3] = 0.0;
- rotationMatrix[3][0] = 0.0;
- rotationMatrix[3][1] = 0.0;
- rotationMatrix[3][2] = 0.0;
- rotationMatrix[3][3] = 1.0;
- for(int i = 0; i < 4; i++ ){
- for(int j = 0; j < 1; j++){
- outputMatrix[i][j] = 0;
- for(int k = 0; k < 4; k++){
- outputMatrix[i][j] += rotationMatrix[i][k] * inputMatrix[k][j];
- }
- }
- }
- returnPoint.x = outputMatrix[0][0];
- returnPoint.y = outputMatrix[1][0];
- returnPoint.z = outputMatrix[2][0];
- return returnPoint;
- }
- } translation;
- point myPoint1;
- point myAxisVector1;
- point myPointOfRotation1;
- float myAngle1;
- //Declare Game Engine Variables and Functions
- int main(int argc, char*argv[]) {
- std::cout << __FILE__ << " was last compiled on " << __DATE__ << " at " << __TIME__ << ".\n";
- bool running = true;
- SDL_Init(SDL_INIT_VIDEO);
- SDL_Window* window = NULL;
- window = SDL_CreateWindow("Game",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480,SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
- if(window == NULL) {
- std::cout << "ERROR:Window could not be created.\n";
- return 0;
- }
- SDL_GLContext ctx = SDL_GL_CreateContext(window);
- SDL_Event mainEvent;
- //Init GL
- glClearColor( 0.f, 0.f, 0.f, 1.f );
- glFrontFace(GL_CCW);
- glCullFace(GL_BACK);
- glEnable(GL_CULL_FACE);
- glEnable(GL_DEPTH_TEST);
- //Init GL
- //Init Variables and game data
- myPoint1.x = 0.5;
- myPoint1.y = 0.5;
- myPoint1.z = 0.0;
- myAngle1 = 10.0;
- myPointOfRotation1.x = 0.0;
- myPointOfRotation1.y = 0.0;
- myPointOfRotation1.z = 0.0;
- myAxisVector1.x = 1.0;
- myAxisVector1.y = 1.0;
- myAxisVector1.z = 1.0;
- //Init Variables and game data
- while(running) {
- while(SDL_PollEvent( &mainEvent)) {
- switch( mainEvent.type ) {
- case SDL_QUIT:
- running = false;
- break;
- default:
- break;
- }
- }
- if(state[SDL_SCANCODE_ESCAPE]) {
- running = false;
- }
- currentTime = SDL_GetTicks();
- delta = (currentTime-lastTime);
- lastTime = SDL_GetTicks();
- SDL_GetWindowSize(window,&width,&height);
- ratio = width / (float) height;
- glViewport(0, 0, width, height);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- //Update
- myPoint1 = translation.rotatePoint(myAxisVector1,myPoint1,myPointOfRotation1,myAngle1 * delta);
- //Update
- //Render
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBegin(GL_LINES);
- glColor3f(0.0,1.0,0.0);
- glVertex3f(0.0,0.0,0.0);
- glVertex3f(myPoint1.x,myPoint1.y,myPoint1.z);
- glEnd();
- SDL_GL_SwapWindow(window);
- //Render
- }
- SDL_DestroyWindow(window);
- SDL_GL_DeleteContext(ctx);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment