Advertisement
ChocoMan

Untitled

Apr 9th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.29 KB | None | 0 0
  1. /////// MODEL.CPP//////////////////
  2.  
  3. Model::MeshEntry::MeshEntry()
  4. {
  5.     VB = INVALID_OGL_VALUE;
  6.     IB = INVALID_OGL_VALUE;
  7.     NumIndices = 0;
  8.     MaterialIndex = INVALID_MATERIAL;
  9. };
  10.  
  11.  
  12. Model::MeshEntry::~MeshEntry()
  13. {
  14.     if (VB != INVALID_OGL_VALUE)
  15.     {
  16.         glDeleteBuffers(1, &VB);
  17.     }
  18.  
  19.     if (IB != INVALID_OGL_VALUE)
  20.     {
  21.         glDeleteBuffers(1, &IB);
  22.     }
  23. }
  24.  
  25.  
  26. void Model::MeshEntry::Init(const std::vector<Vertex> &Vertices,
  27.     const std::vector<unsigned int> &Indices)
  28. {
  29.     NumIndices = Indices.size();
  30.  
  31.     glGenBuffers(1, &VB);
  32.     glBindBuffer(GL_ARRAY_BUFFER, VB);
  33.     glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * Vertices.size(), &Vertices[0], GL_STATIC_DRAW);
  34.  
  35.     glGenBuffers(1, &IB);
  36.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
  37.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * NumIndices, &Indices[0], GL_STATIC_DRAW);
  38. }
  39. Model::Model()
  40. {
  41. }
  42.  
  43.  
  44. Model::~Model()
  45. {
  46. }
  47.  
  48.  
  49.  
  50. void Model::Clear()
  51. {
  52.     for (unsigned int i = 0; i < m_Textures.size(); i++)
  53.     {
  54.         SAFE_DELETE(m_Textures[i]);
  55.     }
  56. }
  57.  
  58.  
  59. bool Model::LoadMesh(const char* Filename)
  60. {
  61.     // Release the previously loaded mesh (if it exists)
  62.     Clear();
  63.  
  64.     bool Ret = false;
  65.     Assimp::Importer Importer;
  66.  
  67.     const aiScene* pScene = Importer.ReadFile(Filename, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs);
  68.  
  69.     if (pScene)
  70.     {
  71.         Ret = InitFromScene(pScene, Filename);
  72.     }
  73.     else
  74.     {
  75.         printf("Error parsing '%s': '%s'\n", Filename, Importer.GetErrorString());
  76.     }
  77.  
  78.     return Ret;
  79. }
  80.  
  81.  
  82. bool Model::InitFromScene(const aiScene* pScene, const char* Filename)
  83. {
  84.     m_Entries.resize(pScene->mNumMeshes);
  85.     m_Textures.resize(pScene->mNumMaterials);
  86.  
  87.     // Initialize the meshes in the scene one by one
  88.     for (unsigned int i = 0; i < m_Entries.size(); i++)
  89.     {
  90.         const aiMesh* paiMesh = pScene->mMeshes[i];
  91.         InitMesh(i, paiMesh);
  92.     }
  93.  
  94.     return InitMaterials(pScene, Filename);
  95. }
  96.  
  97.  
  98. void Model::InitMesh(unsigned int Index, const aiMesh* paiMesh)
  99. {
  100.     m_Entries[Index].MaterialIndex = paiMesh->mMaterialIndex;
  101.  
  102.     std::vector<Vertex> Vertices;
  103.     std::vector<unsigned int> Indices;
  104.  
  105.     const aiVector3D Zero3D(0.0f, 0.0f, 0.0f);
  106.  
  107.     for (unsigned int i = 0; i < paiMesh->mNumVertices; i++)
  108.     {
  109.         const aiVector3D* pPos = &(paiMesh->mVertices[i]);
  110.         const aiVector3D* pNormal = &(paiMesh->mNormals[i]);
  111.         const aiVector3D* pTexCoord = paiMesh->HasTextureCoords(0) ? &(paiMesh->mTextureCoords[0][i]) : &Zero3D;
  112.  
  113.         Vertex v(glm::vec3(pPos->x, pPos->y, pPos->z),
  114.             glm::vec2(pTexCoord->x, pTexCoord->y),
  115.             glm::vec3(pNormal->x, pNormal->y, pNormal->z));
  116.  
  117.         Vertices.push_back(v);
  118.     }
  119.  
  120.     for (unsigned int i = 0; i < paiMesh->mNumFaces; i++)
  121.     {
  122.         const aiFace& Face = paiMesh->mFaces[i];
  123.         assert(Face.mNumIndices == 3);
  124.         Indices.push_back(Face.mIndices[0]);
  125.         Indices.push_back(Face.mIndices[1]);
  126.         Indices.push_back(Face.mIndices[2]);
  127.     }
  128.  
  129.     m_Entries[Index].Init(Vertices, Indices);
  130. }
  131.  
  132.  
  133. bool Model::InitMaterials(const aiScene* pScene, const char* Filename)
  134. {
  135.     std::string file = Filename;
  136.  
  137.     // Extract the directory part from the file name
  138.     std::string::size_type SlashIndex = file.find_last_of("/");
  139.     std::string Dir;
  140.  
  141.     if (SlashIndex == std::string::npos)
  142.         Dir = ".";
  143.     else if (SlashIndex == 0)
  144.         Dir = "/";
  145.     else
  146.         Dir = file.substr(0, SlashIndex);
  147.  
  148.     bool Ret = true;
  149.  
  150.     // Initialize the materials
  151.     for (unsigned int i = 0; i < pScene->mNumMaterials; i++)
  152.     {
  153.         const aiMaterial* pMaterial = pScene->mMaterials[i];
  154.  
  155.         m_Textures[i] = NULL;
  156.  
  157.         std::cout << file << " texture count: " << pMaterial->GetTextureCount(aiTextureType_DIFFUSE) << std::endl;
  158.  
  159.         if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0)
  160.         {
  161.             aiString Path;
  162.  
  163.             if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS)
  164.             {
  165.                 std::string FullPath = Dir + "/" + Path.data;
  166.                 m_Textures[i] = new MTexture(GL_TEXTURE_2D, FullPath.c_str());
  167.  
  168.                 if (!m_Textures[i]->Load())
  169.                 {
  170.                     printf("Error loading texture '%s'\n", FullPath.c_str());
  171.                     delete m_Textures[i];
  172.                     m_Textures[i] = NULL;
  173.                     Ret = false;
  174.                 }
  175.                 else
  176.                 {
  177.                     printf("Loaded texture '%s'\n", FullPath.c_str());
  178.                 }
  179.             }
  180.         }
  181.     }
  182.  
  183.     return Ret;
  184. }
  185.  
  186.  
  187. void Model::Render()
  188. {
  189.     glEnableVertexAttribArray(0);
  190.     glEnableVertexAttribArray(1);
  191.     glEnableVertexAttribArray(2);
  192.  
  193.     for (unsigned int i = 0; i < m_Entries.size(); i++)
  194.     {
  195.         glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB);
  196.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
  197.         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*) 12);
  198.         glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*) 20);
  199.  
  200.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Entries[i].IB);
  201.  
  202.         const unsigned int MaterialIndex = m_Entries[i].MaterialIndex;
  203.  
  204.         if (MaterialIndex < m_Textures.size() && m_Textures[MaterialIndex])
  205.         {
  206.             m_Textures[MaterialIndex]->Bind(GL_TEXTURE0);
  207.         }
  208.  
  209.         glDrawElements(GL_TRIANGLES, m_Entries[i].NumIndices, GL_UNSIGNED_INT, 0);
  210.     }
  211.  
  212.     glDisableVertexAttribArray(0);
  213.     glDisableVertexAttribArray(1);
  214.     glDisableVertexAttribArray(2);
  215. }
  216.  
  217. ////// GAME.CPP ////////////////
  218.  
  219. bool Game::OnInit()
  220. {
  221.     generalSetup();
  222.    
  223.     _shader = new Shader();
  224.     _shaderPrg = new ShaderProgram();
  225.  
  226.     _vSource = _shader->readFile("vShader.vert");
  227.     _vID = _shader->makeVertexShader(_vSource);
  228.     _fSource = _shader->readFile("fShader.frag");
  229.     _fID = _shader->makeFragmentShader(_fSource);
  230.     _shaderProgID = _shaderPrg->makeShaderProgram(_vID, _fID);
  231.     glUseProgram(_shaderProgID);
  232.  
  233.     _projectionMatrix = _shaderPrg->getUniformLoc("projection");
  234.     _viewMatrix = _shaderPrg->getUniformLoc("view");
  235.     _modelMatrix = _shaderPrg->getUniformLoc("model");
  236.     _positionLoc = _shaderPrg->getUniformLoc("position");
  237.     _texCoordLoc = _shaderPrg->getUniformLoc("texCoord");
  238.  
  239.     // Load model
  240.     m_model = Model();
  241.     m_model.LoadMesh("earth02.dae");
  242.     return true;
  243. }
  244.  
  245.  
  246. void Game::OnLoop()
  247. {
  248.     GLfloat aspect = (GLfloat) 800 / 600;
  249.     _projection = glm::perspective(60.0f, aspect, 0.1f, 1000.0f);
  250.     _view = glm::lookAt(Eye, Target, Up);
  251.     glUniformMatrix4fv(_viewMatrix, 1, false, glm::value_ptr(_cam.getMatrix()));
  252.     glUniformMatrix4fv(_projectionMatrix, 1, false, glm::value_ptr(_projection));
  253. }
  254.  
  255.  
  256. void Game::OnRender()
  257. {
  258.     _cam.setPosition(_camPos);
  259.     _cam.setRotation(_camRot);
  260.  
  261.     Eye = glm::vec3(_cam.getPosition().x, _cam.getPosition().y, _cam.getPosition().z);
  262.     Target = glm::vec3(0.0f, 0.0f, 0.0f);
  263.     Up = glm::vec3(0.0, 1.0, 0.0);
  264.     glm::mat4 view = glm::lookAt(Eye, Target, Up);
  265.  
  266.     _model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); // image
  267.  
  268.     glUniformMatrix4fv(_modelMatrix, 1, false, glm::value_ptr(_model));
  269.    
  270.     glClearColor(0.0, 0.0, 0.0, 1.0);
  271.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  272.     glMatrixMode(GL_MODELVIEW);
  273.    
  274.         // load model
  275.     m_model.Render();
  276. }
  277.  
  278. //// VSHADER.VERT   //////////////
  279.  
  280. #version 330
  281.  
  282. uniform mat4 projection;
  283. uniform mat4 view;
  284. uniform mat4 model;
  285.  
  286. layout (location = 0) in vec4 position;
  287. layout (location = 1) in vec2 texCoord;
  288.  
  289. out vec2 TexCoord;
  290.  
  291. void main()
  292. {
  293.     TexCoord = texCoord;
  294.     gl_Position = projection * view * model * position;
  295. }
  296.  
  297. ///// FSHADER.FRAG //////////
  298.  
  299. #version 330
  300.  
  301. uniform vec4 color;
  302. uniform sampler2D tex;
  303. in vec2 TexCoord;
  304.  
  305. out vec4 fragData;
  306.  
  307. void main()
  308. {
  309.     fragData = color * texture(tex, TexCoord); 
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement