Guest User

Untitled

a guest
Jan 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. //includes and shit:
  2. #include <GLTools.h>
  3. #include <GLShaderManager.h>
  4. #include <GLBatch.h>
  5. #include <GLFrame.h>
  6. #include <GLMatrixStack.h>
  7. #include <GLGeometryTransform.h>
  8. #include <GLFrustum.h>
  9.  
  10. #include <windows.h>
  11.      
  12. #ifdef __APPLE__
  13. #include <glut/glut.h>
  14. #else
  15. #define FREEGLUT_STATIC
  16. #include <GL/glut.h>
  17. #endif
  18.      
  19. //Objects:
  20. GLShaderManager shaderManager;
  21. GLMatrixStack modelViewStack;
  22. GLMatrixStack projectionStack;
  23. GLGeometryTransform tPipeline;
  24. GLFrustum viewFrustum;
  25.  
  26. GLFrame cameraFrame;
  27. GLBatch floorBatch;
  28. GLTriangleBatch sphereBatch;
  29.  
  30. //Global vars:
  31. float turnSpeed = 0.0005f;
  32. float stepSize = 0.001f;
  33.  
  34. //Prototypes:
  35. void changeSize(int w, int h); //Runs when the window is modified
  36. void setupRenderingContext(); //Runs once, to setup things
  37. void renderScene(); //Runs every frame; basic rendering pipeline
  38. void keyboardInput(); //Runs every frame to check if any keys are being pressed
  39.    
  40. //Glut functions:
  41. void changeSize(int w, int h)
  42. {
  43.     //Redefine the viewport:
  44.     glViewport(0, 0, w, h);
  45.      
  46.     //prevent future division by zero:
  47.     if(h == 0)  {h = 1;}
  48.  
  49.     //Set up the perspective projection:
  50.     viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 100.0f);
  51.     projectionStack.LoadMatrix(viewFrustum.GetProjectionMatrix());
  52.  
  53.     tPipeline.SetMatrixStacks(modelViewStack, projectionStack);
  54. }
  55.      
  56. void renderScene()
  57. {
  58.     //Color values:
  59.     static GLfloat vFloorColor[] = {0.58f, 0.0f, 0.83f, 1.0f};  //Violet
  60.     static GLfloat vSphereColor[] = {0.0f, 1.0f, 1.0f, 1.0f};   //Cyan
  61.     //Light position:
  62.     //static GLfloat vLightPos[] = {0.0f, 2.0f, 0.0f};
  63.  
  64.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  65.  
  66.     modelViewStack.PushMatrix();
  67.  
  68.     M3DMatrix44f mCamera;
  69.     cameraFrame.GetCameraMatrix(mCamera);
  70.     modelViewStack.PushMatrix(mCamera);
  71.  
  72.     //Check for keyboard input:
  73.     keyboardInput();
  74.  
  75.     shaderManager.UseStockShader(GLT_SHADER_FLAT, tPipeline.GetModelViewProjectionMatrix(), vFloorColor);
  76.     //shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, tPipeline.GetModelViewMatrix(), tPipeline.GetProjectionMatrix(), vFloorColor);
  77.     floorBatch.Draw();
  78.  
  79.     shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, tPipeline.GetModelViewMatrix(), tPipeline.GetProjectionMatrix(), vSphereColor);
  80.     sphereBatch.Draw();
  81.  
  82.     modelViewStack.PopMatrix();
  83.     modelViewStack.PopMatrix();
  84.  
  85.     glutSwapBuffers();
  86.     glutPostRedisplay();
  87. }
  88.  
  89. //Regular functions:
  90. void setupRenderingContext()
  91. {
  92.     //Black blackground
  93.     glClearColor(0, 0, 0, 1);
  94.      
  95.     shaderManager.InitializeStockShaders();
  96.  
  97.     glEnable(GL_DEPTH_TEST);
  98.     //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  99.      
  100.     //Make flor
  101.     floorBatch.Begin(GL_LINES, 324);
  102.     for(GLfloat a = 20.0; a >= -20.0f; a -= 0.5)
  103.     {
  104.         floorBatch.Vertex3f(a, -0.55f, 20.0f);
  105.         floorBatch.Vertex3f(a, -0.55f, -20.0f);
  106.  
  107.         floorBatch.Vertex3f(20.0f, -0.55f, a);
  108.         floorBatch.Vertex3f(-20.0f, -0.55f, a);
  109.     }
  110.     floorBatch.End();
  111.  
  112.     //Make sphere:
  113.     gltMakeSphere(sphereBatch, 0.5f, 10, 20);
  114. }
  115.  
  116. void keyboardInput()
  117. {
  118.     short keyLeft = GetKeyState(VK_LEFT);
  119.     bool downLeft = keyLeft < 0;
  120.     if (downLeft == true)
  121.     {
  122.         cameraFrame.RotateWorld(turnSpeed, 0.0f, 1.0f, 0.0f);
  123.     }
  124.  
  125.     short keyRight = GetKeyState(VK_RIGHT);
  126.     bool downRight = keyRight < 0;
  127.     if (downRight == true)
  128.     {
  129.         cameraFrame.RotateWorld(-turnSpeed, 0.0f, 1.0f, 0.0f);
  130.     }
  131.  
  132.     short keyForward = GetKeyState(VK_UP);
  133.     bool downForward = keyForward < 0;
  134.     if (downForward == true)
  135.     {
  136.         cameraFrame.MoveForward(stepSize);
  137.     }
  138.  
  139.     short keyBackward = GetKeyState(VK_DOWN);
  140.     bool downBackward = keyBackward < 0;
  141.     if (downBackward == true)
  142.     {
  143.         cameraFrame.MoveForward(-stepSize);
  144.     }
  145.    
  146. }
  147.  
  148. //Main:
  149. int main(int argc, char* argv[])
  150. {
  151.     //Setup GLTools:
  152.     gltSetWorkingDirectory(argv[0]);
  153.        
  154.     //Setup glut:
  155.     glutInit(&argc, argv);
  156.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA |GLUT_DEPTH);
  157.     glutInitWindowSize(800, 600);
  158.     glutCreateWindow("La FĂȘnetre");
  159.  
  160.     //Feed glut:
  161.     glutReshapeFunc(changeSize);
  162.     glutDisplayFunc(renderScene);
  163.  
  164.     //Setup GLEW:
  165.     GLenum glewStatus = glewInit();
  166.     if (GLEW_OK != glewStatus)
  167.     {
  168.         fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(glewStatus));
  169.         return 1;
  170.     }
  171.  
  172.     //Begin:
  173.     setupRenderingContext();
  174.     glutMainLoop();
  175.  
  176.     return 0;
  177. }
Add Comment
Please, Sign In to add comment