Advertisement
Guest User

openGL testing

a guest
Apr 20th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/glew.h>
  3. #include <SDL2/SDL.h>
  4.  
  5. #include "shader.h"
  6. #include "texture.h"
  7. #include "mesh.h"
  8. #include "transform.h"
  9. #include "camera.h"
  10.  
  11. #include "entity.h"
  12. #include <vector>
  13.  
  14. #define WIDTH 500
  15. #define HEIGHT 500
  16.  
  17.  
  18. int mouse_x;
  19. int mouse_y;
  20. int x;
  21. int y;
  22. bool key_left = false;
  23. bool key_right = false;
  24. bool key_up = false;
  25. bool key_down = false;
  26. int frame_rate = 1000 / 60;
  27.  
  28.  
  29. void renderLoop (std::vector<Entity*> entities, float* counter);
  30.  
  31. int main () {
  32.  
  33.     // Initialize the gl context
  34.     SDL_Window* m_window;
  35.     SDL_GLContext m_glContext;
  36.  
  37.     SDL_Init(SDL_INIT_EVERYTHING);
  38.  
  39.     // COLOR DEPTH
  40.     SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8);
  41.     SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
  42.     SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8);
  43.     SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
  44.     SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
  45.     SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
  46.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
  47.    
  48.     std::string title = "openGL testing";
  49.    
  50.     m_window = SDL_CreateWindow( title.c_str(),
  51.                                  SDL_WINDOWPOS_CENTERED,
  52.                                  SDL_WINDOWPOS_CENTERED,
  53.                                  WIDTH, HEIGHT,
  54.                                  SDL_WINDOW_OPENGL
  55.                                  );
  56.     m_glContext = SDL_GL_CreateContext(m_window);
  57.  
  58.     if (glewInit() != GLEW_OK) {
  59.         std::cerr << "glew init failure\n";
  60.     }
  61.     // for 3D and hiding faces
  62.     glEnable(GL_DEPTH_TEST);
  63.     glEnable(GL_CULL_FACE);
  64.     glCullFace(GL_BACK);
  65.  
  66.     // Camera Parameters: (const glm::vec3& pos, float fov, float aspect, float zNear, float zFar)
  67.     Camera camera(glm::vec3(0,0,-10), 70.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 1000.0f);
  68.     Camera* pCamera = &camera;
  69.    
  70.     // Create the shader program
  71.     Shader shader("./res/basicShader");
  72.     Shader* pShader = &shader;
  73.    
  74.     // Instantiate the entity objects, passing a pointer to camera and pointer to shader
  75.     Entity entity1("./res/bricks.jpg", "./res/monkey3.obj", pCamera, pShader);
  76.     Entity entity2("./res/texture3.jpg", "./res/monkey3.obj", pCamera, pShader);
  77.     Entity entity3("./res/texture2.jpg", "./res/monkey3.obj", pCamera, pShader);
  78.    
  79.     // Create a vector of type pointer to entity
  80.     std::vector<Entity*> entities = {
  81.         { &entity1 },
  82.         { &entity2 },
  83.         { &entity3 },
  84.     };
  85.     // Set the positions of entities to something so they are separate
  86.     entity1.SetPos(-4.0f, 0.0f, -2.0f);
  87.     entity2.SetPos(0.0f, 0.0f, 0.0f);
  88.     entity3.SetPos(4.0f, 0.0f, 2.0f);
  89.    
  90.     bool terminate = false;
  91.     float counter = 0.0f;
  92.     Uint32 time_start = SDL_GetTicks();
  93.     SDL_Event event;
  94.  
  95.     while (!terminate) {
  96.         if (SDL_GetTicks() >= time_start + frame_rate)
  97.         {
  98.             time_start = SDL_GetTicks();
  99.             while (SDL_PollEvent(&event))
  100.             {
  101.                 if (event.type == SDL_QUIT) {
  102.                     terminate = true;
  103.                     break;
  104.                 }
  105.             }
  106.  
  107.             glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  108.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109.  
  110.             renderLoop(entities, &counter);
  111.             SDL_GL_SwapWindow(m_window);;
  112.             counter += 1.0f;
  113.         }
  114.     }
  115.  
  116.     SDL_GL_DeleteContext(m_glContext);
  117.     SDL_DestroyWindow(m_window);
  118.     SDL_Quit();
  119.    
  120.     return 0;
  121. }
  122.  
  123. void renderLoop (std::vector<Entity*> entities, float* counter)
  124. {
  125.     entities[0]->SetRot(*counter, 0.0f, 0.0f);
  126.     entities[1]->SetRot(0.0f, *counter, 0.0f);
  127.     entities[2]->SetRot(0.0f, 0.0f, *counter);
  128.    
  129.     for(unsigned int i = 0; i < entities.size(); i++)
  130.         entities[i]->Render();
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement