From 0b8596a5ebec39f797fe8a14eed1bda3c2e3a93d Mon Sep 17 00:00:00 2001 From: mattkae Date: Sat, 7 May 2022 12:17:36 -0400 Subject: Working bunny render --- .gitignore | 3 +- .../LeafParticleRender.cpp.C2E152FEB0B3916C.idx | Bin 3400 -> 0 bytes .../LeafParticleRender.h.A0C89D6CC01BA1D6.idx | Bin 2388 -> 0 bytes .../clangd/index/List.h.84187636E582396F.idx | Bin 2316 -> 0 bytes .../clangd/index/Logger.cpp.34B9E643FD45042C.idx | Bin 1838 -> 0 bytes .../clangd/index/Logger.h.2FC9510B2A1DA2B6.idx | Bin 1454 -> 0 bytes .../clangd/index/MainLoop.cpp.0A630693C5FEABA6.idx | Bin 622 -> 0 bytes .../clangd/index/MainLoop.h.553E619C58AEAAC0.idx | Bin 766 -> 0 bytes .../index/Renderer2d.cpp.DF430723818BC87C.idx | Bin 2676 -> 0 bytes .../clangd/index/Renderer2d.h.F1518A7EF7C2E747.idx | Bin 1858 -> 0 bytes .../clangd/index/Shader.cpp.AC882162C5E40BA1.idx | Bin 1220 -> 0 bytes .../clangd/index/Shader.h.24FA31E5639E64F9.idx | Bin 1788 -> 0 bytes .../index/Snowflake.cpp.3E4BE700DA4CDAC9.idx | Bin 3608 -> 0 bytes .../clangd/index/Snowflake.h.CAD2EF1A1EEC6A99.idx | Bin 2606 -> 0 bytes .../index/TreeShape.cpp.3271E9D39A039C82.idx | Bin 4650 -> 0 bytes .../clangd/index/TreeShape.h.68863A800A0A4201.idx | Bin 3368 -> 0 bytes .../index/WebglContext.cpp.3728EAA427EF3918.idx | Bin 664 -> 0 bytes .../index/WebglContext.h.711DD5037F9DAD0A.idx | Bin 700 -> 0 bytes .../clangd/index/list.h.94D5DE92B51D5CD7.idx | Bin 2590 -> 0 bytes .../clangd/index/main.cpp.FFD15187911D63D3.idx | Bin 2662 -> 0 bytes .../clangd/index/mathlib.cpp.859174258F5EDF5F.idx | Bin 14254 -> 0 bytes .../clangd/index/mathlib.h.A720E377CAA2A298.idx | Bin 7208 -> 0 bytes .../clangd/index/types.h.940FD7BC1E2270BF.idx | Bin 814 -> 0 bytes themes/Makefile | 2 +- themes/Renderer3d.cpp | 168 ++++++++++++++++++++- themes/Renderer3d.h | 12 +- themes/dist/output.js | 30 +++- themes/dist/output.wasm | Bin 89301 -> 116476 bytes themes/main.cpp | 24 ++- themes/mathlib.cpp | 14 ++ themes/mathlib.h | 1 + themes/resources/bunny.obj | 9 +- 32 files changed, 239 insertions(+), 24 deletions(-) delete mode 100644 themes/.cache/clangd/index/LeafParticleRender.cpp.C2E152FEB0B3916C.idx delete mode 100644 themes/.cache/clangd/index/LeafParticleRender.h.A0C89D6CC01BA1D6.idx delete mode 100644 themes/.cache/clangd/index/List.h.84187636E582396F.idx delete mode 100644 themes/.cache/clangd/index/Logger.cpp.34B9E643FD45042C.idx delete mode 100644 themes/.cache/clangd/index/Logger.h.2FC9510B2A1DA2B6.idx delete mode 100644 themes/.cache/clangd/index/MainLoop.cpp.0A630693C5FEABA6.idx delete mode 100644 themes/.cache/clangd/index/MainLoop.h.553E619C58AEAAC0.idx delete mode 100644 themes/.cache/clangd/index/Renderer2d.cpp.DF430723818BC87C.idx delete mode 100644 themes/.cache/clangd/index/Renderer2d.h.F1518A7EF7C2E747.idx delete mode 100644 themes/.cache/clangd/index/Shader.cpp.AC882162C5E40BA1.idx delete mode 100644 themes/.cache/clangd/index/Shader.h.24FA31E5639E64F9.idx delete mode 100644 themes/.cache/clangd/index/Snowflake.cpp.3E4BE700DA4CDAC9.idx delete mode 100644 themes/.cache/clangd/index/Snowflake.h.CAD2EF1A1EEC6A99.idx delete mode 100644 themes/.cache/clangd/index/TreeShape.cpp.3271E9D39A039C82.idx delete mode 100644 themes/.cache/clangd/index/TreeShape.h.68863A800A0A4201.idx delete mode 100644 themes/.cache/clangd/index/WebglContext.cpp.3728EAA427EF3918.idx delete mode 100644 themes/.cache/clangd/index/WebglContext.h.711DD5037F9DAD0A.idx delete mode 100644 themes/.cache/clangd/index/list.h.94D5DE92B51D5CD7.idx delete mode 100644 themes/.cache/clangd/index/main.cpp.FFD15187911D63D3.idx delete mode 100644 themes/.cache/clangd/index/mathlib.cpp.859174258F5EDF5F.idx delete mode 100644 themes/.cache/clangd/index/mathlib.h.A720E377CAA2A298.idx delete mode 100644 themes/.cache/clangd/index/types.h.940FD7BC1E2270BF.idx diff --git a/.gitignore b/.gitignore index 4e5d1c3..4cff8d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ *.o -.vscode \ No newline at end of file +.vscode +.cache \ No newline at end of file diff --git a/themes/.cache/clangd/index/LeafParticleRender.cpp.C2E152FEB0B3916C.idx b/themes/.cache/clangd/index/LeafParticleRender.cpp.C2E152FEB0B3916C.idx deleted file mode 100644 index 1023991..0000000 Binary files a/themes/.cache/clangd/index/LeafParticleRender.cpp.C2E152FEB0B3916C.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/LeafParticleRender.h.A0C89D6CC01BA1D6.idx b/themes/.cache/clangd/index/LeafParticleRender.h.A0C89D6CC01BA1D6.idx deleted file mode 100644 index 6578f1f..0000000 Binary files a/themes/.cache/clangd/index/LeafParticleRender.h.A0C89D6CC01BA1D6.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/List.h.84187636E582396F.idx b/themes/.cache/clangd/index/List.h.84187636E582396F.idx deleted file mode 100644 index bc21ab0..0000000 Binary files a/themes/.cache/clangd/index/List.h.84187636E582396F.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/Logger.cpp.34B9E643FD45042C.idx b/themes/.cache/clangd/index/Logger.cpp.34B9E643FD45042C.idx deleted file mode 100644 index 8fd4f7f..0000000 Binary files a/themes/.cache/clangd/index/Logger.cpp.34B9E643FD45042C.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/Logger.h.2FC9510B2A1DA2B6.idx b/themes/.cache/clangd/index/Logger.h.2FC9510B2A1DA2B6.idx deleted file mode 100644 index f56ab56..0000000 Binary files a/themes/.cache/clangd/index/Logger.h.2FC9510B2A1DA2B6.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/MainLoop.cpp.0A630693C5FEABA6.idx b/themes/.cache/clangd/index/MainLoop.cpp.0A630693C5FEABA6.idx deleted file mode 100644 index eb244ee..0000000 Binary files a/themes/.cache/clangd/index/MainLoop.cpp.0A630693C5FEABA6.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/MainLoop.h.553E619C58AEAAC0.idx b/themes/.cache/clangd/index/MainLoop.h.553E619C58AEAAC0.idx deleted file mode 100644 index 96dfe45..0000000 Binary files a/themes/.cache/clangd/index/MainLoop.h.553E619C58AEAAC0.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/Renderer2d.cpp.DF430723818BC87C.idx b/themes/.cache/clangd/index/Renderer2d.cpp.DF430723818BC87C.idx deleted file mode 100644 index 01bf4da..0000000 Binary files a/themes/.cache/clangd/index/Renderer2d.cpp.DF430723818BC87C.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/Renderer2d.h.F1518A7EF7C2E747.idx b/themes/.cache/clangd/index/Renderer2d.h.F1518A7EF7C2E747.idx deleted file mode 100644 index d005647..0000000 Binary files a/themes/.cache/clangd/index/Renderer2d.h.F1518A7EF7C2E747.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/Shader.cpp.AC882162C5E40BA1.idx b/themes/.cache/clangd/index/Shader.cpp.AC882162C5E40BA1.idx deleted file mode 100644 index aeaed79..0000000 Binary files a/themes/.cache/clangd/index/Shader.cpp.AC882162C5E40BA1.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/Shader.h.24FA31E5639E64F9.idx b/themes/.cache/clangd/index/Shader.h.24FA31E5639E64F9.idx deleted file mode 100644 index 43816e1..0000000 Binary files a/themes/.cache/clangd/index/Shader.h.24FA31E5639E64F9.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/Snowflake.cpp.3E4BE700DA4CDAC9.idx b/themes/.cache/clangd/index/Snowflake.cpp.3E4BE700DA4CDAC9.idx deleted file mode 100644 index 16294ae..0000000 Binary files a/themes/.cache/clangd/index/Snowflake.cpp.3E4BE700DA4CDAC9.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/Snowflake.h.CAD2EF1A1EEC6A99.idx b/themes/.cache/clangd/index/Snowflake.h.CAD2EF1A1EEC6A99.idx deleted file mode 100644 index 2f0ad76..0000000 Binary files a/themes/.cache/clangd/index/Snowflake.h.CAD2EF1A1EEC6A99.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/TreeShape.cpp.3271E9D39A039C82.idx b/themes/.cache/clangd/index/TreeShape.cpp.3271E9D39A039C82.idx deleted file mode 100644 index f531d9b..0000000 Binary files a/themes/.cache/clangd/index/TreeShape.cpp.3271E9D39A039C82.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/TreeShape.h.68863A800A0A4201.idx b/themes/.cache/clangd/index/TreeShape.h.68863A800A0A4201.idx deleted file mode 100644 index dbbe718..0000000 Binary files a/themes/.cache/clangd/index/TreeShape.h.68863A800A0A4201.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/WebglContext.cpp.3728EAA427EF3918.idx b/themes/.cache/clangd/index/WebglContext.cpp.3728EAA427EF3918.idx deleted file mode 100644 index b4162e5..0000000 Binary files a/themes/.cache/clangd/index/WebglContext.cpp.3728EAA427EF3918.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/WebglContext.h.711DD5037F9DAD0A.idx b/themes/.cache/clangd/index/WebglContext.h.711DD5037F9DAD0A.idx deleted file mode 100644 index a1c1f7e..0000000 Binary files a/themes/.cache/clangd/index/WebglContext.h.711DD5037F9DAD0A.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/list.h.94D5DE92B51D5CD7.idx b/themes/.cache/clangd/index/list.h.94D5DE92B51D5CD7.idx deleted file mode 100644 index 0133a7f..0000000 Binary files a/themes/.cache/clangd/index/list.h.94D5DE92B51D5CD7.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/main.cpp.FFD15187911D63D3.idx b/themes/.cache/clangd/index/main.cpp.FFD15187911D63D3.idx deleted file mode 100644 index 972d73a..0000000 Binary files a/themes/.cache/clangd/index/main.cpp.FFD15187911D63D3.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/mathlib.cpp.859174258F5EDF5F.idx b/themes/.cache/clangd/index/mathlib.cpp.859174258F5EDF5F.idx deleted file mode 100644 index e3d887c..0000000 Binary files a/themes/.cache/clangd/index/mathlib.cpp.859174258F5EDF5F.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/mathlib.h.A720E377CAA2A298.idx b/themes/.cache/clangd/index/mathlib.h.A720E377CAA2A298.idx deleted file mode 100644 index 6eabda1..0000000 Binary files a/themes/.cache/clangd/index/mathlib.h.A720E377CAA2A298.idx and /dev/null differ diff --git a/themes/.cache/clangd/index/types.h.940FD7BC1E2270BF.idx b/themes/.cache/clangd/index/types.h.940FD7BC1E2270BF.idx deleted file mode 100644 index adc57af..0000000 Binary files a/themes/.cache/clangd/index/types.h.940FD7BC1E2270BF.idx and /dev/null differ diff --git a/themes/Makefile b/themes/Makefile index dce422e..8bdc04e 100644 --- a/themes/Makefile +++ b/themes/Makefile @@ -14,4 +14,4 @@ build: @mkdir -p dist clean: - rm -rf dist \ No newline at end of file + rm -rf dist && rm *.o \ No newline at end of file diff --git a/themes/Renderer3d.cpp b/themes/Renderer3d.cpp index ded364d..771d778 100644 --- a/themes/Renderer3d.cpp +++ b/themes/Renderer3d.cpp @@ -1,8 +1,10 @@ #include "Renderer3d.h" #include "Shader.h" +#include "list.h" #include "mathlib.h" #include "WebglContext.h" #include "Logger.h" +#include // Note: In the 'transform' attribute, the transform.x is the scale, // transform.y is the rotation, and transform.zw is the translatiob. @@ -10,10 +12,11 @@ const char* vertexShader = "attribute vec4 position; \n" "attribute vec4 color; \n" "uniform mat4 projection; \n" +"uniform mat4 view; \n" "uniform mat4 model; \n" "varying lowp vec4 VertexColor; \n" "void main() { \n" -" vec4 fragmentPosition = projection * model * position; \n" +" vec4 fragmentPosition = projection * view * model * position; \n" " gl_Position = fragmentPosition; \n" " VertexColor = color; \n" "}"; @@ -45,8 +48,10 @@ void Renderer3D::load(WebglContext* inContext) { attributes.position = getShaderAttribute(shader, "position"); attributes.color = getShaderAttribute(shader, "color"); uniforms.projection = getShaderUniform(shader, "projection"); + uniforms.view = getShaderUniform(shader, "view"); uniforms.model = getShaderUniform(shader, "model"); - projection = Mat4x4().getPerspectiveProjection(0.1, 100.f, DEG_TO_RAD(60.f), static_cast(context->width) / static_cast(context->height)); + projection = Mat4x4().getPerspectiveProjection(0.1, 1000.f, 0.872f, static_cast(context->width) / static_cast(context->height)); + view = Mat4x4().getLookAt({ 0, 25, 25 }, { 0, 0, 0 }, { 0, 1, 0 }); logger_info("Renderer2d shader compiled.\n"); @@ -64,6 +69,7 @@ void Renderer3D::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); useShader(shader); setShaderMat4(uniforms.projection, projection); + setShaderMat4(uniforms.view, view); } void Renderer3D::unload() { @@ -72,10 +78,164 @@ void Renderer3D::unload() { glDeleteProgram(shader); } -Mesh3d Mesh3d_fromObj(std::string content) { +enum LineType { + LineType_None, + LineType_Comment, + LineType_mtl, + LineType_v, + LineType_f, + LineType_Unsupported +}; + +struct LineItem { + LineType type = LineType_None; + + i32 idx = 0; + + union { + f32 vertices[3]; + i32 indices[3]; + } v; +}; + +inline i32 readPastSpaces(i32 i, const char* content) { + while (content[i] == ' ') i++; + return i; +} + +inline i32 readPastLine(i32 i, const char* content) { + while (content[i] != '\n' && content[i] != '\0') i++; + return i; +} + +inline i32 readToken(i32 i, const char* content, char* output) { + i32 tidx = 0; + i = readPastSpaces(i, content); + while (content[i] != ' ' && content[i] != '\n' && content[i] != '\0') { + output[tidx] = content[i]; + i++; + tidx++; + } + output[tidx] = '\0'; + return i; +} + +Mesh3d Mesh3d_fromObj(Renderer3D* renderer, const char* content, const i32 len) { Mesh3d result; + result.vertices.allocate(2048); + result.indices.allocate(2048); - + LineItem lt; + lt.type = LineType_None; + i32 lineNumber = 0; + i32 i = 0; + while (content[i] != '\0') { + i = readPastSpaces(i, content); + if (lt.type == LineType_None) { + lineNumber++; + char type[32]; + i = readToken(i, content, type); + + if (strncmp(type, "#", 1) == 0) { + lt.type = LineType_Comment; + } + else if (strncmp(type, "mtllib", 6) == 0) { + lt.type = LineType_mtl; + } + else if (strncmp(type, "v", 1) == 0) { + lt.type = LineType_v; + } + else if (strncmp(type, "f", 1) == 0) { + lt.type = LineType_f; + } + else { + i++; + //lt.type = LineType_Unsupported; + //logger_error("Unknown type %s, %d", type, lineNumber); + } + } + else { + char buffer[32]; + switch (lt.type) { + case LineType_mtl: + i = readToken(i, content, buffer); + break; + case LineType_v: + while (content[i] != '\n' && content[i] != '\0') { + i = readToken(i, content, buffer); + lt.v.vertices[lt.idx] = atof(buffer); + lt.idx++; + } + result.vertices.add({ + Vector4(lt.v.vertices[0], lt.v.vertices[1], lt.v.vertices[2], 1.f), + Vector4(randomFloatBetween(0, 1), randomFloatBetween(0, 1), randomFloatBetween(0, 1), 1) + }); + break; + case LineType_f: + while (content[i] != '\n' && content[i] != '\0') { + i = readToken(i, content, buffer); + lt.v.indices[lt.idx] = atoi(buffer); + lt.idx++; + } + result.indices.add(lt.v.indices[0] - 1); + result.indices.add(lt.v.indices[1] - 1); + result.indices.add(lt.v.indices[2] - 1); + break; + case LineType_Comment: + i = readPastLine(i, content); + break; + case LineType_Unsupported: + i = readPastLine(i, content); + break; + default: + break; + } + + lt = LineItem(); + } + } + printf("Completed Mesh3d loading.\n"); + result.load(renderer); return result; +} + +void Mesh3d::load(Renderer3D* renderer) { + glGenVertexArrays(1, &vao); + glGenBuffers(1, &vbo); + glGenBuffers(1, &ebo); + + glBindVertexArray(vao); + + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, vertices.numElements * sizeof(Vertex3d), &vertices.data[0], GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.numElements * sizeof(GLuint), &indices.data[0], GL_STATIC_DRAW); + + // Position + glEnableVertexAttribArray(renderer->attributes.position); + glVertexAttribPointer(renderer->attributes.position, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)0); + + // Normal + glEnableVertexAttribArray(renderer->attributes.color); + glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, color)); + + glBindVertexArray(0); +} + +void Mesh3d::unload() { + if (vao) glDeleteVertexArrays(1, &vao); + if (vbo) glDeleteBuffers(1, &vbo); + if (ebo) glDeleteBuffers(1, &ebo); + vertices.deallocate(); + indices.deallocate(); +} + +void Mesh3d::render(Renderer3D* renderer) { + setShaderMat4(renderer->uniforms.model, model); + + glBindVertexArray(vao); + glDrawElements(GL_TRIANGLES, indices.numElements, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); } \ No newline at end of file diff --git a/themes/Renderer3d.h b/themes/Renderer3d.h index add41d7..71fc8fe 100644 --- a/themes/Renderer3d.h +++ b/themes/Renderer3d.h @@ -5,6 +5,8 @@ #include "types.h" #include +struct Renderer3D; + struct Vertex3d { Vector4 position; Vector4 color; @@ -17,12 +19,17 @@ struct Mesh3d { matte::List vertices; matte::List indices; Mat4x4 model; + + void load(Renderer3D* renderer); + void render(Renderer3D* renderer); + void unload(); }; struct WebglContext; struct Renderer3D { WebglContext* context = NULL; Mat4x4 projection; + Mat4x4 view; u32 shader; Vector4 clearColor; @@ -33,6 +40,7 @@ struct Renderer3D { struct { i32 projection; + i32 view; i32 model; } uniforms; @@ -41,6 +49,6 @@ struct Renderer3D { void unload(); }; -Mesh3d Mesh3d_fromObj(std::string content); +Mesh3d Mesh3d_fromObj(Renderer3D* renderer, const char* content, const i32 len); -#endif \ No newline at end of file +#endif diff --git a/themes/dist/output.js b/themes/dist/output.js index a11cf80..0e9f1a9 100644 --- a/themes/dist/output.js +++ b/themes/dist/output.js @@ -3226,6 +3226,31 @@ var ASM_CONSTS = { GL.postDrawHandleClientVertexAttribBindings(); } + function _glDrawElements(mode, count, type, indices) { + var buf; + if (!GLctx.currentElementArrayBufferBinding) { + var size = GL.calcBufLength(1, type, 0, count); + buf = GL.getTempIndexBuffer(size); + GLctx.bindBuffer(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/, buf); + GLctx.bufferSubData(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/, + 0, + HEAPU8.subarray(indices, indices + size)); + // the index is now 0 + indices = 0; + } + + // bind any client-side buffers + GL.preDrawHandleClientVertexAttribBindings(count); + + GLctx.drawElements(mode, count, type, indices); + + GL.postDrawHandleClientVertexAttribBindings(count); + + if (!GLctx.currentElementArrayBufferBinding) { + GLctx.bindBuffer(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/, null); + } + } + function _glEnable(x0) { GLctx['enable'](x0) } function _glEnableVertexAttribArray(index) { @@ -3629,6 +3654,7 @@ var asmLibraryArg = { "glDepthFunc": _glDepthFunc, "glDepthMask": _glDepthMask, "glDrawArrays": _glDrawArrays, + "glDrawElements": _glDrawElements, "glEnable": _glEnable, "glEnableVertexAttribArray": _glEnableVertexAttribArray, "glGenBuffers": _glGenBuffers, @@ -3656,10 +3682,10 @@ var ___wasm_call_ctors = Module["___wasm_call_ctors"] = createExportWrapper("__w var _main = Module["_main"] = createExportWrapper("main"); /** @type {function(...*):?} */ -var _free = Module["_free"] = createExportWrapper("free"); +var _malloc = Module["_malloc"] = createExportWrapper("malloc"); /** @type {function(...*):?} */ -var _malloc = Module["_malloc"] = createExportWrapper("malloc"); +var _free = Module["_free"] = createExportWrapper("free"); /** @type {function(...*):?} */ var ___errno_location = Module["___errno_location"] = createExportWrapper("__errno_location"); diff --git a/themes/dist/output.wasm b/themes/dist/output.wasm index b8a0e67..4c50e4a 100755 Binary files a/themes/dist/output.wasm and b/themes/dist/output.wasm differ diff --git a/themes/main.cpp b/themes/main.cpp index d3966af..ef7d18b 100644 --- a/themes/main.cpp +++ b/themes/main.cpp @@ -37,6 +37,9 @@ struct WinterTheme { }; struct SpringTheme { + bool canRenderBunny = false; + Mesh3d bunnyMesh; + void load(Renderer3D* renderer); void update(f32 dtSeconds); void render(Renderer3D* renderer); @@ -227,17 +230,21 @@ void WinterTheme::unload() { // -- Spring theme void onBunnySuccess(emscripten_fetch_t *fetch) { - printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url); - // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1]; - emscripten_fetch_close(fetch); // Free data associated with the fetch. + springTheme.canRenderBunny = true; + printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url); + const i32 len = fetch->numBytes; + springTheme.bunnyMesh = Mesh3d_fromObj(&renderer3d, fetch->data, len); + // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1]; + emscripten_fetch_close(fetch); // Free data associated with the fetch. } void onBunnyFail(emscripten_fetch_t *fetch) { - printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status); - emscripten_fetch_close(fetch); // Also free data on failure. + printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status); + emscripten_fetch_close(fetch); // Also free data on failure. } void SpringTheme::load(Renderer3D* renderer) { + canRenderBunny = false; renderer->clearColor = Vector4(160, 231, 160, 255.f).toNormalizedColor(); emscripten_fetch_attr_t attr; @@ -253,7 +260,12 @@ void SpringTheme::update(f32 dtSeconds) { } void SpringTheme::render(Renderer3D* renderer) { + renderer->render(); + if (canRenderBunny) { + bunnyMesh.render(renderer); + } } void SpringTheme::unload() { -} \ No newline at end of file + bunnyMesh.unload(); +} diff --git a/themes/mathlib.cpp b/themes/mathlib.cpp index 2e92aa8..702d86c 100644 --- a/themes/mathlib.cpp +++ b/themes/mathlib.cpp @@ -572,6 +572,20 @@ void Mat4x4::print() { printf(" ]\n"); } +// See https://stackoverflow.com/questions/349050/calculating-a-lookat-matrix for why the dot product is in the bottom +Mat4x4 Mat4x4::getLookAt(Vector3 eye,Vector3 pointToLookAt, Vector3 up) { + Vector3 zAxis = (pointToLookAt - eye).normalize(); + Vector3 xAxis = zAxis.cross(up).normalize(); + Vector3 yAxis = xAxis.cross(zAxis).normalize(); + + return { + { xAxis.x, yAxis.x, -zAxis.x, 0, + xAxis.y, yAxis.y, -zAxis.y, 0, + xAxis.z, yAxis.z, -zAxis.z, 0, + -xAxis.dot(eye), -yAxis.dot(eye), zAxis.dot(eye), 1 } + }; +} + // *************************************** // Quaternion Quaternion::Quaternion() { }; diff --git a/themes/mathlib.h b/themes/mathlib.h index f89addc..be9aaf6 100644 --- a/themes/mathlib.h +++ b/themes/mathlib.h @@ -150,6 +150,7 @@ struct Mat4x4 { Mat4x4 getOrthographicMatrix(float left, float right, float bottom, float top); Mat4x4 inverse(); Mat4x4 getPerspectiveProjection(float near, float far, float fieldOfViewRadians, float aspectRatio); + Mat4x4 getLookAt(Vector3 eye,Vector3 pointToLookAt, Vector3 up); void print(); }; diff --git a/themes/resources/bunny.obj b/themes/resources/bunny.obj index 92bb042..18021c9 100644 --- a/themes/resources/bunny.obj +++ b/themes/resources/bunny.obj @@ -1,19 +1,12 @@ -# 3ds Max Wavefront OBJ Exporter v0.94b - (c)2007 guruware -# File Created: 04.07.2010 09:57:01 - mtllib bunny.mtl -# -# object default -# - v 1.4870 0.3736 2.2576 v 1.5803 0.3451 2.1859 v 1.6275 0.3111 2.2261 v 1.6343 0.0783 2.4352 v 1.5180 0.0644 2.5398 v 1.4568 0.0720 2.5205 -v 1.3953 0.0795 2.5013 +v 1.3953 0.0795 2.g5013 v 1.2850 0.0762 2.5973 v 1.2181 0.0732 2.5862 v 1.1079 0.0698 2.6824 -- cgit v1.2.1