feat(renderer): added cylinder for hinges

This commit is contained in:
maelstrom 2025-04-30 17:10:00 +02:00
parent d100932a9e
commit dcc1f64354
6 changed files with 210 additions and 7 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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:

View file

@ -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();

View file

@ -11,9 +11,11 @@
#include <glm/gtc/quaternion.hpp>
#include <glm/trigonometric.hpp>
#include <memory>
#include <utility>
#include <vector>
#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> part = std::dynamic_pointer_cast<Part>(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<CFrame> DEBUG_CFRAMES;
std::vector<std::pair<CFrame, Color3>> 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)

View file

@ -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);
void addDebugRenderCFrame(Data::CFrame, Data::Color3);