From dcc1f64354a589c71eb7dd6e63dc7af51d55e31b Mon Sep 17 00:00:00 2001 From: maelstrom Date: Wed, 30 Apr 2025 17:10:00 +0200 Subject: [PATCH] feat(renderer): added cylinder for hinges --- assets/shaders/phong.vs | 2 +- core/src/objects/part.h | 2 + core/src/rendering/defaultmeshes.cpp | 140 +++++++++++++++++++++++++++ core/src/rendering/defaultmeshes.h | 1 + core/src/rendering/renderer.cpp | 67 ++++++++++++- core/src/rendering/renderer.h | 5 +- 6 files changed, 210 insertions(+), 7 deletions(-) diff --git a/assets/shaders/phong.vs b/assets/shaders/phong.vs index aa529f3..2b032fa 100644 --- a/assets/shaders/phong.vs +++ b/assets/shaders/phong.vs @@ -46,5 +46,5 @@ void main() aNormal == vec3(0, 0, 1) ? FaceBack : -1; vSurfaceZ = surfaces[vFace]; - // if (surfaces[vFace] > SurfaceUniversal) vSurfaceZ = 0; + if (surfaces[vFace] > SurfaceUniversal) vSurfaceZ = 0; } diff --git a/core/src/objects/part.h b/core/src/objects/part.h index 7a01762..e885033 100644 --- a/core/src/objects/part.h +++ b/core/src/objects/part.h @@ -81,6 +81,8 @@ public: [[ def_prop(name="BackSurface", category=SURFACE) ]] SurfaceType backSurface = SurfaceType::SurfaceSmooth; + inline SurfaceType GetSurfaceFromFace(NormalId face) { return surfaceFromFace(face); } + Part(); Part(PartConstructParams params); ~Part() override; diff --git a/core/src/rendering/defaultmeshes.cpp b/core/src/rendering/defaultmeshes.cpp index d8e4580..78f7285 100644 --- a/core/src/rendering/defaultmeshes.cpp +++ b/core/src/rendering/defaultmeshes.cpp @@ -1589,16 +1589,156 @@ static float OUTLINE_VERTICES[] = { }; +static float CYLINDER_VERTICES[] = { + // positions // normals // texture coords + + 0.0, -0.5, -0.5, 0.2588, -0.9659, -0.0, 1.0, 1.0, + 0.25, -0.4330125, 0.5, 0.2588, -0.9659, -0.0, 0.916667, 0.5, + 0.0, -0.5, 0.5, 0.2588, -0.9659, -0.0, 1.0, 0.5, + 0.25, -0.4330125, -0.5, 0.7071, -0.7071, -0.0, 0.916667, 1.0, + 0.4330125, -0.25, 0.5, 0.7071, -0.7071, -0.0, 0.833333, 0.5, + 0.25, -0.4330125, 0.5, 0.7071, -0.7071, -0.0, 0.916667, 0.5, + 0.4330125, -0.25, -0.5, 0.9659, -0.2588, -0.0, 0.833333, 1.0, + 0.5, 0.0, 0.5, 0.9659, -0.2588, -0.0, 0.75, 0.5, + 0.4330125, -0.25, 0.5, 0.9659, -0.2588, -0.0, 0.833333, 0.5, + 0.5, 0.0, -0.5, 0.9659, 0.2588, -0.0, 0.75, 1.0, + 0.4330125, 0.25, 0.5, 0.9659, 0.2588, -0.0, 0.666667, 0.5, + 0.5, 0.0, 0.5, 0.9659, 0.2588, -0.0, 0.75, 0.5, + 0.4330125, 0.25, -0.5, 0.7071, 0.7071, -0.0, 0.666667, 1.0, + 0.25, 0.4330125, 0.5, 0.7071, 0.7071, -0.0, 0.583333, 0.5, + 0.4330125, 0.25, 0.5, 0.7071, 0.7071, -0.0, 0.666667, 0.5, + 0.25, 0.4330125, -0.5, 0.2588, 0.9659, -0.0, 0.583333, 1.0, + 0.0, 0.5, 0.5, 0.2588, 0.9659, -0.0, 0.5, 0.5, + 0.25, 0.4330125, 0.5, 0.2588, 0.9659, -0.0, 0.583333, 0.5, + 0.0, 0.5, -0.5, -0.2588, 0.9659, -0.0, 0.5, 1.0, + -0.25, 0.4330125, 0.5, -0.2588, 0.9659, -0.0, 0.416667, 0.5, + 0.0, 0.5, 0.5, -0.2588, 0.9659, -0.0, 0.5, 0.5, + -0.25, 0.4330125, -0.5, -0.7071, 0.7071, -0.0, 0.416667, 1.0, + -0.4330125, 0.25, 0.5, -0.7071, 0.7071, -0.0, 0.333333, 0.5, + -0.25, 0.4330125, 0.5, -0.7071, 0.7071, -0.0, 0.416667, 0.5, + -0.4330125, 0.25, -0.5, -0.9659, 0.2588, -0.0, 0.333333, 1.0, + -0.5, 0.0, 0.5, -0.9659, 0.2588, -0.0, 0.25, 0.5, + -0.4330125, 0.25, 0.5, -0.9659, 0.2588, -0.0, 0.333333, 0.5, + -0.5, 0.0, -0.5, -0.9659, -0.2588, -0.0, 0.25, 1.0, + -0.4330125, -0.25, 0.5, -0.9659, -0.2588, -0.0, 0.166667, 0.5, + -0.5, 0.0, 0.5, -0.9659, -0.2588, -0.0, 0.25, 0.5, + -0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.042154, 0.37, + 0.0, 0.5, -0.5, -0.0, -0.0, -1.0, 0.25, 0.01, + 0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.457846, 0.37, + -0.4330125, -0.25, -0.5, -0.7071, -0.7071, -0.0, 0.166667, 1.0, + -0.25, -0.4330125, 0.5, -0.7071, -0.7071, -0.0, 0.083333, 0.5, + -0.4330125, -0.25, 0.5, -0.7071, -0.7071, -0.0, 0.166667, 0.5, + -0.25, -0.4330125, -0.5, -0.2588, -0.9659, -0.0, 0.083333, 1.0, + 0.0, -0.5, 0.5, -0.2588, -0.9659, -0.0, 0.0, 0.5, + -0.25, -0.4330125, 0.5, -0.2588, -0.9659, -0.0, 0.083333, 0.5, + 0.5, 0.0, 0.5, -0.0, -0.0, 1.0, 0.99, 0.25, + -0.25, 0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.042154, + -0.25, -0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.457846, + 0.0, -0.5, -0.5, 0.2588, -0.9659, -0.0, 1.0, 1.0, + 0.25, -0.4330125, -0.5, 0.2588, -0.9659, -0.0, 0.916667, 1.0, + 0.25, -0.4330125, 0.5, 0.2588, -0.9659, -0.0, 0.916667, 0.5, + 0.25, -0.4330125, -0.5, 0.7071, -0.7071, -0.0, 0.916667, 1.0, + 0.4330125, -0.25, -0.5, 0.7071, -0.7071, -0.0, 0.833333, 1.0, + 0.4330125, -0.25, 0.5, 0.7071, -0.7071, -0.0, 0.833333, 0.5, + 0.4330125, -0.25, -0.5, 0.9659, -0.2588, -0.0, 0.833333, 1.0, + 0.5, 0.0, -0.5, 0.9659, -0.2588, -0.0, 0.75, 1.0, + 0.5, 0.0, 0.5, 0.9659, -0.2588, -0.0, 0.75, 0.5, + 0.5, 0.0, -0.5, 0.9659, 0.2588, -0.0, 0.75, 1.0, + 0.4330125, 0.25, -0.5, 0.9659, 0.2588, -0.0, 0.666667, 1.0, + 0.4330125, 0.25, 0.5, 0.9659, 0.2588, -0.0, 0.666667, 0.5, + 0.4330125, 0.25, -0.5, 0.7071, 0.7071, -0.0, 0.666667, 1.0, + 0.25, 0.4330125, -0.5, 0.7071, 0.7071, -0.0, 0.583333, 1.0, + 0.25, 0.4330125, 0.5, 0.7071, 0.7071, -0.0, 0.583333, 0.5, + 0.25, 0.4330125, -0.5, 0.2588, 0.9659, -0.0, 0.583333, 1.0, + 0.0, 0.5, -0.5, 0.2588, 0.9659, -0.0, 0.5, 1.0, + 0.0, 0.5, 0.5, 0.2588, 0.9659, -0.0, 0.5, 0.5, + 0.0, 0.5, -0.5, -0.2588, 0.9659, -0.0, 0.5, 1.0, + -0.25, 0.4330125, -0.5, -0.2588, 0.9659, -0.0, 0.416667, 1.0, + -0.25, 0.4330125, 0.5, -0.2588, 0.9659, -0.0, 0.416667, 0.5, + -0.25, 0.4330125, -0.5, -0.7071, 0.7071, -0.0, 0.416667, 1.0, + -0.4330125, 0.25, -0.5, -0.7071, 0.7071, -0.0, 0.333333, 1.0, + -0.4330125, 0.25, 0.5, -0.7071, 0.7071, -0.0, 0.333333, 0.5, + -0.4330125, 0.25, -0.5, -0.9659, 0.2588, -0.0, 0.333333, 1.0, + -0.5, 0.0, -0.5, -0.9659, 0.2588, -0.0, 0.25, 1.0, + -0.5, 0.0, 0.5, -0.9659, 0.2588, -0.0, 0.25, 0.5, + -0.5, 0.0, -0.5, -0.9659, -0.2588, -0.0, 0.25, 1.0, + -0.4330125, -0.25, -0.5, -0.9659, -0.2588, -0.0, 0.166667, 1.0, + -0.4330125, -0.25, 0.5, -0.9659, -0.2588, -0.0, 0.166667, 0.5, + 0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.457846, 0.37, + 0.25, -0.4330125, -0.5, -0.0, -0.0, -1.0, 0.37, 0.457846, + 0.0, -0.5, -0.5, -0.0, -0.0, -1.0, 0.25, 0.49, + 0.0, -0.5, -0.5, -0.0, -0.0, -1.0, 0.25, 0.49, + -0.25, -0.4330125, -0.5, -0.0, -0.0, -1.0, 0.13, 0.457846, + -0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.042154, 0.37, + -0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.042154, 0.37, + -0.5, 0.0, -0.5, -0.0, -0.0, -1.0, 0.01, 0.25, + -0.4330125, 0.25, -0.5, -0.0, -0.0, -1.0, 0.042154, 0.13, + -0.4330125, 0.25, -0.5, -0.0, -0.0, -1.0, 0.042154, 0.13, + -0.25, 0.4330125, -0.5, -0.0, -0.0, -1.0, 0.13, 0.042154, + -0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.042154, 0.37, + -0.25, 0.4330125, -0.5, -0.0, -0.0, -1.0, 0.13, 0.042154, + 0.0, 0.5, -0.5, -0.0, -0.0, -1.0, 0.25, 0.01, + -0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.042154, 0.37, + 0.0, 0.5, -0.5, -0.0, -0.0, -1.0, 0.25, 0.01, + 0.25, 0.4330125, -0.5, -0.0, -0.0, -1.0, 0.37, 0.042154, + 0.4330125, 0.25, -0.5, -0.0, -0.0, -1.0, 0.457846, 0.13, + 0.4330125, 0.25, -0.5, -0.0, -0.0, -1.0, 0.457846, 0.13, + 0.5, 0.0, -0.5, -0.0, -0.0, -1.0, 0.49, 0.25, + 0.0, 0.5, -0.5, -0.0, -0.0, -1.0, 0.25, 0.01, + 0.5, 0.0, -0.5, -0.0, -0.0, -1.0, 0.49, 0.25, + 0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.457846, 0.37, + 0.0, 0.5, -0.5, -0.0, -0.0, -1.0, 0.25, 0.01, + 0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.457846, 0.37, + 0.0, -0.5, -0.5, -0.0, -0.0, -1.0, 0.25, 0.49, + -0.4330125, -0.25, -0.5, -0.0, -0.0, -1.0, 0.042154, 0.37, + -0.4330125, -0.25, -0.5, -0.7071, -0.7071, -0.0, 0.166667, 1.0, + -0.25, -0.4330125, -0.5, -0.7071, -0.7071, -0.0, 0.083333, 1.0, + -0.25, -0.4330125, 0.5, -0.7071, -0.7071, -0.0, 0.083333, 0.5, + -0.25, -0.4330125, -0.5, -0.2588, -0.9659, -0.0, 0.083333, 1.0, + 0.0, -0.5, -0.5, -0.2588, -0.9659, -0.0, 0.0, 1.0, + 0.0, -0.5, 0.5, -0.2588, -0.9659, -0.0, 0.0, 0.5, + -0.25, -0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.457846, + 0.0, -0.5, 0.5, -0.0, -0.0, 1.0, 0.75, 0.49, + 0.25, -0.4330125, 0.5, -0.0, -0.0, 1.0, 0.87, 0.457846, + 0.25, -0.4330125, 0.5, -0.0, -0.0, 1.0, 0.87, 0.457846, + 0.4330125, -0.25, 0.5, -0.0, -0.0, 1.0, 0.957846, 0.37, + -0.25, -0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.457846, + 0.4330125, -0.25, 0.5, -0.0, -0.0, 1.0, 0.957846, 0.37, + 0.5, 0.0, 0.5, -0.0, -0.0, 1.0, 0.99, 0.25, + -0.25, -0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.457846, + 0.5, 0.0, 0.5, -0.0, -0.0, 1.0, 0.99, 0.25, + 0.4330125, 0.25, 0.5, -0.0, -0.0, 1.0, 0.957846, 0.13, + 0.25, 0.4330125, 0.5, -0.0, -0.0, 1.0, 0.87, 0.042154, + 0.25, 0.4330125, 0.5, -0.0, -0.0, 1.0, 0.87, 0.042154, + 0.0, 0.5, 0.5, -0.0, -0.0, 1.0, 0.75, 0.01, + 0.5, 0.0, 0.5, -0.0, -0.0, 1.0, 0.99, 0.25, + 0.0, 0.5, 0.5, -0.0, -0.0, 1.0, 0.75, 0.01, + -0.25, 0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.042154, + 0.5, 0.0, 0.5, -0.0, -0.0, 1.0, 0.99, 0.25, + -0.25, 0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.042154, + -0.4330125, 0.25, 0.5, -0.0, -0.0, 1.0, 0.542154, 0.13, + -0.5, 0.0, 0.5, -0.0, -0.0, 1.0, 0.51, 0.25, + -0.5, 0.0, 0.5, -0.0, -0.0, 1.0, 0.51, 0.25, + -0.4330125, -0.25, 0.5, -0.0, -0.0, 1.0, 0.542154, 0.37, + -0.25, -0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.457846, + -0.25, 0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.042154, + -0.5, 0.0, 0.5, -0.0, -0.0, 1.0, 0.51, 0.25, + -0.25, -0.4330125, 0.5, -0.0, -0.0, 1.0, 0.63, 0.457846, + +}; + Mesh* CUBE_MESH; Mesh* SPHERE_MESH; Mesh* ARROW_MESH; Mesh* OUTLINE_MESH; +Mesh* CYLINDER_MESH; void initMeshes() { CUBE_MESH = new Mesh(sizeof(CUBE_VERTICES) / sizeof(float) / 8, CUBE_VERTICES); SPHERE_MESH = new Mesh(sizeof(SPHERE_VERTICES) / sizeof(float) / 8, SPHERE_VERTICES); ARROW_MESH = new Mesh(sizeof(ARROW_VERTICES) / sizeof(float) / 8, ARROW_VERTICES); OUTLINE_MESH = new Mesh(sizeof(OUTLINE_VERTICES) / sizeof(float) / 8, OUTLINE_VERTICES); + CYLINDER_MESH = new Mesh(sizeof(CYLINDER_VERTICES) / sizeof(float) / 8, CYLINDER_VERTICES); } /* Python generator: diff --git a/core/src/rendering/defaultmeshes.h b/core/src/rendering/defaultmeshes.h index f5332ab..f5714b1 100644 --- a/core/src/rendering/defaultmeshes.h +++ b/core/src/rendering/defaultmeshes.h @@ -5,5 +5,6 @@ extern Mesh* CUBE_MESH; extern Mesh* SPHERE_MESH; extern Mesh* ARROW_MESH; extern Mesh* OUTLINE_MESH; +extern Mesh* CYLINDER_MESH; void initMeshes(); \ No newline at end of file diff --git a/core/src/rendering/renderer.cpp b/core/src/rendering/renderer.cpp index 75e32d6..032a93d 100644 --- a/core/src/rendering/renderer.cpp +++ b/core/src/rendering/renderer.cpp @@ -11,9 +11,11 @@ #include #include #include +#include #include #include "datatypes/cframe.h" +#include "datatypes/color3.h" #include "objects/handles.h" #include "rendering/torus.h" #include "shader.h" @@ -191,6 +193,58 @@ void renderParts() { } } +static Vector3 FACES[6] = { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1}, + {-1, 0, 0}, + {0, -1, 0}, + {0, 0, -1}, +}; + +void renderSurfaceExtras() { + glDepthMask(GL_TRUE); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glFrontFace(GL_CCW); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + // Use shader + ghostShader->use(); + + // view/projection transformations + glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f); + glm::mat4 view = camera.getLookAt(); + ghostShader->set("projection", projection); + ghostShader->set("view", view); + ghostShader->set("color", glm::vec3(0.87f, 0.87f, 0.0f)); + + // Pass in the camera position + ghostShader->set("viewPos", camera.cameraPos); + + for (auto it = gWorkspace()->GetDescendantsStart(); it != gWorkspace()->GetDescendantsEnd(); it++) { + InstanceRef inst = *it; + if (!inst->IsA("Part")) continue; + std::shared_ptr part = std::dynamic_pointer_cast(inst); + for (int i = 0; i < 6; i++) { + NormalId face = (NormalId)i; + SurfaceType type = part->GetSurfaceFromFace(face); + if (type <= SurfaceType::SurfaceUniversal) continue; + + Vector3 surfaceCenter = part->cframe * (normalFromFace(face) * part->size / 2.f); + + glm::mat4 model = CFrame::pointToward(surfaceCenter, part->cframe.Rotation() * normalFromFace(face)); + model = glm::scale(model, glm::vec3(0.4,0.4,0.4)); + ghostShader->set("model", model); + glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model))); + + CYLINDER_MESH->bind(); + glDrawArrays(GL_TRIANGLES, 0, CYLINDER_MESH->vertexCount); + } + } +} + void renderSkyBox() { glDepthMask(GL_FALSE); glCullFace(GL_FRONT); @@ -454,7 +508,7 @@ void renderRotationArcs() { } } -std::vector DEBUG_CFRAMES; +std::vector> DEBUG_CFRAMES; void renderDebugCFrames() { glDepthMask(GL_TRUE); @@ -480,12 +534,12 @@ void renderDebugCFrames() { // Pass in the camera position handleShader->set("viewPos", camera.cameraPos); - for (CFrame frame : DEBUG_CFRAMES) { + for (auto& [frame, color] : DEBUG_CFRAMES) { glm::mat4 model = frame; model = glm::scale(model, glm::vec3(0.5, 0.5, 1.5)); handleShader->set("model", model); handleShader->set("material", Material { - .diffuse = glm::vec3(0.0f, 0.0f, 1.0f), + .diffuse = color, .specular = glm::vec3(0.5f, 0.5f, 0.5f), .shininess = 16.0f, }); @@ -498,7 +552,11 @@ void renderDebugCFrames() { } void addDebugRenderCFrame(CFrame frame) { - DEBUG_CFRAMES.push_back(frame); + addDebugRenderCFrame(frame, Color3(0, 0, 1)); +} + +void addDebugRenderCFrame(CFrame frame, Color3 color) { + DEBUG_CFRAMES.push_back(std::make_pair(frame, color)); } void render(GLFWwindow* window) { @@ -509,6 +567,7 @@ void render(GLFWwindow* window) { renderHandles(); renderDebugCFrames(); renderParts(); + renderSurfaceExtras(); renderOutlines(); renderRotationArcs(); if (wireframeRendering) diff --git a/core/src/rendering/renderer.h b/core/src/rendering/renderer.h index 27d24d1..0cae941 100644 --- a/core/src/rendering/renderer.h +++ b/core/src/rendering/renderer.h @@ -3,9 +3,10 @@ extern bool wireframeRendering; -namespace Data { class CFrame; }; +namespace Data { class CFrame; class Color3; }; void renderInit(GLFWwindow* window, int width, int height); void render(GLFWwindow* window); void setViewport(int width, int height); -void addDebugRenderCFrame(Data::CFrame); \ No newline at end of file +void addDebugRenderCFrame(Data::CFrame); +void addDebugRenderCFrame(Data::CFrame, Data::Color3); \ No newline at end of file