summaryrefslogtreecommitdiff
path: root/frontend/_wasm/wasm.cpp
blob: 254a5e2ab2aa4aa3d71b7f25ce8e93d4818881b8 (plain)
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/time.h>
#include <emscripten.h>
#include <emscripten/html5.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>

#include "mathlib.h"
#include "Shader.h"

// Shader code inlined
const char* orthographicVertex = 
"attribute vec2 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 * vec4(position, 1, 1); \n"
"    gl_Position = fragmentPosition; \n"
"    VertexColor = color; \n"
"}";

const char* orthographicFragment = 
"varying lowp vec4 VertexColor; \n"
"void main() { \n"
"    gl_FragColor = VertexColor; \n"
"}";


struct OrthographicProgramData {
    GLuint shader;

    struct {
        int position;
        int color;
    } attributes;

    struct {
        int projection;
        int model;
    } uniformVariables;
};

struct OrthographicVertex {
    Vector2 position;
    Vector4 color;
};

// Create a triangle
struct TriangleObject {
    OrthographicVertex vertices[3];
	Vector2 velocity;
	Vector2 position;
    GLuint mVao;
    GLuint mVbo;
    Mat4x4 model;

    void initialize(OrthographicProgramData* programData) {
        useShader(programData->shader);

        glGenVertexArrays(1, &mVao);
        glBindVertexArray(mVao);

        glGenBuffers(1, &mVbo);
        glBindBuffer(GL_ARRAY_BUFFER, mVbo);
        glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(OrthographicVertex), &vertices[0], GL_STATIC_DRAW);

        glEnableVertexAttribArray(programData->attributes.position);
        glVertexAttribPointer(programData->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)0);

        glEnableVertexAttribArray(programData->attributes.color);
        glVertexAttribPointer(programData->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)offsetof(OrthographicVertex, color));

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }

    void render(OrthographicProgramData* programData) {
        setShaderMat4(programData->uniformVariables.model, model);

        glBindVertexArray(mVao);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0);
    }

};

struct Scene {
	Mat4x4 projection;
	TriangleObject triangleObject;
    OrthographicProgramData programData;
	bool isTerminated = false;
};

long long timeInMilliseconds(void) {
    struct timeval tv;

    gettimeofday(&tv,NULL);
    return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
}

long elapsedTime = 0;
long lastTime = 0;
int numFrames = 0;

EM_BOOL update(double time, void* userData);
EM_BOOL runScene(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData);
EM_BOOL terminateScene(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData);

int main() {
    printf("Initializing the canvas...\n");
    emscripten_set_canvas_element_size( "#wasm_canvas", 640, 480 );

    EmscriptenWebGLContextAttributes attrs;
    emscripten_webgl_init_context_attributes(&attrs);

    attrs.enableExtensionsByDefault = 1;
    attrs.majorVersion = 3;
    attrs.minorVersion = 0;

    EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context( "#wasm_canvas", &attrs );

    if (!context)
    {
        attrs.majorVersion = 1;
        context = emscripten_webgl_create_context( "#wasm_canvas", &attrs );
        if (context) printf("Skipping test: WebGL 2.0 is not available.\n");
        else printf("Test failed: WebGL is not available!\n");
        return 0;
    }
    emscripten_webgl_make_context_current(context);
    printf("Canvas ready.\n");
	
	emscripten_set_click_callback("#wasm_canvas_play_button", NULL, false, runScene);
	emscripten_set_click_callback("#wasm_canvas_stop_button", NULL, false, terminateScene);
    return EXIT_SUCCESS;
}

Scene scene;

EM_BOOL runScene(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) {
	scene.programData = OrthographicProgramData();
	printf("Compiling shaders...\n");
    scene.programData.shader = loadShader(orthographicVertex, orthographicFragment);
	printf("Shaders compiled.\n");


	printf("Initializing scene...\n");
    useShader(scene.programData.shader);

    scene.programData.attributes.position = getShaderAttribute(scene.programData.shader, "position");
    scene.programData.attributes.color = getShaderAttribute(scene.programData.shader, "color");
    scene.programData.uniformVariables.projection = getShaderUniform(scene.programData.shader, "projection");
    scene.programData.uniformVariables.model = getShaderUniform(scene.programData.shader, "model");

	scene.triangleObject = TriangleObject();
    scene.triangleObject.vertices[0] = { Vector2 { -100, -50 }, Vector4 { 1.f, 0.f, 0.f, 1.f }};
    scene.triangleObject.vertices[1] = { Vector2 { 0, 50 }, Vector4 { 0.f, 1.f, 0.f, 1.f }};
    scene.triangleObject.vertices[2] = { Vector2 { 100, -50 }, Vector4 { 0.f, 0.f, 1.f, 1.f }};
	scene.triangleObject.position = { 320.f, 480.f - 100.f  };
    scene.triangleObject.initialize(&scene.programData);

	scene.projection = Mat4x4().getOrthographicMatrix(0, 640, 0, 480);
	scene.isTerminated = false;
	printf("Scene initialized.\n");
	
    emscripten_request_animation_frame_loop(update, &scene);
	return true;
}

EM_BOOL update(double time, void* userData) {
	Scene* scene = (Scene*)userData;
	if (scene->isTerminated) {
		return false;
	}
	
	if (lastTime == 0) {
		lastTime = time;
		return true;
	}

	long deltaTime = time - lastTime;
	lastTime = time;
	elapsedTime += deltaTime;
	numFrames++;

	if (elapsedTime >= 1000.0) {
		printf("Frames Per Second: %d\n", numFrames);
		numFrames = 0;
		elapsedTime = 0.0;
	}

    float deltaTimeSeconds = static_cast<float>(deltaTime) / 1000.f;

	// Update
    scene->triangleObject.velocity = scene->triangleObject.velocity + Vector2 { 0, static_cast<float>(-9.8 * deltaTimeSeconds) };
	scene->triangleObject.position = scene->triangleObject.position + scene->triangleObject.velocity * deltaTimeSeconds;
	scene->triangleObject.model = Mat4x4().translateByVec2(scene->triangleObject.position);

	// Render
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glDepthMask(GL_TRUE);
	glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
	glClearDepth(1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);	
    useShader(scene->programData.shader);
    setShaderMat4(scene->programData.uniformVariables.projection, scene->projection);
    scene->triangleObject.render(&scene->programData);

	return true;
}

EM_BOOL terminateScene(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) {
	scene.isTerminated = true;
	glDeleteVertexArrays(1, &scene.triangleObject.mVao);
	glDeleteBuffers(1, &scene.triangleObject.mVbo);
	glDeleteProgram(scene.programData.shader);
	return true;
}