-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangleScene.cpp
More file actions
104 lines (83 loc) · 2.71 KB
/
TriangleScene.cpp
File metadata and controls
104 lines (83 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Created by Yury Rakhuba on 11/05/15.
//
#include "TriangleScene.hpp"
#include "DummyController.hpp"
#include "ogl/GLUtils.hpp"
#include "base/Platform.hpp"
#include "base/assert.hpp"
#include "base/math.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
TriangleScene::TriangleScene()
: m_vertexBuffer(Buffer::Type::VertexBuffer)
{
float vertices[] =
{
1.0, 1.0, 1.0,
2.0, 3.0, 1.0,
3.0, 1.0, 1.0
};
m_vertexBuffer.Create();
m_vertexBuffer.Bind();
m_vertexBuffer.Allocate(sizeof(float) * std::distance(std::begin(vertices), std::end(vertices)), vertices);
m_prg.reset(new ShaderProgram(Platform::GetReader("simple.vert"),
Platform::GetReader("solid.frag")));
GLCHECK(glClearColor(0.65f, 0.65f, 0.65f, 1.0f));
}
TriangleScene::~TriangleScene()
{
m_vertexBuffer.Destroy();
}
void TriangleScene::Update(double elapsedSeconds)
{
static float s_rotationAngle = 0.0f;
s_rotationAngle += math::pi2 * elapsedSeconds;
glm::mat4 shiftM = glm::translate(glm::mat4(1.0), glm::vec3(2.0f, 2.0f, 0.0f));
glm::mat4 rotateM = glm::rotate(shiftM, s_rotationAngle, glm::vec3(0.0, 1.0, 0.0));
glm::mat4 backShiftM = glm::translate(rotateM, glm::vec3(-2.0f, -2.0f, 0.0f));
glm::vec4 eye(2.0f, 2.0f, -5.0f, 1.0f);
eye = backShiftM * eye;
m_modelView = glm::lookAt(eye.xyz(),
glm::vec3(2.0f, 2.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
m_modelViewChanged = true;
}
void TriangleScene::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_prg->Bind();
if (m_projectionChanged)
{
GLint projectionLoc = m_prg->GetUniform("u_projection");
ASSERT(projectionLoc != -1, "");
GLCHECK(glUniformMatrix4fv(projectionLoc, 1, GL_TRUE, glm::value_ptr(m_projection)));
m_projectionChanged = false;
}
if (m_modelViewChanged)
{
GLint modelViewLoc = m_prg->GetUniform("u_modelView");
ASSERT(modelViewLoc != -1, "");
GLCHECK(glUniformMatrix4fv(modelViewLoc, 1, GL_TRUE, glm::value_ptr(m_modelView)));
m_modelViewChanged = false;
}
GLint attribLocation = m_prg->GetAttribute("a_position");
ASSERT(attribLocation != -1, "");
GLCHECK(glEnableVertexAttribArray(attribLocation));
GLCHECK(glVertexAttribPointer(attribLocation, 3, GL_FLOAT, GL_TRUE, 3 * sizeof(float), nullptr));
GLCHECK(glDrawArrays(GL_TRIANGLES, 0, 3));
}
IController & TriangleScene::GetController()
{
static DummyController controller;
return controller;
}
void TriangleScene::OnSize(int w, int h)
{
float fW = w;
float fH = h;
if (fW < fH)
std::swap(fW, fH);
m_projection = glm::perspective(static_cast<float>(math::pi2), fW / fH, 1.0f, 100.0f);
m_projectionChanged = true;
}