Skip to content

Commit ffe4fc8

Browse files
committed
Completed integration
1 parent e7fca86 commit ffe4fc8

File tree

7 files changed

+46
-18
lines changed

7 files changed

+46
-18
lines changed

Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@ SRCS = main.c
33

44
FILES = $(addprefix src/, $(SRCS)) # Add 'src/' to each source
55
OBJS = $(FILES:.c=.o) # Modify file extensions of FILES
6-
EOPT = USE_WEBGL2=1 FULL_ES3=1 USE_GLFW=3 WASM=1 SIDE_MODULE=1 # Emscripten specific options
6+
EOPT = USE_WEBGL2=1 FULL_ES3=1 USE_GLFW=3 # Emscripten specific options
77
EOPTS = $(addprefix -s $(EMPTY), $(EOPT)) # Add '-s ' to each option
88

99
# Builds necessary files
1010
build: $(OBJS)
1111
mkdir build
1212
mkdir app/wasm
13-
$(CC) $(FILES) -O3 $(EOPTS) -o app/wasm/module.wasm
14-
$(CC) src/counter.c -O3 $(EOPTS) -o app/wasm/counter.wasm
13+
$(CC) $(FILES) -O3 $(EOPTS) -o app/wasm/module.js -s WASM=1 -Wall -s MODULARIZE=1
14+
$(CC) src/counter.c -O3 $(EOPTS) -s SIDE_MODULE=1 -s WASM=1 -o app/wasm/counter.wasm
1515
emcc src/engine.c -O3 -o app/wasm/engine.js -s WASM=1 -Wall -s MODULARIZE=1
1616

1717
# O3 is is a optimization setting
1818
# WASM=1 means set web assembly to ture
19+
# SIDE_MODULE=1 will only produce a wasm file, you have to build out your own integration
1920
# MODULARIZE provides a ASM JS module we can interact with our client code, so we don't need to build any integration code from scratch
2021

22+
# Test run
23+
test:
24+
emcc src/main.c -O3 $(EOPTS) -o build/index.html -s WASM=1
25+
2126
# Removes object files, but leaves build for serving
2227
dist: build
2328
rm $(OBJS)

app/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import './style.scss';
22
import { Counter } from './counter';
3+
import { OpenGLDemo } from "./opengl-demo";
34
import Module from './wasm/engine.js'
45

56
// Get Worker Thread
6-
const WorkerThread = require('worker-loader!./worker.js');
7+
// const WorkerThread = require('worker-loader!./worker.js');
78

89
function main() {
910
addHeader();
1011
const counter = new Counter();
11-
const worker = new WorkerThread();
12+
const demo = new OpenGLDemo();
1213
let module = Module({wasmBinaryFile: '/wasm/engine.wasm'});
13-
handleWorkerThread(worker);
14+
// const worker = new WorkerThread();
15+
// handleWorkerThread(worker);
1416
}
1517

1618
function addHeader() {

app/opengl-demo.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import ModuleWA from './wasm/module.wasm';
1+
import Module from './wasm/module.js'
22

33
export class OpenGLDemo {
44

55
module;
66

77
constructor() {
8+
this.insertCanvas();
89
this.loadDemoWasm();
910
}
1011

12+
/**
13+
* Insert the canvas with id 'canvas' for GLFW to pick up
14+
*/
15+
insertCanvas() {
16+
document.querySelector('#app').insertAdjacentHTML('afterend', '<div id="opengl-demo"><canvas id="canvas"></canvas></div>');
17+
}
18+
1119
loadDemoWasm() {
12-
this.module = new ModuleWA({
13-
'env': {
14-
'memoryBase': 0,
15-
'tableBase': 0,
16-
'memory': new WebAssembly.Memory({initial: 256}),
17-
'table': new WebAssembly.Table({initial: 0, element: 'anyfunc'})
18-
}
20+
this.module = Module({
21+
wasmBinaryFile: '/wasm/module.wasm',
22+
canvas: document.getElementById('canvas')
1923
});
2024
}
2125

app/wasm/module.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/wasm/module.wasm

28.8 KB
Binary file not shown.

src/linmath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static inline void mat4x4_rotate(mat4x4 R, mat4x4 M, float x, float y, float z,
192192
vec3 u = {x, y, z};
193193

194194
if(vec3_len(u) > 1e-4) {
195-
mat4x4 T, C, S = {0};
195+
mat4x4 T, C, S = {};
196196

197197
vec3_norm(u, u);
198198
mat4x4_from_vec3_mul_outer(T, u, u);

src/main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "linmath.h"
22

3-
#include <emscripten/emscripten.h>
3+
#include <emscripten.h>
44

55
#define GLFW_INCLUDE_ES3
66

@@ -41,10 +41,12 @@ static const char* fragment_shader_text =
4141
" o_color = vec4(i_color, 1.0);\n"
4242
"}\n";
4343

44+
EMSCRIPTEN_KEEPALIVE
4445
static void output_error(int error, const char* msg) {
4546
fprintf(stderr, "Error: %s\n", msg);
4647
}
4748

49+
EMSCRIPTEN_KEEPALIVE
4850
static void generate_frame() {
4951
float ratio;
5052
int width, height;
@@ -65,6 +67,7 @@ static void generate_frame() {
6567
glfwPollEvents();
6668
}
6769

70+
EMSCRIPTEN_KEEPALIVE
6871
static int check_compiled(shader) {
6972
GLint success = 0;
7073
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
@@ -83,6 +86,7 @@ static int check_compiled(shader) {
8386
return success;
8487
}
8588

89+
EMSCRIPTEN_KEEPALIVE
8690
static int check_linked(program) {
8791
GLint success = 0;
8892
glGetProgramiv(program, GL_LINK_STATUS, &success);
@@ -100,8 +104,7 @@ static int check_linked(program) {
100104
return success;
101105
}
102106

103-
extern void run();
104-
107+
EMSCRIPTEN_KEEPALIVE
105108
int main() {
106109
glfwSetErrorCallback(output_error);
107110

0 commit comments

Comments
 (0)