fix bug and add test code

This commit is contained in:
ayachi3 2022-12-13 20:09:50 +08:00
parent 13bd34973c
commit cef83fe19e
3 changed files with 26 additions and 5 deletions

View File

@ -2,7 +2,7 @@
#include "renderable.h" #include "renderable.h"
// 极值点一定在model包围盒的顶点取到 // 极值点一定在model包围盒的顶点取到
Renderable::Renderable(Model* model) : _model(model) {} Renderable::Renderable(Model* model) : _model(model) {}
@ -40,16 +40,17 @@ void Renderable::render(ShaderProgram shader) {
} }
// check here to get global boundary // check here to get global boundary
// must check before get boundary
void Renderable::check_boundary() { void Renderable::check_boundary() {
std::vector<glm::vec3> temp = { _lower_bound,_upper_bound }; std::vector<glm::vec3> temp = {_model->get_upper_bound(),_model->get_lower_bound()};
_lower_bound = glm::vec3(3e36, 3e36, 3e36); _lower_bound = glm::vec3(3e36, 3e36, 3e36);
_upper_bound = -_lower_bound; _upper_bound = -_lower_bound;
auto model = this->modelMatrix();// 变换矩阵 auto model = this->modelMatrix();// 变换矩阵
// 位运算 从000到111的每一种组合 // 位运算 从000到111的每一种组合
for (int i = 0; i < 7; i++) { for (int i = 0; i < 8; i++) {
//遍历每一个顶点 //遍历每一个顶点
glm::vec4 vex = glm::vec4(temp[i & 4][0], temp[i & 2][1], temp[i & 1][2],1.0f); glm::vec4 vx = glm::vec4(temp[(i & 4) >> 2][0], temp[(i & 2)>>1][1], temp[i & 1][2], 1.0f);
vex = model * vex; // 经过变化之后的点的坐标 auto vex = model * vx; // 经过变化之后的点的坐标
for (int j = 0; j < 3; j++) { for (int j = 0; j < 3; j++) {
_lower_bound[j] = _lower_bound[j] < vex[j] ? _lower_bound[j] : vex[j]; _lower_bound[j] = _lower_bound[j] < vex[j] ? _lower_bound[j] : vex[j];
_upper_bound[j] = _upper_bound[j] > vex[j] ? _upper_bound[j] : vex[j]; _upper_bound[j] = _upper_bound[j] > vex[j] ? _upper_bound[j] : vex[j];

View File

@ -30,6 +30,9 @@ public:
inline glm::mat4 modelMatrix() const; inline glm::mat4 modelMatrix() const;
inline glm::vec3 get_upper_boundary()const;
inline glm::vec3 get_lower_boundary()const;
public: public:
void render(ShaderProgram shader); void render(ShaderProgram shader);
// check here to get global boundary // check here to get global boundary
@ -39,7 +42,18 @@ public:
inline glm::mat4 Renderable::modelMatrix() const { inline glm::mat4 Renderable::modelMatrix() const {
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, _position); model = glm::translate(model, _position);
// 左乘与右乘应该是不一样的,但是这里的表现相同
model = model * _rotation; model = model * _rotation;
//model = _rotation * model;
model = glm::scale(model, _scale); model = glm::scale(model, _scale);
return model; return model;
}
inline glm::vec3 Renderable::get_lower_boundary()const {
return _lower_bound;
}
inline glm::vec3 Renderable::get_upper_boundary()const {
return _upper_bound;
} }

View File

@ -86,6 +86,12 @@ void SceneViewer::initializeGL() {
Renderable renderable(backpackModel); Renderable renderable(backpackModel);
_objects.push_back(backpackModel); _objects.push_back(backpackModel);
//renderable.scale(1);
//renderable.rotate(glm::vec3(1, 0, 0), 2);
//renderable.check_boundary();
//auto t1 = renderable.get_lower_boundary();
//auto t2 = renderable.get_upper_boundary();
_camera.setPosition(glm::vec3(0.0f, 15.0f, 8.0f)); _camera.setPosition(glm::vec3(0.0f, 15.0f, 8.0f));
_camera.setYaw(-90.0f); _camera.setYaw(-90.0f);
_camera.setPitch(-30.0f); _camera.setPitch(-30.0f);