Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "Game.h"
- #include "TextFileReader.h"
- #include "ShaderManager.h"
- namespace key{
- Game::Game():
- _vboPosition(0),
- _vboIndices(0),
- _vboColor(0),
- _uiIdProgram(0),
- _uiIdShaderFragment(0),
- _uiIdShaderVertex(0),
- _uiLocationColor(-1),
- _uiLocationPosition(-1),
- _vao(0),
- _uiLocationCurrentTime(-1),
- _uiLocationMVPMatrix(-1),
- _m4ProjMatrix(1.0f)
- {
- glClearColor(0.20f, 0.20f, 0.20f, 1.0f);
- float fAspectRation = _pSettings->GetWindowsDisplaySize().x / static_cast<float>(_pSettings->GetWindowsDisplaySize().y);
- glm::perspective(45.0f, fAspectRation, 0.1f, 100.0f);
- std::vector<float> _fColor;
- std::vector<float> _fVertex;
- std::vector<unsigned char> _iIndicies;
- _fColor.push_back(1);
- _fColor.push_back(0);
- _fColor.push_back(0);
- _fColor.push_back(1);
- _fColor.push_back(0);
- _fColor.push_back(1);
- _fColor.push_back(0);
- _fColor.push_back(1);
- _fColor.push_back(0);
- _fColor.push_back(0);
- _fColor.push_back(1);
- _fColor.push_back(1);
- _fColor.push_back(1);
- _fColor.push_back(1);
- _fColor.push_back(0);
- _fColor.push_back(1);
- _fColor.push_back(0);
- _fColor.push_back(1);
- _fColor.push_back(1);
- _fColor.push_back(1);
- _fColor.push_back(1);
- _fColor.push_back(0);
- _fColor.push_back(1);
- _fColor.push_back(1);
- _fVertex.push_back(-0.433f);
- _fVertex.push_back(-0.25f);
- _fVertex.push_back(0);
- _fVertex.push_back(0.433f);
- _fVertex.push_back(-0.25f);
- _fVertex.push_back(0);
- _fVertex.push_back(0);
- _fVertex.push_back(0.5f);
- _fVertex.push_back(0);
- _fVertex.push_back(-0.5f);
- _fVertex.push_back(0.0f);
- _fVertex.push_back(0);
- _fVertex.push_back(0.50f);
- _fVertex.push_back(0.0f);
- _fVertex.push_back(0);
- _fVertex.push_back(0.0);
- _fVertex.push_back(0.50f);
- _fVertex.push_back(0);
- _iIndicies.push_back(0);
- _iIndicies.push_back(1);
- _iIndicies.push_back(2);
- InitShaders(); // Load up shaders
- glGenVertexArrays(1, &_vao);
- glBindVertexArray(_vao);
- // glGenBuffers(1, &_vboColor);
- // glBindBuffer(GL_ARRAY_BUFFER, _vboColor);
- // glBufferData(GL_ARRAY_BUFFER, _fColor.size()*sizeof _fColor[0], &_fColor[0], GL_STATIC_DRAW);
- // glVertexAttribPointer(_uiLocationColor, 4, GL_FLOAT, GL_FALSE, 0, NULL);
- // _pShaderManager->EnableAttribute(A_COLOR);
- glGenBuffers(1, &_vboPosition);
- glBindBuffer(GL_ARRAY_BUFFER, _vboPosition);
- glBufferData(GL_ARRAY_BUFFER, _fVertex.size()*sizeof _fVertex[0], &_fVertex[0], GL_STATIC_DRAW);
- glVertexAttribPointer(_uiLocationPosition, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- _pShaderManager->EnableAttribute(A_POSITION);
- glGenBuffers(1, &_vboIndices);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndices);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, _iIndicies.size()*sizeof _iIndicies[0], &_iIndicies[0], GL_STATIC_DRAW);
- // Unbind VAO first!
- glBindVertexArray(0);
- // _pShaderManager->DisableAttribute(A_COLOR);
- _pShaderManager->DisableAttribute(A_POSITION);
- // Must be done AFTER VAO is complete
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
- Game::~Game(){
- if(_vboPosition){
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glDeleteBuffers(1, &_vboPosition);
- _vboPosition = 0;
- }
- if(_vboColor){
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glDeleteBuffers(1, &_vboColor);
- _vboColor = 0;
- }
- if(_vboIndices){
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glDeleteBuffers(1, &_vboIndices);
- _vboIndices = 0;
- }
- if(_vao){
- glBindVertexArray(0);
- glDeleteVertexArrays(1, &_vao);
- _vao = 0;
- }
- }
- void Game::keyboardInput(unsigned k, bool p){
- if(k==VK_ESCAPE) PostQuitMessage(0);
- std::ostringstream strStream;
- strStream << "Key 0x" << std::hex<<k
- << " is at state " << std::dec << p << std::endl;
- Logger::log(strStream);
- }
- void Game::render(float deltaTime){
- glm::mat4 m4Result = glm::translate(glm::mat4(1.0f), glm::vec3(0,0,-1.5f));
- m4Result = _m4ProjMatrix*m4Result;
- _pShaderManager->SetUniform(U_MVP_MATRIX, m4Result);
- glBindVertexArray(_vao);
- glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, NULL);
- glBindVertexArray(0);
- }
- void Game::InitShaders(){
- ShaderProgramSettings shaderProgramSettings(P_TEST, "Shaders/basic.vert", "Shaders/basic.frag");
- shaderProgramSettings.AddVariable(ShaderAttribute(A_POSITION, AT_FLOAT_VEC3), "inPosition");
- shaderProgramSettings.AddVariable(ShaderAttribute(A_COLOR , AT_FLOAT_VEC4), "myColor");
- shaderProgramSettings.AddVariable(ShaderUniform(U_MVP_MATRIX, UT_FLOAT_MAT4), "modelViewProjectionMatrix");
- _pShaderManager->Create(shaderProgramSettings);
- _pShaderManager->Enable(P_TEST);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement