[CORE][CHG] Change boundary APIs

- Add 'updateBoundary' func to update boundary after transformation
- Add 'hit' to get the hit record of a ray
This commit is contained in:
Linloir 2022-12-18 16:07:58 +08:00
parent 4732c0810c
commit 308dd81f06
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
2 changed files with 31 additions and 37 deletions

View File

@ -2,9 +2,13 @@
#include "renderable.h"
Renderable::Renderable(Model* model) : _model(model) {}
Renderable::Renderable(Model* model) : _model(model) {
_boundary = model->boundBox();
}
Renderable::Renderable(Model* model, glm::vec3 position) : _model(model), _position(position) {}
Renderable::Renderable(Model* model, glm::vec3 position) : _model(model), _position(position) {
_boundary = model->boundBox();
}
Renderable::~Renderable() {
if (_light != nullptr) {
@ -15,6 +19,7 @@ Renderable::~Renderable() {
void Renderable::setModel(Model* model) {
_model = model;
_boundary = model->boundBox();
}
void Renderable::move(glm::vec3 deltaVec) {
@ -67,27 +72,24 @@ void Renderable::render(ShaderProgram shader) {
_model->render(shader);
}
// check here to get global boundary
// must check before get boundary
void Renderable::checkBoundary() {
if (_model == nullptr) {
return;
}
std::vector<glm::vec3> temp = {_model->upperBoundVex(),_model->lowerBoundVex()};
_lowerBoundVex = glm::vec3(3e36, 3e36, 3e36);
_upperBoundVex = -_lowerBoundVex;
auto model = this->modelMatrix(); // transform matrix
// Bit calculation
for (int i = 0; i < 8; i++) {
glm::vec4 vx = glm::vec4(temp[(i & 4) >> 2][0], temp[(i & 2)>>1][1], temp[i & 1][2], 1.0f);
auto vex = model * vx; // Transformed vertex position
for (int j = 0; j < 3; j++) {
_lowerBoundVex[j] = _lowerBoundVex[j] < vex[j] ? _lowerBoundVex[j] : vex[j];
_upperBoundVex[j] = _upperBoundVex[j] > vex[j] ? _upperBoundVex[j] : vex[j];
void Renderable::updateBoundary() {
// Traverse every vertex in the transferred model and update the boundary
Boundary newBoundary;
for (auto& mesh : _model->meshes()) {
for (auto& vertex : mesh.vertices()) {
glm::vec4 transformedVertex = modelMatrix() * glm::vec4(vertex._position, 1.0f);
newBoundary.updateControlPoints(glm::vec3(transformedVertex));
}
}
}
HitRecord Renderable::hit(const Ray& ray) const {
// First test if the ray hits the boundary box
if (!_boundary.hit(ray)) {
return HitRecord();
}
else {
// If the ray hits the boundary box
return _model->hit(ray.toLocalSpace(modelMatrix()));
}
}

View File

@ -6,6 +6,8 @@
#include "model.h"
#include "illuminer.h"
#include "shader.h"
#include "boundary.h"
#include "hitrecord.h"
class Renderable {
public:
@ -22,8 +24,7 @@ private:
glm::vec3 _position = glm::vec3(0.0f);
glm::mat4 _rotation = glm::mat4(1.0f);
glm::vec3 _scale = glm::vec3(1.0f);
glm::vec3 _lowerBoundVex;
glm::vec3 _upperBoundVex;
Boundary _boundary; // the renderable's boudary box, should be updated after a transformation is done
public:
Renderable(Model* model);
@ -46,12 +47,10 @@ public:
inline glm::mat4 modelMatrix() const;
inline glm::vec3 upperBoundVex()const;
inline glm::vec3 lowerBoundVex()const;
public:
void updateBoundary(); // update the boundary of the renderable after a transformation is done
HitRecord hit(const Ray& ray) const; // test the hit record of an input array
void render(ShaderProgram shader);
void checkBoundary();
};
inline glm::mat4 Renderable::modelMatrix() const {
@ -61,10 +60,3 @@ inline glm::mat4 Renderable::modelMatrix() const {
model = glm::scale(model, _scale);
return model;
}
inline glm::vec3 Renderable::lowerBoundVex() const {
return _lowerBoundVex;
}
inline glm::vec3 Renderable::upperBoundVex() const {
return _upperBoundVex;
}