Unified · Split
| @@ -3403,7 +3403,7 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se · 3402 unmodified lines | ||
| 3403 | 3403 | } |
| 3404 | 3404 | } |
| 3405 | 3405 | |
| 3406 | − render::Scene::PendingChanges pendingChanges; | |
| 3406 | + render::PendingChanges pendingChanges; | |
| 3407 | 3407 | |
| 3408 | 3408 | // Make sure the WorldBox is in the scene |
| 3409 | 3409 | if (WorldBoxRenderData::_item == 0) { |
| @@ -3415,18 +3415,23 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se · 5 unmodified lines | ||
| 3415 | 3415 | pendingChanges.resetItem(WorldBoxRenderData::_item, worldBoxRenderPayload); |
| 3416 | 3416 | } |
| 3417 | 3417 | |
| 3418 | + { | |
| 3419 | + PerformanceTimer perfTimer("SceneProcessPendingChanges"); | |
| 3420 | + _main3DScene->enqueuePendingChanges(pendingChanges); | |
| 3418 | 3421 | |
| 3419 | − _main3DScene->enqueuePendingChanges(pendingChanges); | |
| 3420 | − | |
| 3421 | − _main3DScene->processPendingChangesQueue(); | |
| 3422 | + _main3DScene->processPendingChangesQueue(); | |
| 3423 | + } | |
| 3422 | 3424 | |
| 3423 | 3425 | // FOr now every frame pass the renderCOntext |
| 3424 | − render::RenderContext renderContext; | |
| 3425 | − renderContext.args = renderArgs; | |
| 3426 | − _renderEngine->setRenderContext(renderContext); | |
| 3426 | + { | |
| 3427 | + PerformanceTimer perfTimer("EngineRun"); | |
| 3428 | + render::RenderContext renderContext; | |
| 3429 | + renderContext.args = renderArgs; | |
| 3430 | + _renderEngine->setRenderContext(renderContext); | |
| 3427 | 3431 | |
| 3428 | − // Before the deferred pass, let's try to use the render engine | |
| 3429 | − _renderEngine->run(); | |
| 3432 | + // Before the deferred pass, let's try to use the render engine | |
| 3433 | + _renderEngine->run(); | |
| 3434 | + } | |
| 3430 | 3435 | |
| 3431 | 3436 | { |
| 3432 | 3437 | DependencyManager::get<DeferredLightingEffect>()->setAmbientLightMode(getRenderAmbientLight()); |
| @@ -40,13 +40,13 @@ void AudioToolBox::render(int x, int y, int padding, bool boxed) { · 39 unmodified lines | ||
| 40 | 40 | glEnable(GL_TEXTURE_2D); |
| 41 | 41 | |
| 42 | 42 | if (!_micTexture) { |
| 43 | − _micTexture = DependencyManager::get<TextureCache>()->getImageTexture(PathUtils::resourcesPath() + "images/mic.svg"); | |
| 43 | + _micTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/mic.svg"); | |
| 44 | 44 | } |
| 45 | 45 | if (!_muteTexture) { |
| 46 | − _muteTexture = DependencyManager::get<TextureCache>()->getImageTexture(PathUtils::resourcesPath() + "images/mic-mute.svg"); | |
| 46 | + _muteTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/mic-mute.svg"); | |
| 47 | 47 | } |
| 48 | 48 | if (_boxTexture) { |
| 49 | − _boxTexture = DependencyManager::get<TextureCache>()->getImageTexture(PathUtils::resourcesPath() + "images/audio-box.svg"); | |
| 49 | + _boxTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/audio-box.svg"); | |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | auto audioIO = DependencyManager::get<AudioClient>(); |
| @@ -67,7 +67,7 @@ void AvatarManager::init() { · 66 unmodified lines | ||
| 67 | 67 | auto avatarPayloadPointer = Avatar::PayloadPointer(avatarPayload); |
| 68 | 68 | static_cast<Avatar*>(_myAvatar.get())->_renderItemID = scene->allocateID(); |
| 69 | 69 | |
| 70 | − render::Scene::PendingChanges pendingChanges; | |
| 70 | + render::PendingChanges pendingChanges; | |
| 71 | 71 | pendingChanges.resetItem(static_cast<Avatar*>(_myAvatar.get())->_renderItemID, avatarPayloadPointer); |
| 72 | 72 | |
| 73 | 73 | scene->enqueuePendingChanges(pendingChanges); |
| @@ -152,7 +152,7 @@ AvatarSharedPointer AvatarManager::addAvatar(const QUuid& sessionUUID, const QWe · 78 unmodified lines | ||
| 152 | 152 | auto avatarPayloadPointer = Avatar::PayloadPointer(avatarPayload); |
| 153 | 153 | static_cast<Avatar*>(avatar.get())->_renderItemID = scene->allocateID(); |
| 154 | 154 | |
| 155 | − render::Scene::PendingChanges pendingChanges; | |
| 155 | + render::PendingChanges pendingChanges; | |
| 156 | 156 | pendingChanges.resetItem(static_cast<Avatar*>(avatar.get())->_renderItemID, avatarPayloadPointer); |
| 157 | 157 | |
| 158 | 158 | scene->enqueuePendingChanges(pendingChanges); |
| @@ -186,7 +186,7 @@ void AvatarManager::removeAvatar(const QUuid& sessionUUID) { · 27 unmodified lines | ||
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | render::ScenePointer scene = Application::getInstance()->getMain3DScene(); |
| 189 | − render::Scene::PendingChanges pendingChanges; | |
| 189 | + render::PendingChanges pendingChanges; | |
| 190 | 190 | pendingChanges.removeItem(avatar->_renderItemID); |
| 191 | 191 | scene->enqueuePendingChanges(pendingChanges); |
| 192 | 192 | } |
| @@ -76,10 +76,10 @@ void CameraToolBox::render(int x, int y, bool boxed) { · 75 unmodified lines | ||
| 76 | 76 | glEnable(GL_TEXTURE_2D); |
| 77 | 77 | |
| 78 | 78 | if (!_enabledTexture) { |
| 79 | − _enabledTexture = DependencyManager::get<TextureCache>()->getImageTexture(PathUtils::resourcesPath() + "images/face.svg"); | |
| 79 | + _enabledTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/face.svg"); | |
| 80 | 80 | } |
| 81 | 81 | if (!_mutedTexture) { |
| 82 | − _mutedTexture = DependencyManager::get<TextureCache>()->getImageTexture(PathUtils::resourcesPath() + "images/face-mute.svg"); | |
| 82 | + _mutedTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/face-mute.svg"); | |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | const int MUTE_ICON_SIZE = 24; |
| @@ -423,8 +423,8 @@ void ApplicationOverlay::displayOverlayTextureStereo(Camera& whichCamera, float · 422 unmodified lines | ||
| 423 | 423 | }); |
| 424 | 424 | |
| 425 | 425 | if (!_crosshairTexture) { |
| 426 | − _crosshairTexture = DependencyManager::get<TextureCache>()-> | |
| 427 | − getImageTexture(PathUtils::resourcesPath() + "images/sixense-reticle.png"); | |
| 426 | + _crosshairTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + | |
| 427 | + "images/sixense-reticle.png"); | |
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | //draw the mouse pointer |
| @@ -564,8 +564,7 @@ bool ApplicationOverlay::calculateRayUICollisionPoint(const glm::vec3& position, · 133 unmodified lines | ||
| 564 | 564 | void ApplicationOverlay::renderPointers() { |
| 565 | 565 | //lazily load crosshair texture |
| 566 | 566 | if (_crosshairTexture == 0) { |
| 567 | − _crosshairTexture = DependencyManager::get<TextureCache>()-> | |
| 568 | − getImageTexture(PathUtils::resourcesPath() + "images/sixense-reticle.png"); | |
| 567 | + _crosshairTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/sixense-reticle.png"); | |
| 569 | 568 | } |
| 570 | 569 | glEnable(GL_TEXTURE_2D); |
| 571 | 570 | |
| @@ -34,11 +34,10 @@ RearMirrorTools::RearMirrorTools(QRect& bounds) : · 33 unmodified lines | ||
| 34 | 34 | _windowed(false), |
| 35 | 35 | _fullScreen(false) |
| 36 | 36 | { |
| 37 | − auto textureCache = DependencyManager::get<TextureCache>(); | |
| 38 | − _closeTexture = textureCache->getImageTexture(PathUtils::resourcesPath() + "images/close.svg"); | |
| 37 | + _closeTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/close.svg"); | |
| 39 | 38 | |
| 40 | − _zoomHeadTexture = textureCache->getImageTexture(PathUtils::resourcesPath() + "images/plus.svg"); | |
| 41 | − _zoomBodyTexture = textureCache->getImageTexture(PathUtils::resourcesPath() + "images/minus.svg"); | |
| 39 | + _zoomHeadTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/plus.svg"); | |
| 40 | + _zoomBodyTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() + "images/minus.svg"); | |
| 42 | 41 | |
| 43 | 42 | _shrinkIconRect = QRect(ICON_PADDING, ICON_PADDING, ICON_SIZE, ICON_SIZE); |
| 44 | 43 | _closeIconRect = QRect(_bounds.left() + ICON_PADDING, _bounds.top() + ICON_PADDING, ICON_SIZE, ICON_SIZE); |
| @@ -24,6 +24,7 @@ TextOverlay::TextOverlay() : · 23 unmodified lines | ||
| 24 | 24 | _topMargin(DEFAULT_MARGIN), |
| 25 | 25 | _fontSize(DEFAULT_FONTSIZE) |
| 26 | 26 | { |
| 27 | + _textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); | |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | TextOverlay::TextOverlay(const TextOverlay* textOverlay) : |
| @@ -35,6 +36,7 @@ TextOverlay::TextOverlay(const TextOverlay* textOverlay) : · 5 unmodified lines | ||
| 35 | 36 | _topMargin(textOverlay->_topMargin), |
| 36 | 37 | _fontSize(textOverlay->_fontSize) |
| 37 | 38 | { |
| 39 | + _textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); | |
| 38 | 40 | } |
| 39 | 41 | |
| 40 | 42 | TextOverlay::~TextOverlay() { |
| @@ -60,7 +60,7 @@ public: · 59 unmodified lines | ||
| 60 | 60 | |
| 61 | 61 | private: |
| 62 | 62 | |
| 63 | − TextRenderer* _textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); | |
| 63 | + TextRenderer* _textRenderer = nullptr; | |
| 64 | 64 | |
| 65 | 65 | QString _text; |
| 66 | 66 | xColor _backgroundColor; |
| @@ -96,9 +96,13 @@ void EntityTreeRenderer::clear() { · 95 unmodified lines | ||
| 96 | 96 | OctreeRenderer::clear(); |
| 97 | 97 | _entityScripts.clear(); |
| 98 | 98 | |
| 99 | − // TODO/FIXME - this needs to be fixed... we need to clear all items out of the scene in this case. | |
| 100 | − qDebug() << "EntityTreeRenderer::clear() need to clear the scene... "; | |
| 101 | − | |
| 99 | + auto scene = _viewState->getMain3DScene(); | |
| 100 | + render::PendingChanges pendingChanges; | |
| 101 | + foreach(auto entity, _entitiesInScene) { | |
| 102 | + entity->removeFromScene(entity, scene, pendingChanges); | |
| 103 | + } | |
| 104 | + scene->enqueuePendingChanges(pendingChanges); | |
| 105 | + _entitiesInScene.clear(); | |
| 102 | 106 | } |
| 103 | 107 | |
| 104 | 108 | void EntityTreeRenderer::init() { |
| @@ -704,8 +708,8 @@ void EntityTreeRenderer::renderElement(OctreeElement* element, RenderArgs* args) · 599 unmodified lines | ||
| 704 | 708 | } |
| 705 | 709 | } |
| 706 | 710 | |
| 707 | − // hack for models. :( | |
| 708 | − if (entityItem->getType() == EntityTypes::Model) { | |
| 711 | + // hack for models and other entities that don't yet play well with others. :( | |
| 712 | + if (!entityItem->canRenderInScene()) { | |
| 709 | 713 | // render entityItem |
| 710 | 714 | AABox entityBox = entityItem->getAABox(); |
| 711 | 715 | |
| @@ -1063,12 +1067,13 @@ void EntityTreeRenderer::deletingEntity(const EntityItemID& entityID) { · 351 unmodified lines | ||
| 1063 | 1067 | _entityScripts.remove(entityID); |
| 1064 | 1068 | |
| 1065 | 1069 | // here's where we remove the entity payload from the scene |
| 1066 | − | |
| 1067 | − render::Scene::PendingChanges pendingChanges; | |
| 1068 | − if (_entityToSceneItems.contains(entityID)) { | |
| 1069 | − render::ItemID renderItem = _entityToSceneItems[entityID]; | |
| 1070 | − pendingChanges.removeItem(renderItem); | |
| 1071 | − _viewState->getMain3DScene()->enqueuePendingChanges(pendingChanges); | |
| 1070 | + auto entity = static_cast<EntityTree*>(_tree)->findEntityByID(entityID); | |
| 1071 | + if (entity && _entitiesInScene.contains(entity)) { | |
| 1072 | + render::PendingChanges pendingChanges; | |
| 1073 | + auto scene = _viewState->getMain3DScene(); | |
| 1074 | + entity->removeFromScene(entity, scene, pendingChanges); | |
| 1075 | + scene->enqueuePendingChanges(pendingChanges); | |
| 1076 | + _entitiesInScene.remove(entity); | |
| 1072 | 1077 | } |
| 1073 | 1078 | } |
| 1074 | 1079 | |
| @@ -1076,19 +1081,15 @@ void EntityTreeRenderer::addingEntity(const EntityItemID& entityID) { · 1 unmodified line | ||
| 1076 | 1081 | checkAndCallPreload(entityID); |
| 1077 | 1082 | |
| 1078 | 1083 | // here's where we add the entity payload to the scene |
| 1079 | − | |
| 1080 | − render::Scene::PendingChanges pendingChanges; | |
| 1081 | − render::ItemID renderItem = _viewState->getMain3DScene()->allocateID(); | |
| 1082 | − _entityToSceneItems[entityID] = renderItem; | |
| 1083 | − EntityItemPointer entity = static_cast<EntityTree*>(_tree)->findEntityByID(entityID); | |
| 1084 | − | |
| 1085 | − auto renderData = RenderableEntityItem::Pointer(new RenderableEntityItem(entity)); | |
| 1086 | − auto renderPayload = render::PayloadPointer(new RenderableEntityItem::Payload(renderData)); | |
| 1087 | − | |
| 1088 | − pendingChanges.resetItem(renderItem, renderPayload); | |
| 1089 | − | |
| 1090 | − _viewState->getMain3DScene()->enqueuePendingChanges(pendingChanges); | |
| 1091 | − _viewState->getMain3DScene()->processPendingChangesQueue(); | |
| 1084 | + auto entity = static_cast<EntityTree*>(_tree)->findEntityByID(entityID); | |
| 1085 | + if (entity && entity->canRenderInScene()) { | |
| 1086 | + render::PendingChanges pendingChanges; | |
| 1087 | + auto scene = _viewState->getMain3DScene(); | |
| 1088 | + if (entity->addToScene(entity, scene, pendingChanges)) { | |
| 1089 | + _entitiesInScene.insert(entity); | |
| 1090 | + } | |
| 1091 | + scene->enqueuePendingChanges(pendingChanges); | |
| 1092 | + } | |
| 1092 | 1093 | } |
| 1093 | 1094 | |
| 1094 | 1095 | void EntityTreeRenderer::entitySciptChanging(const EntityItemID& entityID) { |
| @@ -187,7 +187,7 @@ private: · 186 unmodified lines | ||
| 187 | 187 | float _previousStageHour; |
| 188 | 188 | int _previousStageDay; |
| 189 | 189 | |
| 190 | − QHash<EntityItemID, render::ItemID> _entityToSceneItems; | |
| 190 | + QSet<EntityItemPointer> _entitiesInScene; | |
| 191 | 191 | }; |
| 192 | 192 | |
| 193 | 193 | |
| @@ -14,6 +14,7 @@ · 13 unmodified lines | ||
| 14 | 14 | |
| 15 | 15 | #include <BoxEntityItem.h> |
| 16 | 16 | #include "RenderableDebugableEntityItem.h" |
| 17 | +#include "RenderableEntityItem.h" | |
| 17 | 18 | |
| 18 | 19 | class RenderableBoxEntityItem : public BoxEntityItem { |
| 19 | 20 | public: |
| @@ -24,6 +25,8 @@ public: · 4 unmodified lines | ||
| 24 | 25 | { } |
| 25 | 26 | |
| 26 | 27 | virtual void render(RenderArgs* args); |
| 28 | + | |
| 29 | + SIMPLE_RENDERABLE() | |
| 27 | 30 | }; |
| 28 | 31 | |
| 29 | 32 | |
| @@ -13,7 +13,7 @@ · 12 unmodified lines | ||
| 13 | 13 | #include "RenderableEntityItem.h" |
| 14 | 14 | |
| 15 | 15 | namespace render { |
| 16 | − template <> const ItemKey payloadGetKey(const RenderableEntityItem::Pointer& payload) { | |
| 16 | + template <> const ItemKey payloadGetKey(const RenderableEntityItemProxy::Pointer& payload) { | |
| 17 | 17 | if (payload && payload->entity) { |
| 18 | 18 | if (payload->entity->getType() == EntityTypes::Light) { |
| 19 | 19 | return ItemKey::Builder::light(); |
| @@ -22,13 +22,13 @@ namespace render { · 2 unmodified lines | ||
| 22 | 22 | return ItemKey::Builder::opaqueShape(); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | − template <> const Item::Bound payloadGetBound(const RenderableEntityItem::Pointer& payload) { | |
| 25 | + template <> const Item::Bound payloadGetBound(const RenderableEntityItemProxy::Pointer& payload) { | |
| 26 | 26 | if (payload && payload->entity) { |
| 27 | 27 | return payload->entity->getAABox(); |
| 28 | 28 | } |
| 29 | 29 | return render::Item::Bound(); |
| 30 | 30 | } |
| 31 | − template <> void payloadRender(const RenderableEntityItem::Pointer& payload, RenderArgs* args) { | |
| 31 | + template <> void payloadRender(const RenderableEntityItemProxy::Pointer& payload, RenderArgs* args) { | |
| 32 | 32 | if (args) { |
| 33 | 33 | args->_elementsTouched++; |
| 34 | 34 | if (payload && payload->entity) { |
| @@ -15,19 +15,53 @@ · 14 unmodified lines | ||
| 15 | 15 | #include <render/Scene.h> |
| 16 | 16 | #include <EntityItem.h> |
| 17 | 17 | |
| 18 | −class RenderableEntityItem { | |
| 18 | + | |
| 19 | +class RenderableEntityItemProxy { | |
| 19 | 20 | public: |
| 20 | − RenderableEntityItem(EntityItemPointer entity) : entity(entity) { } | |
| 21 | − typedef render::Payload<RenderableEntityItem> Payload; | |
| 21 | + RenderableEntityItemProxy(EntityItemPointer entity) : entity(entity) { } | |
| 22 | + typedef render::Payload<RenderableEntityItemProxy> Payload; | |
| 22 | 23 | typedef Payload::DataPointer Pointer; |
| 23 | 24 | |
| 24 | 25 | EntityItemPointer entity; |
| 25 | 26 | }; |
| 26 | 27 | |
| 27 | 28 | namespace render { |
| 28 | − template <> const ItemKey payloadGetKey(const RenderableEntityItem::Pointer& payload); | |
| 29 | − template <> const Item::Bound payloadGetBound(const RenderableEntityItem::Pointer& payload); | |
| 30 | − template <> void payloadRender(const RenderableEntityItem::Pointer& payload, RenderArgs* args); | |
| 29 | + template <> const ItemKey payloadGetKey(const RenderableEntityItemProxy::Pointer& payload); | |
| 30 | + template <> const Item::Bound payloadGetBound(const RenderableEntityItemProxy::Pointer& payload); | |
| 31 | + template <> void payloadRender(const RenderableEntityItemProxy::Pointer& payload, RenderArgs* args); | |
| 31 | 32 | } |
| 32 | 33 | |
| 34 | +// Mixin class for implementing basic single item rendering | |
| 35 | +class SimpleRenderableEntityItem { | |
| 36 | +public: | |
| 37 | + bool addToScene(EntityItemPointer self, std::shared_ptr<render::Scene> scene, render::PendingChanges& pendingChanges) { | |
| 38 | + _myItem = scene->allocateID(); | |
| 39 | + | |
| 40 | + auto renderData = RenderableEntityItemProxy::Pointer(new RenderableEntityItemProxy(self)); | |
| 41 | + auto renderPayload = render::PayloadPointer(new RenderableEntityItemProxy::Payload(renderData)); | |
| 42 | + | |
| 43 | + pendingChanges.resetItem(_myItem, renderPayload); | |
| 44 | + | |
| 45 | + return true; | |
| 46 | + } | |
| 47 | + | |
| 48 | + void removeFromScene(EntityItemPointer self, std::shared_ptr<render::Scene> scene, render::PendingChanges& pendingChanges) { | |
| 49 | + pendingChanges.removeItem(_myItem); | |
| 50 | + } | |
| 51 | + | |
| 52 | +private: | |
| 53 | + render::ItemID _myItem; | |
| 54 | +}; | |
| 55 | + | |
| 56 | + | |
| 57 | +#define SIMPLE_RENDERABLE() \ | |
| 58 | +public: \ | |
| 59 | + virtual bool canRenderInScene() { return true; } \ | |
| 60 | + virtual bool addToScene(EntityItemPointer self, std::shared_ptr<render::Scene> scene, render::PendingChanges& pendingChanges) { return _renderHelper.addToScene(self, scene, pendingChanges); } \ | |
| 61 | + virtual void removeFromScene(EntityItemPointer self, std::shared_ptr<render::Scene> scene, render::PendingChanges& pendingChanges) { _renderHelper.removeFromScene(self, scene, pendingChanges); } \ | |
| 62 | +private: \ | |
| 63 | + SimpleRenderableEntityItem _renderHelper; | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 33 | 67 | #endif // hifi_RenderableEntityItem_h |
| @@ -22,6 +22,7 @@ public: · 21 unmodified lines | ||
| 22 | 22 | LightEntityItem(entityItemID, properties) |
| 23 | 23 | { } |
| 24 | 24 | |
| 25 | + virtual bool canRenderInScene() { return false; } // we don't yet play well with others | |
| 25 | 26 | virtual void render(RenderArgs* args); |
| 26 | 27 | virtual bool supportsDetailedRayIntersection() const { return true; } |
| 27 | 28 | virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction, |
| @@ -14,6 +14,7 @@ · 13 unmodified lines | ||
| 14 | 14 | |
| 15 | 15 | #include <LineEntityItem.h> |
| 16 | 16 | #include "RenderableDebugableEntityItem.h" |
| 17 | +#include "RenderableEntityItem.h" | |
| 17 | 18 | |
| 18 | 19 | class RenderableLineEntityItem : public LineEntityItem { |
| 19 | 20 | public: |
| @@ -23,6 +24,8 @@ public: · 3 unmodified lines | ||
| 23 | 24 | LineEntityItem(entityItemID, properties) { } |
| 24 | 25 | |
| 25 | 26 | virtual void render(RenderArgs* args); |
| 27 | + | |
| 28 | + SIMPLE_RENDERABLE() | |
| 26 | 29 | }; |
| 27 | 30 | |
| 28 | 31 | |
| @@ -43,6 +43,7 @@ public: · 42 unmodified lines | ||
| 43 | 43 | |
| 44 | 44 | virtual void somethingChangedNotification() { _needsInitialSimulation = true; } |
| 45 | 45 | |
| 46 | + virtual bool canRenderInScene() { return false; } // we don't yet play well with others | |
| 46 | 47 | virtual void render(RenderArgs* args); |
| 47 | 48 | virtual bool supportsDetailedRayIntersection() const { return true; } |
| 48 | 49 | virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction, |
| @@ -13,6 +13,7 @@ · 12 unmodified lines | ||
| 13 | 13 | |
| 14 | 14 | #include <ParticleEffectEntityItem.h> |
| 15 | 15 | #include <TextureCache.h> |
| 16 | +#include "RenderableEntityItem.h" | |
| 16 | 17 | |
| 17 | 18 | class RenderableParticleEffectEntityItem : public ParticleEffectEntityItem { |
| 18 | 19 | public: |
| @@ -22,6 +23,8 @@ public: · 3 unmodified lines | ||
| 22 | 23 | |
| 23 | 24 | void updateQuads(RenderArgs* args, bool textured); |
| 24 | 25 | |
| 26 | + SIMPLE_RENDERABLE() | |
| 27 | + | |
| 25 | 28 | protected: |
| 26 | 29 | |
| 27 | 30 | int _cacheID; |
| @@ -14,6 +14,8 @@ · 13 unmodified lines | ||
| 14 | 14 | |
| 15 | 15 | #include <SphereEntityItem.h> |
| 16 | 16 | |
| 17 | +#include "RenderableEntityItem.h" | |
| 18 | + | |
| 17 | 19 | class RenderableSphereEntityItem : public SphereEntityItem { |
| 18 | 20 | public: |
| 19 | 21 | static EntityItemPointer factory(const EntityItemID& entityID, const EntityItemProperties& properties); |
| @@ -23,6 +25,8 @@ public: · 3 unmodified lines | ||
| 23 | 25 | { } |
| 24 | 26 | |
| 25 | 27 | virtual void render(RenderArgs* args); |
| 28 | + | |
| 29 | + SIMPLE_RENDERABLE() | |
| 26 | 30 | }; |
| 27 | 31 | |
| 28 | 32 | |
| @@ -20,52 +20,41 @@ · 19 unmodified lines | ||
| 20 | 20 | #include "RenderableTextEntityItem.h" |
| 21 | 21 | #include "GLMHelpers.h" |
| 22 | 22 | |
| 23 | − | |
| 24 | 23 | EntityItemPointer RenderableTextEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) { |
| 25 | 24 | return EntityItemPointer(new RenderableTextEntityItem(entityID, properties)); |
| 26 | 25 | } |
| 27 | 26 | |
| 28 | 27 | void RenderableTextEntityItem::render(RenderArgs* args) { |
| 29 | 28 | PerformanceTimer perfTimer("RenderableTextEntityItem::render"); |
| 30 | − assert(getType() == EntityTypes::Text); | |
| 31 | − glm::vec3 position = getPosition(); | |
| 29 | + Q_ASSERT(getType() == EntityTypes::Text); | |
| 30 | + | |
| 31 | + static const float SLIGHTLY_BEHIND = -0.005f; | |
| 32 | + glm::vec4 textColor = glm::vec4(toGlm(getTextColorX()), 1.0f); | |
| 33 | + glm::vec4 backgroundColor = glm::vec4(toGlm(getBackgroundColorX()), 1.0f); | |
| 32 | 34 | glm::vec3 dimensions = getDimensions(); |
| 33 | − glm::vec3 halfDimensions = dimensions / 2.0f; | |
| 34 | − glm::quat rotation = getRotation(); | |
| 35 | − float leftMargin = 0.1f; | |
| 36 | − float topMargin = 0.1f; | |
| 37 | − | |
| 38 | − //qCDebug(entitytree) << "RenderableTextEntityItem::render() id:" << getEntityItemID() << "text:" << getText(); | |
| 39 | − | |
| 40 | − glPushMatrix(); | |
| 41 | − { | |
| 42 | − glTranslatef(position.x, position.y, position.z); | |
| 43 | − glm::vec3 axis = glm::axis(rotation); | |
| 44 | − glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); | |
| 45 | − | |
| 46 | − float alpha = 1.0f; //getBackgroundAlpha(); | |
| 47 | − static const float SLIGHTLY_BEHIND = -0.005f; | |
| 48 | − | |
| 49 | − glm::vec3 topLeft(-halfDimensions.x, -halfDimensions.y, SLIGHTLY_BEHIND); | |
| 50 | − glm::vec3 bottomRight(halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND); | |
| 51 | − | |
| 52 | − // TODO: Determine if we want these entities to have the deferred lighting effect? I think we do, so that the color | |
| 53 | − // used for a sphere, or box have the same look as those used on a text entity. | |
| 54 | − //DependencyManager::get<DeferredLightingEffect>()->bindSimpleProgram(); | |
| 55 | − DependencyManager::get<GeometryCache>()->renderQuad(topLeft, bottomRight, glm::vec4(toGlm(getBackgroundColorX()), alpha)); | |
| 56 | − //DependencyManager::get<DeferredLightingEffect>()->releaseSimpleProgram(); | |
| 57 | − | |
| 58 | − glTranslatef(-(halfDimensions.x - leftMargin), halfDimensions.y - topMargin, 0.0f); | |
| 59 | − glm::vec4 textColor(toGlm(getTextColorX()), alpha); | |
| 60 | − // this is a ratio determined through experimentation | |
| 61 | − const float scaleFactor = 0.08f * _lineHeight; | |
| 62 | − glScalef(scaleFactor, -scaleFactor, scaleFactor); | |
| 63 | − glm::vec2 bounds(dimensions.x / scaleFactor, dimensions.y / scaleFactor); | |
| 64 | − _textRenderer->draw(0, 0, _text, textColor, bounds); | |
| 65 | − } | |
| 66 | − glPopMatrix(); | |
| 35 | + | |
| 36 | + Transform transformToTopLeft = getTransformToCenter(); | |
| 37 | + transformToTopLeft.postTranslate(glm::vec3(-0.5f, 0.5f, 0.0f)); // Go to the top left | |
| 38 | + transformToTopLeft.setScale(1.0f); // Use a scale of one so that the text is not deformed | |
| 39 | + | |
| 40 | + // Batch render calls | |
| 41 | + Q_ASSERT(args->_batch); | |
| 42 | + gpu::Batch& batch = *args->_batch; | |
| 43 | + batch.setModelTransform(transformToTopLeft); | |
| 44 | + | |
| 45 | + // Render background | |
| 46 | + glm::vec3 minCorner = glm::vec3(0.0f, -dimensions.y, SLIGHTLY_BEHIND); | |
| 47 | + glm::vec3 maxCorner = glm::vec3(dimensions.x, 0.0f, SLIGHTLY_BEHIND); | |
| 48 | + DependencyManager::get<DeferredLightingEffect>()->renderQuad(batch, minCorner, maxCorner, backgroundColor); | |
| 49 | + | |
| 50 | + float scale = _lineHeight / _textRenderer->getRowHeight(); | |
| 51 | + transformToTopLeft.setScale(scale); // Scale to have the correct line height | |
| 52 | + batch.setModelTransform(transformToTopLeft); | |
| 53 | + | |
| 54 | + float leftMargin = 0.5f * _lineHeight, topMargin = 0.5f * _lineHeight; | |
| 55 | + glm::vec2 bounds = glm::vec2(dimensions.x - 2.0f * leftMargin, dimensions.y - 2.0f * topMargin); | |
| 56 | + _textRenderer->draw(batch, leftMargin / scale, -topMargin / scale, _text, textColor, bounds / scale); | |
| 67 | 57 | } |
| 68 | 58 | |
| 69 | 59 | |
| 70 | 60 | |
| 71 | − | |
| @@ -13,7 +13,7 @@ · 12 unmodified lines | ||
| 13 | 13 | #define hifi_RenderableTextEntityItem_h |
| 14 | 14 | |
| 15 | 15 | #include <TextEntityItem.h> |
| 16 | −#include <TextRenderer.h> | |
| 16 | +#include <TextRenderer3D.h> | |
| 17 | 17 | |
| 18 | 18 | const int FIXED_FONT_POINT_SIZE = 40; |
| 19 | 19 | |
| @@ -27,9 +27,10 @@ public: · 7 unmodified lines | ||
| 27 | 27 | ~RenderableTextEntityItem() { delete _textRenderer; } |
| 28 | 28 | |
| 29 | 29 | virtual void render(RenderArgs* args); |
| 30 | + virtual bool canRenderInScene() { return false; } // we don't yet play well with others | |
| 30 | 31 | |
| 31 | 32 | private: |
| 32 | − TextRenderer* _textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE / 2.0f); | |
| 33 | + TextRenderer3D* _textRenderer = TextRenderer3D::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE / 2.0f); | |
| 33 | 34 | }; |
| 34 | 35 | |
| 35 | 36 | |
| @@ -24,6 +24,7 @@ public: · 23 unmodified lines | ||
| 24 | 24 | |
| 25 | 25 | virtual void render(RenderArgs* args); |
| 26 | 26 | virtual void setSourceUrl(const QString& value); |
| 27 | + virtual bool canRenderInScene() { return false; } // we don't yet play well with others | |
| 27 | 28 | |
| 28 | 29 | private: |
| 29 | 30 | OffscreenQmlSurface* _webSurface{ nullptr }; |
| @@ -14,3 +14,4 @@ target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${BULLET_INCLUDE_DIRS}) · 13 unmodified lines | ||
| 14 | 14 | target_link_libraries(${TARGET_NAME} ${BULLET_LIBRARIES}) |
| 15 | 15 | |
| 16 | 16 | link_hifi_libraries(avatars shared octree gpu model fbx networking animation environment) |
| 17 | +include_hifi_library_headers(render) | |
| @@ -1005,6 +1005,13 @@ void EntityItem::setTranformToCenter(const Transform& transform) { · 1004 unmodified lines | ||
| 1005 | 1005 | setTransform(copy); |
| 1006 | 1006 | } |
| 1007 | 1007 | |
| 1008 | +void EntityItem::setDimensions(const glm::vec3& value) { | |
| 1009 | + if (value.x == 0.0f || value.y == 0.0f || value.z == 0.0f) { | |
| 1010 | + return; | |
| 1011 | + } | |
| 1012 | + _transform.setScale(value); | |
| 1013 | +} | |
| 1014 | + | |
| 1008 | 1015 | /// The maximum bounding cube for the entity, independent of it's rotation. |
| 1009 | 1016 | /// This accounts for the registration point (upon which rotation occurs around). |
| 1010 | 1017 | /// |
| @@ -12,6 +12,7 @@ · 11 unmodified lines | ||
| 12 | 12 | #ifndef hifi_EntityItem_h |
| 13 | 13 | #define hifi_EntityItem_h |
| 14 | 14 | |
| 15 | +#include <memory> | |
| 15 | 16 | #include <stdint.h> |
| 16 | 17 | |
| 17 | 18 | #include <glm/glm.hpp> |
| @@ -33,6 +34,11 @@ class EntitySimulation; · 15 unmodified lines | ||
| 33 | 34 | class EntityTreeElement; |
| 34 | 35 | class EntityTreeElementExtraEncodeData; |
| 35 | 36 | |
| 37 | +namespace render { | |
| 38 | + class Scene; | |
| 39 | + class PendingChanges; | |
| 40 | +} | |
| 41 | + | |
| 36 | 42 | // these thesholds determine what updates will be ignored (client and server) |
| 37 | 43 | const float IGNORE_POSITION_DELTA = 0.0001f; |
| 38 | 44 | const float IGNORE_DIMENSIONS_DELTA = 0.0005f; |
| @@ -151,6 +157,11 @@ public: · 112 unmodified lines | ||
| 151 | 157 | EntityPropertyFlags& propertyFlags, bool overwriteLocalData) |
| 152 | 158 | { return 0; } |
| 153 | 159 | |
| 160 | + virtual bool canRenderInScene() { return false; } // does your entity property render using Render Items and Payloads | |
| 161 | + virtual bool addToScene(EntityItemPointer self, std::shared_ptr<render::Scene> scene, | |
| 162 | + render::PendingChanges& pendingChanges) { return false; } // by default entity items don't add to scene | |
| 163 | + virtual void removeFromScene(EntityItemPointer self, std::shared_ptr<render::Scene> scene, | |
| 164 | + render::PendingChanges& pendingChanges) { } // by default entity items don't add to scene | |
| 154 | 165 | virtual void render(RenderArgs* args) { } // by default entity items don't know how to render |
| 155 | 166 | |
| 156 | 167 | static int expectedBytes(); |
| @@ -195,7 +206,7 @@ public: · 38 unmodified lines | ||
| 195 | 206 | |
| 196 | 207 | /// Dimensions in meters (0.0 - TREE_SCALE) |
| 197 | 208 | inline const glm::vec3& getDimensions() const { return _transform.getScale(); } |
| 198 | − inline virtual void setDimensions(const glm::vec3& value) { _transform.setScale(glm::abs(value)); } | |
| 209 | + virtual void setDimensions(const glm::vec3& value); | |
| 199 | 210 | |
| 200 | 211 | |
| 201 | 212 | float getGlowLevel() const { return _glowLevel; } |
| @@ -66,22 +66,23 @@ void GLBackend::updateInput() { · 65 unmodified lines | ||
| 66 | 66 | newActivation.set(attrib._slot); |
| 67 | 67 | } |
| 68 | 68 | } |
| 69 | − | |
| 69 | + | |
| 70 | 70 | // Manage Activation what was and what is expected now |
| 71 | 71 | for (unsigned int i = 0; i < newActivation.size(); i++) { |
| 72 | 72 | bool newState = newActivation[i]; |
| 73 | 73 | if (newState != _input._attributeActivation[i]) { |
| 74 | 74 | #if defined(SUPPORT_LEGACY_OPENGL) |
| 75 | − if (i < NUM_CLASSIC_ATTRIBS) { | |
| 75 | + const bool useClientState = i < NUM_CLASSIC_ATTRIBS; | |
| 76 | +#else | |
| 77 | + const bool useClientState = false; | |
| 78 | +#endif | |
| 79 | + if (useClientState) { | |
| 76 | 80 | if (newState) { |
| 77 | 81 | glEnableClientState(attributeSlotToClassicAttribName[i]); |
| 78 | 82 | } else { |
| 79 | 83 | glDisableClientState(attributeSlotToClassicAttribName[i]); |
| 80 | 84 | } |
| 81 | 85 | } else { |
| 82 | −#else | |
| 83 | − { | |
| 84 | −#endif | |
| 85 | 86 | if (newState) { |
| 86 | 87 | glEnableVertexAttribArray(i); |
| 87 | 88 | } else { |
| @@ -89,7 +90,7 @@ void GLBackend::updateInput() { · 1 unmodified line | ||
| 89 | 90 | } |
| 90 | 91 | } |
| 91 | 92 | (void) CHECK_GL_ERROR(); |
| 92 | − | |
| 93 | + | |
| 93 | 94 | _input._attributeActivation.flip(i); |
| 94 | 95 | } |
| 95 | 96 | } |
| @@ -123,29 +124,30 @@ void GLBackend::updateInput() { · 27 unmodified lines | ||
| 123 | 124 | GLenum type = _elementTypeToGLType[attrib._element.getType()]; |
| 124 | 125 | GLuint stride = strides[bufferNum]; |
| 125 | 126 | GLuint pointer = attrib._offset + offsets[bufferNum]; |
| 126 | − #if defined(SUPPORT_LEGACY_OPENGL) | |
| 127 | − if (slot < NUM_CLASSIC_ATTRIBS) { | |
| 127 | +#if defined(SUPPORT_LEGACY_OPENGL) | |
| 128 | + const bool useClientState = slot < NUM_CLASSIC_ATTRIBS; | |
| 129 | +#else | |
| 130 | + const bool useClientState = false; | |
| 131 | +#endif | |
| 132 | + if (useClientState) { | |
| 128 | 133 | switch (slot) { |
| 129 | − case Stream::POSITION: | |
| 130 | − glVertexPointer(count, type, stride, reinterpret_cast<GLvoid*>(pointer)); | |
| 131 | − break; | |
| 132 | − case Stream::NORMAL: | |
| 133 | − glNormalPointer(type, stride, reinterpret_cast<GLvoid*>(pointer)); | |
| 134 | − break; | |
| 135 | − case Stream::COLOR: | |
| 136 | − glColorPointer(count, type, stride, reinterpret_cast<GLvoid*>(pointer)); | |
| 137 | − break; | |
| 138 | − case Stream::TEXCOORD: | |
| 139 | − glTexCoordPointer(count, type, stride, reinterpret_cast<GLvoid*>(pointer)); | |
| 140 | − break; | |
| 134 | + case Stream::POSITION: | |
| 135 | + glVertexPointer(count, type, stride, reinterpret_cast<GLvoid*>(pointer)); | |
| 136 | + break; | |
| 137 | + case Stream::NORMAL: | |
| 138 | + glNormalPointer(type, stride, reinterpret_cast<GLvoid*>(pointer)); | |
| 139 | + break; | |
| 140 | + case Stream::COLOR: | |
| 141 | + glColorPointer(count, type, stride, reinterpret_cast<GLvoid*>(pointer)); | |
| 142 | + break; | |
| 143 | + case Stream::TEXCOORD: | |
| 144 | + glTexCoordPointer(count, type, stride, reinterpret_cast<GLvoid*>(pointer)); | |
| 145 | + break; | |
| 141 | 146 | }; |
| 142 | 147 | } else { |
| 143 | − #else | |
| 144 | − { | |
| 145 | − #endif | |
| 146 | 148 | GLboolean isNormalized = attrib._element.isNormalized(); |
| 147 | 149 | glVertexAttribPointer(slot, count, type, isNormalized, stride, |
| 148 | − reinterpret_cast<GLvoid*>(pointer)); | |
| 150 | + reinterpret_cast<GLvoid*>(pointer)); | |
| 149 | 151 | } |
| 150 | 152 | (void) CHECK_GL_ERROR(); |
| 151 | 153 | } |
| @@ -10,8 +10,8 @@ · 9 unmodified lines | ||
| 10 | 10 | // |
| 11 | 11 | |
| 12 | 12 | #include "Stream.h" |
| 13 | − | |
| 14 | −#include <algorithm> //min max and more | |
| 13 | + | |
| 14 | +#include <algorithm> //min max and more | |
| 15 | 15 | |
| 16 | 16 | using namespace gpu; |
| 17 | 17 | |
| @@ -17,11 +17,11 @@ · 16 unmodified lines | ||
| 17 | 17 | |
| 18 | 18 | namespace gpu { |
| 19 | 19 | |
| 20 | −// THe spherical harmonics is a nice tool for cubemap, so if required, the irradiance SH can be automatically generated | |
| 21 | −// with the cube texture | |
| 22 | −class Texture; | |
| 23 | −class SphericalHarmonics { | |
| 24 | −public: | |
| 20 | +// THe spherical harmonics is a nice tool for cubemap, so if required, the irradiance SH can be automatically generated | |
| 21 | +// with the cube texture | |
| 22 | +class Texture; | |
| 23 | +class SphericalHarmonics { | |
| 24 | +public: | |
| 25 | 25 | glm::vec3 L00 ; float spare0; |
| 26 | 26 | glm::vec3 L1m1 ; float spare1; |
| 27 | 27 | glm::vec3 L10 ; float spare2; |
| @@ -44,15 +44,15 @@ public: · 16 unmodified lines | ||
| 44 | 44 | VINE_STREET_KITCHEN, |
| 45 | 45 | BREEZEWAY, |
| 46 | 46 | CAMPUS_SUNSET, |
| 47 | − FUNSTON_BEACH_SUNSET, | |
| 48 | − | |
| 49 | − NUM_PRESET, | |
| 47 | + FUNSTON_BEACH_SUNSET, | |
| 48 | + | |
| 49 | + NUM_PRESET, | |
| 50 | 50 | }; |
| 51 | 51 | |
| 52 | 52 | void assignPreset(int p); |
| 53 | 53 | |
| 54 | 54 | void evalFromTexture(const Texture& texture); |
| 55 | −}; | |
| 55 | +}; | |
| 56 | 56 | typedef std::shared_ptr< SphericalHarmonics > SHPointer; |
| 57 | 57 | |
| 58 | 58 | class Sampler { |
| @@ -438,7 +438,7 @@ public: · 379 unmodified lines | ||
| 438 | 438 | explicit operator bool() const { return bool(_texture); } |
| 439 | 439 | bool operator !() const { return (!_texture); } |
| 440 | 440 | }; |
| 441 | −typedef std::vector<TextureView> TextureViews; | |
| 441 | +typedef std::vector<TextureView> TextureViews; | |
| 442 | 442 | |
| 443 | 443 | }; |
| 444 | 444 | |
| @@ -62,8 +62,8 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { · 61 unmodified lines | ||
| 62 | 62 | state->setCullMode(gpu::State::CULL_BACK); |
| 63 | 63 | state->setDepthTest(true, true, gpu::LESS_EQUAL); |
| 64 | 64 | state->setBlendFunction(false, |
| 65 | − gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, | |
| 66 | − gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); | |
| 65 | + gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, | |
| 66 | + gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); | |
| 67 | 67 | _simpleProgram = gpu::PipelinePointer(gpu::Pipeline::create(program, state)); |
| 68 | 68 | |
| 69 | 69 | _viewState = viewState; |
| @@ -815,9 +815,9 @@ void GeometryCache::renderSolidCube(gpu::Batch& batch, float size, const glm::ve · 814 unmodified lines | ||
| 815 | 815 | const int VERTEX_STRIDE = sizeof(GLfloat) * FLOATS_PER_VERTEX * 2; // vertices and normals |
| 816 | 816 | const int NORMALS_OFFSET = sizeof(GLfloat) * FLOATS_PER_VERTEX; |
| 817 | 817 | |
| 818 | − if (!_solidCubeVerticies.contains(size)) { | |
| 818 | + if (!_solidCubeVertices.contains(size)) { | |
| 819 | 819 | gpu::BufferPointer verticesBuffer(new gpu::Buffer()); |
| 820 | − _solidCubeVerticies[size] = verticesBuffer; | |
| 820 | + _solidCubeVertices[size] = verticesBuffer; | |
| 821 | 821 | |
| 822 | 822 | GLfloat* vertexData = new GLfloat[vertexPoints * 2]; // vertices and normals |
| 823 | 823 | GLfloat* vertex = vertexData; |
| @@ -892,7 +892,7 @@ void GeometryCache::renderSolidCube(gpu::Batch& batch, float size, const glm::ve · 68 unmodified lines | ||
| 892 | 892 | |
| 893 | 893 | colorBuffer->append(sizeof(colors), (gpu::Byte*) colors); |
| 894 | 894 | } |
| 895 | − gpu::BufferPointer verticesBuffer = _solidCubeVerticies[size]; | |
| 895 | + gpu::BufferPointer verticesBuffer = _solidCubeVertices[size]; | |
| 896 | 896 | gpu::BufferPointer colorBuffer = _solidCubeColors[colorKey]; |
| 897 | 897 | |
| 898 | 898 | const int VERTICES_SLOT = 0; |
| @@ -270,7 +270,7 @@ private: · 269 unmodified lines | ||
| 270 | 270 | QHash<Vec2Pair, gpu::BufferPointer> _cubeColors; |
| 271 | 271 | gpu::BufferPointer _wireCubeIndexBuffer; |
| 272 | 272 | |
| 273 | − QHash<float, gpu::BufferPointer> _solidCubeVerticies; | |
| 273 | + QHash<float, gpu::BufferPointer> _solidCubeVertices; | |
| 274 | 274 | QHash<Vec2Pair, gpu::BufferPointer> _solidCubeColors; |
| 275 | 275 | gpu::BufferPointer _solidCubeIndexBuffer; |
| 276 | 276 | |
| @@ -12,8 +12,6 @@ · 11 unmodified lines | ||
| 12 | 12 | #ifndef hifi_RenderUtil_h |
| 13 | 13 | #define hifi_RenderUtil_h |
| 14 | 14 | |
| 15 | −#include <MatrixStack.h> | |
| 16 | − | |
| 17 | 15 | /// Renders a quad from (-1, -1, 0) to (1, 1, 0) with texture coordinates from (sMin, tMin) to (sMax, tMax). |
| 18 | 16 | void renderFullscreenQuad(float sMin = 0.0f, float sMax = 1.0f, float tMin = 0.0f, float tMax = 1.0f); |
| 19 | 17 | |
| @@ -0,0 +1,498 @@ | ||
| 1 | +// | |
| 2 | +// TextRenderer3D.cpp | |
| 3 | +// interface/src/ui | |
| 4 | +// | |
| 5 | +// Created by Andrzej Kapolka on 4/24/13. | |
| 6 | +// Copyright 2013 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include "TextRenderer3D.h" | |
| 13 | + | |
| 14 | +#include <gpu/GPUConfig.h> | |
| 15 | +#include <gpu/GLBackend.h> | |
| 16 | +#include <gpu/Shader.h> | |
| 17 | + | |
| 18 | +#include <QBuffer> | |
| 19 | +#include <QImage> | |
| 20 | +#include <QStringList> | |
| 21 | +#include <QFile> | |
| 22 | + | |
| 23 | +#include "GLMHelpers.h" | |
| 24 | +#include "MatrixStack.h" | |
| 25 | +#include "RenderUtilsLogging.h" | |
| 26 | + | |
| 27 | +#include "sdf_text3D_vert.h" | |
| 28 | +#include "sdf_text3D_frag.h" | |
| 29 | + | |
| 30 | +#include "GeometryCache.h" | |
| 31 | +#include "DeferredLightingEffect.h" | |
| 32 | + | |
| 33 | +// FIXME support the shadow effect, or remove it from the API | |
| 34 | +// FIXME figure out how to improve the anti-aliasing on the | |
| 35 | +// interior of the outline fonts | |
| 36 | +const float DEFAULT_POINT_SIZE = 12; | |
| 37 | + | |
| 38 | +// Helper functions for reading binary data from an IO device | |
| 39 | +template<class T> | |
| 40 | +void readStream(QIODevice& in, T& t) { | |
| 41 | + in.read((char*) &t, sizeof(t)); | |
| 42 | +} | |
| 43 | + | |
| 44 | +template<typename T, size_t N> | |
| 45 | +void readStream(QIODevice& in, T (&t)[N]) { | |
| 46 | + in.read((char*) t, N); | |
| 47 | +} | |
| 48 | + | |
| 49 | +template<class T, size_t N> | |
| 50 | +void fillBuffer(QBuffer& buffer, T (&t)[N]) { | |
| 51 | + buffer.setData((const char*) t, N); | |
| 52 | +} | |
| 53 | + | |
| 54 | +// stores the font metrics for a single character | |
| 55 | +struct Glyph3D { | |
| 56 | + QChar c; | |
| 57 | + glm::vec2 texOffset; | |
| 58 | + glm::vec2 texSize; | |
| 59 | + glm::vec2 size; | |
| 60 | + glm::vec2 offset; | |
| 61 | + float d; // xadvance - adjusts character positioning | |
| 62 | + size_t indexOffset; | |
| 63 | + | |
| 64 | + // We adjust bounds because offset is the bottom left corner of the font but the top left corner of a QRect | |
| 65 | + QRectF bounds() const { return glmToRect(offset, size).translated(0.0f, -size.y); } | |
| 66 | + QRectF textureBounds() const { return glmToRect(texOffset, texSize); } | |
| 67 | + | |
| 68 | + void read(QIODevice& in); | |
| 69 | +}; | |
| 70 | + | |
| 71 | +void Glyph3D::read(QIODevice& in) { | |
| 72 | + uint16_t charcode; | |
| 73 | + readStream(in, charcode); | |
| 74 | + c = charcode; | |
| 75 | + readStream(in, texOffset); | |
| 76 | + readStream(in, size); | |
| 77 | + readStream(in, offset); | |
| 78 | + readStream(in, d); | |
| 79 | + texSize = size; | |
| 80 | +} | |
| 81 | + | |
| 82 | +struct TextureVertex { | |
| 83 | + glm::vec2 pos; | |
| 84 | + glm::vec2 tex; | |
| 85 | + TextureVertex() {} | |
| 86 | + TextureVertex(const glm::vec2& pos, const glm::vec2& tex) : pos(pos), tex(tex) {} | |
| 87 | +}; | |
| 88 | + | |
| 89 | +struct QuadBuilder { | |
| 90 | + TextureVertex vertices[4]; | |
| 91 | + QuadBuilder(const glm::vec2& min, const glm::vec2& size, | |
| 92 | + const glm::vec2& texMin, const glm::vec2& texSize) { | |
| 93 | + // min = bottomLeft | |
| 94 | + vertices[0] = TextureVertex(min, | |
| 95 | + texMin + glm::vec2(0.0f, texSize.y)); | |
| 96 | + vertices[1] = TextureVertex(min + glm::vec2(size.x, 0.0f), | |
| 97 | + texMin + texSize); | |
| 98 | + vertices[2] = TextureVertex(min + size, | |
| 99 | + texMin + glm::vec2(texSize.x, 0.0f)); | |
| 100 | + vertices[3] = TextureVertex(min + glm::vec2(0.0f, size.y), | |
| 101 | + texMin); | |
| 102 | + } | |
| 103 | + QuadBuilder(const Glyph3D& glyph, const glm::vec2& offset) : | |
| 104 | + QuadBuilder(offset + glyph.offset - glm::vec2(0.0f, glyph.size.y), glyph.size, | |
| 105 | + glyph.texOffset, glyph.texSize) {} | |
| 106 | + | |
| 107 | +}; | |
| 108 | + | |
| 109 | +class Font3D { | |
| 110 | +public: | |
| 111 | + Font3D(); | |
| 112 | + | |
| 113 | + void read(QIODevice& path); | |
| 114 | + | |
| 115 | + glm::vec2 computeExtent(const QString& str) const; | |
| 116 | + float getRowHeight() const { return _rowHeight; } | |
| 117 | + | |
| 118 | + // Render string to batch | |
| 119 | + void drawString(gpu::Batch& batch, float x, float y, const QString& str, | |
| 120 | + const glm::vec4& color, TextRenderer3D::EffectType effectType, | |
| 121 | + const glm::vec2& bound); | |
| 122 | + | |
| 123 | +private: | |
| 124 | + QStringList tokenizeForWrapping(const QString& str) const; | |
| 125 | + QStringList splitLines(const QString& str) const; | |
| 126 | + glm::vec2 computeTokenExtent(const QString& str) const; | |
| 127 | + | |
| 128 | + const Glyph3D& getGlyph(const QChar& c) const; | |
| 129 | + | |
| 130 | + void setupGPU(); | |
| 131 | + | |
| 132 | + // maps characters to cached glyph info | |
| 133 | + // HACK... the operator[] const for QHash returns a | |
| 134 | + // copy of the value, not a const value reference, so | |
| 135 | + // we declare the hash as mutable in order to avoid such | |
| 136 | + // copies | |
| 137 | + mutable QHash<QChar, Glyph3D> _glyphs; | |
| 138 | + | |
| 139 | + // Font characteristics | |
| 140 | + QString _family; | |
| 141 | + float _fontSize = 0.0f; | |
| 142 | + float _rowHeight = 0.0f; | |
| 143 | + float _leading = 0.0f; | |
| 144 | + float _ascent = 0.0f; | |
| 145 | + float _descent = 0.0f; | |
| 146 | + float _spaceWidth = 0.0f; | |
| 147 | + | |
| 148 | + bool _initialized = false; | |
| 149 | + | |
| 150 | + // gpu structures | |
| 151 | + gpu::PipelinePointer _pipeline; | |
| 152 | + gpu::TexturePointer _texture; | |
| 153 | + gpu::Stream::FormatPointer _format; | |
| 154 | + gpu::BufferPointer _verticesBuffer; | |
| 155 | + gpu::BufferStreamPointer _stream; | |
| 156 | + unsigned int _numVertices = 0; | |
| 157 | + | |
| 158 | + int _fontLoc = -1; | |
| 159 | + int _outlineLoc = -1; | |
| 160 | + int _colorLoc = -1; | |
| 161 | + | |
| 162 | + // last string render characteristics | |
| 163 | + QString _lastStringRendered; | |
| 164 | + glm::vec2 _lastBounds; | |
| 165 | +}; | |
| 166 | + | |
| 167 | +static QHash<QString, Font3D*> LOADED_FONTS; | |
| 168 | + | |
| 169 | +Font3D* loadFont3D(QIODevice& fontFile) { | |
| 170 | + Font3D* result = new Font3D(); | |
| 171 | + result->read(fontFile); | |
| 172 | + return result; | |
| 173 | +} | |
| 174 | + | |
| 175 | +Font3D* loadFont3D(const QString& family) { | |
| 176 | + if (!LOADED_FONTS.contains(family)) { | |
| 177 | + | |
| 178 | + const QString SDFF_COURIER_PRIME_FILENAME = ":/CourierPrime.sdff"; | |
| 179 | + const QString SDFF_INCONSOLATA_MEDIUM_FILENAME = ":/InconsolataMedium.sdff"; | |
| 180 | + const QString SDFF_ROBOTO_FILENAME = ":/Roboto.sdff"; | |
| 181 | + const QString SDFF_TIMELESS_FILENAME = ":/Timeless.sdff"; | |
| 182 | + | |
| 183 | + QString loadFilename; | |
| 184 | + | |
| 185 | + if (family == MONO_FONT_FAMILY) { | |
| 186 | + loadFilename = SDFF_COURIER_PRIME_FILENAME; | |
| 187 | + } else if (family == INCONSOLATA_FONT_FAMILY) { | |
| 188 | + loadFilename = SDFF_INCONSOLATA_MEDIUM_FILENAME; | |
| 189 | + } else if (family == SANS_FONT_FAMILY) { | |
| 190 | + loadFilename = SDFF_ROBOTO_FILENAME; | |
| 191 | + } else { | |
| 192 | + if (!LOADED_FONTS.contains(SERIF_FONT_FAMILY)) { | |
| 193 | + loadFilename = SDFF_TIMELESS_FILENAME; | |
| 194 | + } else { | |
| 195 | + LOADED_FONTS[family] = LOADED_FONTS[SERIF_FONT_FAMILY]; | |
| 196 | + } | |
| 197 | + } | |
| 198 | + | |
| 199 | + if (!loadFilename.isEmpty()) { | |
| 200 | + QFile fontFile(loadFilename); | |
| 201 | + fontFile.open(QIODevice::ReadOnly); | |
| 202 | + | |
| 203 | + qCDebug(renderutils) << "Loaded font" << loadFilename << "from Qt Resource System."; | |
| 204 | + | |
| 205 | + LOADED_FONTS[family] = loadFont3D(fontFile); | |
| 206 | + } | |
| 207 | + } | |
| 208 | + return LOADED_FONTS[family]; | |
| 209 | +} | |
| 210 | + | |
| 211 | +Font3D::Font3D() { | |
| 212 | + static bool fontResourceInitComplete = false; | |
| 213 | + if (!fontResourceInitComplete) { | |
| 214 | + Q_INIT_RESOURCE(fonts); | |
| 215 | + fontResourceInitComplete = true; | |
| 216 | + } | |
| 217 | +} | |
| 218 | + | |
| 219 | +// NERD RAGE: why doesn't QHash have a 'const T & operator[] const' member | |
| 220 | +const Glyph3D& Font3D::getGlyph(const QChar& c) const { | |
| 221 | + if (!_glyphs.contains(c)) { | |
| 222 | + return _glyphs[QChar('?')]; | |
| 223 | + } | |
| 224 | + return _glyphs[c]; | |
| 225 | +} | |
| 226 | + | |
| 227 | +QStringList Font3D::splitLines(const QString& str) const { | |
| 228 | + return str.split('\n'); | |
| 229 | +} | |
| 230 | + | |
| 231 | +QStringList Font3D::tokenizeForWrapping(const QString& str) const { | |
| 232 | + QStringList tokens; | |
| 233 | + for(auto line : splitLines(str)) { | |
| 234 | + if (!tokens.empty()) { | |
| 235 | + tokens << QString('\n'); | |
| 236 | + } | |
| 237 | + tokens << line.split(' '); | |
| 238 | + } | |
| 239 | + return tokens; | |
| 240 | +} | |
| 241 | + | |
| 242 | +glm::vec2 Font3D::computeTokenExtent(const QString& token) const { | |
| 243 | + glm::vec2 advance(0, _fontSize); | |
| 244 | + foreach(QChar c, token) { | |
| 245 | + Q_ASSERT(c != '\n'); | |
| 246 | + advance.x += (c == ' ') ? _spaceWidth : getGlyph(c).d; | |
| 247 | + } | |
| 248 | + return advance; | |
| 249 | +} | |
| 250 | + | |
| 251 | +glm::vec2 Font3D::computeExtent(const QString& str) const { | |
| 252 | + glm::vec2 extent = glm::vec2(0.0f, 0.0f); | |
| 253 | + | |
| 254 | + QStringList tokens = splitLines(str); | |
| 255 | + foreach(const QString& token, tokens) { | |
| 256 | + glm::vec2 tokenExtent = computeTokenExtent(token); | |
| 257 | + extent.x = std::max(tokenExtent.x, extent.x); | |
| 258 | + } | |
| 259 | + extent.y = tokens.count() * _rowHeight; | |
| 260 | + | |
| 261 | + return extent; | |
| 262 | +} | |
| 263 | + | |
| 264 | +void Font3D::read(QIODevice& in) { | |
| 265 | + uint8_t header[4]; | |
| 266 | + readStream(in, header); | |
| 267 | + if (memcmp(header, "SDFF", 4)) { | |
| 268 | + qFatal("Bad SDFF file"); | |
| 269 | + } | |
| 270 | + | |
| 271 | + uint16_t version; | |
| 272 | + readStream(in, version); | |
| 273 | + | |
| 274 | + // read font name | |
| 275 | + _family = ""; | |
| 276 | + if (version > 0x0001) { | |
| 277 | + char c; | |
| 278 | + readStream(in, c); | |
| 279 | + while (c) { | |
| 280 | + _family += c; | |
| 281 | + readStream(in, c); | |
| 282 | + } | |
| 283 | + } | |
| 284 | + | |
| 285 | + // read font data | |
| 286 | + readStream(in, _leading); | |
| 287 | + readStream(in, _ascent); | |
| 288 | + readStream(in, _descent); | |
| 289 | + readStream(in, _spaceWidth); | |
| 290 | + _fontSize = _ascent + _descent; | |
| 291 | + _rowHeight = _fontSize + _leading; | |
| 292 | + | |
| 293 | + // Read character count | |
| 294 | + uint16_t count; | |
| 295 | + readStream(in, count); | |
| 296 | + // read metrics data for each character | |
| 297 | + QVector<Glyph3D> glyphs(count); | |
| 298 | + // std::for_each instead of Qt foreach because we need non-const references | |
| 299 | + std::for_each(glyphs.begin(), glyphs.end(), [&](Glyph3D& g) { | |
| 300 | + g.read(in); | |
| 301 | + }); | |
| 302 | + | |
| 303 | + // read image data | |
| 304 | + QImage image; | |
| 305 | + if (!image.loadFromData(in.readAll(), "PNG")) { | |
| 306 | + qFatal("Failed to read SDFF image"); | |
| 307 | + } | |
| 308 | + | |
| 309 | + _glyphs.clear(); | |
| 310 | + glm::vec2 imageSize = toGlm(image.size()); | |
| 311 | + foreach(Glyph3D g, glyphs) { | |
| 312 | + // Adjust the pixel texture coordinates into UV coordinates, | |
| 313 | + g.texSize /= imageSize; | |
| 314 | + g.texOffset /= imageSize; | |
| 315 | + // store in the character to glyph hash | |
| 316 | + _glyphs[g.c] = g; | |
| 317 | + }; | |
| 318 | + | |
| 319 | + image = image.convertToFormat(QImage::Format_RGBA8888); | |
| 320 | + | |
| 321 | + gpu::Element formatGPU = gpu::Element(gpu::VEC3, gpu::UINT8, gpu::RGB); | |
| 322 | + gpu::Element formatMip = gpu::Element(gpu::VEC3, gpu::UINT8, gpu::RGB); | |
| 323 | + if (image.hasAlphaChannel()) { | |
| 324 | + formatGPU = gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA); | |
| 325 | + formatMip = gpu::Element(gpu::VEC4, gpu::UINT8, gpu::BGRA); | |
| 326 | + } | |
| 327 | + _texture = gpu::TexturePointer(gpu::Texture::create2D(formatGPU, image.width(), image.height(), | |
| 328 | + gpu::Sampler(gpu::Sampler::FILTER_MIN_POINT_MAG_LINEAR))); | |
| 329 | + _texture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits()); | |
| 330 | + _texture->autoGenerateMips(-1); | |
| 331 | +} | |
| 332 | + | |
| 333 | +void Font3D::setupGPU() { | |
| 334 | + if (!_initialized) { | |
| 335 | + _initialized = true; | |
| 336 | + | |
| 337 | + // Setup render pipeline | |
| 338 | + auto vertexShader = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(sdf_text3D_vert))); | |
| 339 | + auto pixelShader = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(sdf_text3D_frag))); | |
| 340 | + gpu::ShaderPointer program = gpu::ShaderPointer(gpu::Shader::createProgram(vertexShader, pixelShader)); | |
| 341 | + | |
| 342 | + gpu::Shader::BindingSet slotBindings; | |
| 343 | + gpu::Shader::makeProgram(*program, slotBindings); | |
| 344 | + | |
| 345 | + _fontLoc = program->getTextures().findLocation("Font"); | |
| 346 | + _outlineLoc = program->getUniforms().findLocation("Outline"); | |
| 347 | + _colorLoc = program->getUniforms().findLocation("Color"); | |
| 348 | + | |
| 349 | + gpu::StatePointer state = gpu::StatePointer(new gpu::State()); | |
| 350 | + state->setCullMode(gpu::State::CULL_BACK); | |
| 351 | + state->setDepthTest(true, true, gpu::LESS_EQUAL); | |
| 352 | + state->setBlendFunction(false, | |
| 353 | + gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, | |
| 354 | + gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); | |
| 355 | + _pipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, state)); | |
| 356 | + | |
| 357 | + // Sanity checks | |
| 358 | + static const int OFFSET = offsetof(TextureVertex, tex); | |
| 359 | + assert(OFFSET == sizeof(glm::vec2)); | |
| 360 | + assert(sizeof(glm::vec2) == 2 * sizeof(float)); | |
| 361 | + assert(sizeof(TextureVertex) == 2 * sizeof(glm::vec2)); | |
| 362 | + assert(sizeof(QuadBuilder) == 4 * sizeof(TextureVertex)); | |
| 363 | + | |
| 364 | + // Setup rendering structures | |
| 365 | + _format.reset(new gpu::Stream::Format()); | |
| 366 | + _format->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0); | |
| 367 | + _format->setAttribute(gpu::Stream::TEXCOORD, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV), OFFSET); | |
| 368 | + } | |
| 369 | +} | |
| 370 | + | |
| 371 | +void Font3D::drawString(gpu::Batch& batch, float x, float y, const QString& str, const glm::vec4& color, | |
| 372 | + TextRenderer3D::EffectType effectType, const glm::vec2& bounds) { | |
| 373 | + if (str == "") { | |
| 374 | + return; | |
| 375 | + } | |
| 376 | + | |
| 377 | + if (str != _lastStringRendered || bounds != _lastBounds) { | |
| 378 | + _verticesBuffer.reset(new gpu::Buffer()); | |
| 379 | + _numVertices = 0; | |
| 380 | + _lastStringRendered = str; | |
| 381 | + _lastBounds = bounds; | |
| 382 | + | |
| 383 | + // Top left of text | |
| 384 | + glm::vec2 advance = glm::vec2(x, y); | |
| 385 | + foreach(const QString& token, tokenizeForWrapping(str)) { | |
| 386 | + bool isNewLine = (token == QString('\n')); | |
| 387 | + bool forceNewLine = false; | |
| 388 | + | |
| 389 | + // Handle wrapping | |
| 390 | + if (!isNewLine && (bounds.x != -1) && (advance.x + computeExtent(token).x > x + bounds.x)) { | |
| 391 | + // We are out of the x bound, force new line | |
| 392 | + forceNewLine = true; | |
| 393 | + } | |
| 394 | + if (isNewLine || forceNewLine) { | |
| 395 | + // Character return, move the advance to a new line | |
| 396 | + advance = glm::vec2(x, advance.y - _rowHeight); | |
| 397 | + | |
| 398 | + if (isNewLine) { | |
| 399 | + // No need to draw anything, go directly to next token | |
| 400 | + continue; | |
| 401 | + } else if (computeExtent(token).x > bounds.x) { | |
| 402 | + // token will never fit, stop drawing | |
| 403 | + break; | |
| 404 | + } | |
| 405 | + } | |
| 406 | + if ((bounds.y != -1) && (advance.y - _fontSize < -y - bounds.y)) { | |
| 407 | + // We are out of the y bound, stop drawing | |
| 408 | + break; | |
| 409 | + } | |
| 410 | + | |
| 411 | + // Draw the token | |
| 412 | + if (!isNewLine) { | |
| 413 | + for (auto c : token) { | |
| 414 | + auto glyph = _glyphs[c]; | |
| 415 | + | |
| 416 | + QuadBuilder qd(glyph, advance - glm::vec2(0.0f, _fontSize)); | |
| 417 | + _verticesBuffer->append(sizeof(QuadBuilder), (const gpu::Byte*)&qd); | |
| 418 | + _numVertices += 4; | |
| 419 | + | |
| 420 | + // Advance by glyph size | |
| 421 | + advance.x += glyph.d; | |
| 422 | + } | |
| 423 | + | |
| 424 | + // Add space after all non return tokens | |
| 425 | + advance.x += _spaceWidth; | |
| 426 | + } | |
| 427 | + } | |
| 428 | + } | |
| 429 | + | |
| 430 | + setupGPU(); | |
| 431 | + batch.setPipeline(_pipeline); | |
| 432 | + batch.setUniformTexture(_fontLoc, _texture); | |
| 433 | + batch._glUniform1f(_outlineLoc, (effectType == TextRenderer3D::OUTLINE_EFFECT) ? 1.0f : 0.0f); | |
| 434 | + batch._glUniform4fv(_colorLoc, 1, (const GLfloat*)&color); | |
| 435 | + | |
| 436 | + batch.setInputFormat(_format); | |
| 437 | + batch.setInputBuffer(0, _verticesBuffer, 0, _format->getChannels().at(0)._stride); | |
| 438 | + batch.draw(gpu::QUADS, _numVertices, 0); | |
| 439 | +} | |
| 440 | + | |
| 441 | +TextRenderer3D* TextRenderer3D::getInstance(const char* family, float pointSize, | |
| 442 | + int weight, bool italic, EffectType effect, int effectThickness, | |
| 443 | + const QColor& color) { | |
| 444 | + if (pointSize < 0) { | |
| 445 | + pointSize = DEFAULT_POINT_SIZE; | |
| 446 | + } | |
| 447 | + return new TextRenderer3D(family, pointSize, weight, italic, effect, | |
| 448 | + effectThickness, color); | |
| 449 | +} | |
| 450 | + | |
| 451 | +TextRenderer3D::TextRenderer3D(const char* family, float pointSize, int weight, bool italic, | |
| 452 | + EffectType effect, int effectThickness, const QColor& color) : | |
| 453 | + _effectType(effect), | |
| 454 | + _effectThickness(effectThickness), | |
| 455 | + _pointSize(pointSize), | |
| 456 | + _color(toGlm(color)), | |
| 457 | + _font(loadFont3D(family)) { | |
| 458 | + if (!_font) { | |
| 459 | + qWarning() << "Unable to load font with family " << family; | |
| 460 | + _font = loadFont3D("Courier"); | |
| 461 | + } | |
| 462 | + if (1 != _effectThickness) { | |
| 463 | + qWarning() << "Effect thickness not current supported"; | |
| 464 | + } | |
| 465 | + if (NO_EFFECT != _effectType && OUTLINE_EFFECT != _effectType) { | |
| 466 | + qWarning() << "Effect thickness not current supported"; | |
| 467 | + } | |
| 468 | +} | |
| 469 | + | |
| 470 | +TextRenderer3D::~TextRenderer3D() { | |
| 471 | +} | |
| 472 | + | |
| 473 | +glm::vec2 TextRenderer3D::computeExtent(const QString& str) const { | |
| 474 | + if (_font) { | |
| 475 | + return _font->computeExtent(str); | |
| 476 | + } | |
| 477 | + return glm::vec2(0.0f, 0.0f); | |
| 478 | +} | |
| 479 | + | |
| 480 | +float TextRenderer3D::getRowHeight() const { | |
| 481 | + if (_font) { | |
| 482 | + return _font->getRowHeight(); | |
| 483 | + } | |
| 484 | + return 0.0f; | |
| 485 | +} | |
| 486 | + | |
| 487 | +void TextRenderer3D::draw(gpu::Batch& batch, float x, float y, const QString& str, const glm::vec4& color, | |
| 488 | + const glm::vec2& bounds) { | |
| 489 | + // The font does all the OpenGL work | |
| 490 | + if (_font) { | |
| 491 | + glm::vec4 actualColor(color); | |
| 492 | + if (actualColor.r < 0) { | |
| 493 | + actualColor = _color; | |
| 494 | + } | |
| 495 | + _font->drawString(batch, x, y, str, actualColor, _effectType, bounds); | |
| 496 | + } | |
| 497 | +} | |
| 498 | + | |
| @@ -0,0 +1,77 @@ | ||
| 1 | +// | |
| 2 | +// TextRenderer3D.h | |
| 3 | +// interface/src/ui | |
| 4 | +// | |
| 5 | +// Created by Andrzej Kapolka on 4/26/13. | |
| 6 | +// Copyright 2013 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#ifndef hifi_TextRenderer3D_h | |
| 13 | +#define hifi_TextRenderer3D_h | |
| 14 | + | |
| 15 | +#include <glm/glm.hpp> | |
| 16 | +#include <QColor> | |
| 17 | + | |
| 18 | +// the standard sans serif font family | |
| 19 | +#define SANS_FONT_FAMILY "Helvetica" | |
| 20 | + | |
| 21 | +// the standard sans serif font family | |
| 22 | +#define SERIF_FONT_FAMILY "Timeless" | |
| 23 | + | |
| 24 | +// the standard mono font family | |
| 25 | +#define MONO_FONT_FAMILY "Courier" | |
| 26 | + | |
| 27 | +// the Inconsolata font family | |
| 28 | +#ifdef Q_OS_WIN | |
| 29 | +#define INCONSOLATA_FONT_FAMILY "Fixedsys" | |
| 30 | +#define INCONSOLATA_FONT_WEIGHT QFont::Normal | |
| 31 | +#else | |
| 32 | +#define INCONSOLATA_FONT_FAMILY "Inconsolata" | |
| 33 | +#define INCONSOLATA_FONT_WEIGHT QFont::Bold | |
| 34 | +#endif | |
| 35 | + | |
| 36 | +namespace gpu { | |
| 37 | +class Batch; | |
| 38 | +} | |
| 39 | +class Font3D; | |
| 40 | + | |
| 41 | +// TextRenderer3D is actually a fairly thin wrapper around a Font class | |
| 42 | +// defined in the cpp file. | |
| 43 | +class TextRenderer3D { | |
| 44 | +public: | |
| 45 | + enum EffectType { NO_EFFECT, SHADOW_EFFECT, OUTLINE_EFFECT }; | |
| 46 | + | |
| 47 | + static TextRenderer3D* getInstance(const char* family, float pointSize = -1, int weight = -1, bool italic = false, | |
| 48 | + EffectType effect = NO_EFFECT, int effectThickness = 1, const QColor& color = QColor(255, 255, 255)); | |
| 49 | + | |
| 50 | + ~TextRenderer3D(); | |
| 51 | + | |
| 52 | + glm::vec2 computeExtent(const QString& str) const; | |
| 53 | + float getRowHeight() const; | |
| 54 | + | |
| 55 | + void draw(gpu::Batch& batch, float x, float y, const QString& str, const glm::vec4& color = glm::vec4(-1.0f), | |
| 56 | + const glm::vec2& bounds = glm::vec2(-1.0f)); | |
| 57 | + | |
| 58 | +private: | |
| 59 | + TextRenderer3D(const char* family, float pointSize = -1, int weight = -1, bool italic = false, | |
| 60 | + EffectType effect = NO_EFFECT, int effectThickness = 1, const QColor& color = QColor(255, 255, 255)); | |
| 61 | + | |
| 62 | + // the type of effect to apply | |
| 63 | + const EffectType _effectType; | |
| 64 | + | |
| 65 | + // the thickness of the effect | |
| 66 | + const int _effectThickness; | |
| 67 | + | |
| 68 | + const float _pointSize; | |
| 69 | + | |
| 70 | + // text color | |
| 71 | + const glm::vec4 _color; | |
| 72 | + | |
| 73 | + Font3D* _font; | |
| 74 | +}; | |
| 75 | + | |
| 76 | + | |
| 77 | +#endif // hifi_TextRenderer3D_h | |
| @@ -296,7 +296,7 @@ GLuint TextureCache::getShadowDepthTextureID() { · 295 unmodified lines | ||
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | /// Returns a texture version of an image file |
| 299 | −gpu::TexturePointer TextureCache::getImageTexture(const QString & path) { | |
| 299 | +gpu::TexturePointer TextureCache::getImageTexture(const QString& path) { | |
| 300 | 300 | QImage image = QImage(path).mirrored(false, true); |
| 301 | 301 | gpu::Element formatGPU = gpu::Element(gpu::VEC3, gpu::UINT8, gpu::RGB); |
| 302 | 302 | gpu::Element formatMip = gpu::Element(gpu::VEC3, gpu::UINT8, gpu::RGB); |
| @@ -383,9 +383,9 @@ ImageReader::ImageReader(const QWeakPointer<Resource>& texture, TextureType type · 80 unmodified lines | ||
| 383 | 383 | _content(content) { |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | −std::once_flag onceListSuppoertedFormatsflag; | |
| 386 | +std::once_flag onceListSupportedFormatsflag; | |
| 387 | 387 | void listSupportedImageFormats() { |
| 388 | − std::call_once(onceListSuppoertedFormatsflag, [](){ | |
| 388 | + std::call_once(onceListSupportedFormatsflag, [](){ | |
| 389 | 389 | auto supportedFormats = QImageReader::supportedImageFormats(); |
| 390 | 390 | QString formats; |
| 391 | 391 | foreach(const QByteArray& f, supportedFormats) { |
| @@ -56,7 +56,7 @@ public: · 55 unmodified lines | ||
| 56 | 56 | const gpu::TexturePointer& getBlueTexture(); |
| 57 | 57 | |
| 58 | 58 | /// Returns a texture version of an image file |
| 59 | − gpu::TexturePointer getImageTexture(const QString & path); | |
| 59 | + static gpu::TexturePointer getImageTexture(const QString& path); | |
| 60 | 60 | |
| 61 | 61 | /// Loads a texture from the specified URL. |
| 62 | 62 | NetworkTexturePointer getTexture(const QUrl& url, TextureType type = DEFAULT_TEXTURE, bool dilatable = false, |
| @@ -0,0 +1,47 @@ | ||
| 1 | +<@include gpu/Config.slh@> | |
| 2 | +<$VERSION_HEADER$> | |
| 3 | +// Generated on <$_SCRIBE_DATE$> | |
| 4 | +// sdf_text.frag | |
| 5 | +// fragment shader | |
| 6 | +// | |
| 7 | +// Created by Bradley Austin Davis on 2015-02-04 | |
| 8 | +// Based on fragment shader code from | |
| 9 | +// https://github.com/paulhoux/Cinder-Samples/blob/master/TextRendering/include/text/Text.cpp | |
| 10 | +// Distributed under the Apache License, Version 2.0. | |
| 11 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 12 | + | |
| 13 | +uniform sampler2D Font; | |
| 14 | +uniform float Outline; | |
| 15 | +uniform vec4 Color; | |
| 16 | + | |
| 17 | +const float gamma = 2.6; | |
| 18 | +const float smoothing = 100.0; | |
| 19 | +const float interiorCutoff = 0.8; | |
| 20 | +const float outlineExpansion = 0.2; | |
| 21 | + | |
| 22 | +void main() { | |
| 23 | + // retrieve signed distance | |
| 24 | + float sdf = texture2D(Font, gl_TexCoord[0].xy).g; | |
| 25 | + if (Outline == 1.0f) { | |
| 26 | + if (sdf > interiorCutoff) { | |
| 27 | + sdf = 1.0 - sdf; | |
| 28 | + } else { | |
| 29 | + sdf += outlineExpansion; | |
| 30 | + } | |
| 31 | + } | |
| 32 | + // perform adaptive anti-aliasing of the edges | |
| 33 | + // The larger we're rendering, the less anti-aliasing we need | |
| 34 | + float s = smoothing * length(fwidth(gl_TexCoord[0])); | |
| 35 | + float w = clamp( s, 0.0, 0.5); | |
| 36 | + float a = smoothstep(0.5 - w, 0.5 + w, sdf); | |
| 37 | + | |
| 38 | + // gamma correction for linear attenuation | |
| 39 | + a = pow(a, 1.0 / gamma); | |
| 40 | + | |
| 41 | + if (a < 0.01) { | |
| 42 | + discard; | |
| 43 | + } | |
| 44 | + | |
| 45 | + // final color | |
| 46 | + gl_FragColor = vec4(Color.rgb, a); | |
| 47 | +} | |
| @@ -0,0 +1,23 @@ | ||
| 1 | +<@include gpu/Config.slh@> | |
| 2 | +<$VERSION_HEADER$> | |
| 3 | +// Generated on <$_SCRIBE_DATE$> | |
| 4 | +// sdf_text.vert | |
| 5 | +// vertex shader | |
| 6 | +// | |
| 7 | +// Created by Brad Davis on 10/14/13. | |
| 8 | +// | |
| 9 | +// Distributed under the Apache License, Version 2.0. | |
| 10 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 11 | +// | |
| 12 | +<@include gpu/Transform.slh@> | |
| 13 | + | |
| 14 | +<$declareStandardTransform()$> | |
| 15 | + | |
| 16 | +void main() { | |
| 17 | + gl_TexCoord[0] = gl_MultiTexCoord0; | |
| 18 | + | |
| 19 | + // standard transform | |
| 20 | + TransformCamera cam = getTransformCamera(); | |
| 21 | + TransformObject obj = getTransformObject(); | |
| 22 | + <$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$> | |
| 23 | +} | |
| @@ -16,96 +16,259 @@ · 15 unmodified lines | ||
| 16 | 16 | #include "gpu/Batch.h" |
| 17 | 17 | #include "gpu/Context.h" |
| 18 | 18 | |
| 19 | +#include "ViewFrustum.h" | |
| 20 | +#include "RenderArgs.h" | |
| 21 | + | |
| 22 | +#include <algorithm> | |
| 23 | +#include <assert.h> | |
| 19 | 24 | |
| 20 | 25 | using namespace render; |
| 21 | 26 | |
| 22 | −Job::~Job() { | |
| 23 | −} | |
| 27 | +DrawSceneTask::DrawSceneTask() : Task() { | |
| 24 | 28 | |
| 25 | −template <> void render::jobRun(const FilterItems& filterItems, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { | |
| 26 | − auto& scene = sceneContext->_scene; | |
| 29 | + _jobs.push_back(Job(DrawOpaque())); | |
| 30 | + _jobs.push_back(Job(DrawLight())); | |
| 31 | + _jobs.push_back(Job(DrawTransparent())); | |
| 32 | +} | |
| 27 | 33 | |
| 34 | +DrawSceneTask::~DrawSceneTask() { | |
| 28 | 35 | } |
| 29 | 36 | |
| 30 | −template <> void render::jobRun(const RenderItems& renderItems, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { | |
| 31 | − auto& scene = sceneContext->_scene; | |
| 32 | − RenderArgs* args = renderContext->args; | |
| 33 | − // render | |
| 34 | − for (auto id : renderItems._items) { | |
| 35 | − auto item = scene->getItem(id); | |
| 36 | − item.render(args); | |
| 37 | +void DrawSceneTask::run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { | |
| 38 | + // sanity checks | |
| 39 | + assert(sceneContext); | |
| 40 | + if (!sceneContext->_scene) { | |
| 41 | + return; | |
| 37 | 42 | } |
| 38 | −} | |
| 39 | 43 | |
| 40 | 44 | |
| 45 | + // Is it possible that we render without a viewFrustum ? | |
| 46 | + if (!(renderContext->args && renderContext->args->_viewFrustum)) { | |
| 47 | + return; | |
| 48 | + } | |
| 49 | + | |
| 50 | + for (auto job : _jobs) { | |
| 51 | + job.run(sceneContext, renderContext); | |
| 52 | + } | |
| 53 | +}; | |
| 41 | 54 | |
| 55 | +Job::~Job() { | |
| 56 | +} | |
| 42 | 57 | |
| 43 | 58 | template <> void render::jobRun(const DrawOpaque& job, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { |
| 59 | + assert(renderContext->args); | |
| 60 | + assert(renderContext->args->_viewFrustum); | |
| 61 | + | |
| 44 | 62 | // render opaques |
| 45 | 63 | auto& scene = sceneContext->_scene; |
| 46 | 64 | auto& items = scene->getMasterBucket().at(ItemFilter::Builder::opaqueShape()); |
| 47 | 65 | |
| 48 | − RenderArgs* args = renderContext->args; | |
| 49 | − gpu::Batch theBatch; | |
| 50 | − args->_batch = &theBatch; | |
| 66 | + ItemIDs inItems; | |
| 67 | + inItems.reserve(items.size()); | |
| 51 | 68 | for (auto id : items) { |
| 52 | − auto item = scene->getItem(id); | |
| 53 | − item.render(args); | |
| 69 | + inItems.push_back(id); | |
| 54 | 70 | } |
| 55 | 71 | |
| 56 | − args->_context->enqueueBatch((*args->_batch)); | |
| 57 | − args->_batch = nullptr; | |
| 72 | + ItemIDs culledItems; | |
| 73 | + cullItems(sceneContext, renderContext, inItems, culledItems); | |
| 74 | + | |
| 75 | + ItemIDs sortedItems; | |
| 76 | + depthSortItems(sceneContext, renderContext, true, culledItems, sortedItems); // Sort Front to back opaque items! | |
| 77 | + | |
| 78 | + renderItems(sceneContext, renderContext, sortedItems); | |
| 58 | 79 | } |
| 59 | 80 | |
| 60 | 81 | |
| 61 | 82 | template <> void render::jobRun(const DrawTransparent& job, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { |
| 83 | + assert(renderContext->args); | |
| 84 | + assert(renderContext->args->_viewFrustum); | |
| 85 | + | |
| 62 | 86 | // render transparents |
| 63 | 87 | auto& scene = sceneContext->_scene; |
| 64 | 88 | auto& items = scene->getMasterBucket().at(ItemFilter::Builder::transparentShape()); |
| 65 | 89 | |
| 66 | − RenderArgs* args = renderContext->args; | |
| 67 | − gpu::Batch theBatch; | |
| 68 | − args->_batch = &theBatch; | |
| 90 | + ItemIDs inItems; | |
| 91 | + inItems.reserve(items.size()); | |
| 69 | 92 | for (auto id : items) { |
| 70 | − auto item = scene->getItem(id); | |
| 71 | − item.render(args); | |
| 93 | + inItems.push_back(id); | |
| 72 | 94 | } |
| 73 | 95 | |
| 74 | − args->_context->enqueueBatch((*args->_batch)); | |
| 75 | − args->_batch = nullptr; | |
| 96 | + ItemIDs culledItems; | |
| 97 | + cullItems(sceneContext, renderContext, inItems, culledItems); | |
| 98 | + | |
| 99 | + ItemIDs sortedItems; | |
| 100 | + depthSortItems(sceneContext, renderContext, false, culledItems, sortedItems); // Sort Back to front transparent items! | |
| 101 | + | |
| 102 | + renderItems(sceneContext, renderContext, sortedItems); | |
| 76 | 103 | } |
| 77 | 104 | |
| 78 | 105 | template <> void render::jobRun(const DrawLight& job, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { |
| 106 | + assert(renderContext->args); | |
| 107 | + assert(renderContext->args->_viewFrustum); | |
| 108 | + | |
| 79 | 109 | // render lights |
| 80 | 110 | auto& scene = sceneContext->_scene; |
| 81 | 111 | auto& items = scene->getMasterBucket().at(ItemFilter::Builder::light()); |
| 82 | − | |
| 83 | − RenderArgs* args = renderContext->args; | |
| 112 | + | |
| 113 | + | |
| 114 | + ItemIDs inItems; | |
| 115 | + inItems.reserve(items.size()); | |
| 84 | 116 | for (auto id : items) { |
| 85 | − auto item = scene->getItem(id); | |
| 86 | − item.render(args); | |
| 117 | + inItems.push_back(id); | |
| 87 | 118 | } |
| 119 | + | |
| 120 | + ItemIDs culledItems; | |
| 121 | + cullItems(sceneContext, renderContext, inItems, culledItems); | |
| 122 | + renderItems(sceneContext, renderContext, culledItems); | |
| 88 | 123 | } |
| 89 | 124 | |
| 90 | −DrawSceneTask::DrawSceneTask() : Task() { | |
| 125 | +/* | |
| 126 | +bool LODManager::shouldRenderMesh(float largestDimension, float distanceToCamera) { | |
| 127 | + const float octreeToMeshRatio = 4.0f; // must be this many times closer to a mesh than a voxel to see it. | |
| 128 | + float octreeSizeScale = getOctreeSizeScale(); | |
| 129 | + int boundaryLevelAdjust = getBoundaryLevelAdjust(); | |
| 130 | + float maxScale = (float)TREE_SCALE; | |
| 131 | + float visibleDistanceAtMaxScale = boundaryDistanceForRenderLevel(boundaryLevelAdjust, octreeSizeScale) / octreeToMeshRatio; | |
| 132 | + | |
| 133 | + if (_shouldRenderTableNeedsRebuilding) { | |
| 134 | + _shouldRenderTable.clear(); | |
| 135 | + | |
| 136 | + float SMALLEST_SCALE_IN_TABLE = 0.001f; // 1mm is plenty small | |
| 137 | + float scale = maxScale; | |
| 138 | + float visibleDistanceAtScale = visibleDistanceAtMaxScale; | |
| 139 | + | |
| 140 | + while (scale > SMALLEST_SCALE_IN_TABLE) { | |
| 141 | + scale /= 2.0f; | |
| 142 | + visibleDistanceAtScale /= 2.0f; | |
| 143 | + _shouldRenderTable[scale] = visibleDistanceAtScale; | |
| 144 | + } | |
| 145 | + _shouldRenderTableNeedsRebuilding = false; | |
| 146 | + } | |
| 147 | + | |
| 148 | + float closestScale = maxScale; | |
| 149 | + float visibleDistanceAtClosestScale = visibleDistanceAtMaxScale; | |
| 150 | + QMap<float, float>::const_iterator lowerBound = _shouldRenderTable.lowerBound(largestDimension); | |
| 151 | + if (lowerBound != _shouldRenderTable.constEnd()) { | |
| 152 | + closestScale = lowerBound.key(); | |
| 153 | + visibleDistanceAtClosestScale = lowerBound.value(); | |
| 154 | + } | |
| 155 | + | |
| 156 | + if (closestScale < largestDimension) { | |
| 157 | + visibleDistanceAtClosestScale *= 2.0f; | |
| 158 | + } | |
| 159 | + | |
| 160 | + return (distanceToCamera <= visibleDistanceAtClosestScale); | |
| 161 | +}*/ | |
| 91 | 162 | |
| 92 | − _jobs.push_back(Job(DrawOpaque())); | |
| 93 | − _jobs.push_back(Job(DrawLight())); | |
| 94 | − _jobs.push_back(Job(DrawTransparent())); | |
| 95 | −} | |
| 163 | +void render::cullItems(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, const ItemIDs& inItems, ItemIDs& outItems) { | |
| 164 | + assert(renderContext->args); | |
| 165 | + assert(renderContext->args->_viewFrustum); | |
| 166 | + | |
| 167 | + auto& scene = sceneContext->_scene; | |
| 168 | + RenderArgs* args = renderContext->args; | |
| 169 | + | |
| 170 | + // Culling / LOD | |
| 171 | + for (auto id : inItems) { | |
| 172 | + auto item = scene->getItem(id); | |
| 173 | + auto bound = item.getBound(); | |
| 174 | + | |
| 175 | + if (bound.isNull()) { | |
| 176 | + outItems.push_back(id); // One more Item to render | |
| 177 | + args->_itemsRendered++; | |
| 178 | + continue; | |
| 179 | + } | |
| 180 | + | |
| 181 | + // TODO: some entity types (like lights) might want to be rendered even | |
| 182 | + // when they are outside of the view frustum... | |
| 183 | + | |
| 184 | + float distance = args->_viewFrustum->distanceToCamera(bound.calcCenter()); | |
| 185 | + | |
| 186 | + bool outOfView = args->_viewFrustum->boxInFrustum(bound) == ViewFrustum::OUTSIDE; | |
| 187 | + if (!outOfView) { | |
| 188 | + bool bigEnoughToRender = true; //_viewState->shouldRenderMesh(bound.getLargestDimension(), distance); | |
| 189 | + | |
| 190 | + if (bigEnoughToRender) { | |
| 191 | + outItems.push_back(id); // One more Item to render | |
| 192 | + args->_itemsRendered++; | |
| 193 | + } else { | |
| 194 | + args->_itemsTooSmall++; | |
| 195 | + } | |
| 196 | + } else { | |
| 197 | + args->_itemsOutOfView++; | |
| 198 | + } | |
| 199 | + } | |
| 96 | 200 | |
| 97 | −DrawSceneTask::~DrawSceneTask() { | |
| 98 | 201 | } |
| 99 | 202 | |
| 100 | −void DrawSceneTask::run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { | |
| 101 | − // sanity checks | |
| 102 | − assert(sceneContext); | |
| 103 | − if (!sceneContext->_scene) { | |
| 104 | − return; | |
| 203 | + | |
| 204 | +void render::depthSortItems(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, bool frontToBack, const ItemIDs& inItems, ItemIDs& outItems) { | |
| 205 | + assert(renderContext->args); | |
| 206 | + assert(renderContext->args->_viewFrustum); | |
| 207 | + | |
| 208 | + auto& scene = sceneContext->_scene; | |
| 209 | + RenderArgs* args = renderContext->args; | |
| 210 | + | |
| 211 | + // Allocate and simply copy | |
| 212 | + outItems.reserve(inItems.size()); | |
| 213 | + | |
| 214 | + | |
| 215 | + // Make a local dataset of the center distance and closest point distance | |
| 216 | + struct ItemBound { | |
| 217 | + float _centerDepth = 0.0f; | |
| 218 | + float _nearDepth = 0.0f; | |
| 219 | + float _farDepth = 0.0f; | |
| 220 | + ItemID _id = 0; | |
| 221 | + | |
| 222 | + ItemBound() {} | |
| 223 | + ItemBound(float centerDepth, float nearDepth, float farDepth, ItemID id) : _centerDepth(centerDepth), _nearDepth(nearDepth), _farDepth(farDepth), _id(id) {} | |
| 224 | + }; | |
| 225 | + std::vector<ItemBound> itemBounds; | |
| 226 | + itemBounds.reserve(outItems.size()); | |
| 227 | + | |
| 228 | + for (auto id : inItems) { | |
| 229 | + auto item = scene->getItem(id); | |
| 230 | + auto bound = item.getBound(); | |
| 231 | + float distance = args->_viewFrustum->distanceToCamera(bound.calcCenter()); | |
| 232 | + | |
| 233 | + itemBounds.emplace_back(ItemBound(distance, distance, distance, id)); | |
| 105 | 234 | } |
| 106 | 235 | |
| 107 | − for (auto job : _jobs) { | |
| 108 | − job.run(sceneContext, renderContext); | |
| 236 | + // sort against Z | |
| 237 | + if (frontToBack) { | |
| 238 | + struct FrontToBackSort { | |
| 239 | + bool operator() (ItemBound& left, ItemBound& right) { | |
| 240 | + return (left._centerDepth < right._centerDepth); | |
| 241 | + } | |
| 242 | + } frontToBackSort; | |
| 243 | + std::sort (itemBounds.begin(), itemBounds.end(), frontToBackSort); | |
| 244 | + } else { | |
| 245 | + struct BackToFrontSort { | |
| 246 | + bool operator() (ItemBound& left, ItemBound& right) { | |
| 247 | + return (left._centerDepth > right._centerDepth); | |
| 248 | + } | |
| 249 | + } backToFrontSort; | |
| 250 | + std::sort (itemBounds.begin(), itemBounds.end(), backToFrontSort); | |
| 109 | 251 | } |
| 110 | −}; | |
| 111 | 252 | |
| 253 | + // FInally once sorted result to a list of itemID | |
| 254 | + for (auto& itemBound : itemBounds) { | |
| 255 | + outItems.emplace_back(itemBound._id); | |
| 256 | + } | |
| 257 | +} | |
| 258 | + | |
| 259 | +void render::renderItems(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, const ItemIDs& inItems) { | |
| 260 | + auto& scene = sceneContext->_scene; | |
| 261 | + RenderArgs* args = renderContext->args; | |
| 262 | + gpu::Batch theBatch; | |
| 263 | + args->_batch = &theBatch; | |
| 264 | + | |
| 265 | + // render | |
| 266 | + for (auto id : inItems) { | |
| 267 | + auto item = scene->getItem(id); | |
| 268 | + item.render(args); | |
| 269 | + } | |
| 270 | + | |
| 271 | + | |
| 272 | + args->_context->enqueueBatch((*args->_batch)); | |
| 273 | + args->_batch = nullptr; | |
| 274 | +} | |
| @@ -57,11 +57,13 @@ protected: · 56 unmodified lines | ||
| 57 | 57 | |
| 58 | 58 | typedef std::vector<Job> Jobs; |
| 59 | 59 | |
| 60 | +void cullItems(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, const ItemIDs& inItems, ItemIDs& outITems); | |
| 61 | +void depthSortItems(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, bool frontToBack, const ItemIDs& inItems, ItemIDs& outITems); | |
| 62 | +void renderItems(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, const ItemIDs& inItems); | |
| 60 | 63 | |
| 61 | 64 | |
| 62 | 65 | class DrawOpaque { |
| 63 | 66 | public: |
| 64 | − Jobs _jobs; | |
| 65 | 67 | }; |
| 66 | 68 | template <> void jobRun(const DrawOpaque& job, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext); |
| 67 | 69 | |
| @@ -77,19 +79,6 @@ public: · 9 unmodified lines | ||
| 77 | 79 | template <> void jobRun(const DrawLight& job, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext); |
| 78 | 80 | |
| 79 | 81 | |
| 80 | −class FilterItems { | |
| 81 | −public: | |
| 82 | − ItemIDs _items; | |
| 83 | −}; | |
| 84 | −template <> void jobRun(const FilterItems& job, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext); | |
| 85 | − | |
| 86 | −class RenderItems { | |
| 87 | −public: | |
| 88 | − ItemIDs _items; | |
| 89 | −}; | |
| 90 | −template <> void jobRun(const RenderItems& job, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext); | |
| 91 | − | |
| 92 | − | |
| 93 | 82 | class DrawSceneTask : public Task { |
| 94 | 83 | public: |
| 95 | 84 | |
| @@ -99,6 +88,7 @@ public: · 3 unmodified lines | ||
| 99 | 88 | Jobs _jobs; |
| 100 | 89 | |
| 101 | 90 | virtual void run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext); |
| 91 | + | |
| 102 | 92 | }; |
| 103 | 93 | |
| 104 | 94 | } |
| @@ -70,21 +70,21 @@ void Item::move() { · 69 unmodified lines | ||
| 70 | 70 | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | −void Scene::PendingChanges::resetItem(ItemID id, const PayloadPointer& payload) { | |
| 73 | +void PendingChanges::resetItem(ItemID id, const PayloadPointer& payload) { | |
| 74 | 74 | _resetItems.push_back(id); |
| 75 | 75 | _resetPayloads.push_back(payload); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | −void Scene::PendingChanges::removeItem(ItemID id) { | |
| 78 | +void PendingChanges::removeItem(ItemID id) { | |
| 79 | 79 | _removedItems.push_back(id); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | −void Scene::PendingChanges::moveItem(ItemID id) { | |
| 82 | +void PendingChanges::moveItem(ItemID id) { | |
| 83 | 83 | _movedItems.push_back(id); |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | |
| 87 | −void Scene::PendingChanges::merge(PendingChanges& changes) { | |
| 87 | +void PendingChanges::merge(PendingChanges& changes) { | |
| 88 | 88 | _resetItems.insert(_resetItems.end(), changes._resetItems.begin(), changes._resetItems.end()); |
| 89 | 89 | _resetPayloads.insert(_resetPayloads.end(), changes._resetPayloads.begin(), changes._resetPayloads.end()); |
| 90 | 90 | _removedItems.insert(_removedItems.end(), changes._removedItems.begin(), changes._removedItems.end()); |
| @@ -108,7 +108,7 @@ void Scene::enqueuePendingChanges(const PendingChanges& pendingChanges) { · 17 unmodified lines | ||
| 108 | 108 | _changeQueueMutex.unlock(); |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | −void consolidateChangeQueue(Scene::PendingChangesQueue& queue, Scene::PendingChanges& singleBatch) { | |
| 111 | +void consolidateChangeQueue(PendingChangesQueue& queue, PendingChanges& singleBatch) { | |
| 112 | 112 | while (!queue.empty()) { |
| 113 | 113 | auto pendingChanges = queue.front(); |
| 114 | 114 | singleBatch.merge(pendingChanges); |
| @@ -305,6 +305,27 @@ public: · 304 unmodified lines | ||
| 305 | 305 | class Engine; |
| 306 | 306 | class Observer; |
| 307 | 307 | |
| 308 | +class PendingChanges { | |
| 309 | +public: | |
| 310 | + PendingChanges() {} | |
| 311 | + ~PendingChanges() {} | |
| 312 | + | |
| 313 | + void resetItem(ItemID id, const PayloadPointer& payload); | |
| 314 | + void removeItem(ItemID id); | |
| 315 | + void moveItem(ItemID id); | |
| 316 | + | |
| 317 | + void merge(PendingChanges& changes); | |
| 318 | + | |
| 319 | + Payloads _resetPayloads; | |
| 320 | + ItemIDs _resetItems; | |
| 321 | + ItemIDs _removedItems; | |
| 322 | + ItemIDs _movedItems; | |
| 323 | + | |
| 324 | +protected: | |
| 325 | +}; | |
| 326 | +typedef std::queue<PendingChanges> PendingChangesQueue; | |
| 327 | + | |
| 328 | + | |
| 308 | 329 | // Scene is a container for Items |
| 309 | 330 | // Items are introduced, modified or erased in the scene through PendingChanges |
| 310 | 331 | // Once per Frame, the PendingChanges are all flushed |
| @@ -342,26 +363,6 @@ public: · 31 unmodified lines | ||
| 342 | 363 | typedef std::shared_ptr< Observer > ObserverPointer; |
| 343 | 364 | typedef std::vector< ObserverPointer > Observers; |
| 344 | 365 | |
| 345 | − class PendingChanges { | |
| 346 | − public: | |
| 347 | − PendingChanges() {} | |
| 348 | − ~PendingChanges() {} | |
| 349 | − | |
| 350 | − void resetItem(ItemID id, const PayloadPointer& payload); | |
| 351 | − void removeItem(ItemID id); | |
| 352 | − void moveItem(ItemID id); | |
| 353 | − | |
| 354 | − void merge(PendingChanges& changes); | |
| 355 | − | |
| 356 | − Payloads _resetPayloads; | |
| 357 | − ItemIDs _resetItems; | |
| 358 | − ItemIDs _removedItems; | |
| 359 | − ItemIDs _movedItems; | |
| 360 | − | |
| 361 | − protected: | |
| 362 | − }; | |
| 363 | − typedef std::queue<PendingChanges> PendingChangesQueue; | |
| 364 | − | |
| 365 | 366 | Scene(); |
| 366 | 367 | ~Scene() {} |
| 367 | 368 | |
| @@ -9,7 +9,6 @@ · 8 unmodified lines | ||
| 9 | 9 | // |
| 10 | 10 | |
| 11 | 11 | #include "TextRenderer.h" |
| 12 | −#include "MatrixStack.h" | |
| 13 | 12 | |
| 14 | 13 | #include <QWindow> |
| 15 | 14 | #include <QFile> |