diff options
author | mattkae <mattkae@protonmail.com> | 2022-05-07 12:17:36 -0400 |
---|---|---|
committer | mattkae <mattkae@protonmail.com> | 2022-05-07 12:17:36 -0400 |
commit | 0b8596a5ebec39f797fe8a14eed1bda3c2e3a93d (patch) | |
tree | ea248ce7d55046d578f5ad371f4083405a4f7892 /themes | |
parent | 1c051dffdb27ff6ebaa96802b021e99e49afb861 (diff) |
Working bunny render
Diffstat (limited to 'themes')
31 files changed, 237 insertions, 23 deletions
diff --git a/themes/.cache/clangd/index/LeafParticleRender.cpp.C2E152FEB0B3916C.idx b/themes/.cache/clangd/index/LeafParticleRender.cpp.C2E152FEB0B3916C.idx Binary files differdeleted file mode 100644 index 1023991..0000000 --- a/themes/.cache/clangd/index/LeafParticleRender.cpp.C2E152FEB0B3916C.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/LeafParticleRender.h.A0C89D6CC01BA1D6.idx b/themes/.cache/clangd/index/LeafParticleRender.h.A0C89D6CC01BA1D6.idx Binary files differdeleted file mode 100644 index 6578f1f..0000000 --- a/themes/.cache/clangd/index/LeafParticleRender.h.A0C89D6CC01BA1D6.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/List.h.84187636E582396F.idx b/themes/.cache/clangd/index/List.h.84187636E582396F.idx Binary files differdeleted file mode 100644 index bc21ab0..0000000 --- a/themes/.cache/clangd/index/List.h.84187636E582396F.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/Logger.cpp.34B9E643FD45042C.idx b/themes/.cache/clangd/index/Logger.cpp.34B9E643FD45042C.idx Binary files differdeleted file mode 100644 index 8fd4f7f..0000000 --- a/themes/.cache/clangd/index/Logger.cpp.34B9E643FD45042C.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/Logger.h.2FC9510B2A1DA2B6.idx b/themes/.cache/clangd/index/Logger.h.2FC9510B2A1DA2B6.idx Binary files differdeleted file mode 100644 index f56ab56..0000000 --- a/themes/.cache/clangd/index/Logger.h.2FC9510B2A1DA2B6.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/MainLoop.cpp.0A630693C5FEABA6.idx b/themes/.cache/clangd/index/MainLoop.cpp.0A630693C5FEABA6.idx Binary files differdeleted file mode 100644 index eb244ee..0000000 --- a/themes/.cache/clangd/index/MainLoop.cpp.0A630693C5FEABA6.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/MainLoop.h.553E619C58AEAAC0.idx b/themes/.cache/clangd/index/MainLoop.h.553E619C58AEAAC0.idx Binary files differdeleted file mode 100644 index 96dfe45..0000000 --- a/themes/.cache/clangd/index/MainLoop.h.553E619C58AEAAC0.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/Renderer2d.cpp.DF430723818BC87C.idx b/themes/.cache/clangd/index/Renderer2d.cpp.DF430723818BC87C.idx Binary files differdeleted file mode 100644 index 01bf4da..0000000 --- a/themes/.cache/clangd/index/Renderer2d.cpp.DF430723818BC87C.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/Renderer2d.h.F1518A7EF7C2E747.idx b/themes/.cache/clangd/index/Renderer2d.h.F1518A7EF7C2E747.idx Binary files differdeleted file mode 100644 index d005647..0000000 --- a/themes/.cache/clangd/index/Renderer2d.h.F1518A7EF7C2E747.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/Shader.cpp.AC882162C5E40BA1.idx b/themes/.cache/clangd/index/Shader.cpp.AC882162C5E40BA1.idx Binary files differdeleted file mode 100644 index aeaed79..0000000 --- a/themes/.cache/clangd/index/Shader.cpp.AC882162C5E40BA1.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/Shader.h.24FA31E5639E64F9.idx b/themes/.cache/clangd/index/Shader.h.24FA31E5639E64F9.idx Binary files differdeleted file mode 100644 index 43816e1..0000000 --- a/themes/.cache/clangd/index/Shader.h.24FA31E5639E64F9.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/Snowflake.cpp.3E4BE700DA4CDAC9.idx b/themes/.cache/clangd/index/Snowflake.cpp.3E4BE700DA4CDAC9.idx Binary files differdeleted file mode 100644 index 16294ae..0000000 --- a/themes/.cache/clangd/index/Snowflake.cpp.3E4BE700DA4CDAC9.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/Snowflake.h.CAD2EF1A1EEC6A99.idx b/themes/.cache/clangd/index/Snowflake.h.CAD2EF1A1EEC6A99.idx Binary files differdeleted file mode 100644 index 2f0ad76..0000000 --- a/themes/.cache/clangd/index/Snowflake.h.CAD2EF1A1EEC6A99.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/TreeShape.cpp.3271E9D39A039C82.idx b/themes/.cache/clangd/index/TreeShape.cpp.3271E9D39A039C82.idx Binary files differdeleted file mode 100644 index f531d9b..0000000 --- a/themes/.cache/clangd/index/TreeShape.cpp.3271E9D39A039C82.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/TreeShape.h.68863A800A0A4201.idx b/themes/.cache/clangd/index/TreeShape.h.68863A800A0A4201.idx Binary files differdeleted file mode 100644 index dbbe718..0000000 --- a/themes/.cache/clangd/index/TreeShape.h.68863A800A0A4201.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/WebglContext.cpp.3728EAA427EF3918.idx b/themes/.cache/clangd/index/WebglContext.cpp.3728EAA427EF3918.idx Binary files differdeleted file mode 100644 index b4162e5..0000000 --- a/themes/.cache/clangd/index/WebglContext.cpp.3728EAA427EF3918.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/WebglContext.h.711DD5037F9DAD0A.idx b/themes/.cache/clangd/index/WebglContext.h.711DD5037F9DAD0A.idx Binary files differdeleted file mode 100644 index a1c1f7e..0000000 --- a/themes/.cache/clangd/index/WebglContext.h.711DD5037F9DAD0A.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/list.h.94D5DE92B51D5CD7.idx b/themes/.cache/clangd/index/list.h.94D5DE92B51D5CD7.idx Binary files differdeleted file mode 100644 index 0133a7f..0000000 --- a/themes/.cache/clangd/index/list.h.94D5DE92B51D5CD7.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/main.cpp.FFD15187911D63D3.idx b/themes/.cache/clangd/index/main.cpp.FFD15187911D63D3.idx Binary files differdeleted file mode 100644 index 972d73a..0000000 --- a/themes/.cache/clangd/index/main.cpp.FFD15187911D63D3.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/mathlib.cpp.859174258F5EDF5F.idx b/themes/.cache/clangd/index/mathlib.cpp.859174258F5EDF5F.idx Binary files differdeleted file mode 100644 index e3d887c..0000000 --- a/themes/.cache/clangd/index/mathlib.cpp.859174258F5EDF5F.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/mathlib.h.A720E377CAA2A298.idx b/themes/.cache/clangd/index/mathlib.h.A720E377CAA2A298.idx Binary files differdeleted file mode 100644 index 6eabda1..0000000 --- a/themes/.cache/clangd/index/mathlib.h.A720E377CAA2A298.idx +++ /dev/null diff --git a/themes/.cache/clangd/index/types.h.940FD7BC1E2270BF.idx b/themes/.cache/clangd/index/types.h.940FD7BC1E2270BF.idx Binary files differdeleted file mode 100644 index adc57af..0000000 --- a/themes/.cache/clangd/index/types.h.940FD7BC1E2270BF.idx +++ /dev/null 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 <cstdio> // 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<f32>(context->width) / static_cast<f32>(context->height)); + projection = Mat4x4().getPerspectiveProjection(0.1, 1000.f, 0.872f, static_cast<f32>(context->width) / static_cast<f32>(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 <string> +struct Renderer3D; + struct Vertex3d { Vector4 position; Vector4 color; @@ -17,12 +19,17 @@ struct Mesh3d { matte::List<Vertex3d> vertices; matte::List<u32> 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 Binary files differindex b8a0e67..4c50e4a 100755 --- a/themes/dist/output.wasm +++ b/themes/dist/output.wasm 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
|