Advertisement
ChocoMan

Untitled

Apr 9th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////// MODEL.CPP//////////////////
  2.  
  3. void Model::Render()
  4. {
  5.     glEnableVertexAttribArray(0);
  6.     glEnableVertexAttribArray(1);
  7.     glEnableVertexAttribArray(2);
  8.  
  9.     for (unsigned int i = 0; i < m_Entries.size(); i++)
  10.     {
  11.         glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB);
  12.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
  13.         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*) 12);
  14.         glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*) 20);
  15.  
  16.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Entries[i].IB);
  17.  
  18.         const unsigned int MaterialIndex = m_Entries[i].MaterialIndex;
  19.  
  20.         if (MaterialIndex < m_Textures.size() && m_Textures[MaterialIndex])
  21.         {
  22.             m_Textures[MaterialIndex]->Bind(GL_TEXTURE0);
  23.         }
  24.  
  25.         glDrawElements(GL_TRIANGLES, m_Entries[i].NumIndices, GL_UNSIGNED_INT, 0);
  26.     }
  27.  
  28.     glDisableVertexAttribArray(0);
  29.     glDisableVertexAttribArray(1);
  30.     glDisableVertexAttribArray(2);
  31. }
  32.  
  33. ////// GAME.CPP ////////////////
  34.  
  35. bool Game::OnInit()
  36. {
  37.     generalSetup();
  38.    
  39.     _shader = new Shader();
  40.     _shaderPrg = new ShaderProgram();
  41.  
  42.     _vSource = _shader->readFile("vShader.vert");
  43.     _vID = _shader->makeVertexShader(_vSource);
  44.     _fSource = _shader->readFile("fShader.frag");
  45.     _fID = _shader->makeFragmentShader(_fSource);
  46.     _shaderProgID = _shaderPrg->makeShaderProgram(_vID, _fID);
  47.     glUseProgram(_shaderProgID);
  48.  
  49.     _projectionMatrix = _shaderPrg->getUniformLoc("projection");
  50.     _viewMatrix = _shaderPrg->getUniformLoc("view");
  51.     _modelMatrix = _shaderPrg->getUniformLoc("model");
  52.     _positionLoc = _shaderPrg->getUniformLoc("position");
  53.     _texCoordLoc = _shaderPrg->getUniformLoc("texCoord");
  54.  
  55.     // Load model
  56.     m_model = Model();
  57.     m_model.LoadMesh("earth02.dae");
  58.     return true;
  59. }
  60.  
  61.  
  62. void Game::OnLoop()
  63. {
  64.     GLfloat aspect = (GLfloat) 800 / 600;
  65.     _projection = glm::perspective(60.0f, aspect, 0.1f, 1000.0f);
  66.     _view = glm::lookAt(Eye, Target, Up);
  67.     glUniformMatrix4fv(_viewMatrix, 1, false, glm::value_ptr(_cam.getMatrix()));
  68.     glUniformMatrix4fv(_projectionMatrix, 1, false, glm::value_ptr(_projection));
  69. }
  70.  
  71.  
  72. void Game::OnRender()
  73. {
  74.     _cam.setPosition(_camPos);
  75.     _cam.setRotation(_camRot);
  76.  
  77.     Eye = glm::vec3(_cam.getPosition().x, _cam.getPosition().y, _cam.getPosition().z);
  78.     Target = glm::vec3(0.0f, 0.0f, 0.0f);
  79.     Up = glm::vec3(0.0, 1.0, 0.0);
  80.     glm::mat4 view = glm::lookAt(Eye, Target, Up);
  81.  
  82.     _model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); // image
  83.  
  84.     glUniformMatrix4fv(_modelMatrix, 1, false, glm::value_ptr(_model));
  85.    
  86.     glClearColor(0.0, 0.0, 0.0, 1.0);
  87.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  88.     glMatrixMode(GL_MODELVIEW);
  89.    
  90.         // load model
  91.     m_model.Render();
  92. }
  93.  
  94. //// VSHADER.VERT   //////////////
  95.  
  96. #version 330
  97.  
  98. uniform mat4 projection;
  99. uniform mat4 view;
  100. uniform mat4 model;
  101.  
  102. layout (location = 0) in vec4 position;
  103. layout (location = 1) in vec2 texCoord;
  104.  
  105. out vec2 TexCoord;
  106.  
  107. void main()
  108. {
  109.     TexCoord = texCoord;
  110.     gl_Position = projection * view * model * position;
  111. }
  112.  
  113. ///// FSHADER.FRAG //////////
  114.  
  115. #version 330
  116.  
  117. uniform vec4 color;
  118. uniform sampler2D tex;
  119. in vec2 TexCoord;
  120.  
  121. out vec4 fragData;
  122.  
  123. void main()
  124. {
  125.     fragData = color * texture(tex, TexCoord); 
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement