Compare commits

...

3 commits

7 changed files with 159 additions and 68 deletions

View file

@ -6,6 +6,7 @@
#include "objects/joint/rotatev.h"
#include "objects/joint/weld.h"
#include "objects/message.h"
#include "objects/part/wedgepart.h"
#include "objects/service/jointsservice.h"
#include "objects/model.h"
#include "objects/part/part.h"
@ -23,6 +24,7 @@ std::map<std::string, const InstanceType*> INSTANCE_MAP = {
{ "BasePart", &BasePart::TYPE },
{ "Part", &Part::TYPE },
{ "WedgePart", &WedgePart::TYPE },
{ "Snap", &Snap::TYPE },
{ "Weld", &Weld::TYPE },
{ "Rotate", &Rotate::TYPE },

View file

@ -19,7 +19,7 @@
#include <memory>
#include <optional>
BasePart::BasePart(const InstanceType* type): BasePart(type, PartConstructParams { .size = glm::vec3(2, 1.2, 4), .color = Color3(0.639216f, 0.635294f, 0.647059f) }) {
BasePart::BasePart(const InstanceType* type): BasePart(type, PartConstructParams { .size = glm::vec3(4, 1.2, 2), .color = Color3(0.639216f, 0.635294f, 0.647059f) }) {
}
BasePart::BasePart(const InstanceType* type, PartConstructParams params): PVInstance(type), cframe(CFrame::FromEulerAnglesXYZ((Vector3)params.rotation) + params.position),

View file

@ -0,0 +1,91 @@
#include "wedgepart.h"
#include "physics/util.h"
#include <reactphysics3d/collision/ConvexMesh.h>
#include <reactphysics3d/collision/shapes/ConvexMeshShape.h>
rp::ConvexMesh* wedgePhysMesh;
WedgePart::WedgePart(): BasePart(&TYPE) {
}
WedgePart::WedgePart(PartConstructParams params): BasePart(&TYPE, params) {
}
void WedgePart::updateCollider(rp::PhysicsCommon* common) {
rp::ConvexMeshShape* shape = common->createConvexMeshShape(wedgePhysMesh, glmToRp(size * glm::vec3(0.5f)));
// Recreate the rigidbody if the shape changes
if (rigidBody->getNbColliders() > 0
&& dynamic_cast<rp::ConvexMeshShape*>(rigidBody->getCollider(0)->getCollisionShape())->getScale() != shape->getScale()) {
// TODO: This causes Touched to get called twice. Fix this.
rigidBody->removeCollider(rigidBody->getCollider(0));
rigidBody->addCollider(shape, rp::Transform());
}
if (rigidBody->getNbColliders() == 0)
rigidBody->addCollider(shape, rp::Transform());
}
void WedgePart::createWedgeShape(rp::PhysicsCommon* common) {
// https://www.reactphysics3d.com/documentation/index.html#creatingbody
float vertices[] = {
// X Y Z
/*0*/ -1, 1, 1, // 0
/*1*/ -1, -1, 1, // |
/*2*/ -1, -1, -1, // 1---2
/*3*/ 1, 1, 1,
/*4*/ 1, -1, 1,
/*5*/ 1, -1, -1,
};
// -x +x
// +z 1----------4
// | bottom |
// -z 2----------5
// -x +x
// +y 0----------3
// | front |
// -y 1----------4
// -x +x
// +yz 0----------3
// | slope |
// -yz 2----------5
int indices[] = {
// Base
1, 2, 5, 4,
// Back-face
0, 1, 4, 3,
// 4, 1, 0, 3,
// Slope
0, 2, 5, 3,
// 3, 5, 2, 0,
// Sides
0, 1, 2,
3, 4, 5,
};
// Description of the six faces of the convex mesh
rp::PolygonVertexArray::PolygonFace* polygonFaces = new rp::PolygonVertexArray::PolygonFace[5];
polygonFaces[0] = { 4, 0 }; // Bottom
polygonFaces[1] = { 4, 4 }; // Front
polygonFaces[2] = { 4, 8 }; // Slope
polygonFaces[3] = { 3, 12 }; // Side
polygonFaces[4] = { 3, 15 }; // Side
// Create the polygon vertex array
rp::PolygonVertexArray polygonVertexArray(6, vertices, 3 * sizeof(float), indices, sizeof(int), 5, polygonFaces,
rp::PolygonVertexArray::VertexDataType::VERTEX_FLOAT_TYPE,
rp::PolygonVertexArray::IndexDataType::INDEX_INTEGER_TYPE);
// Create the convex mesh
std::vector<rp3d::Message> messages;
wedgePhysMesh = common->createConvexMesh(polygonVertexArray, messages);
}

View file

@ -0,0 +1,21 @@
#pragma once
#include "basepart.h"
#include "objects/annotation.h"
class DEF_INST WedgePart : public BasePart {
AUTOGEN_PREAMBLE
protected:
void updateCollider(rp::PhysicsCommon* common) override;
static void createWedgeShape(rp::PhysicsCommon* common);
friend Workspace;
public:
WedgePart();
WedgePart(PartConstructParams params);
static inline std::shared_ptr<WedgePart> New() { return std::make_shared<WedgePart>(); };
static inline std::shared_ptr<WedgePart> New(PartConstructParams params) { return std::make_shared<WedgePart>(params); };
static inline std::shared_ptr<Instance> Create() { return std::make_shared<WedgePart>(); };
};

View file

@ -5,6 +5,7 @@
#include "logger.h"
#include "objects/base/instance.h"
#include "objects/part/part.h"
#include "objects/part/wedgepart.h"
#include "objects/service/jointsservice.h"
#include "objects/joint/jointinstance.h"
#include "objects/datamodel.h"
@ -82,6 +83,9 @@ void Workspace::InitService() {
physicsWorld->setEventListener(&physicsEventListener);
// Create meshes
WedgePart::createWedgeShape(physicsCommon);
// Sync all parts
for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) {
std::shared_ptr<Instance> obj = *it;

View file

@ -2,6 +2,7 @@
#include "mesh.h"
extern Mesh* CUBE_MESH;
extern Mesh* WEDGE_MESH;
extern Mesh* SPHERE_MESH;
extern Mesh* ARROW_MESH;
extern Mesh* OUTLINE_MESH;

View file

@ -22,6 +22,7 @@
#include "math_helper.h"
#include "objects/hint.h"
#include "objects/message.h"
#include "objects/part/wedgepart.h"
#include "objects/service/selection.h"
#include "partassembly.h"
#include "rendering/font.h"
@ -121,30 +122,54 @@ void renderInit(int width, int height) {
sansSerif = loadFont("LiberationSans-Regular.ttf");
}
static void renderPart(std::shared_ptr<BasePart> part) {
glm::mat4 model = part->cframe;
// if (part->name == "camera") model = camera.getLookAt();
model = glm::scale(model, (glm::vec3)part->size);
shader->set("model", model);
shader->set("material", Material {
.diffuse = part->color,
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 16.0f,
});
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
shader->set("normalMatrix", normalMatrix);
shader->set("texScale", part->size);
shader->set("transparency", part->transparency);
shader->set("surfaces[" + std::to_string(NormalId::Right) + "]", (int)part->rightSurface);
shader->set("surfaces[" + std::to_string(NormalId::Top) + "]", (int)part->topSurface);
shader->set("surfaces[" + std::to_string(NormalId::Back) + "]", (int)part->backSurface);
shader->set("surfaces[" + std::to_string(NormalId::Left) + "]", (int)part->leftSurface);
shader->set("surfaces[" + std::to_string(NormalId::Bottom) + "]", (int)part->bottomSurface);
shader->set("surfaces[" + std::to_string(NormalId::Front) + "]", (int)part->frontSurface);
if (part->IsA<WedgePart>()) {
glFrontFace(GL_CCW);
WEDGE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, WEDGE_MESH->vertexCount);
} else {
glFrontFace(GL_CW);
CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, CUBE_MESH->vertexCount);
}
}
void renderParts() {
glDepthMask(GL_TRUE);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Use shader
shader->use();
// shader->set("objectColor", glm::vec3(1.0f, 0.5f, 0.31f));
// shader->set("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
// 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();
shader->set("projection", projection);
shader->set("view", view);
// shader->set("material", Material {
// // .ambient = glm::vec3(1.0f, 0.5f, 0.31f),
// .diffuse = glm::vec3(0.639216f, 0.635294f, 0.647059f),
// .specular = glm::vec3(0.5f, 0.5f, 0.5f),
// .shininess = 16.0f,
// });
shader->set("sunLight", DirLight {
.direction = glm::vec3(-0.2f, -1.0f, -0.3f),
.ambient = glm::vec3(0.2f, 0.2f, 0.2f),
@ -152,15 +177,6 @@ void renderParts() {
.specular = glm::vec3(1.0f, 1.0f, 1.0f),
});
shader->set("numPointLights", 0);
// shader->set("pointLights[0]", PointLight {
// .position = lightPos,
// .ambient = glm::vec3(0.4f, 0.4f, 0.4f),
// .diffuse = glm::vec3(1.0f, 1.0f, 1.0f),
// .specular = glm::vec3(1.0f, 1.0f, 1.0f),
// .constant = 1.0,
// .linear = 0.9,
// .quadratic = 0.32,
// });
studsTexture->activate(0);
shader->set("studs", 0);
@ -172,36 +188,14 @@ void renderParts() {
// Sort by nearest
std::map<float, std::shared_ptr<BasePart>> sorted;
for (auto it = gWorkspace()->GetDescendantsStart(); it != gWorkspace()->GetDescendantsEnd(); it++) {
std::shared_ptr<Instance> inst = *it;
if (inst->GetClass()->className != "Part") continue;
std::shared_ptr<BasePart> part = std::dynamic_pointer_cast<BasePart>(inst);
if (!it->IsA<BasePart>()) continue;
std::shared_ptr<BasePart> part = std::dynamic_pointer_cast<BasePart>(*it);
if (part->transparency > 0.00001) {
float distance = glm::length(glm::vec3(Vector3(camera.cameraPos) - part->position()));
sorted[distance] = part;
} else {
glm::mat4 model = part->cframe;
// if (part->name == "camera") model = camera.getLookAt();
model = glm::scale(model, (glm::vec3)part->size);
shader->set("model", model);
shader->set("material", Material {
.diffuse = part->color,
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 16.0f,
});
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
shader->set("normalMatrix", normalMatrix);
shader->set("texScale", part->size);
shader->set("transparency", part->transparency);
shader->set("surfaces[" + std::to_string(NormalId::Right) + "]", (int)part->rightSurface);
shader->set("surfaces[" + std::to_string(NormalId::Top) + "]", (int)part->topSurface);
shader->set("surfaces[" + std::to_string(NormalId::Back) + "]", (int)part->backSurface);
shader->set("surfaces[" + std::to_string(NormalId::Left) + "]", (int)part->leftSurface);
shader->set("surfaces[" + std::to_string(NormalId::Bottom) + "]", (int)part->bottomSurface);
shader->set("surfaces[" + std::to_string(NormalId::Front) + "]", (int)part->frontSurface);
CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, CUBE_MESH->vertexCount);
renderPart(part);
}
}
@ -209,29 +203,7 @@ void renderParts() {
// According to LearnOpenGL, std::map automatically sorts its contents.
for (std::map<float, std::shared_ptr<BasePart>>::reverse_iterator it = sorted.rbegin(); it != sorted.rend(); it++) {
std::shared_ptr<BasePart> part = it->second;
glm::mat4 model = part->cframe;
// if (part->name == "camera") model = camera.getLookAt();
model = glm::scale(model, (glm::vec3)part->size);
shader->set("model", model);
shader->set("material", Material {
.diffuse = part->color,
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 16.0f,
});
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
shader->set("normalMatrix", normalMatrix);
shader->set("texScale", part->size);
shader->set("transparency", part->transparency);
shader->set("surfaces[" + std::to_string(NormalId::Right) + "]", (int)part->rightSurface);
shader->set("surfaces[" + std::to_string(NormalId::Top) + "]", (int)part->topSurface);
shader->set("surfaces[" + std::to_string(NormalId::Back) + "]", (int)part->backSurface);
shader->set("surfaces[" + std::to_string(NormalId::Left) + "]", (int)part->leftSurface);
shader->set("surfaces[" + std::to_string(NormalId::Bottom) + "]", (int)part->bottomSurface);
shader->set("surfaces[" + std::to_string(NormalId::Front) + "]", (int)part->frontSurface);
CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, CUBE_MESH->vertexCount);
renderPart(part);
}
}