Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////// MODEL.CPP//////////////////
- 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