[CORE][FIX] Null safety

- prevent hit test to null terrain
- prevent not initialized model (wrong path)
This commit is contained in:
Linloir 2022-12-19 22:17:02 +08:00
parent a5e071f4dd
commit 36225d7ea3
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
7 changed files with 54 additions and 45 deletions

View File

@ -15,10 +15,10 @@ public:
~ModelAttributeSlide();
private:
QHBoxLayout* _stretchLayout;
QLabel* _label;
QLabel* _val;
Slider* _slider;
QHBoxLayout* _stretchLayout = nullptr;
QLabel* _label = nullptr;
QLabel* _val = nullptr;
Slider* _slider = nullptr;
public:
// Getter APIs

View File

@ -20,7 +20,7 @@ public:
private:
// Data
Model* _model;
Model* _model = nullptr;
// UI control variables
const QColor _borderColor = QColor(58, 143, 183);

View File

@ -42,7 +42,13 @@ void ModelSelector::addNewObject() {
return;
}
Logger::info("Loading model file from " + path.toStdString());
Model* model = loadObject(path);
if (model->status() != Model::LOADED) {
delete model;
Logger::error("Invalid model file");
return;
}
ModelSelectable* newSelectable = new ModelSelectable(model, this);
_objectSelectables.push_back(newSelectable);

View File

@ -19,39 +19,39 @@ public:
private:
// UI Elemenets
QHBoxLayout* _mainLayout;
QHBoxLayout* _mainLayout = nullptr;
QWidget* _objectSettingPanel;
QVBoxLayout* _objectSettingLayout;
QWidget* _objectSettingPanel = nullptr;
QVBoxLayout* _objectSettingLayout = nullptr;
QWidget* _lightSettingButtons;
QVBoxLayout* _lightSettingsButtonsLayout;
QWidget* _lightSettingButtons = nullptr;
QVBoxLayout* _lightSettingsButtonsLayout = nullptr;
QWidget* _lightSettingPanel;
QVBoxLayout* _lightSettingLayout;
QWidget* _lightSettingPanel = nullptr;
QVBoxLayout* _lightSettingLayout = nullptr;
QWidget* _lightColorSettingPanel;
QVBoxLayout* _lightColorSettingPanelLayout;
QWidget* _lightColorSettingPanel = nullptr;
QVBoxLayout* _lightColorSettingPanelLayout = nullptr;
ModelAttributeSlide* _scale;
ModelAttributeSlide* _rotateX;
ModelAttributeSlide* _rotateY;
ModelAttributeSlide* _rotateZ;
ModelAttributeSlide* _lightDistance;
ModelAttributeSlide* _lightRotateTheta;
ModelAttributeSlide* _lightRotatePhi;
ModelAttributeSlide* _lightCutoffAngle;
ModelAttributeSlide* _lightR;
ModelAttributeSlide* _lightG;
ModelAttributeSlide* _lightB;
QWidget* _lightSwitchPanel;
QVBoxLayout* _lightSwitchLayout;
PushButton* _lightSwitch;
QLabel* _lightSwitchIcon;
PushButton* _lightColorPanel;
QLabel* _lightColorPanelIcon;
PushButton* _deleteBtn;
QLabel* _deleteIcon;
ModelAttributeSlide* _scale = nullptr;
ModelAttributeSlide* _rotateX = nullptr;
ModelAttributeSlide* _rotateY = nullptr;
ModelAttributeSlide* _rotateZ = nullptr;
ModelAttributeSlide* _lightDistance = nullptr;
ModelAttributeSlide* _lightRotateTheta = nullptr;
ModelAttributeSlide* _lightRotatePhi = nullptr;
ModelAttributeSlide* _lightCutoffAngle = nullptr;
ModelAttributeSlide* _lightR = nullptr;
ModelAttributeSlide* _lightG = nullptr;
ModelAttributeSlide* _lightB = nullptr;
QWidget* _lightSwitchPanel = nullptr;
QVBoxLayout* _lightSwitchLayout = nullptr;
PushButton* _lightSwitch = nullptr;
QLabel* _lightSwitchIcon = nullptr;
PushButton* _lightColorPanel = nullptr;
QLabel* _lightColorPanelIcon = nullptr;
PushButton* _deleteBtn = nullptr;
QLabel* _deleteIcon = nullptr;
// State
Renderable* _object = nullptr;

View File

@ -92,16 +92,18 @@ Renderable* SceneViewer::hitTest(const Ray& ray) {
}
}
// Terrain hit test
HitRecord hitRecord = _terrain->hit(ray);
if (hitRecord.hitted()) {
Logger::debug("Hitted terrain");
}
else {
Logger::debug("Missed terrain");
}
if (hitRecord.hitted() && hitRecord.t() < newRecord.t()) {
newRecord = hitRecord;
newObject = nullptr;
if (_terrain != nullptr) {
HitRecord hitRecord = _terrain->hit(ray);
if (hitRecord.hitted()) {
Logger::debug("Hitted terrain");
}
else {
Logger::debug("Missed terrain");
}
if (hitRecord.hitted() && hitRecord.t() < newRecord.t()) {
newRecord = hitRecord;
newObject = nullptr;
}
}
_hitRecord = newRecord;
return newObject;

View File

@ -298,7 +298,8 @@ void SettingPage::selectTerrain() {
}
QDir terrainDir(dir);
QStringList terrainFiles = terrainDir.entryList(QStringList() << "*.jpg", QDir::Files);
// filter *.jpg and *.png
QStringList terrainFiles = terrainDir.entryList(QStringList() << "*.jpg" << "*.png", QDir::Files);
if (
terrainFiles.indexOf("heightmap.png") == -1 ||
terrainFiles.indexOf("texture.jpg") == -1

View File

@ -72,7 +72,7 @@ Terrain::Terrain(std::string path){
//textureID = loadTexture2(texName, GL_REPEAT, GL_REPEAT, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
tex = loadTexture(path + "/grass.jpg");
tex = loadTexture(path + "/texture.jpg");
}