Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////// MODEL.CPP//////////////////
- Model::MeshEntry::MeshEntry()
- {
- VB = INVALID_OGL_VALUE;
- IB = INVALID_OGL_VALUE;
- NumIndices = 0;
- MaterialIndex = INVALID_MATERIAL;
- };
- Model::MeshEntry::~MeshEntry()
- {
- if (VB != INVALID_OGL_VALUE)
- {
- glDeleteBuffers(1, &VB);
- }
- if (IB != INVALID_OGL_VALUE)
- {
- glDeleteBuffers(1, &IB);
- }
- }
- void Model::MeshEntry::Init(const std::vector<Vertex> &Vertices,
- const std::vector<unsigned int> &Indices)
- {
- NumIndices = Indices.size();
- glGenBuffers(1, &VB);
- glBindBuffer(GL_ARRAY_BUFFER, VB);
- glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * Vertices.size(), &Vertices[0], GL_STATIC_DRAW);
- glGenBuffers(1, &IB);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * NumIndices, &Indices[0], GL_STATIC_DRAW);
- }
- Model::Model()
- {
- }
- Model::~Model()
- {
- }
- void Model::Clear()
- {
- for (unsigned int i = 0; i < m_Textures.size(); i++)
- {
- SAFE_DELETE(m_Textures[i]);
- }
- }
- bool Model::LoadMesh(const char* Filename)
- {
- // Release the previously loaded mesh (if it exists)
- Clear();
- bool Ret = false;
- Assimp::Importer Importer;
- const aiScene* pScene = Importer.ReadFile(Filename, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs);
- if (pScene)
- {
- Ret = InitFromScene(pScene, Filename);
- }
- else
- {
- printf("Error parsing '%s': '%s'\n", Filename, Importer.GetErrorString());
- }
- return Ret;
- }
- bool Model::InitFromScene(const aiScene* pScene, const char* Filename)
- {
- m_Entries.resize(pScene->mNumMeshes);
- m_Textures.resize(pScene->mNumMaterials);
- // Initialize the meshes in the scene one by one
- for (unsigned int i = 0; i < m_Entries.size(); i++)
- {
- const aiMesh* paiMesh = pScene->mMeshes[i];
- InitMesh(i, paiMesh);
- }
- return InitMaterials(pScene, Filename);
- }
- void Model::InitMesh(unsigned int Index, const aiMesh* paiMesh)
- {
- m_Entries[Index].MaterialIndex = paiMesh->mMaterialIndex;
- std::vector<Vertex> Vertices;
- std::vector<unsigned int> Indices;
- const aiVector3D Zero3D(0.0f, 0.0f, 0.0f);
- for (unsigned int i = 0; i < paiMesh->mNumVertices; i++)
- {
- const aiVector3D* pPos = &(paiMesh->mVertices[i]);
- const aiVector3D* pNormal = &(paiMesh->mNormals[i]);
- const aiVector3D* pTexCoord = paiMesh->HasTextureCoords(0) ? &(paiMesh->mTextureCoords[0][i]) : &Zero3D;
- Vertex v(glm::vec3(pPos->x, pPos->y, pPos->z),
- glm::vec2(pTexCoord->x, pTexCoord->y),
- glm::vec3(pNormal->x, pNormal->y, pNormal->z));
- Vertices.push_back(v);
- }
- for (unsigned int i = 0; i < paiMesh->mNumFaces; i++)
- {
- const aiFace& Face = paiMesh->mFaces[i];
- assert(Face.mNumIndices == 3);
- Indices.push_back(Face.mIndices[0]);
- Indices.push_back(Face.mIndices[1]);
- Indices.push_back(Face.mIndices[2]);
- }
- m_Entries[Index].Init(Vertices, Indices);
- }
- bool Model::InitMaterials(const aiScene* pScene, const char* Filename)
- {
- std::string file = Filename;
- // Extract the directory part from the file name
- std::string::size_type SlashIndex = file.find_last_of("/");
- std::string Dir;
- if (SlashIndex == std::string::npos)
- Dir = ".";
- else if (SlashIndex == 0)
- Dir = "/";
- else
- Dir = file.substr(0, SlashIndex);
- bool Ret = true;
- // Initialize the materials
- for (unsigned int i = 0; i < pScene->mNumMaterials; i++)
- {
- const aiMaterial* pMaterial = pScene->mMaterials[i];
- m_Textures[i] = NULL;
- std::cout << file << " texture count: " << pMaterial->GetTextureCount(aiTextureType_DIFFUSE) << std::endl;
- if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0)
- {
- aiString Path;
- if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS)
- {
- std::string FullPath = Dir + "/" + Path.data;
- m_Textures[i] = new MTexture(GL_TEXTURE_2D, FullPath.c_str());
- if (!m_Textures[i]->Load())
- {
- printf("Error loading texture '%s'\n", FullPath.c_str());
- delete m_Textures[i];
- m_Textures[i] = NULL;
- Ret = false;
- }
- else
- {
- printf("Loaded texture '%s'\n", FullPath.c_str());
- }
- }
- }
- }
- return Ret;
- }
- void Model::Render()
- {
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glEnableVertexAttribArray(2);
- for (unsigned int i = 0; i < m_Entries.size(); i++)
- {
- glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*) 12);
- glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*) 20);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Entries[i].IB);
- const unsigned int MaterialIndex = m_Entries[i].MaterialIndex;
- if (MaterialIndex < m_Textures.size() && m_Textures[MaterialIndex])
- {
- m_Textures[MaterialIndex]->Bind(GL_TEXTURE0);
- }
- glDrawElements(GL_TRIANGLES, m_Entries[i].NumIndices, GL_UNSIGNED_INT, 0);
- }
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- glDisableVertexAttribArray(2);
- }
- ////// GAME.CPP ////////////////
- bool Game::OnInit()
- {
- generalSetup();
- _shader = new Shader();
- _shaderPrg = new ShaderProgram();
- _vSource = _shader->readFile("vShader.vert");
- _vID = _shader->makeVertexShader(_vSource);
- _fSource = _shader->readFile("fShader.frag");
- _fID = _shader->makeFragmentShader(_fSource);
- _shaderProgID = _shaderPrg->makeShaderProgram(_vID, _fID);
- glUseProgram(_shaderProgID);
- _projectionMatrix = _shaderPrg->getUniformLoc("projection");
- _viewMatrix = _shaderPrg->getUniformLoc("view");
- _modelMatrix = _shaderPrg->getUniformLoc("model");
- _positionLoc = _shaderPrg->getUniformLoc("position");
- _texCoordLoc = _shaderPrg->getUniformLoc("texCoord");
- // Load model
- m_model = Model();
- m_model.LoadMesh("earth02.dae");
- return true;
- }
- void Game::OnLoop()
- {
- GLfloat aspect = (GLfloat) 800 / 600;
- _projection = glm::perspective(60.0f, aspect, 0.1f, 1000.0f);
- _view = glm::lookAt(Eye, Target, Up);
- glUniformMatrix4fv(_viewMatrix, 1, false, glm::value_ptr(_cam.getMatrix()));
- glUniformMatrix4fv(_projectionMatrix, 1, false, glm::value_ptr(_projection));
- }
- void Game::OnRender()
- {
- _cam.setPosition(_camPos);
- _cam.setRotation(_camRot);
- Eye = glm::vec3(_cam.getPosition().x, _cam.getPosition().y, _cam.getPosition().z);
- Target = glm::vec3(0.0f, 0.0f, 0.0f);
- Up = glm::vec3(0.0, 1.0, 0.0);
- glm::mat4 view = glm::lookAt(Eye, Target, Up);
- _model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); // image
- glUniformMatrix4fv(_modelMatrix, 1, false, glm::value_ptr(_model));
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- // load model
- m_model.Render();
- }
- //// VSHADER.VERT //////////////
- #version 330
- uniform mat4 projection;
- uniform mat4 view;
- uniform mat4 model;
- layout (location = 0) in vec4 position;
- layout (location = 1) in vec2 texCoord;
- out vec2 TexCoord;
- void main()
- {
- TexCoord = texCoord;
- gl_Position = projection * view * model * position;
- }
- ///// FSHADER.FRAG //////////
- #version 330
- uniform vec4 color;
- uniform sampler2D tex;
- in vec2 TexCoord;
- out vec4 fragData;
- void main()
- {
- fragData = color * texture(tex, TexCoord);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement