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
|
#include "Renderer3d.h"
#include "Shader.h"
#include "mathlib.h"
#include "WebglContext.h"
#include "Logger.h"
// Note: In the 'transform' attribute, the transform.x is the scale,
// transform.y is the rotation, and transform.zw is the translatiob.
const char* vertexShader =
"attribute vec4 position; \n"
"attribute vec4 color; \n"
"uniform mat4 projection; \n"
"uniform mat4 model; \n"
"varying lowp vec4 VertexColor; \n"
"void main() { \n"
" vec4 fragmentPosition = projection * model * position; \n"
" gl_Position = fragmentPosition; \n"
" VertexColor = color; \n"
"}";
const char* fragmentShader =
"varying lowp vec4 VertexColor; \n"
"void main() { \n"
" gl_FragColor = VertexColor; \n"
"}";
EM_BOOL onScreenSizeChanged_3D(int eventType, const EmscriptenUiEvent *uiEvent, void *userData) {
Renderer3D* renderer = (Renderer3D*)userData;
EMSCRIPTEN_RESULT result = emscripten_set_canvas_element_size( renderer->context->query, uiEvent->documentBodyClientWidth, uiEvent->documentBodyClientHeight);
if (result != EMSCRIPTEN_RESULT_SUCCESS) {
logger_error("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 Renderer3D::load(WebglContext* inContext) {
context = inContext;
printf("Compiling Renderer2d shader...\n");
shader = loadShader(vertexShader, fragmentShader);
useShader(shader);
attributes.position = getShaderAttribute(shader, "position");
attributes.color = getShaderAttribute(shader, "color");
uniforms.projection = getShaderUniform(shader, "projection");
uniforms.model = getShaderUniform(shader, "model");
projection = Mat4x4().getPerspectiveProjection(0.1, 100.f, DEG_TO_RAD(60.f), static_cast<f32>(context->width) / static_cast<f32>(context->height));
logger_info("Renderer2d shader compiled.\n");
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, false, onScreenSizeChanged_3D);
}
void Renderer3D::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 Renderer3D::unload() {
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT);
glDeleteProgram(shader);
}
Mesh3d Mesh3d_fromObj(std::string content) {
Mesh3d result;
return result;
}
|