[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(); ~ModelAttributeSlide();
private: private:
QHBoxLayout* _stretchLayout; QHBoxLayout* _stretchLayout = nullptr;
QLabel* _label; QLabel* _label = nullptr;
QLabel* _val; QLabel* _val = nullptr;
Slider* _slider; Slider* _slider = nullptr;
public: public:
// Getter APIs // Getter APIs

View File

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

View File

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

View File

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

View File

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

View File

@ -298,7 +298,8 @@ void SettingPage::selectTerrain() {
} }
QDir terrainDir(dir); 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 ( if (
terrainFiles.indexOf("heightmap.png") == -1 || terrainFiles.indexOf("heightmap.png") == -1 ||
terrainFiles.indexOf("texture.jpg") == -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); //textureID = loadTexture2(texName, GL_REPEAT, GL_REPEAT, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
tex = loadTexture(path + "/grass.jpg"); tex = loadTexture(path + "/texture.jpg");
} }