204 lines
6.8 KiB
C++
204 lines
6.8 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "SceneDebugLine.hpp"
|
|
#include "display/shader/SimpleTexturedShader.hpp"
|
|
#include "scene/components/display/Camera.hpp"
|
|
#include "scene/Scene.hpp"
|
|
#include "scene/components/physics/3d/Collider3D.hpp"
|
|
#include "scene/components/physics/3d/CubeCollider.hpp"
|
|
#include "scene/components/physics/2d/Collider2D.hpp"
|
|
#include "scene/components/physics/2d/BoxCollider.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
struct ShaderPassItem SceneDebugLine::createShaderItem(
|
|
Mesh *mesh,
|
|
int32_t *lineIndex,
|
|
Camera *camera,
|
|
SimpleTexturedShader *shader
|
|
) {
|
|
struct ShaderPassItem item;
|
|
item.priority = this->priority;
|
|
|
|
item.shaderProgram = &shader->program;
|
|
item.colorValues[shader->program.paramColor] = this->color;
|
|
item.matrixValues[shader->program.paramModel] = this->transform;
|
|
item.matrixValues[shader->program.paramView] = camera->transform->getWorldTransform();
|
|
item.matrixValues[shader->program.paramProjection] = camera->getProjection();
|
|
item.boolValues[shader->program.paramHasTexture] = false;
|
|
|
|
auto i = *lineIndex;
|
|
item.mesh = mesh;
|
|
item.drawMode = MESH_DRAW_MODE_LINES;
|
|
item.renderFlags = RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST;
|
|
item.start = i * SCENE_DEBUG_LINE_INDICE_COUNT;
|
|
item.count = SCENE_DEBUG_LINE_INDICE_COUNT;
|
|
|
|
glm::vec3 positions[SCENE_DEBUG_LINE_VERTICE_COUNT] = { this->v0, this->v1 };
|
|
mesh->bufferPositions(
|
|
i * SCENE_DEBUG_LINE_VERTICE_COUNT,
|
|
positions,
|
|
SCENE_DEBUG_LINE_VERTICE_COUNT
|
|
);
|
|
|
|
meshindice_t indices[SCENE_DEBUG_LINE_INDICE_COUNT] = {
|
|
i * SCENE_DEBUG_LINE_VERTICE_COUNT,
|
|
(i*SCENE_DEBUG_LINE_VERTICE_COUNT) + 1
|
|
};
|
|
mesh->bufferIndices(
|
|
i * SCENE_DEBUG_LINE_INDICE_COUNT,
|
|
indices,
|
|
SCENE_DEBUG_LINE_INDICE_COUNT
|
|
);
|
|
return item;
|
|
}
|
|
|
|
// Scene Implementations (done here to keep things cleaner in scene)
|
|
void Scene::debugLine(struct SceneDebugLine line) {
|
|
this->debugLines.push_back(line);
|
|
}
|
|
|
|
void Scene::debugRay(struct SceneDebugRay ray) {
|
|
struct SceneDebugLine line;
|
|
line.v0 = ray.start;
|
|
line.v1 = ray.start + ray.direction;
|
|
line.color = ray.color;
|
|
this->debugLine(line);
|
|
}
|
|
|
|
void Scene::debugCube(struct SceneDebugCube cube) {
|
|
auto min = cube.min;
|
|
auto max = cube.max;
|
|
|
|
struct SceneDebugLine line;
|
|
line.color = cube.color;
|
|
line.transform = cube.transform;
|
|
|
|
// Bottom Face
|
|
line.v0 = glm::vec3(min.x, min.y, min.z), line.v1 = glm::vec3(max.x, min.y, min.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(min.x, min.y, min.z), line.v1 = glm::vec3(min.x, min.y, max.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(max.x, min.y, min.z), line.v1 = glm::vec3(max.x, min.y, max.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(min.x, min.y, max.z), line.v1 = glm::vec3(max.x, min.y, max.z);
|
|
this->debugLine(line);
|
|
|
|
// Top Face
|
|
line.v0 = glm::vec3(min.x, max.y, min.z), line.v1 = glm::vec3(max.x, max.y, min.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(min.x, max.y, min.z), line.v1 = glm::vec3(min.x, max.y, max.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(max.x, max.y, min.z), line.v1 = glm::vec3(max.x, max.y, max.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(min.x, max.y, max.z), line.v1 = glm::vec3(max.x, max.y, max.z);
|
|
this->debugLine(line);
|
|
|
|
// Rails
|
|
line.v0 = glm::vec3(min.x, min.y, min.z), line.v1 = glm::vec3(min.x, max.y, min.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(max.x, min.y, min.z), line.v1 = glm::vec3(max.x, max.y, min.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(min.x, min.y, max.z), line.v1 = glm::vec3(min.x, max.y, max.z);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(max.x, min.y, max.z), line.v1 = glm::vec3(max.x, max.y, max.z);
|
|
this->debugLine(line);
|
|
}
|
|
|
|
void Scene::debugOrigin() {
|
|
struct SceneDebugLine line;
|
|
line.v1 = glm::vec3(1, 0, 0), line.color = COLOR_RED;
|
|
this->debugLine(line);
|
|
line.v1 = glm::vec3(0, 1, 0), line.color = COLOR_GREEN;
|
|
this->debugLine(line);
|
|
line.v1 = glm::vec3(0, 0, 1), line.color = COLOR_BLUE;
|
|
this->debugLine(line);
|
|
}
|
|
|
|
void Scene::debugGrid() {
|
|
float_t s = 10.0f;
|
|
float_t t = 1.0f;
|
|
|
|
struct SceneDebugLine line;
|
|
line.color = COLOR_LIGHT_GREY;
|
|
line.color.a = 0.2f;
|
|
|
|
line.v0 = glm::vec3(-s, 0, -s), line.v1 = glm::vec3(s, 0, -s);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(-s, 0, -s), line.v1 = glm::vec3(-s, 0, s);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(s, 0, -s), line.v1 = glm::vec3(s, 0, s);
|
|
this->debugLine(line);
|
|
line.v0 = glm::vec3(-s, 0, s), line.v1 = glm::vec3(s, 0, s);
|
|
this->debugLine(line);
|
|
|
|
for(float_t x = -s+t; x < s; x += t) {
|
|
line.v0 = glm::vec3(x, 0, -s), line.v1 = glm::vec3(x, 0, s);
|
|
this->debugLine(line);
|
|
}
|
|
|
|
for(float_t z = -s+t; z < s; z += t) {
|
|
line.v0 = glm::vec3(-s, 0, z), line.v1 = glm::vec3(s, 0, z);
|
|
this->debugLine(line);
|
|
}
|
|
}
|
|
|
|
void Scene::debugHitboxes() {
|
|
auto colliders = this->findComponents<Collider3D>();
|
|
auto itColliders = colliders.begin();
|
|
while(itColliders != colliders.end()) {
|
|
auto c = *itColliders;
|
|
switch(c->getColliderType()) {
|
|
case COLLIDER3D_TYPE_CUBE:
|
|
auto asCube = dynamic_cast<CubeCollider*>(c);
|
|
struct SceneDebugCube c2;
|
|
c2.min = asCube->min;
|
|
c2.max = asCube->max;
|
|
c2.color = COLOR_BLUE;
|
|
c2.transform = asCube->transform->getWorldTransform();
|
|
|
|
this->debugCube(c2);
|
|
break;
|
|
}
|
|
++itColliders;
|
|
}
|
|
|
|
auto colliders2d = this->findComponents<Collider2D>();
|
|
auto itColliders2 = colliders2d.begin();
|
|
while(itColliders2 != colliders2d.end()) {
|
|
auto c = *itColliders2;
|
|
switch(c->getColliderType()) {
|
|
case COLLIDER2D_TYPE_BOX:
|
|
auto asBox = dynamic_cast<BoxCollider*>(c);
|
|
this->debugLine({
|
|
.v0 = glm::vec3(asBox->min.x, 0, asBox->min.y),
|
|
.v1 = glm::vec3(asBox->max.x, 0, asBox->min.y),
|
|
.color = COLOR_BLUE,
|
|
.transform = c->transform->getWorldTransform()
|
|
});
|
|
this->debugLine({
|
|
.v0 = glm::vec3(asBox->max.x, 0, asBox->min.y),
|
|
.v1 = glm::vec3(asBox->max.x, 0, asBox->max.y),
|
|
.color = COLOR_BLUE,
|
|
.transform = c->transform->getWorldTransform()
|
|
});
|
|
this->debugLine({
|
|
.v0 = glm::vec3(asBox->max.x, 0, asBox->max.y),
|
|
.v1 = glm::vec3(asBox->min.x, 0, asBox->max.y),
|
|
.color = COLOR_BLUE,
|
|
.transform = c->transform->getWorldTransform()
|
|
});
|
|
this->debugLine({
|
|
.v0 = glm::vec3(asBox->min.x, 0, asBox->max.y),
|
|
.v1 = glm::vec3(asBox->min.x, 0, asBox->min.y),
|
|
.color = COLOR_BLUE,
|
|
.transform = c->transform->getWorldTransform()
|
|
});
|
|
break;
|
|
}
|
|
++itColliders2;
|
|
}
|
|
} |