-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.cpp
More file actions
169 lines (133 loc) · 4.11 KB
/
graphics.cpp
File metadata and controls
169 lines (133 loc) · 4.11 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "graphics.h"
Graphics::Graphics()
{
}
Graphics::~Graphics()
{
}
bool Graphics::Initialize(int width, int height)
{
// For OpenGL 3
// Init Camera
m_camera = new Camera();
if (!m_camera->Initialize(width, height))
{
printf("Camera Failed to Initialize\n");
return false;
}
// Set up the shaders
m_shader = new Shader();
if (!m_shader->Initialize())
{
printf("Shader Failed to Initialize\n");
return false;
}
// Add the vertex shader
if (!m_shader->AddShader(GL_VERTEX_SHADER))
{
printf("Vertex Shader failed to Initialize\n");
return false;
}
// Add the fragment shader
if (!m_shader->AddShader(GL_FRAGMENT_SHADER))
{
printf("Fragment Shader failed to Initialize\n");
return false;
}
// Connect the program
if (!m_shader->Finalize())
{
printf("Program to Finalize\n");
return false;
}
// Locate the projection matrix in the shader
m_projectionMatrix = m_shader->GetUniformLocation("projectionMatrix");
if (m_projectionMatrix == INVALID_UNIFORM_LOCATION)
{
printf("m_projectionMatrix not found\n");
return false;
}
// Locate the view matrix in the shader
m_viewMatrix = m_shader->GetUniformLocation("viewMatrix");
if (m_viewMatrix == INVALID_UNIFORM_LOCATION)
{
printf("m_viewMatrix not found\n");
return false;
}
// Locate the model matrix in the shader
m_modelMatrix = m_shader->GetUniformLocation("modelMatrix");
if (m_modelMatrix == INVALID_UNIFORM_LOCATION)
{
printf("m_modelMatrix not found\n");
return false;
}
// Find where vertex attributes are in shader
m_vertPos = m_shader->GetAttribLocation("v_position");
m_vertCol = m_shader->GetAttribLocation("v_color");
// Create the object
//m_triangle = new Object();
//m_triangle->Initialize(m_vertPos, m_vertCol);
//CREATES CUBE
m_cube = new Cube();
m_cube->Initialize(m_vertPos, m_vertCol);
//enable depth testing
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
return true;
}
void Graphics::Update(unsigned int dt, glm::vec3 speed)
{
// Update the object
m_cube->Update(dt);
}
void Graphics::Render()
{
//clear the screen
glClearColor(0.5, 0.2, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Start the correct program
m_shader->Enable();
// Send in the projection and view to the shader
glUniformMatrix4fv(m_projectionMatrix, 1, GL_FALSE, glm::value_ptr(m_camera->GetProjection()));
glUniformMatrix4fv(m_viewMatrix, 1, GL_FALSE, glm::value_ptr(m_camera->GetView()));
// Render the object
glUniformMatrix4fv(m_modelMatrix, 1, GL_FALSE, glm::value_ptr(m_cube->GetModel()));
m_cube->Render(m_vertPos, m_vertCol);
// Get any errors from OpenGL
auto error = glGetError();
if (error != GL_NO_ERROR)
{
string val = ErrorString(error);
std::cout << "Error initializing OpenGL! " << error << ", " << val << std::endl;
}
}
Cube* Graphics::getCubeInteractWith() {
return m_cube;
}
std::string Graphics::ErrorString(GLenum error)
{
if (error == GL_INVALID_ENUM)
{
return "GL_INVALID_ENUM: An unacceptable value is specified for an enumerated argument.";
}
else if (error == GL_INVALID_VALUE)
{
return "GL_INVALID_VALUE: A numeric argument is out of range.";
}
else if (error == GL_INVALID_OPERATION)
{
return "GL_INVALID_OPERATION: The specified operation is not allowed in the current state.";
}
else if (error == GL_INVALID_FRAMEBUFFER_OPERATION)
{
return "GL_INVALID_FRAMEBUFFER_OPERATION: The framebuffer object is not complete.";
}
else if (error == GL_OUT_OF_MEMORY)
{
return "GL_OUT_OF_MEMORY: There is not enough memory left to execute the command.";
}
else
{
return "None";
}
}