Advertisement
Guest User

Untitled

a guest
Nov 30th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "Game.h"
  3. #include "TextFileReader.h"
  4. #include "ShaderManager.h"
  5.  
  6. namespace key{
  7.  
  8. Game::Game():
  9.     _vboPosition(0),
  10.     _vboIndices(0),
  11.     _vboColor(0),
  12.     _uiIdProgram(0),
  13.     _uiIdShaderFragment(0),
  14.     _uiIdShaderVertex(0),
  15.     _uiLocationColor(-1),
  16.     _uiLocationPosition(-1),
  17.     _vao(0),
  18.     _uiLocationCurrentTime(-1),
  19.     _uiLocationMVPMatrix(-1),
  20.     _m4ProjMatrix(1.0f)
  21. {
  22.     glClearColor(0.20f, 0.20f, 0.20f, 1.0f);
  23.     float fAspectRation = _pSettings->GetWindowsDisplaySize().x / static_cast<float>(_pSettings->GetWindowsDisplaySize().y);
  24.     glm::perspective(45.0f, fAspectRation, 0.1f, 100.0f);
  25.  
  26.     std::vector<float>          _fColor;
  27.     std::vector<float>          _fVertex;
  28.     std::vector<unsigned char>  _iIndicies;
  29.  
  30.     _fColor.push_back(1);
  31.     _fColor.push_back(0);
  32.     _fColor.push_back(0);
  33.     _fColor.push_back(1);
  34.  
  35.     _fColor.push_back(0);
  36.     _fColor.push_back(1);
  37.     _fColor.push_back(0);
  38.     _fColor.push_back(1);
  39.  
  40.     _fColor.push_back(0);
  41.     _fColor.push_back(0);
  42.     _fColor.push_back(1);
  43.     _fColor.push_back(1);
  44.  
  45.     _fColor.push_back(1);
  46.     _fColor.push_back(1);
  47.     _fColor.push_back(0);
  48.     _fColor.push_back(1);
  49.  
  50.     _fColor.push_back(0);
  51.     _fColor.push_back(1);
  52.     _fColor.push_back(1);
  53.     _fColor.push_back(1);
  54.  
  55.     _fColor.push_back(1);
  56.     _fColor.push_back(0);
  57.     _fColor.push_back(1);
  58.     _fColor.push_back(1);
  59.  
  60.     _fVertex.push_back(-0.433f);
  61.     _fVertex.push_back(-0.25f);
  62.     _fVertex.push_back(0);
  63.  
  64.     _fVertex.push_back(0.433f);
  65.     _fVertex.push_back(-0.25f);
  66.     _fVertex.push_back(0);
  67.  
  68.     _fVertex.push_back(0);
  69.     _fVertex.push_back(0.5f);
  70.     _fVertex.push_back(0);
  71.  
  72.     _fVertex.push_back(-0.5f);
  73.     _fVertex.push_back(0.0f);
  74.     _fVertex.push_back(0);
  75.  
  76.     _fVertex.push_back(0.50f);
  77.     _fVertex.push_back(0.0f);
  78.     _fVertex.push_back(0);
  79.  
  80.     _fVertex.push_back(0.0);
  81.     _fVertex.push_back(0.50f);
  82.     _fVertex.push_back(0);
  83.  
  84.     _iIndicies.push_back(0);
  85.     _iIndicies.push_back(1);
  86.     _iIndicies.push_back(2);
  87.  
  88.     InitShaders();  // Load up shaders
  89.  
  90.     glGenVertexArrays(1, &_vao);
  91.     glBindVertexArray(_vao);
  92.  
  93. //  glGenBuffers(1, &_vboColor);
  94. //  glBindBuffer(GL_ARRAY_BUFFER, _vboColor);
  95. //  glBufferData(GL_ARRAY_BUFFER, _fColor.size()*sizeof _fColor[0], &_fColor[0], GL_STATIC_DRAW);
  96. //  glVertexAttribPointer(_uiLocationColor, 4, GL_FLOAT, GL_FALSE, 0, NULL);
  97. //  _pShaderManager->EnableAttribute(A_COLOR);
  98.  
  99.     glGenBuffers(1, &_vboPosition);
  100.     glBindBuffer(GL_ARRAY_BUFFER, _vboPosition);
  101.     glBufferData(GL_ARRAY_BUFFER, _fVertex.size()*sizeof _fVertex[0], &_fVertex[0], GL_STATIC_DRAW);
  102.     glVertexAttribPointer(_uiLocationPosition, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  103.     _pShaderManager->EnableAttribute(A_POSITION);
  104.  
  105.     glGenBuffers(1, &_vboIndices);
  106.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndices);
  107.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, _iIndicies.size()*sizeof _iIndicies[0], &_iIndicies[0], GL_STATIC_DRAW);
  108.  
  109.     // Unbind VAO first!
  110.     glBindVertexArray(0);
  111.  
  112. //  _pShaderManager->DisableAttribute(A_COLOR);
  113.     _pShaderManager->DisableAttribute(A_POSITION);
  114.  
  115.     // Must be done AFTER VAO is complete
  116.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  117.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  118.    
  119. }
  120.  
  121. Game::~Game(){
  122.     if(_vboPosition){
  123.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  124.         glDeleteBuffers(1, &_vboPosition);
  125.         _vboPosition = 0;
  126.     }
  127.     if(_vboColor){
  128.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  129.         glDeleteBuffers(1, &_vboColor);
  130.         _vboColor = 0;
  131.     }
  132.     if(_vboIndices){
  133.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  134.         glDeleteBuffers(1, &_vboIndices);
  135.         _vboIndices = 0;
  136.     }
  137.     if(_vao){
  138.         glBindVertexArray(0);
  139.         glDeleteVertexArrays(1, &_vao);
  140.         _vao = 0;
  141.     }
  142. }
  143.  
  144. void Game::keyboardInput(unsigned k, bool p){
  145.     if(k==VK_ESCAPE) PostQuitMessage(0);
  146.     std::ostringstream strStream;
  147.     strStream << "Key 0x" << std::hex<<k
  148.         << " is at state " << std::dec << p << std::endl;
  149.     Logger::log(strStream);
  150. }
  151. void Game::render(float deltaTime){
  152.     glm::mat4 m4Result = glm::translate(glm::mat4(1.0f), glm::vec3(0,0,-1.5f));
  153.     m4Result = _m4ProjMatrix*m4Result;
  154.     _pShaderManager->SetUniform(U_MVP_MATRIX, m4Result);
  155.  
  156.     glBindVertexArray(_vao);
  157.     glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, NULL);
  158.     glBindVertexArray(0);
  159. }
  160. void Game::InitShaders(){
  161.     ShaderProgramSettings shaderProgramSettings(P_TEST, "Shaders/basic.vert", "Shaders/basic.frag");
  162.     shaderProgramSettings.AddVariable(ShaderAttribute(A_POSITION, AT_FLOAT_VEC3), "inPosition");
  163.     shaderProgramSettings.AddVariable(ShaderAttribute(A_COLOR   , AT_FLOAT_VEC4), "myColor");
  164.     shaderProgramSettings.AddVariable(ShaderUniform(U_MVP_MATRIX, UT_FLOAT_MAT4), "modelViewProjectionMatrix");
  165.    
  166.     _pShaderManager->Create(shaderProgramSettings);
  167.     _pShaderManager->Enable(P_TEST);
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement