-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorTriangle.cpp
242 lines (186 loc) · 5.74 KB
/
ColorTriangle.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "ColorTriangle.h"
#include <memory>
#include <vector>
using namespace emscripten;
using namespace EmscriptenWebComponent;
ColorTriangle::ColorTriangle(std::string canvasIdentifier, uint8_t red, uint8_t green, uint8_t blue)
{
_red = red;
_green = green;
_blue = blue;
int width, height;
Initialize(canvasIdentifier);
emscripten_get_canvas_element_size(canvasIdentifier.c_str(), &width, &height);
SetSize(width, height);
ConfigureOpenGL();
CreateShaders();
CreateBuffer();
UpdateColor();
}
ColorTriangle::~ColorTriangle()
{
if(_program != 0)
{
glDeleteProgram(_program);
_program = 0;
}
if(_vertexBuffer != 0)
{
glDeleteBuffers(1, &_vertexBuffer);
_vertexBuffer = 0;
}
}
void ColorTriangle::SetSize(float width, float height)
{
glViewport(0, 0, width, height);
}
void ColorTriangle::StartRenderingLoop()
{
emscripten_set_main_loop_arg(&ColorTriangle::Render, this, -1, 0);
}
void ColorTriangle::StopRenderingLoop()
{
emscripten_cancel_main_loop();
}
void ColorTriangle::SetRed(uint8_t red)
{
_red = red;
UpdateColor();
ColorChangedCallback();
}
void ColorTriangle::SetGreen(uint8_t green)
{
_green = green;
UpdateColor();
ColorChangedCallback();
}
void ColorTriangle::SetBlue(uint8_t blue)
{
_blue = blue;
UpdateColor();
ColorChangedCallback();
}
uint8_t ColorTriangle::GetRed() const
{
return _red;
}
uint8_t ColorTriangle::GetGreen() const
{
return _green;
}
uint8_t ColorTriangle::GetBlue() const
{
return _blue;
}
GLuint ColorTriangle::CompileShader(GLenum type, const std::string& source)
{
GLint compilationSuccessful;
GLuint shader = glCreateShader(type);
const char* shaderSource = source.c_str();
glShaderSource(shader, 1, &shaderSource, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compilationSuccessful);
if(!compilationSuccessful)
{
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<GLchar> infoLog(infoLogLength);
glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, infoLog.data());
std::string errorMessage = std::string("Error compiling ") + (type == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader: ";
errorMessage += std::string(infoLog.begin(), infoLog.end());
printf("%s\n", errorMessage.c_str());
}
return shader;
}
GLuint ColorTriangle::CompileProgram(const std::string& vertexShaderSource, const std::string& fragmentShaderSource)
{
GLint linkingSuccessful;
GLuint program = glCreateProgram();
if(program == 0)
{
printf("Error creating OpenGL program, return value = %u\n", program);
}
GLuint vertexShader = CompileShader(GL_VERTEX_SHADER, vertexShaderSource);
GLuint fragmentShader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
if(vertexShader == 0 || fragmentShader == 0)
{
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
glDeleteProgram(program);
return 0;
}
glAttachShader(program, vertexShader);
glDeleteShader(vertexShader);
glAttachShader(program, fragmentShader);
glDeleteShader(fragmentShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &linkingSuccessful);
if(!linkingSuccessful)
{
GLint infoLogLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<GLchar> infoLog(infoLogLength);
glGetProgramInfoLog(program, static_cast<GLsizei>(infoLog.size()), NULL, infoLog.data());
std::string errorMessage = std::string("Error linking the OpenGL program: ");
errorMessage += std::string(infoLog.begin(), infoLog.end());
printf("%s\n", errorMessage.c_str());
}
return program;
}
void ColorTriangle::Initialize(std::string canvasIdentifier)
{
EmscriptenWebGLContextAttributes contextAttributes;
contextAttributes.alpha = EM_TRUE;
contextAttributes.depth = EM_TRUE;
contextAttributes.stencil = EM_TRUE;
contextAttributes.antialias = EM_TRUE;
contextAttributes.explicitSwapControl = EM_FALSE;
contextAttributes.majorVersion = 2;
contextAttributes.minorVersion = 0;
_context = emscripten_webgl_create_context(canvasIdentifier.c_str(), &contextAttributes);
emscripten_webgl_make_context_current(_context);
}
void ColorTriangle::Destroy()
{
emscripten_webgl_destroy_context(_context);
}
void ColorTriangle::ConfigureOpenGL()
{
glDisable(GL_DITHER);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
}
void ColorTriangle::CreateShaders()
{
_program = CompileProgram(vertexShaderSource, fragmentShaderSource);
_coordinateAttribLocation = glGetAttribLocation(_program, "aCoordinate");
_colorUniformLocation = glGetUniformLocation(_program, "uColor");
glUseProgram(_program);
}
void ColorTriangle::CreateBuffer()
{
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(_coordinateAttribLocation);
glVertexAttribPointer(_coordinateAttribLocation, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, reinterpret_cast<GLvoid*>(0));
}
void ColorTriangle::UpdateColor()
{
glUniform3f(_colorUniformLocation, _red / 255.0f, _green / 255.0f, _blue / 255.0f);
}
void ColorTriangle::RenderFrame()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void ColorTriangle::ColorChangedCallback()
{
uint32_t color = static_cast<uint32_t>(_red) << 16 | static_cast<uint32_t>(_green) << 8 | static_cast<uint32_t>(_blue);
EM_ASM_ARGS(onColorChanged($0, $1), this, color);
}
void ColorTriangle::Render(void* arg)
{
static_cast<ColorTriangle*>(arg)->RenderFrame();
}