Skip to content

Commit

Permalink
#5532: Working on the preview scene
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Mar 6, 2021
1 parent 6ddf6b4 commit 69a5bb5
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
108 changes: 107 additions & 1 deletion radiant/ui/materials/MaterialPreview.cpp
@@ -1,10 +1,46 @@
#include "MaterialPreview.h"

#include "ibrush.h"
#include "ientity.h"
#include "ieclass.h"
#include "wxutil/dialog/MessageBox.h"

namespace ui
{

namespace
{
const char* const FUNC_STATIC_CLASS = "func_static";

inline scene::INodePtr createCubicBrush(const scene::INodePtr& parent,
const Vector3& origin = Vector3(0, 0, 0),
const std::string& material = "_default")
{
auto brushNode = GlobalBrushCreator().createBrush();
parent->addChildNode(brushNode);

auto& brush = *Node_getIBrush(brushNode);

auto translation = Matrix4::getTranslation(origin);
brush.addFace(Plane3(+1, 0, 0, 64).transform(translation));
brush.addFace(Plane3(-1, 0, 0, 64).transform(translation));
brush.addFace(Plane3(0, +1, 0, 64).transform(translation));
brush.addFace(Plane3(0, -1, 0, 64).transform(translation));
brush.addFace(Plane3(0, 0, +1, 64).transform(translation));
brush.addFace(Plane3(0, 0, -1, 64).transform(translation));

brush.setShader(material);

brush.evaluateBRep();

return brushNode;
}
}

MaterialPreview::MaterialPreview(wxWindow* parent) :
RenderPreview(parent, true)
RenderPreview(parent, true),
_sceneIsReady(false),
_defaultCamDistanceFactor(2.8f)
{}

const MaterialPtr& MaterialPreview::getMaterial()
Expand All @@ -15,11 +51,81 @@ const MaterialPtr& MaterialPreview::getMaterial()
void MaterialPreview::setMaterial(const MaterialPtr& material)
{
_material = material;
_sceneIsReady = false;

if (_brush)
{
auto& brush = *Node_getIBrush(_brush);
for (auto i = 0; i < brush.getNumFaces(); ++i)
{
brush.getFace(i).setShader(_material ? _material->getName() : "");
brush.getFace(i).fitTexture(1, 1);
}
}

queueDraw();
}

bool MaterialPreview::onPreRender()
{
if (!_sceneIsReady)
{
prepareScene();
}

return RenderPreview::onPreRender();
}

void MaterialPreview::prepareScene()
{
_sceneIsReady = true;

if (!_brush) return;

// Reset the default view, facing down to the model from diagonally above the bounding box
double distance = _brush->localAABB().getRadius() * _defaultCamDistanceFactor;

setViewOrigin(Vector3(1, 1, 1) * distance);
setViewAngles(Vector3(34, 135, 0));
}

bool MaterialPreview::canDrawGrid()
{
return false;
}

void MaterialPreview::setupSceneGraph()
{
RenderPreview::setupSceneGraph();

try
{
_rootNode = std::make_shared<scene::BasicRootNode>();

_entity = GlobalEntityModule().createEntity(
GlobalEntityClassManager().findClass(FUNC_STATIC_CLASS));

_rootNode->addChildNode(_entity);

// Set up a brush
_brush = createCubicBrush(_entity);

getScene()->setRoot(_rootNode);

// Set up the light
_light = GlobalEntityModule().createEntity(
GlobalEntityClassManager().findClass("light"));

Node_getEntity(_light)->setKeyValue("light_radius", "600 600 600");
Node_getEntity(_light)->setKeyValue("origin", "0 0 300");

_rootNode->addChildNode(_light);
}
catch (std::runtime_error&)
{
wxutil::Messagebox::ShowError(fmt::format(_("Unable to setup the preview,\n"
"could not find the entity class {0}"), FUNC_STATIC_CLASS));
}
}

}
16 changes: 16 additions & 0 deletions radiant/ui/materials/MaterialPreview.h
@@ -1,6 +1,7 @@
#pragma once

#include "ishaders.h"
#include "scene/BasicRootNode.h"
#include "wxutil/preview/RenderPreview.h"

namespace ui
Expand All @@ -10,8 +11,18 @@ class MaterialPreview :
public wxutil::RenderPreview
{
private:
bool _sceneIsReady;

MaterialPtr _material;

scene::IMapRootNodePtr _rootNode;

scene::INodePtr _entity; // The func_static entity
scene::INodePtr _brush; // The textured brush
scene::INodePtr _light; // The light

float _defaultCamDistanceFactor;

public:
MaterialPreview(wxWindow* parent);

Expand All @@ -20,6 +31,11 @@ class MaterialPreview :

protected:
bool canDrawGrid() override;
void setupSceneGraph() override;

private:
bool onPreRender() override;
void prepareScene();
};

}

0 comments on commit 69a5bb5

Please sign in to comment.