Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "game.h"
- Game::Game()
- {
- _isRunning = true;
- _xAngle = _yAngle = _zAngle = 0.0f;
- _newPos = glm::vec3(0, 0, 20);
- m_pEffect = NULL;
- m_scale = 0.f;
- m_directionalLight.Color = glm::vec3(1.f, 1.f, 1.f);
- m_directionalLight.AmbientIntensity = 0.25f;
- m_directionalLight.DiffuseIntensity = 0.9f;
- m_directionalLight.Direction = glm::vec3(1.f, 0.f, 0.f);
- m_persProjInfo.FOV = 60.f;
- m_persProjInfo.Height = 600.0f;
- m_persProjInfo.Width = 800.0f;
- m_persProjInfo.zNear = 0.1f;
- m_persProjInfo.zFar = 1000.0f;
- }
- Game::~Game()
- {
- }
- int Game::OnExecute()
- {
- if (!OnInit())
- return -1;
- SDL_Event Event;
- while (_isRunning)
- {
- while (SDL_PollEvent(&Event))
- {
- OnEvent(&Event);
- }
- OnRender();
- OnLoop();
- /* Swap our back buffer to the front */
- SDL_GL_SwapWindow(_screen);
- }
- OnCleanup();
- return 0;
- }
- void Game::generalSetup()
- {
- // Initialize SDL2
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- sdldie("Failed to initial SDL2.");
- else
- {
- /* Request OpenGL 3.3 */
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); // SDL_GL_CONTEXT_PROFILE_CORE
- // Create window
- _screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
- SDL_GetRendererInfo(_render, &_renderInfo);
- // Create Context
- _mainContext = SDL_GL_CreateContext(_screen);
- //SDL_GL_MakeCurrent(_screen, _mainContext);
- // Create Surface
- _surface = SDL_GetWindowSurface(_screen);
- SDL_FillRect(_surface, NULL, SDL_MapRGB(_surface->format, 0xCC, 0x20, 0x20));
- SDL_UpdateWindowSurface(_screen);
- /* swap synchronized */
- SDL_GL_SetSwapInterval(1);
- // Initialize GLew 1.10
- glewExperimental = GL_TRUE;
- GLenum error = glewInit();
- if (error != GLEW_OK)
- printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
- else
- {
- std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
- std::cout << "GLew Version: 1.10.0" << std::endl;
- }
- // InitGLs
- glClearColor(1.0, 0.0, 0.0, 1.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glShadeModel(GL_SMOOTH);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_NORMALIZE);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glm::vec3 Pos(3.0f, 7.0f, -20.0f);
- //_cam = GCamera();
- //_cam.setPosition(Pos);
- //gluPerspective(45.0, 800.0 / 600.0, 1.0, 500.0);
- //gluLookAt(_cam.X(), _cam.Y(), _cam.Z(), 0, 0, 0, 0, 1, 0);
- glMatrixMode(GL_MODELVIEW);
- }
- }
- bool Game::OnInit()
- {
- generalSetup();
- m_pEffect = new Lighting();
- if (!m_pEffect->Init())
- {
- std::cout << "Error initializing lighting" << std::endl;
- return false;
- }
- m_pEffect->Enable();
- m_pEffect->SetColorTextureUnit(COLOR_TEXTURE_UNIT_INDEX);
- m_pEffect->setDirectionalLight(m_directionalLight);
- m_pEffect->setMatSpecularIntensity(0.0f);
- m_pEffect->setMatSpecularPower(0);
- m_model = Model();
- if (!m_model.LoadMesh("earth02.dae"))
- {
- std::cout << "ERROR: model unable to laod" << std::endl;
- }
- else
- std::cout << "Model loaded" << std::endl;
- return true;
- }
- void Game::OnEvent(SDL_Event* Event)
- {
- if (Event->type == SDL_QUIT)
- _isRunning = false;
- }
- void Game::OnLoop()
- {
- displayFPS();
- }
- void Game::OnRender()
- {
- m_scale += 0.01f;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //glViewport(0, 0, 800, 600);
- //glLoadIdentity();
- m_pEffect->setEyeWorldPos(glm::vec3(3.0f, 7.0f, -20.0f));
- Pipeline p;
- p.SetCamera(glm::vec3(3.0f, 7.0f, -20.0f), glm::vec3(0.0f, -0.2f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
- p.Rotate(0.0f, m_scale, 0.0f);
- p.SetPerpsectiveProj(m_persProjInfo); // m_persProjInfo.FOV, m_persProjInfo.Width, m_persProjInfo.Height, m_persProjInfo.zNear, m_persProjInfo.zFar
- p.Scale(.1f, .1f, .1f);
- p.WorldPos(0.0f, 0.0f, 10.f);
- m_pEffect->SetWVP(p.GetWVPTrans());
- m_pEffect->SetWorldMatrix(p.GetWorldTrans());
- m_model.Render();
- }
- void Game::OnCleanup()
- {
- SDL_GL_DeleteContext(_mainContext);
- SDL_DestroyWindow(_screen);
- SDL_Quit();
- }
- void Game::displayFPS()
- {
- static long lastTime = SDL_GetTicks();
- static long loops = 0;
- static GLfloat fps = 0.0f;
- int newTime = SDL_GetTicks();
- loops++;
- if (newTime - lastTime > 100)
- {
- float newFPS = (float) loops / float(newTime - lastTime) * 1000.0f;
- fps = (fps + newFPS) / 2.0f;
- //std::cout << "FPS: " << (int)fps << std::endl;
- lastTime = newTime;
- loops = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement