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
|
#include "Renderer2d.h"
#include "Shader.h"
#include "WebglContext.h"
#include "mathlib.h"
#include <cstdio>
// Note: In the 'transform' attribute, the transform.x is the scale,
// transform.y is the rotation, and transform.zw is the translatiob.
const char* renderer2dVertexShader =
"attribute vec2 position; \n"
"attribute vec4 color; \n"
"attribute mat4 vMatrix; \n"
"uniform mat4 projection; \n"
"uniform mat4 model; \n"
"varying lowp vec4 VertexColor; \n"
"void main() { \n"
" vec4 fragmentPosition = projection * model * vMatrix * vec4(position.x, position.y, 0, 1); \n"
" gl_Position = fragmentPosition; \n"
" VertexColor = color; \n"
"}";
const char* renderer2dFragmentShader =
"varying lowp vec4 VertexColor; \n"
"void main() { \n"
" gl_FragColor = VertexColor; \n"
"}";
EM_BOOL onScreenSizeChanged(int eventType, const EmscriptenUiEvent *uiEvent, void *userData) {
Renderer2d* renderer = (Renderer2d*)userData;
EMSCRIPTEN_RESULT result = emscripten_set_canvas_element_size( renderer->context->query, uiEvent->documentBodyClientWidth, uiEvent->documentBodyClientHeight);
if (result != EMSCRIPTEN_RESULT_SUCCESS) {
printf("Failed to resize element at query: %s\n", renderer->context->query);
}
renderer->projection = Mat4x4().getOrthographicMatrix(0, renderer->context->width, 0, renderer->context->height);
return true;
}
void Renderer2d::load(WebglContext* inContext) {
context = inContext;
printf("Compiling Renderer2d shader...\n");
shader = loadShader(renderer2dVertexShader, renderer2dFragmentShader);
useShader(shader);
attributes.position = getShaderAttribute(shader, "position");
attributes.color = getShaderAttribute(shader, "color");
attributes.vMatrix = getShaderAttribute(shader, "vMatrix");
uniforms.projection = getShaderUniform(shader, "projection");
uniforms.model = getShaderUniform(shader, "model");
projection = Mat4x4().getOrthographicMatrix(0, context->width, 0, context->height);
printf("Renderer2d shader compiled.\n");
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, false, onScreenSizeChanged);
}
void Renderer2d::render() {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
useShader(shader);
setShaderMat4(uniforms.projection, projection);
}
void Renderer2d::unload() {
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT);
glDeleteProgram(shader);
}
void Renderer2dShape::load(Renderer2dVertex* inVertices, uint32 inNumVertices, Renderer2d* renderer) {
numVertices = inNumVertices;
useShader(renderer->shader);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Renderer2dVertex), &inVertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(renderer->attributes.position);
glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Renderer2dVertex), (GLvoid *)0);
glEnableVertexAttribArray(renderer->attributes.color);
glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Renderer2dVertex), (GLvoid *)offsetof(Renderer2dVertex, color));
for (int32 idx = 0; idx < 4; idx++) {
glEnableVertexAttribArray(renderer->attributes.vMatrix + idx);
glVertexAttribPointer(renderer->attributes.vMatrix + idx, 4, GL_FLOAT, GL_FALSE, sizeof(Renderer2dVertex), (GLvoid *)(offsetof(Renderer2dVertex, vMatrix) + (idx * 16)));
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Renderer2dShape::render(Renderer2d* renderer, GLenum drawType) {
setShaderMat4(renderer->uniforms.model, model);
glBindVertexArray(vao);
glDrawArrays(drawType, 0, numVertices);
glBindVertexArray(0);
}
void Renderer2dShape::unload() {
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
}
|