Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <GL/glew.h>
- #include <SDL2/SDL.h>
- #include "shader.h"
- #include "texture.h"
- #include "mesh.h"
- #include "transform.h"
- #include "camera.h"
- #include "entity.h"
- #include <vector>
- #define WIDTH 500
- #define HEIGHT 500
- int mouse_x;
- int mouse_y;
- int x;
- int y;
- bool key_left = false;
- bool key_right = false;
- bool key_up = false;
- bool key_down = false;
- int frame_rate = 1000 / 60;
- void renderLoop (std::vector<Entity*> entities, float* counter);
- int main () {
- // Initialize the gl context
- SDL_Window* m_window;
- SDL_GLContext m_glContext;
- SDL_Init(SDL_INIT_EVERYTHING);
- // COLOR DEPTH
- SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8);
- SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
- SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8);
- SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
- SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
- SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
- SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
- std::string title = "openGL testing";
- m_window = SDL_CreateWindow( title.c_str(),
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- WIDTH, HEIGHT,
- SDL_WINDOW_OPENGL
- );
- m_glContext = SDL_GL_CreateContext(m_window);
- if (glewInit() != GLEW_OK) {
- std::cerr << "glew init failure\n";
- }
- // for 3D and hiding faces
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- // Camera Parameters: (const glm::vec3& pos, float fov, float aspect, float zNear, float zFar)
- Camera camera(glm::vec3(0,0,-10), 70.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 1000.0f);
- Camera* pCamera = &camera;
- // Create the shader program
- Shader shader("./res/basicShader");
- Shader* pShader = &shader;
- // Instantiate the entity objects, passing a pointer to camera and pointer to shader
- Entity entity1("./res/bricks.jpg", "./res/monkey3.obj", pCamera, pShader);
- Entity entity2("./res/texture3.jpg", "./res/monkey3.obj", pCamera, pShader);
- Entity entity3("./res/texture2.jpg", "./res/monkey3.obj", pCamera, pShader);
- // Create a vector of type pointer to entity
- std::vector<Entity*> entities = {
- { &entity1 },
- { &entity2 },
- { &entity3 },
- };
- // Set the positions of entities to something so they are separate
- entity1.SetPos(-4.0f, 0.0f, -2.0f);
- entity2.SetPos(0.0f, 0.0f, 0.0f);
- entity3.SetPos(4.0f, 0.0f, 2.0f);
- bool terminate = false;
- float counter = 0.0f;
- Uint32 time_start = SDL_GetTicks();
- SDL_Event event;
- while (!terminate) {
- if (SDL_GetTicks() >= time_start + frame_rate)
- {
- time_start = SDL_GetTicks();
- while (SDL_PollEvent(&event))
- {
- if (event.type == SDL_QUIT) {
- terminate = true;
- break;
- }
- }
- glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- renderLoop(entities, &counter);
- SDL_GL_SwapWindow(m_window);;
- counter += 1.0f;
- }
- }
- SDL_GL_DeleteContext(m_glContext);
- SDL_DestroyWindow(m_window);
- SDL_Quit();
- return 0;
- }
- void renderLoop (std::vector<Entity*> entities, float* counter)
- {
- entities[0]->SetRot(*counter, 0.0f, 0.0f);
- entities[1]->SetRot(0.0f, *counter, 0.0f);
- entities[2]->SetRot(0.0f, 0.0f, *counter);
- for(unsigned int i = 0; i < entities.size(); i++)
- entities[i]->Render();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement