mirror of
https://github.com/overte-org/overte.git
synced 2025-08-04 04:23:33 +02:00
Update grid overlay to use new renderGrid, sane properties
This commit is contained in:
parent
f79fc30b83
commit
0c1353e6cc
2 changed files with 70 additions and 54 deletions
|
@ -13,6 +13,9 @@
|
|||
|
||||
#include <QScriptValue>
|
||||
|
||||
#include <avatar/AvatarManager.h>
|
||||
#include <avatar/MyAvatar.h>
|
||||
|
||||
#include <DependencyManager.h>
|
||||
#include <GeometryCache.h>
|
||||
#include <PathUtils.h>
|
||||
|
@ -20,17 +23,19 @@
|
|||
|
||||
|
||||
QString const Grid3DOverlay::TYPE = "grid";
|
||||
const float DEFAULT_SCALE = 100.0f;
|
||||
|
||||
Grid3DOverlay::Grid3DOverlay() :
|
||||
_minorGridWidth(1.0),
|
||||
_majorGridEvery(5) {
|
||||
Grid3DOverlay::Grid3DOverlay() {
|
||||
setDimensions(DEFAULT_SCALE);
|
||||
updateGrid();
|
||||
}
|
||||
|
||||
Grid3DOverlay::Grid3DOverlay(const Grid3DOverlay* grid3DOverlay) :
|
||||
Planar3DOverlay(grid3DOverlay),
|
||||
_minorGridWidth(grid3DOverlay->_minorGridWidth),
|
||||
_majorGridEvery(grid3DOverlay->_majorGridEvery)
|
||||
_majorGridEvery(grid3DOverlay->_majorGridEvery),
|
||||
_minorGridEvery(grid3DOverlay->_minorGridEvery)
|
||||
{
|
||||
updateGrid();
|
||||
}
|
||||
|
||||
void Grid3DOverlay::render(RenderArgs* args) {
|
||||
|
@ -38,15 +43,8 @@ void Grid3DOverlay::render(RenderArgs* args) {
|
|||
return; // do nothing if we're not visible
|
||||
}
|
||||
|
||||
const int MINOR_GRID_DIVISIONS = 200;
|
||||
const int MAJOR_GRID_DIVISIONS = 100;
|
||||
const float MAX_COLOR = 255.0f;
|
||||
|
||||
// center the grid around the camera position on the plane
|
||||
glm::vec3 rotated = glm::inverse(getRotation()) * args->_viewFrustum->getPosition();
|
||||
|
||||
float spacing = _minorGridWidth;
|
||||
|
||||
float alpha = getAlpha();
|
||||
xColor color = getColor();
|
||||
glm::vec4 gridColor(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha);
|
||||
|
@ -54,72 +52,68 @@ void Grid3DOverlay::render(RenderArgs* args) {
|
|||
auto batch = args->_batch;
|
||||
|
||||
if (batch) {
|
||||
auto minCorner = glm::vec2(-0.5f, -0.5f);
|
||||
auto maxCorner = glm::vec2(0.5f, 0.5f);
|
||||
|
||||
auto position = getPosition();
|
||||
if (_followCamera) {
|
||||
auto avatar = DependencyManager::get<AvatarManager>()->getMyAvatar();
|
||||
|
||||
// Add the camera position at the plane of the avatar's base (the floor)
|
||||
auto cameraPosition = args->_viewFrustum->getPosition();
|
||||
auto avatarBaseHeight = avatar->getPosition().y - avatar->getUniformScale();
|
||||
position += glm::vec3(cameraPosition.x, avatarBaseHeight, cameraPosition.z);
|
||||
}
|
||||
|
||||
Transform transform;
|
||||
transform.setRotation(getRotation());
|
||||
|
||||
transform.setScale(glm::vec3(getDimensions(), 1.0f));
|
||||
transform.setTranslation(position);
|
||||
batch->setModelTransform(transform);
|
||||
|
||||
// Minor grid
|
||||
{
|
||||
auto position = glm::vec3(_minorGridWidth * (floorf(rotated.x / spacing) - MINOR_GRID_DIVISIONS / 2),
|
||||
spacing * (floorf(rotated.y / spacing) - MINOR_GRID_DIVISIONS / 2),
|
||||
getPosition().z);
|
||||
float scale = MINOR_GRID_DIVISIONS * spacing;
|
||||
|
||||
transform.setTranslation(position);
|
||||
transform.setScale(scale);
|
||||
|
||||
batch->setModelTransform(transform);
|
||||
|
||||
DependencyManager::get<GeometryCache>()->renderGrid(*batch, MINOR_GRID_DIVISIONS, MINOR_GRID_DIVISIONS, gridColor);
|
||||
}
|
||||
const float MINOR_GRID_EDGE = 0.0025f;
|
||||
DependencyManager::get<GeometryCache>()->renderGrid(*batch,
|
||||
minCorner, maxCorner, _minorGridRowDivisions, _minorGridColDivisions, gridColor, MINOR_GRID_EDGE);
|
||||
|
||||
// Major grid
|
||||
{
|
||||
spacing *= _majorGridEvery;
|
||||
auto position = glm::vec3(spacing * (floorf(rotated.x / spacing) - MAJOR_GRID_DIVISIONS / 2),
|
||||
spacing * (floorf(rotated.y / spacing) - MAJOR_GRID_DIVISIONS / 2),
|
||||
getPosition().z);
|
||||
float scale = MAJOR_GRID_DIVISIONS * spacing;
|
||||
|
||||
transform.setTranslation(position);
|
||||
transform.setScale(scale);
|
||||
|
||||
// FIXME: THe line width of 4.0f is not supported anymore, we ll need a workaround
|
||||
|
||||
batch->setModelTransform(transform);
|
||||
|
||||
DependencyManager::get<GeometryCache>()->renderGrid(*batch, MAJOR_GRID_DIVISIONS, MAJOR_GRID_DIVISIONS, gridColor);
|
||||
}
|
||||
const float MAJOR_GRID_EDGE = 0.01f;
|
||||
DependencyManager::get<GeometryCache>()->renderGrid(*batch,
|
||||
minCorner, maxCorner, _majorGridRowDivisions, _majorGridColDivisions, gridColor, MAJOR_GRID_EDGE);
|
||||
}
|
||||
}
|
||||
|
||||
const render::ShapeKey Grid3DOverlay::getShapeKey() {
|
||||
auto builder = render::ShapeKey::Builder();
|
||||
if (getAlpha() != 1.0f) {
|
||||
builder.withTranslucent();
|
||||
}
|
||||
return builder.build();
|
||||
return render::ShapeKey::Builder().withTranslucent();
|
||||
}
|
||||
|
||||
void Grid3DOverlay::setProperties(const QScriptValue& properties) {
|
||||
Planar3DOverlay::setProperties(properties);
|
||||
|
||||
if (properties.property("minorGridWidth").isValid()) {
|
||||
_minorGridWidth = properties.property("minorGridWidth").toVariant().toFloat();
|
||||
if (properties.property("followCamera").isValid()) {
|
||||
_followCamera = properties.property("followCamera").toVariant().toBool();
|
||||
}
|
||||
|
||||
if (properties.property("majorGridEvery").isValid()) {
|
||||
_majorGridEvery = properties.property("majorGridEvery").toVariant().toInt();
|
||||
}
|
||||
|
||||
if (properties.property("minorGridEvery").isValid()) {
|
||||
_minorGridEvery = properties.property("minorGridEvery").toVariant().toFloat();
|
||||
}
|
||||
|
||||
updateGrid();
|
||||
}
|
||||
|
||||
QScriptValue Grid3DOverlay::getProperty(const QString& property) {
|
||||
if (property == "minorGridWidth") {
|
||||
return _minorGridWidth;
|
||||
if (property == "followCamera") {
|
||||
return _followCamera;
|
||||
}
|
||||
if (property == "majorGridEvery") {
|
||||
return _majorGridEvery;
|
||||
}
|
||||
if (property == "minorGridEvery") {
|
||||
return _minorGridEvery;
|
||||
}
|
||||
|
||||
return Planar3DOverlay::getProperty(property);
|
||||
}
|
||||
|
@ -128,3 +122,16 @@ Grid3DOverlay* Grid3DOverlay::createClone() const {
|
|||
return new Grid3DOverlay(this);
|
||||
}
|
||||
|
||||
void Grid3DOverlay::updateGrid() {
|
||||
const int MAJOR_GRID_EVERY_MIN = 5;
|
||||
const float MINOR_GRID_EVERY_MIN = 0.2f;
|
||||
|
||||
_majorGridEvery = std::max(_majorGridEvery, MAJOR_GRID_EVERY_MIN);
|
||||
_minorGridEvery = std::max(_minorGridEvery, MINOR_GRID_EVERY_MIN);
|
||||
|
||||
_majorGridRowDivisions = getDimensions().x / _majorGridEvery;
|
||||
_majorGridColDivisions = getDimensions().y / _majorGridEvery;
|
||||
|
||||
_minorGridRowDivisions = getDimensions().x / _minorGridEvery;
|
||||
_minorGridColDivisions = getDimensions().y / _minorGridEvery;
|
||||
}
|
||||
|
|
|
@ -32,8 +32,17 @@ public:
|
|||
virtual Grid3DOverlay* createClone() const;
|
||||
|
||||
private:
|
||||
float _minorGridWidth;
|
||||
int _majorGridEvery;
|
||||
void updateGrid();
|
||||
|
||||
bool _followCamera { true };
|
||||
|
||||
int _majorGridEvery { 5 };
|
||||
float _majorGridRowDivisions;
|
||||
float _majorGridColDivisions;
|
||||
|
||||
float _minorGridEvery { 1.0f };
|
||||
float _minorGridRowDivisions;
|
||||
float _minorGridColDivisions;
|
||||
};
|
||||
|
||||
#endif // hifi_Grid3DOverlay_h
|
||||
|
|
Loading…
Reference in a new issue