SHOW:
|
|
- or go back to the newest paste.
1 | /////// MODEL.CPP////////////////// | |
2 | ||
3 | - | Model::MeshEntry::MeshEntry() |
3 | + | |
4 | { | |
5 | - | VB = INVALID_OGL_VALUE; |
5 | + | |
6 | - | IB = INVALID_OGL_VALUE; |
6 | + | |
7 | - | NumIndices = 0; |
7 | + | |
8 | - | MaterialIndex = INVALID_MATERIAL; |
8 | + | |
9 | - | }; |
9 | + | |
10 | { | |
11 | glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB); | |
12 | - | Model::MeshEntry::~MeshEntry() |
12 | + | |
13 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*) 12); | |
14 | - | if (VB != INVALID_OGL_VALUE) |
14 | + | |
15 | ||
16 | - | glDeleteBuffers(1, &VB); |
16 | + | |
17 | ||
18 | const unsigned int MaterialIndex = m_Entries[i].MaterialIndex; | |
19 | - | if (IB != INVALID_OGL_VALUE) |
19 | + | |
20 | if (MaterialIndex < m_Textures.size() && m_Textures[MaterialIndex]) | |
21 | - | glDeleteBuffers(1, &IB); |
21 | + | |
22 | m_Textures[MaterialIndex]->Bind(GL_TEXTURE0); | |
23 | } | |
24 | ||
25 | glDrawElements(GL_TRIANGLES, m_Entries[i].NumIndices, GL_UNSIGNED_INT, 0); | |
26 | - | void Model::MeshEntry::Init(const std::vector<Vertex> &Vertices, |
26 | + | |
27 | - | const std::vector<unsigned int> &Indices) |
27 | + | |
28 | glDisableVertexAttribArray(0); | |
29 | - | NumIndices = Indices.size(); |
29 | + | |
30 | glDisableVertexAttribArray(2); | |
31 | - | glGenBuffers(1, &VB); |
31 | + | |
32 | - | glBindBuffer(GL_ARRAY_BUFFER, VB); |
32 | + | |
33 | - | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * Vertices.size(), &Vertices[0], GL_STATIC_DRAW); |
33 | + | |
34 | ||
35 | - | glGenBuffers(1, &IB); |
35 | + | |
36 | - | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB); |
36 | + | |
37 | - | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * NumIndices, &Indices[0], GL_STATIC_DRAW); |
37 | + | |
38 | ||
39 | - | Model::Model() |
39 | + | |
40 | _shaderPrg = new ShaderProgram(); | |
41 | ||
42 | _vSource = _shader->readFile("vShader.vert"); | |
43 | _vID = _shader->makeVertexShader(_vSource); | |
44 | - | Model::~Model() |
44 | + | |
45 | _fID = _shader->makeFragmentShader(_fSource); | |
46 | _shaderProgID = _shaderPrg->makeShaderProgram(_vID, _fID); | |
47 | glUseProgram(_shaderProgID); | |
48 | ||
49 | _projectionMatrix = _shaderPrg->getUniformLoc("projection"); | |
50 | - | void Model::Clear() |
50 | + | |
51 | _modelMatrix = _shaderPrg->getUniformLoc("model"); | |
52 | - | for (unsigned int i = 0; i < m_Textures.size(); i++) |
52 | + | |
53 | _texCoordLoc = _shaderPrg->getUniformLoc("texCoord"); | |
54 | - | SAFE_DELETE(m_Textures[i]); |
54 | + | |
55 | // Load model | |
56 | m_model = Model(); | |
57 | m_model.LoadMesh("earth02.dae"); | |
58 | return true; | |
59 | - | bool Model::LoadMesh(const char* Filename) |
59 | + | |
60 | ||
61 | - | // Release the previously loaded mesh (if it exists) |
61 | + | |
62 | - | Clear(); |
62 | + | |
63 | { | |
64 | - | bool Ret = false; |
64 | + | |
65 | - | Assimp::Importer Importer; |
65 | + | |
66 | _view = glm::lookAt(Eye, Target, Up); | |
67 | - | const aiScene* pScene = Importer.ReadFile(Filename, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs); |
67 | + | |
68 | glUniformMatrix4fv(_projectionMatrix, 1, false, glm::value_ptr(_projection)); | |
69 | - | if (pScene) |
69 | + | |
70 | ||
71 | - | Ret = InitFromScene(pScene, Filename); |
71 | + | |
72 | void Game::OnRender() | |
73 | - | else |
73 | + | |
74 | _cam.setPosition(_camPos); | |
75 | - | printf("Error parsing '%s': '%s'\n", Filename, Importer.GetErrorString()); |
75 | + | |
76 | ||
77 | Eye = glm::vec3(_cam.getPosition().x, _cam.getPosition().y, _cam.getPosition().z); | |
78 | - | return Ret; |
78 | + | |
79 | Up = glm::vec3(0.0, 1.0, 0.0); | |
80 | glm::mat4 view = glm::lookAt(Eye, Target, Up); | |
81 | ||
82 | - | bool Model::InitFromScene(const aiScene* pScene, const char* Filename) |
82 | + | |
83 | ||
84 | - | m_Entries.resize(pScene->mNumMeshes); |
84 | + | |
85 | - | m_Textures.resize(pScene->mNumMaterials); |
85 | + | |
86 | glClearColor(0.0, 0.0, 0.0, 1.0); | |
87 | - | // Initialize the meshes in the scene one by one |
87 | + | |
88 | glMatrixMode(GL_MODELVIEW); | |
89 | ||
90 | - | const aiMesh* paiMesh = pScene->mMeshes[i]; |
90 | + | |
91 | - | InitMesh(i, paiMesh); |
91 | + | |
92 | } | |
93 | ||
94 | - | return InitMaterials(pScene, Filename); |
94 | + | |
95 | ||
96 | #version 330 | |
97 | ||
98 | - | void Model::InitMesh(unsigned int Index, const aiMesh* paiMesh) |
98 | + | |
99 | uniform mat4 view; | |
100 | - | m_Entries[Index].MaterialIndex = paiMesh->mMaterialIndex; |
100 | + | |
101 | ||
102 | - | std::vector<Vertex> Vertices; |
102 | + | |
103 | - | std::vector<unsigned int> Indices; |
103 | + | |
104 | ||
105 | - | const aiVector3D Zero3D(0.0f, 0.0f, 0.0f); |
105 | + | |
106 | ||
107 | - | for (unsigned int i = 0; i < paiMesh->mNumVertices; i++) |
107 | + | |
108 | { | |
109 | - | const aiVector3D* pPos = &(paiMesh->mVertices[i]); |
109 | + | |
110 | - | const aiVector3D* pNormal = &(paiMesh->mNormals[i]); |
110 | + | |
111 | - | const aiVector3D* pTexCoord = paiMesh->HasTextureCoords(0) ? &(paiMesh->mTextureCoords[0][i]) : &Zero3D; |
111 | + | |
112 | ||
113 | - | Vertex v(glm::vec3(pPos->x, pPos->y, pPos->z), |
113 | + | |
114 | - | glm::vec2(pTexCoord->x, pTexCoord->y), |
114 | + | |
115 | - | glm::vec3(pNormal->x, pNormal->y, pNormal->z)); |
115 | + | |
116 | ||
117 | - | Vertices.push_back(v); |
117 | + | |
118 | uniform sampler2D tex; | |
119 | in vec2 TexCoord; | |
120 | - | for (unsigned int i = 0; i < paiMesh->mNumFaces; i++) |
120 | + | |
121 | out vec4 fragData; | |
122 | - | const aiFace& Face = paiMesh->mFaces[i]; |
122 | + | |
123 | - | assert(Face.mNumIndices == 3); |
123 | + | |
124 | - | Indices.push_back(Face.mIndices[0]); |
124 | + | |
125 | - | Indices.push_back(Face.mIndices[1]); |
125 | + | |
126 | - | Indices.push_back(Face.mIndices[2]); |
126 | + |