Merge branch 'master' of https://github.com/highfidelity/hifi into clear_cache_and_restart

This commit is contained in:
Atlante45 2015-07-09 16:30:56 -07:00
commit bfeb095fb4
78 changed files with 1747 additions and 688 deletions

View file

@ -13,9 +13,8 @@
#include "AssignmentAction.h"
AssignmentAction::AssignmentAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) :
_id(id),
_type(type),
AssignmentAction::AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) :
EntityActionInterface(type, id),
_data(QByteArray()),
_active(false),
_ownerEntity(ownerEntity) {
@ -28,7 +27,7 @@ void AssignmentAction::removeFromSimulation(EntitySimulation* simulation) const
simulation->removeAction(_id);
}
QByteArray AssignmentAction::serialize() {
QByteArray AssignmentAction::serialize() const {
return _data;
}

View file

@ -21,23 +21,19 @@
class AssignmentAction : public EntityActionInterface {
public:
AssignmentAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity);
AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity);
virtual ~AssignmentAction();
const QUuid& getID() const { return _id; }
virtual EntityActionType getType() { return _type; }
virtual void removeFromSimulation(EntitySimulation* simulation) const;
virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; }
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; }
virtual bool updateArguments(QVariantMap arguments);
virtual QVariantMap getArguments();
virtual QByteArray serialize();
virtual QByteArray serialize() const;
virtual void deserialize(QByteArray serializedArguments);
private:
QUuid _id;
EntityActionType _type;
QByteArray _data;
protected:

View file

@ -12,14 +12,14 @@
#include "AssignmentActionFactory.h"
EntityActionPointer assignmentActionFactory(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) {
EntityActionPointer assignmentActionFactory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) {
return (EntityActionPointer) new AssignmentAction(type, id, ownerEntity);
}
EntityActionPointer AssignmentActionFactory::factory(EntitySimulation* simulation,
EntityActionType type,
QUuid id,
const QUuid& id,
EntityItemPointer ownerEntity,
QVariantMap arguments) {
EntityActionPointer action = assignmentActionFactory(type, id, ownerEntity);

View file

@ -21,7 +21,7 @@ public:
virtual ~AssignmentActionFactory() { }
virtual EntityActionPointer factory(EntitySimulation* simulation,
EntityActionType type,
QUuid id,
const QUuid& id,
EntityItemPointer ownerEntity,
QVariantMap arguments);
virtual EntityActionPointer factoryBA(EntitySimulation* simulation,

25
examples/example/games/airHockey.js Normal file → Executable file
View file

@ -83,6 +83,30 @@ var puck_name_index = 2;
var light_name_index = 3;
var floor_name_index = 4;
//Create Spawn and Del. Button Vars.
function updateButtonPosition() {
Overlays.editOverlay(spawnButton, {
x: screenSize.x / 2 + PADDING,
y: screenSize.y - (BUTTON_SIZE * 2 + PADDING),
});
Overlays.editOverlay(deleteButton, {
x: screenSize.x / 2 - BUTTON_SIZE,
y: screenSize.y - (BUTTON_SIZE * 2 + PADDING),
});
}
function onScriptUpdate() {
var oldScreenSize = screenSize;
screenSize = Controller.getViewportDimensions();
if (screenSize.x !== oldScreenSize.x || screenSize.y !== oldScreenSize.y) {
updateButtonPosition();
}
}
screenSize = Controller.getViewportDimensions();
var deleteButton = Overlays.addOverlay("image", {
x: screenSize.x / 2 - BUTTON_SIZE,
@ -112,6 +136,7 @@ var spawnButton = Overlays.addOverlay("image", {
alpha: 1
});
Script.update.connect(onScriptUpdate);
var floor, edge1, edge2, edge3a, edge3b, edge4a, edge4b, light;

View file

@ -11,7 +11,8 @@
//
"use strict";
/*jslint vars: true*/
var Script, Entities, MyAvatar, Window, Controller, Vec3, Quat; // Referenced globals provided by High Fidelity.
var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print, ToolBar; // Referenced globals provided by High Fidelity.
Script.include(["../../libraries/toolBars.js"]);
var hand = "right";
var nullActionID = "00000000-0000-0000-0000-000000000000";
@ -19,40 +20,135 @@ var controllerID;
var controllerActive;
var stickID = null;
var actionID = nullActionID;
var targetIDs = [];
var dimensions = { x: 0.3, y: 0.1, z: 2.0 };
var AWAY_ORIENTATION = Quat.fromPitchYawRollDegrees(-90, 0, 0);
var BUTTON_SIZE = 32;
var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx";
var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx";
var whichModel = "sword";
var attachmentOffset, MOUSE_CONTROLLER_OFFSET = {x: 0.5, y: 0.4, z: 0.0}; // A fudge when using mouse rather than hand-controller, to hit yourself less often.
// sometimes if this is run immediately the stick doesn't get created? use a timer.
Script.setTimeout(function () {
stickID = Entities.addEntity({
type: "Model",
modelURL: (whichModel === "sword") ? swordModel : stickModel,
//compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj",
shapeType: "box",
dimensions: dimensions,
position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close
rotation: MyAvatar.orientation,
damping: 0.1,
collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav",
restitution: 0.01,
collisionsWillMove: true
var toolBar = new ToolBar(0, 0, ToolBar.vertical, "highfidelity.sword.toolbar", function () {
return {x: 100, y: 380};
});
var SWORD_IMAGE = "http://s3.amazonaws.com/hifi-public/images/billiardsReticle.png"; // Toggle between brandishing/sheathing sword (creating if necessary)
var TARGET_IMAGE = "http://s3.amazonaws.com/hifi-public/images/puck.png"; // Create a target dummy
var CLEANUP_IMAGE = "http://s3.amazonaws.com/hifi-public/images/delete.png"; // Remove sword and all target dummies.f
var swordButton = toolBar.addOverlay("image", {
width: BUTTON_SIZE,
height: BUTTON_SIZE,
imageURL: SWORD_IMAGE,
alpha: 1
});
var targetButton = toolBar.addOverlay("image", {
width: BUTTON_SIZE,
height: BUTTON_SIZE,
imageURL: TARGET_IMAGE,
alpha: 1
});
var cleanupButton = toolBar.addOverlay("image", {
width: BUTTON_SIZE,
height: BUTTON_SIZE,
imageURL: CLEANUP_IMAGE,
alpha: 1
});
var flasher;
function clearFlash() {
if (!flasher) {
return;
}
Script.clearTimeout(flasher.timer);
Overlays.deleteOverlay(flasher.overlay);
flasher = null;
}
function flash(color) {
clearFlash();
flasher = {};
flasher.overlay = Overlays.addOverlay("text", {
backgroundColor: color,
backgroundAlpha: 0.7,
width: Window.innerWidth,
height: Window.innerHeight
});
actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2},
hand: hand,
timeScale: 0.15});
}, 3000);
function cleanUp() {
Entities.deleteEntity(stickID);
flasher.timer = Script.setTimeout(clearFlash, 500);
}
var health = 100;
var display;
var isAway = false;
function updateDisplay() {
var text = health.toString();
if (!display) {
health = 100;
display = Overlays.addOverlay("text", {
text: text,
font: { size: 20 },
color: {red: 0, green: 255, blue: 0},
backgroundColor: {red: 100, green: 100, blue: 100}, // Why doesn't this and the next work?
backgroundAlpha: 0.9,
x: Window.innerWidth - 50,
y: 50
});
} else {
Overlays.editOverlay(display, {text: text});
}
}
function removeDisplay() {
if (display) {
Overlays.deleteOverlay(display);
display = null;
}
}
function cleanUp(leaveButtons) {
attachmentOffset = {x: 0, y: 0, z: 0};
if (stickID) {
Entities.deleteAction(stickID, actionID);
Entities.deleteEntity(stickID);
stickID = null;
actionID = null;
}
targetIDs.forEach(function (id) {
Entities.deleteAction(id.entity, id.action);
Entities.deleteEntity(id.entity);
});
targetIDs = [];
removeDisplay();
if (!leaveButtons) {
toolBar.cleanup();
}
}
function computeEnergy(collision, entityID) {
var id = entityID || collision.idA || collision.idB;
var entity = id && Entities.getEntityProperties(id);
var mass = entity ? (entity.density * entity.dimensions.x * entity.dimensions.y * entity.dimensions.z) : 1;
var linearVelocityChange = Vec3.length(collision.velocityChange);
var energy = 0.5 * mass * linearVelocityChange * linearVelocityChange;
return Math.min(Math.max(1.0, Math.round(energy)), 20);
}
function gotHit(collision) {
if (isAway) { return; }
var energy = computeEnergy(collision);
health -= energy;
flash({red: 255, green: 0, blue: 0});
updateDisplay();
}
function scoreHit(idA, idB, collision) {
if (isAway) { return; }
var energy = computeEnergy(collision, idA);
health += energy;
flash({red: 0, green: 255, blue: 0});
updateDisplay();
}
function positionStick(stickOrientation) {
var baseOffset = {x: 0.0, y: 0.0, z: -dimensions.z / 2};
var baseOffset = Vec3.sum(attachmentOffset, {x: 0.0, y: 0.0, z: -dimensions.z / 2});
var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset);
Entities.updateAction(stickID, actionID, {relativePosition: offset,
relativeRotation: stickOrientation});
@ -60,7 +156,8 @@ function positionStick(stickOrientation) {
function mouseMoveEvent(event) {
if (!stickID || actionID === nullActionID) {
attachmentOffset = MOUSE_CONTROLLER_OFFSET;
if (!stickID || actionID === nullActionID || isAway) {
return;
}
var windowCenterX = Window.innerWidth / 2;
@ -98,7 +195,75 @@ function update() {
positionStick(stickOrientation);
}
function toggleAway() {
isAway = !isAway;
if (isAway) {
positionStick(AWAY_ORIENTATION);
removeDisplay();
} else {
updateDisplay();
}
}
function onClick(event) {
switch (Overlays.getOverlayAtPoint(event)) {
case swordButton:
if (!stickID) {
initControls();
stickID = Entities.addEntity({
type: "Model",
modelURL: (whichModel === "sword") ? swordModel : stickModel,
//compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj",
shapeType: "box",
dimensions: dimensions,
position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close
rotation: MyAvatar.orientation,
damping: 0.1,
collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav",
restitution: 0.01,
collisionsWillMove: true
});
actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2},
hand: hand,
timeScale: 0.15});
if (actionID === nullActionID) {
print('*** FAILED TO MAKE SWORD ACTION ***');
cleanUp();
}
Script.addEventHandler(stickID, 'collisionWithEntity', scoreHit);
updateDisplay();
} else {
toggleAway();
}
break;
case targetButton:
var position = Vec3.sum(MyAvatar.position, {x: 1.0, y: 0.4, z: 0.0});
var boxId = Entities.addEntity({
type: "Box",
name: "dummy",
position: position,
dimensions: {x: 0.3, y: 0.7, z: 0.3},
gravity: {x: 0.0, y: -3.0, z: 0.0},
damping: 0.2,
collisionsWillMove: true
});
var pointToOffsetFrom = Vec3.sum(position, {x: 0.0, y: 2.0, z: 0.0});
var action = Entities.addAction("offset", boxId, {pointToOffsetFrom: pointToOffsetFrom,
linearDistance: 2.0,
// linearTimeScale: 0.005
linearTimeScale: 0.1
});
targetIDs.push({entity: boxId, action: action});
break;
case cleanupButton:
cleanUp('leaveButtons');
break;
}
}
Script.scriptEnding.connect(cleanUp);
Controller.mouseMoveEvent.connect(mouseMoveEvent);
Controller.mousePressEvent.connect(onClick);
Script.update.connect(update);
MyAvatar.collisionWithEntity.connect(gotHit);

View file

@ -24,7 +24,7 @@ public:
inline bool extraDebugging() { return _extraDebugging; }
inline void setExtraDebugging(bool debugging) { _extraDebugging = debugging; }
virtual void addMessage(QString) = 0;
virtual void addMessage(const QString&) = 0;
virtual QString getLogData() = 0;
virtual void locateLog() = 0;
@ -32,7 +32,7 @@ signals:
void logReceived(QString message);
private:
bool _extraDebugging;
bool _extraDebugging{ false };
};
#endif // hifi_AbstractLoggerInterface_h

View file

@ -1022,6 +1022,7 @@ void Application::paintGL() {
void Application::runTests() {
runTimingTests();
runUnitTests();
}
void Application::audioMuteToggled() {
@ -1190,7 +1191,6 @@ bool Application::event(QEvent* event) {
}
bool Application::eventFilter(QObject* object, QEvent* event) {
if (event->type() == QEvent::ShortcutOverride) {
if (DependencyManager::get<OffscreenUi>()->shouldSwallowShortcut(event)) {
event->accept();
@ -1795,6 +1795,7 @@ void Application::checkFPS() {
}
void Application::idle() {
PROFILE_RANGE(__FUNCTION__);
static SimpleAverage<float> interIdleDurations;
static uint64_t lastIdleEnd{ 0 };
@ -3403,14 +3404,6 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se
// load the view frustum
loadViewFrustum(theCamera, _displayViewFrustum);
// flip x if in mirror mode (also requires reversing winding order for backface culling)
if (theCamera.getMode() == CAMERA_MODE_MIRROR) {
//glScalef(-1.0f, 1.0f, 1.0f);
//glFrontFace(GL_CW);
} else {
glFrontFace(GL_CCW);
}
// transform view according to theCamera
// could be myCamera (if in normal mode)
// or could be viewFrustumOffsetCamera if in offset mode
@ -3428,9 +3421,6 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se
Transform viewTransform;
viewTransform.setTranslation(theCamera.getPosition());
viewTransform.setRotation(rotation);
if (theCamera.getMode() == CAMERA_MODE_MIRROR) {
// viewTransform.setScale(Transform::Vec3(-1.0f, 1.0f, 1.0f));
}
if (renderArgs->_renderSide != RenderArgs::MONO) {
glm::mat4 invView = glm::inverse(_untranslatedViewMatrix);

View file

@ -21,11 +21,34 @@ const QString FILENAME_FORMAT = "hifi-log_%1_%2.txt";
const QString DATETIME_FORMAT = "yyyy-MM-dd_hh.mm.ss";
const QString LOGS_DIRECTORY = "Logs";
class FilePersistThread : public GenericQueueThread < QString > {
public:
FilePersistThread(const FileLogger& logger) : _logger(logger) {
setObjectName("LogFileWriter");
}
protected:
virtual bool processQueueItems(const Queue& messages) {
QFile file(_logger._fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
QTextStream out(&file);
foreach(const QString& message, messages) {
out << message;
}
}
return true;
}
private:
const FileLogger& _logger;
};
static FilePersistThread* _persistThreadInstance;
FileLogger::FileLogger(QObject* parent) :
AbstractLoggerInterface(parent),
_logData("")
AbstractLoggerInterface(parent)
{
setExtraDebugging(false);
_persistThreadInstance = new FilePersistThread(*this);
_persistThreadInstance->initialize(true, QThread::LowestPriority);
_fileName = FileUtils::standardPath(LOGS_DIRECTORY);
QHostAddress clientAddress = getLocalAddress();
@ -33,18 +56,24 @@ FileLogger::FileLogger(QObject* parent) :
_fileName.append(QString(FILENAME_FORMAT).arg(clientAddress.toString(), now.toString(DATETIME_FORMAT)));
}
void FileLogger::addMessage(QString message) {
QMutexLocker locker(&_mutex);
emit logReceived(message);
_logData += message;
FileLogger::~FileLogger() {
_persistThreadInstance->terminate();
}
QFile file(_fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
QTextStream out(&file);
out << message;
}
void FileLogger::addMessage(const QString& message) {
_persistThreadInstance->queueItem(message);
emit logReceived(message);
}
void FileLogger::locateLog() {
FileUtils::locateFile(_fileName);
}
QString FileLogger::getLogData() {
QString result;
QFile f(_fileName);
if (f.open(QFile::ReadOnly | QFile::Text)) {
result = QTextStream(&f).readAll();
}
return result;
}

View file

@ -13,23 +13,24 @@
#define hifi_FileLogger_h
#include "AbstractLoggerInterface.h"
#include <QMutex>
#include <GenericQueueThread.h>
class FileLogger : public AbstractLoggerInterface {
Q_OBJECT
public:
FileLogger(QObject* parent = NULL);
virtual ~FileLogger();
virtual void addMessage(QString);
virtual QString getLogData() { return _logData; }
virtual void locateLog();
virtual void addMessage(const QString&) override;
virtual QString getLogData() override;
virtual void locateLog() override;
private:
QString _logData;
QString _fileName;
QMutex _mutex;
friend class FilePersistThread;
};
#endif // hifi_FileLogger_h

View file

@ -58,6 +58,7 @@ void GLCanvas::initializeGL() {
}
void GLCanvas::paintGL() {
PROFILE_RANGE(__FUNCTION__);
if (!_throttleRendering && !Application::getInstance()->getWindow()->isMinimized()) {
Application::getInstance()->paintGL();
}

View file

@ -18,16 +18,16 @@
#include "InterfaceActionFactory.h"
EntityActionPointer interfaceActionFactory(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) {
EntityActionPointer interfaceActionFactory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) {
switch (type) {
case ACTION_TYPE_NONE:
return nullptr;
case ACTION_TYPE_OFFSET:
return (EntityActionPointer) new ObjectActionOffset(type, id, ownerEntity);
return (EntityActionPointer) new ObjectActionOffset(id, ownerEntity);
case ACTION_TYPE_SPRING:
return (EntityActionPointer) new ObjectActionSpring(type, id, ownerEntity);
return (EntityActionPointer) new ObjectActionSpring(id, ownerEntity);
case ACTION_TYPE_HOLD:
return (EntityActionPointer) new AvatarActionHold(type, id, ownerEntity);
return (EntityActionPointer) new AvatarActionHold(id, ownerEntity);
}
assert(false);
@ -37,7 +37,7 @@ EntityActionPointer interfaceActionFactory(EntityActionType type, QUuid id, Enti
EntityActionPointer InterfaceActionFactory::factory(EntitySimulation* simulation,
EntityActionType type,
QUuid id,
const QUuid& id,
EntityItemPointer ownerEntity,
QVariantMap arguments) {
EntityActionPointer action = interfaceActionFactory(type, id, ownerEntity);

View file

@ -20,7 +20,7 @@ public:
virtual ~InterfaceActionFactory() { }
virtual EntityActionPointer factory(EntitySimulation* simulation,
EntityActionType type,
QUuid id,
const QUuid& id,
EntityItemPointer ownerEntity,
QVariantMap arguments);
virtual EntityActionPointer factoryBA(EntitySimulation* simulation,

View file

@ -21,6 +21,7 @@
#include <QThread>
#include <ByteCountCoding.h>
#include <SharedUtil.h>
#include <TextRenderer.h>
@ -229,6 +230,43 @@ void runTimingTests() {
elapsedUsecs = (float)startTime.nsecsElapsed() * NSEC_TO_USEC;
qCDebug(interfaceapp, "vec3 assign and dot() usecs: %f, last result:%f",
(double)(elapsedUsecs / numTests), (double)result);
quint64 BYTE_CODE_MAX_TEST_VALUE = 99999999;
quint64 BYTE_CODE_TESTS_SKIP = 999;
QByteArray extraJunk;
const int EXTRA_JUNK_SIZE = 200;
extraJunk.append((unsigned char)255);
for (int i = 0; i < EXTRA_JUNK_SIZE; i++) {
extraJunk.append(QString("junk"));
}
{
startTime.start();
quint64 tests = 0;
quint64 failed = 0;
for (quint64 value = 0; value < BYTE_CODE_MAX_TEST_VALUE; value += BYTE_CODE_TESTS_SKIP) {
quint64 valueA = value; // usecTimestampNow();
ByteCountCoded<quint64> codedValueA = valueA;
QByteArray codedValueABuffer = codedValueA;
codedValueABuffer.append(extraJunk);
ByteCountCoded<quint64> decodedValueA;
decodedValueA.decode(codedValueABuffer);
quint64 valueADecoded = decodedValueA;
tests++;
if (valueA != valueADecoded) {
qDebug() << "FAILED! value:" << valueA << "decoded:" << valueADecoded;
failed++;
}
}
elapsedUsecs = (float)startTime.nsecsElapsed() * NSEC_TO_USEC;
qCDebug(interfaceapp) << "ByteCountCoded<quint64> usecs: " << elapsedUsecs
<< "per test:" << (double) (elapsedUsecs / tests)
<< "tests:" << tests
<< "failed:" << failed;
}
}
bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection,
@ -271,3 +309,39 @@ bool pointInSphere(glm::vec3& point, glm::vec3& sphereCenter, double sphereRadiu
}
return false;
}
void runUnitTests() {
quint64 LAST_TEST = 10;
quint64 SKIP_BY = 1;
for (quint64 value = 0; value <= LAST_TEST; value += SKIP_BY) {
qDebug() << "value:" << value;
ByteCountCoded<quint64> codedValue = value;
QByteArray codedValueBuffer = codedValue;
codedValueBuffer.append((unsigned char)255);
codedValueBuffer.append(QString("junk"));
qDebug() << "codedValueBuffer:";
outputBufferBits((const unsigned char*)codedValueBuffer.constData(), codedValueBuffer.size());
ByteCountCoded<quint64> valueDecoder;
size_t bytesConsumed = valueDecoder.decode(codedValueBuffer);
quint64 valueDecoded = valueDecoder;
qDebug() << "valueDecoded:" << valueDecoded;
qDebug() << "bytesConsumed:" << bytesConsumed;
if (value == valueDecoded) {
qDebug() << "SUCCESS!";
} else {
qDebug() << "FAILED!";
}
}
}

View file

@ -30,6 +30,7 @@ void drawText(int x, int y, float scale, float radians, int mono,
void renderCollisionOverlay(int width, int height, float magnitude, float red = 0, float blue = 0, float green = 0);
void runTimingTests();
void runUnitTests();
bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection,
const glm::vec3& sphereCenter, float sphereRadius, float& distance);

View file

@ -17,13 +17,14 @@
const uint16_t AvatarActionHold::holdVersion = 1;
AvatarActionHold::AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) :
ObjectActionSpring(type, id, ownerEntity),
AvatarActionHold::AvatarActionHold(const QUuid& id, EntityItemPointer ownerEntity) :
ObjectActionSpring(id, ownerEntity),
_relativePosition(glm::vec3(0.0f)),
_relativeRotation(glm::quat()),
_hand("right"),
_mine(false)
{
_type = ACTION_TYPE_HOLD;
#if WANT_DEBUG
qDebug() << "AvatarActionHold::AvatarActionHold";
#endif
@ -166,8 +167,7 @@ QVariantMap AvatarActionHold::getArguments() {
void AvatarActionHold::deserialize(QByteArray serializedArguments) {
if (_mine) {
return;
if (!_mine) {
ObjectActionSpring::deserialize(serializedArguments);
}
ObjectActionSpring::deserialize(serializedArguments);
}

View file

@ -19,11 +19,9 @@
class AvatarActionHold : public ObjectActionSpring {
public:
AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity);
AvatarActionHold(const QUuid& id, EntityItemPointer ownerEntity);
virtual ~AvatarActionHold();
virtual EntityActionType getType() { return ACTION_TYPE_HOLD; }
virtual bool updateArguments(QVariantMap arguments);
virtual QVariantMap getArguments();

View file

@ -161,6 +161,7 @@ namespace render {
template <> void payloadRender(const RenderableModelEntityItemMeta::Pointer& payload, RenderArgs* args) {
if (args) {
if (payload && payload->entity) {
PROFILE_RANGE("MetaModelRender");
payload->entity->render(args);
}
}

View file

@ -25,7 +25,7 @@ class EntityActionFactoryInterface : public QObject, public Dependency {
virtual ~EntityActionFactoryInterface() { }
virtual EntityActionPointer factory(EntitySimulation* simulation,
EntityActionType type,
QUuid id,
const QUuid& id,
EntityItemPointer ownerEntity,
QVariantMap arguments) { assert(false); return nullptr; }
virtual EntityActionPointer factoryBA(EntitySimulation* simulation,

View file

@ -29,10 +29,10 @@ enum EntityActionType {
class EntityActionInterface {
public:
EntityActionInterface() { }
EntityActionInterface(EntityActionType type, const QUuid& id) : _id(id), _type(type) { }
virtual ~EntityActionInterface() { }
virtual const QUuid& getID() const = 0;
virtual EntityActionType getType() { assert(false); return ACTION_TYPE_NONE; }
const QUuid& getID() const { return _id; }
EntityActionType getType() const { return _type; }
virtual void removeFromSimulation(EntitySimulation* simulation) const = 0;
virtual EntityItemWeakPointer getOwnerEntity() const = 0;
@ -40,7 +40,7 @@ public:
virtual bool updateArguments(QVariantMap arguments) = 0;
virtual QVariantMap getArguments() = 0;
virtual QByteArray serialize() = 0;
virtual QByteArray serialize() const = 0;
virtual void deserialize(QByteArray serializedArguments) = 0;
static EntityActionType actionTypeFromString(QString actionTypeString);
@ -68,6 +68,8 @@ protected:
static QString extractStringArgument(QString objectName, QVariantMap arguments,
QString argumentName, bool& ok, bool required = true);
QUuid _id;
EntityActionType _type;
};

View file

@ -12,9 +12,11 @@
#include "EntityItem.h"
#include <QtCore/QObject>
#include <QtEndian>
#include <glm/gtx/transform.hpp>
#include <BufferParser.h>
#include <ByteCountCoding.h>
#include <GLMHelpers.h>
#include <Octree.h>
@ -354,50 +356,78 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
// ~27-35 bytes...
const int MINIMUM_HEADER_BYTES = 27;
int bytesRead = 0;
if (bytesLeftToRead < MINIMUM_HEADER_BYTES) {
return 0;
}
int clockSkew = args.sourceNode ? args.sourceNode->getClockSkewUsec() : 0;
BufferParser parser(data, bytesLeftToRead);
#ifdef DEBUG
#define VALIDATE_ENTITY_ITEM_PARSER 1
#endif
#ifdef VALIDATE_ENTITY_ITEM_PARSER
int bytesRead = 0;
int originalLength = bytesLeftToRead;
// TODO: figure out a way to avoid the big deep copy below.
QByteArray originalDataBuffer((const char*)data, originalLength); // big deep copy!
int clockSkew = args.sourceNode ? args.sourceNode->getClockSkewUsec() : 0;
const unsigned char* dataAt = data;
#endif
// id
QByteArray encodedID = originalDataBuffer.mid(bytesRead, NUM_BYTES_RFC4122_UUID); // maximum possible size
_id = QUuid::fromRfc4122(encodedID);
dataAt += encodedID.size();
bytesRead += encodedID.size();
parser.readUuid(_id);
#ifdef VALIDATE_ENTITY_ITEM_PARSER
{
QByteArray encodedID = originalDataBuffer.mid(bytesRead, NUM_BYTES_RFC4122_UUID); // maximum possible size
QUuid id = QUuid::fromRfc4122(encodedID);
dataAt += encodedID.size();
bytesRead += encodedID.size();
Q_ASSERT(id == _id);
Q_ASSERT(parser.offset() == bytesRead);
}
#endif
// type
parser.readCompressedCount<quint32>((quint32&)_type);
#ifdef VALIDATE_ENTITY_ITEM_PARSER
QByteArray encodedType = originalDataBuffer.mid(bytesRead); // maximum possible size
ByteCountCoded<quint32> typeCoder = encodedType;
encodedType = typeCoder; // determine true length
dataAt += encodedType.size();
bytesRead += encodedType.size();
quint32 type = typeCoder;
_type = (EntityTypes::EntityType)type;
EntityTypes::EntityType oldType = (EntityTypes::EntityType)type;
Q_ASSERT(oldType == _type);
Q_ASSERT(parser.offset() == bytesRead);
#endif
bool overwriteLocalData = true; // assume the new content overwrites our local data
quint64 now = usecTimestampNow();
// _created
quint64 createdFromBuffer = 0;
memcpy(&createdFromBuffer, dataAt, sizeof(createdFromBuffer));
dataAt += sizeof(createdFromBuffer);
bytesRead += sizeof(createdFromBuffer);
quint64 now = usecTimestampNow();
if (_created == UNKNOWN_CREATED_TIME) {
// we don't yet have a _created timestamp, so we accept this one
createdFromBuffer -= clockSkew;
if (createdFromBuffer > now || createdFromBuffer == UNKNOWN_CREATED_TIME) {
createdFromBuffer = now;
{
quint64 createdFromBuffer = 0;
parser.readValue(createdFromBuffer);
#ifdef VALIDATE_ENTITY_ITEM_PARSER
{
quint64 createdFromBuffer2 = 0;
memcpy(&createdFromBuffer2, dataAt, sizeof(createdFromBuffer2));
dataAt += sizeof(createdFromBuffer2);
bytesRead += sizeof(createdFromBuffer2);
Q_ASSERT(createdFromBuffer2 == createdFromBuffer);
Q_ASSERT(parser.offset() == bytesRead);
}
#endif
if (_created == UNKNOWN_CREATED_TIME) {
// we don't yet have a _created timestamp, so we accept this one
createdFromBuffer -= clockSkew;
if (createdFromBuffer > now || createdFromBuffer == UNKNOWN_CREATED_TIME) {
createdFromBuffer = now;
}
_created = createdFromBuffer;
}
_created = createdFromBuffer;
}
#ifdef WANT_DEBUG
@ -417,15 +447,21 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
#endif
quint64 lastEditedFromBuffer = 0;
quint64 lastEditedFromBufferAdjusted = 0;
// TODO: we could make this encoded as a delta from _created
// _lastEdited
memcpy(&lastEditedFromBuffer, dataAt, sizeof(lastEditedFromBuffer));
dataAt += sizeof(lastEditedFromBuffer);
bytesRead += sizeof(lastEditedFromBuffer);
lastEditedFromBufferAdjusted = lastEditedFromBuffer - clockSkew;
parser.readValue(lastEditedFromBuffer);
#ifdef VALIDATE_ENTITY_ITEM_PARSER
{
quint64 lastEditedFromBuffer2 = 0;
memcpy(&lastEditedFromBuffer2, dataAt, sizeof(lastEditedFromBuffer2));
dataAt += sizeof(lastEditedFromBuffer2);
bytesRead += sizeof(lastEditedFromBuffer2);
Q_ASSERT(lastEditedFromBuffer2 == lastEditedFromBuffer);
Q_ASSERT(parser.offset() == bytesRead);
}
#endif
quint64 lastEditedFromBufferAdjusted = lastEditedFromBuffer - clockSkew;
if (lastEditedFromBufferAdjusted > now) {
lastEditedFromBufferAdjusted = now;
}
@ -487,9 +523,21 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
}
// last updated is stored as ByteCountCoded delta from lastEdited
QByteArray encodedUpdateDelta = originalDataBuffer.mid(bytesRead); // maximum possible size
ByteCountCoded<quint64> updateDeltaCoder = encodedUpdateDelta;
quint64 updateDelta = updateDeltaCoder;
quint64 updateDelta;
parser.readCompressedCount(updateDelta);
#ifdef VALIDATE_ENTITY_ITEM_PARSER
{
QByteArray encodedUpdateDelta = originalDataBuffer.mid(bytesRead); // maximum possible size
ByteCountCoded<quint64> updateDeltaCoder = encodedUpdateDelta;
quint64 updateDelta2 = updateDeltaCoder;
Q_ASSERT(updateDelta == updateDelta2);
encodedUpdateDelta = updateDeltaCoder; // determine true length
dataAt += encodedUpdateDelta.size();
bytesRead += encodedUpdateDelta.size();
Q_ASSERT(parser.offset() == bytesRead);
}
#endif
if (overwriteLocalData) {
_lastUpdated = lastEditedFromBufferAdjusted + updateDelta; // don't adjust for clock skew since we already did that
#ifdef WANT_DEBUG
@ -499,17 +547,25 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
#endif
}
encodedUpdateDelta = updateDeltaCoder; // determine true length
dataAt += encodedUpdateDelta.size();
bytesRead += encodedUpdateDelta.size();
// Newer bitstreams will have a last simulated and a last updated value
quint64 lastSimulatedFromBufferAdjusted = now;
if (args.bitstreamVersion >= VERSION_ENTITIES_HAS_LAST_SIMULATED_TIME) {
// last simulated is stored as ByteCountCoded delta from lastEdited
QByteArray encodedSimulatedDelta = originalDataBuffer.mid(bytesRead); // maximum possible size
ByteCountCoded<quint64> simulatedDeltaCoder = encodedSimulatedDelta;
quint64 simulatedDelta = simulatedDeltaCoder;
quint64 simulatedDelta;
parser.readCompressedCount(simulatedDelta);
#ifdef VALIDATE_ENTITY_ITEM_PARSER
{
QByteArray encodedSimulatedDelta = originalDataBuffer.mid(bytesRead); // maximum possible size
ByteCountCoded<quint64> simulatedDeltaCoder = encodedSimulatedDelta;
quint64 simulatedDelta2 = simulatedDeltaCoder;
Q_ASSERT(simulatedDelta2 == simulatedDelta);
encodedSimulatedDelta = simulatedDeltaCoder; // determine true length
dataAt += encodedSimulatedDelta.size();
bytesRead += encodedSimulatedDelta.size();
Q_ASSERT(parser.offset() == bytesRead);
}
#endif
if (overwriteLocalData) {
lastSimulatedFromBufferAdjusted = lastEditedFromBufferAdjusted + simulatedDelta; // don't adjust for clock skew since we already did that
if (lastSimulatedFromBufferAdjusted > now) {
@ -521,9 +577,6 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
qCDebug(entities) << " lastSimulatedFromBufferAdjusted:" << debugTime(lastSimulatedFromBufferAdjusted, now);
#endif
}
encodedSimulatedDelta = simulatedDeltaCoder; // determine true length
dataAt += encodedSimulatedDelta.size();
bytesRead += encodedSimulatedDelta.size();
}
#ifdef WANT_DEBUG
@ -537,10 +590,26 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
// Property Flags
QByteArray encodedPropertyFlags = originalDataBuffer.mid(bytesRead); // maximum possible size
EntityPropertyFlags propertyFlags = encodedPropertyFlags;
dataAt += propertyFlags.getEncodedLength();
bytesRead += propertyFlags.getEncodedLength();
EntityPropertyFlags propertyFlags;
parser.readFlags(propertyFlags);
#ifdef VALIDATE_ENTITY_ITEM_PARSER
{
QByteArray encodedPropertyFlags = originalDataBuffer.mid(bytesRead); // maximum possible size
EntityPropertyFlags propertyFlags2 = encodedPropertyFlags;
dataAt += propertyFlags.getEncodedLength();
bytesRead += propertyFlags.getEncodedLength();
Q_ASSERT(propertyFlags2 == propertyFlags);
Q_ASSERT(parser.offset() == bytesRead);
}
#endif
#ifdef VALIDATE_ENTITY_ITEM_PARSER
Q_ASSERT(parser.data() + parser.offset() == dataAt);
#else
const unsigned char* dataAt = parser.data() + parser.offset();
int bytesRead = parser.offset();
#endif
if (args.bitstreamVersion >= VERSION_ENTITIES_HAVE_SIMULATION_OWNER_AND_ACTIONS_OVER_WIRE) {
// pack SimulationOwner and terse update properties near each other
@ -549,6 +618,7 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
// even when we would otherwise ignore the rest of the packet.
if (propertyFlags.getHasProperty(PROP_SIMULATION_OWNER)) {
QByteArray simOwnerData;
int bytes = OctreePacketData::unpackDataFromBytes(dataAt, simOwnerData);
SimulationOwner newSimOwner;

View file

@ -11,7 +11,7 @@
#include <QtCore/QObject>
#include <QDebug>
#include <BufferParser.h>
#include <PacketHeaders.h>
#include "RegisteredMetaTypes.h"
@ -33,11 +33,8 @@ EntityItemID::EntityItemID(const QUuid& id) : QUuid(id)
EntityItemID EntityItemID::readEntityItemIDFromBuffer(const unsigned char* data, int bytesLeftToRead) {
EntityItemID result;
if (bytesLeftToRead >= NUM_BYTES_RFC4122_UUID) {
// id
QByteArray encodedID((const char*)data, NUM_BYTES_RFC4122_UUID);
result = QUuid::fromRfc4122(encodedID);
BufferParser(data, bytesLeftToRead).readUuid(result);
}
return result;
}

View file

@ -12,6 +12,17 @@
#include <QDebug>
#if defined(NSIGHT_FOUND)
#include "nvToolsExt.h"
ProfileRange::ProfileRange(const char *name) {
nvtxRangePush(name);
}
ProfileRange::~ProfileRange() {
nvtxRangePop();
}
#endif
#define ADD_COMMAND(call) _commands.push_back(COMMAND_##call); _commandOffsets.push_back(_params.size());
using namespace gpu;
@ -103,6 +114,23 @@ void Batch::clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, floa
_params.push_back(targets);
}
void Batch::clearColorFramebuffer(Framebuffer::Masks targets, const Vec4& color) {
clearFramebuffer(targets & Framebuffer::BUFFER_COLORS, color, 1.0f, 0);
}
void Batch::clearDepthFramebuffer(float depth) {
clearFramebuffer(Framebuffer::BUFFER_DEPTH, Vec4(0.0f), depth, 0);
}
void Batch::clearStencilFramebuffer(int stencil) {
clearFramebuffer(Framebuffer::BUFFER_STENCIL, Vec4(0.0f), 1.0f, stencil);
}
void Batch::clearDepthStencilFramebuffer(float depth, int stencil) {
clearFramebuffer(Framebuffer::BUFFER_DEPTHSTENCIL, Vec4(0.0f), depth, stencil);
}
void Batch::setInputFormat(const Stream::FormatPointer& format) {
ADD_COMMAND(setInputFormat);
@ -141,6 +169,10 @@ void Batch::setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset
_params.push_back(type);
}
void Batch::setIndexBuffer(const BufferView& buffer) {
setIndexBuffer(buffer._element.getType(), buffer._buffer, buffer._offset);
}
void Batch::setModelTransform(const Transform& model) {
ADD_COMMAND(setModelTransform);
@ -159,10 +191,10 @@ void Batch::setProjectionTransform(const Mat4& proj) {
_params.push_back(cacheData(sizeof(Mat4), &proj));
}
void Batch::setViewportTransform(const Vec4i& viewport) {
ADD_COMMAND(setViewportTransform);
_params.push_back(cacheData(sizeof(Vec4i), &viewport));
void Batch::setViewportTransform(const Vec4i& viewport) {
ADD_COMMAND(setViewportTransform);
_params.push_back(cacheData(sizeof(Vec4i), &viewport));
}
void Batch::setPipeline(const PipelinePointer& pipeline) {
@ -207,7 +239,7 @@ void Batch::setUniformTexture(uint32 slot, const TextureView& view) {
}
void Batch::setFramebuffer(const FramebufferPointer& framebuffer) {
ADD_COMMAND(setUniformTexture);
ADD_COMMAND(setFramebuffer);
_params.push_back(_framebuffers.cache(framebuffer));

View file

@ -26,17 +26,11 @@
#include "Framebuffer.h"
#if defined(NSIGHT_FOUND)
#include "nvToolsExt.h"
class ProfileRange {
public:
ProfileRange(const char *name) {
nvtxRangePush(name);
}
~ProfileRange() {
nvtxRangePop();
}
ProfileRange(const char *name);
~ProfileRange();
};
#define PROFILE_RANGE(name) ProfileRange profileRangeThis(name);
#else
#define PROFILE_RANGE(name)
@ -44,19 +38,6 @@
namespace gpu {
enum Primitive {
POINTS = 0,
LINES,
LINE_STRIP,
TRIANGLES,
TRIANGLE_STRIP,
TRIANGLE_FAN,
QUADS,
QUAD_STRIP,
NUM_PRIMITIVES,
};
enum ReservedSlot {
/* TRANSFORM_OBJECT_SLOT = 6,
TRANSFORM_CAMERA_SLOT = 7,
@ -82,7 +63,12 @@ public:
void drawIndexedInstanced(uint32 nbInstances, Primitive primitiveType, uint32 nbIndices, uint32 startIndex = 0, uint32 startInstance = 0);
// Clear framebuffer layers
// Targets can be any of the render buffers contained in the Framebuffer
void clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, float depth, int stencil);
void clearColorFramebuffer(Framebuffer::Masks targets, const Vec4& color); // not a command, just a shortcut for clearFramebuffer, mask out targets to make sure it touches only color targets
void clearDepthFramebuffer(float depth); // not a command, just a shortcut for clearFramebuffer, it touches only depth target
void clearStencilFramebuffer(int stencil); // not a command, just a shortcut for clearFramebuffer, it touches only stencil target
void clearDepthStencilFramebuffer(float depth, int stencil); // not a command, just a shortcut for clearFramebuffer, it touches depth and stencil target
// Input Stage
// InputFormat
@ -95,6 +81,7 @@ public:
void setInputStream(Slot startChannel, const BufferStream& stream); // not a command, just unroll into a loop of setInputBuffer
void setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset);
void setIndexBuffer(const BufferView& buffer); // not a command, just a shortcut from a BufferView
// Transform Stage
// Vertex position is transformed by ModelTransform from object space to world space

View file

@ -33,9 +33,11 @@ bool Context::makeProgram(Shader& shader, const Shader::BindingSet& bindings) {
}
void Context::render(Batch& batch) {
PROFILE_RANGE(__FUNCTION__);
_backend->render(batch);
}
void Context::syncCache() {
PROFILE_RANGE(__FUNCTION__);
_backend->syncCache();
}

View file

@ -42,7 +42,7 @@ public:
Mat4 _projectionViewUntranslated;
Mat4 _projection;
Mat4 _projectionInverse;
Vec4 _viewport;
Vec4 _viewport; // Public value is int but float in the shader to stay in floats for all the transform computations.
};
template< typename T >

View file

@ -0,0 +1,21 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Draw texture 0 fetched at texcoord.xy
//
// Created by Sam Gateau on 6/22/2015
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
uniform sampler2D colorMap;
varying vec2 varTexcoord;
void main(void) {
gl_FragColor = texture2D(colorMap, varTexcoord);
}

View file

@ -0,0 +1,36 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Draw and transform the unit quad [-1,-1 -> 1,1]
// Simply draw a Triangle_strip of 2 triangles, no input buffers or index buffer needed
//
// Created by Sam Gateau on 6/22/2015
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
varying vec2 varTexcoord;
void main(void) {
const vec4 UNIT_QUAD[4] = vec4[4](
vec4(-1.0, -1.0, 0.0, 1.0),
vec4(1.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4(1.0, 1.0, 0.0, 1.0)
);
vec4 pos = UNIT_QUAD[gl_VertexID];
// standard transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
<$transformModelToClipPos(cam, obj, pos, gl_Position)$>
varTexcoord = (pos.xy + 1) * 0.5;
}

View file

@ -0,0 +1,38 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// Draw the unit quad [-1,-1 -> 1,1] filling in
// Simply draw a Triangle_strip of 2 triangles, no input buffers or index buffer needed
//
// Created by Sam Gateau on 6/22/2015
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
varying vec2 varTexcoord;
void main(void) {
const vec4 UNIT_QUAD[4] = vec4[4](
vec4(-1.0, -1.0, 0.0, 1.0),
vec4(1.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4(1.0, 1.0, 0.0, 1.0)
);
vec4 pos = UNIT_QUAD[gl_VertexID];
// standard transform but applied to the Texcoord
vec4 tc = vec4((pos.xy + 1) * 0.5, pos.zw);
TransformObject obj = getTransformObject();
<$transformModelToWorldPos(obj, tc, tc)$>
gl_Position = pos;
varTexcoord = tc.xy;
}

View file

@ -182,6 +182,9 @@ public:
}
static const Element COLOR_RGBA_32;
static const Element VEC3F_XYZ;
static const Element INDEX_UINT16;
static const Element PART_DRAWCALL;
protected:
uint8 _semantic;
@ -203,6 +206,19 @@ enum ComparisonFunction {
NUM_COMPARISON_FUNCS,
};
enum Primitive {
POINTS = 0,
LINES,
LINE_STRIP,
TRIANGLES,
TRIANGLE_STRIP,
TRIANGLE_FAN,
QUADS,
QUAD_STRIP,
NUM_PRIMITIVES,
};
};

View file

@ -233,13 +233,31 @@ void GLBackend::do_clearFramebuffer(Batch& batch, uint32 paramOffset) {
glmask |= GL_DEPTH_BUFFER_BIT;
}
std::vector<GLenum> drawBuffers;
if (masks & Framebuffer::BUFFER_COLORS) {
glClearColor(color.x, color.y, color.z, color.w);
glmask |= GL_COLOR_BUFFER_BIT;
for (int i = 0; i < Framebuffer::MAX_NUM_RENDER_BUFFERS; i++) {
if (masks & (1 << i)) {
drawBuffers.push_back(GL_COLOR_ATTACHMENT0 + i);
}
}
if (!drawBuffers.empty()) {
glDrawBuffers(drawBuffers.size(), drawBuffers.data());
glClearColor(color.x, color.y, color.z, color.w);
glmask |= GL_COLOR_BUFFER_BIT;
}
}
glClear(glmask);
// Restore the color draw buffers only if a frmaebuffer is bound
if (_output._framebuffer && !drawBuffers.empty()) {
auto glFramebuffer = syncGPUObject(*_output._framebuffer);
if (glFramebuffer) {
glDrawBuffers(glFramebuffer->_colorBuffers.size(), glFramebuffer->_colorBuffers.data());
}
}
(void) CHECK_GL_ERROR();
}
@ -598,10 +616,11 @@ void GLBackend::do_glUniform4fv(Batch& batch, uint32 paramOffset) {
return;
}
updatePipeline();
glUniform4fv(
batch._params[paramOffset + 2]._int,
batch._params[paramOffset + 1]._uint,
(const GLfloat*)batch.editData(batch._params[paramOffset + 0]._uint));
GLint location = batch._params[paramOffset + 2]._int;
GLsizei count = batch._params[paramOffset + 1]._uint;
const GLfloat* value = (const GLfloat*)batch.editData(batch._params[paramOffset + 0]._uint);
glUniform4fv(location, count, value);
(void) CHECK_GL_ERROR();
}

View file

@ -181,6 +181,7 @@ public:
class GLFramebuffer : public GPUObject {
public:
GLuint _fbo = 0;
std::vector<GLenum> _colorBuffers;
GLFramebuffer();
~GLFramebuffer();
@ -198,7 +199,7 @@ public:
void do_setStateFillMode(int32 mode);
void do_setStateCullMode(int32 mode);
void do_setStateFrontFaceClockwise(bool isClockwise);
void do_setStateDepthClipEnable(bool enable);
void do_setStateDepthClampEnable(bool enable);
void do_setStateScissorEnable(bool enable);
void do_setStateMultisampleEnable(bool enable);
void do_setStateAntialiasedLineEnable(bool enable);
@ -298,6 +299,7 @@ protected:
_model(),
_view(),
_projection(),
_viewport(0,0,1,1),
_invalidModel(true),
_invalidView(true),
_invalidProj(false),

View file

@ -40,8 +40,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
unsigned int nbColorBuffers = 0;
GLenum colorBuffers[16];
std::vector<GLenum> colorBuffers;
if (framebuffer.hasColor()) {
static const GLenum colorAttachments[] = {
GL_COLOR_ATTACHMENT0,
@ -69,8 +68,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe
if (gltexture) {
glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[unit], GL_TEXTURE_2D, gltexture->_texture, 0);
}
colorBuffers[nbColorBuffers] = colorAttachments[unit];
nbColorBuffers++;
colorBuffers.push_back(colorAttachments[unit]);
unit++;
}
}
@ -100,8 +98,8 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe
}
// Last but not least, define where we draw
if (nbColorBuffers > 0) {
glDrawBuffers(nbColorBuffers, colorBuffers);
if (!colorBuffers.empty()) {
glDrawBuffers(colorBuffers.size(), colorBuffers.data());
} else {
glDrawBuffer( GL_NONE );
}
@ -139,6 +137,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe
// All is green, assign the gpuobject to the Framebuffer
object = new GLFramebuffer();
object->_fbo = fbo;
object->_colorBuffers = colorBuffers;
Backend::setGPUObject(framebuffer, object);
}

View file

@ -147,9 +147,9 @@ void GLBackend::updatePipeline() {
#if (GPU_TRANSFORM_PROFILE == GPU_CORE)
#else
// If shader program needs the model we need to provide it
if (_pipeline._program_transformObject_model >= 0) {
glUniformMatrix4fv(_pipeline._program_transformObject_model, 1, false, (const GLfloat*) &_transform._transformObject._model);
// If shader program needs the model we need to provide it
if (_pipeline._program_transformObject_model >= 0) {
glUniformMatrix4fv(_pipeline._program_transformObject_model, 1, false, (const GLfloat*) &_transform._transformObject._model);
}
// If shader program needs the inverseView we need to provide it

View file

@ -111,10 +111,10 @@ void makeBindings(GLBackend::GLShader* shader) {
shader->_transformCameraSlot = gpu::TRANSFORM_CAMERA_SLOT;
}
#else
loc = glGetUniformLocation(glprogram, "transformObject_model");
if (loc >= 0) {
shader->_transformObject_model = loc;
}
loc = glGetUniformLocation(glprogram, "transformObject_model");
if (loc >= 0) {
shader->_transformObject_model = loc;
}
loc = glGetUniformLocation(glprogram, "transformCamera_viewInverse");
if (loc >= 0) {

View file

@ -51,7 +51,7 @@ const GLBackend::GLState::Commands makeResetStateCommands() {
CommandPointer(new Command1I(&GLBackend::do_setStateFillMode, DEFAULT.fillMode)),
CommandPointer(new Command1I(&GLBackend::do_setStateCullMode, DEFAULT.cullMode)),
CommandPointer(new Command1B(&GLBackend::do_setStateFrontFaceClockwise, DEFAULT.frontFaceClockwise)),
CommandPointer(new Command1B(&GLBackend::do_setStateDepthClipEnable, DEFAULT.depthClipEnable)),
CommandPointer(new Command1B(&GLBackend::do_setStateDepthClampEnable, DEFAULT.depthClampEnable)),
CommandPointer(new Command1B(&GLBackend::do_setStateScissorEnable, DEFAULT.scissorEnable)),
CommandPointer(new Command1B(&GLBackend::do_setStateMultisampleEnable, DEFAULT.multisampleEnable)),
CommandPointer(new Command1B(&GLBackend::do_setStateAntialiasedLineEnable, DEFAULT.antialisedLineEnable)),
@ -89,8 +89,8 @@ void generateFrontFaceClockwise(GLBackend::GLState::Commands& commands, bool isC
commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateFrontFaceClockwise, isClockwise)));
}
void generateDepthClipEnable(GLBackend::GLState::Commands& commands, bool enable) {
commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateDepthClipEnable, enable)));
void generateDepthClampEnable(GLBackend::GLState::Commands& commands, bool enable) {
commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateDepthClampEnable, enable)));
}
void generateScissorEnable(GLBackend::GLState::Commands& commands, bool enable) {
@ -176,8 +176,8 @@ GLBackend::GLState* GLBackend::syncGPUObject(const State& state) {
generateFrontFaceClockwise(object->_commands, state.isFrontFaceClockwise());
break;
}
case State::DEPTH_CLIP_ENABLE: {
generateDepthClipEnable(object->_commands, state.isDepthClipEnable());
case State::DEPTH_CLAMP_ENABLE: {
generateDepthClampEnable(object->_commands, state.isDepthClampEnable());
break;
}
case State::SCISSOR_ENABLE: {
@ -373,7 +373,7 @@ void GLBackend::getCurrentGLState(State::Data& state) {
GLint winding;
glGetIntegerv(GL_FRONT_FACE, &winding);
state.frontFaceClockwise = (winding == GL_CW);
state.depthClipEnable = glIsEnabled(GL_DEPTH_CLAMP);
state.depthClampEnable = glIsEnabled(GL_DEPTH_CLAMP);
state.scissorEnable = glIsEnabled(GL_SCISSOR_TEST);
state.multisampleEnable = glIsEnabled(GL_MULTISAMPLE);
state.antialisedLineEnable = glIsEnabled(GL_LINE_SMOOTH);
@ -533,8 +533,8 @@ void GLBackend::do_setStateFrontFaceClockwise(bool isClockwise) {
}
}
void GLBackend::do_setStateDepthClipEnable(bool enable) {
if (_pipeline._stateCache.depthClipEnable != enable) {
void GLBackend::do_setStateDepthClampEnable(bool enable) {
if (_pipeline._stateCache.depthClampEnable != enable) {
if (enable) {
glEnable(GL_DEPTH_CLAMP);
} else {
@ -542,7 +542,7 @@ void GLBackend::do_setStateDepthClipEnable(bool enable) {
}
(void) CHECK_GL_ERROR();
_pipeline._stateCache.depthClipEnable = enable;
_pipeline._stateCache.depthClampEnable = enable;
}
}

View file

@ -15,7 +15,6 @@
using namespace gpu;
// Transform Stage
void GLBackend::do_setModelTransform(Batch& batch, uint32 paramOffset) {
_transform._model = batch._transforms.get(batch._params[paramOffset]._uint);
_transform._invalidModel = true;
@ -31,11 +30,10 @@ void GLBackend::do_setProjectionTransform(Batch& batch, uint32 paramOffset) {
_transform._invalidProj = true;
}
void GLBackend::do_setViewportTransform(Batch& batch, uint32 paramOffset) {
memcpy(&_transform._viewport, batch.editData(batch._params[paramOffset]._uint), sizeof(Vec4i));
_transform._invalidViewport = true;
}
void GLBackend::do_setViewportTransform(Batch& batch, uint32 paramOffset) {
memcpy(&_transform._viewport, batch.editData(batch._params[paramOffset]._uint), sizeof(Vec4i));
_transform._invalidViewport = true;
}
void GLBackend::initTransform() {
#if (GPU_TRANSFORM_PROFILE == GPU_CORE)
@ -68,7 +66,7 @@ void GLBackend::syncTransformStateCache() {
_transform._invalidView = true;
_transform._invalidModel = true;
glGetIntegerv(GL_VIEWPORT, (GLint*) &_transform._viewport);
glGetIntegerv(GL_VIEWPORT, (GLint*) &_transform._viewport);
GLint currentMode;
glGetIntegerv(GL_MATRIX_MODE, &currentMode);
@ -87,11 +85,11 @@ void GLBackend::updateTransform() {
GLint originalMatrixMode;
glGetIntegerv(GL_MATRIX_MODE, &originalMatrixMode);
// Check all the dirty flags and update the state accordingly
if (_transform._invalidViewport) {
_transform._transformCamera._viewport = glm::vec4(_transform._viewport);
// Where we assign the GL viewport
glViewport(_transform._viewport.x, _transform._viewport.y, _transform._viewport.z, _transform._viewport.w);
if (_transform._invalidViewport) {
_transform._transformCamera._viewport = glm::vec4(_transform._viewport);
// Where we assign the GL viewport
glViewport(_transform._viewport.x, _transform._viewport.y, _transform._viewport.z, _transform._viewport.w);
}
if (_transform._invalidProj) {
@ -116,13 +114,13 @@ void GLBackend::updateTransform() {
}
#if (GPU_TRANSFORM_PROFILE == GPU_CORE)
if (_transform._invalidView || _transform._invalidProj || _transform._invalidViewport) {
if (_transform._invalidView || _transform._invalidProj || _transform._invalidViewport) {
glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_CAMERA_SLOT, 0);
glBindBuffer(GL_ARRAY_BUFFER, _transform._transformCameraBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformCamera), (const void*) &_transform._transformCamera, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
CHECK_GL_ERROR();
}
}
if (_transform._invalidModel) {
glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_OBJECT_SLOT, 0);

View file

@ -15,6 +15,9 @@
using namespace gpu;
const Element Element::COLOR_RGBA_32 = Element(VEC4, UINT8, RGBA);
const Element Element::VEC3F_XYZ = Element(VEC3, FLOAT, XYZ);
const Element Element::INDEX_UINT16 = Element(SCALAR, UINT16, INDEX);
const Element Element::PART_DRAWCALL = Element(VEC4, UINT32, PART);
Resource::Size Resource::Sysmem::allocateMemory(Byte** dataAllocated, Size size) {
if ( !dataAllocated ) {

View file

@ -0,0 +1,44 @@
//
// StandardShaderLib.cpp
// libraries/gpu/src/gpu
//
// Collection of standard shaders that can be used all over the place
//
// Created by Sam Gateau on 6/22/2015.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "StandardShaderLib.h"
#include "DrawTransformUnitQuad_vert.h"
#include "DrawViewportQuadTransformTexcoord_vert.h"
#include "DrawTexture_frag.h"
using namespace gpu;
ShaderPointer StandardShaderLib::_drawTransformUnitQuadVS;
ShaderPointer StandardShaderLib::_drawViewportQuadTransformTexcoordVS;
ShaderPointer StandardShaderLib::_drawTexturePS;
ShaderPointer StandardShaderLib::getDrawTransformUnitQuadVS() {
if (!_drawTransformUnitQuadVS) {
_drawTransformUnitQuadVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(DrawTransformUnitQuad_vert)));
}
return _drawTransformUnitQuadVS;
}
ShaderPointer StandardShaderLib::getDrawViewportQuadTransformTexcoordVS() {
if (!_drawViewportQuadTransformTexcoordVS) {
_drawViewportQuadTransformTexcoordVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(DrawViewportQuadTransformTexcoord_vert)));
}
return _drawViewportQuadTransformTexcoordVS;
}
ShaderPointer StandardShaderLib::getDrawTexturePS() {
if (!_drawTexturePS) {
_drawTexturePS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(DrawTexture_frag)));
}
return _drawTexturePS;
}

View file

@ -0,0 +1,44 @@
//
// StandardShaderLib.h
// libraries/gpu/src/gpu
//
// Collection of standard shaders that can be used all over the place
//
// Created by Sam Gateau on 6/22/2015.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_gpu_StandardShaderLib_h
#define hifi_gpu_StandardShaderLib_h
#include <assert.h>
#include "Shader.h"
namespace gpu {
class StandardShaderLib {
public:
// Shader draw the unit quad objectPos = ([(-1,-1),(1,1)]) and transform it by the full model transform stack (Model, View, Proj).
// A texcoord attribute is also generated texcoord = [(0,0),(1,1)]
static ShaderPointer getDrawTransformUnitQuadVS();
// Shader draws the unit quad in the full viewport clipPos = ([(-1,-1),(1,1)]) and transform the texcoord = [(0,0),(1,1)] by the model transform.
static ShaderPointer getDrawViewportQuadTransformTexcoordVS();
static ShaderPointer getDrawTexturePS();
protected:
static ShaderPointer _drawTransformUnitQuadVS;
static ShaderPointer _drawViewportQuadTransformTexcoordVS;
static ShaderPointer _drawTexturePS;
};
};
#endif

View file

@ -24,20 +24,20 @@ State::~State() {
// Please make sure to go check makeResetStateCommands() before modifying this value
const State::Data State::DEFAULT = State::Data();
State::Signature State::evalSignature(const Data& state) {
State::Signature State::evalSignature(const Data& state) {
Signature signature(0);
if (state.fillMode != State::DEFAULT.fillMode) {
signature.set(State::FILL_MODE);
}
}
if (state.cullMode != State::DEFAULT.cullMode) {
signature.set(State::CULL_MODE);
}
if (state.frontFaceClockwise != State::DEFAULT.frontFaceClockwise) {
signature.set(State::FRONT_FACE_CLOCKWISE);
}
if (state.depthClipEnable != State::DEFAULT.depthClipEnable) {
signature.set(State::DEPTH_CLIP_ENABLE);
if (state.depthClampEnable != State::DEFAULT.depthClampEnable) {
signature.set(State::DEPTH_CLAMP_ENABLE);
}
if (state.scissorEnable != State::DEFAULT.scissorEnable) {
signature.set(State::SCISSOR_ENABLE);
@ -47,16 +47,16 @@ State::Signature State::evalSignature(const Data& state) {
}
if (state.antialisedLineEnable != State::DEFAULT.antialisedLineEnable) {
signature.set(State::ANTIALISED_LINE_ENABLE);
}
}
if (state.depthBias != State::DEFAULT.depthBias) {
signature.set(State::DEPTH_BIAS);
}
if (state.depthBiasSlopeScale != State::DEFAULT.depthBiasSlopeScale) {
signature.set(State::DEPTH_BIAS_SLOPE_SCALE);
}
}
if (state.depthTest != State::DEFAULT.depthTest) {
signature.set(State::DEPTH_TEST);
}
}
if (state.stencilActivation != State::DEFAULT.stencilActivation) {
signature.set(State::STENCIL_ACTIVATION);
}
@ -68,21 +68,21 @@ State::Signature State::evalSignature(const Data& state) {
}
if (state.sampleMask != State::DEFAULT.sampleMask) {
signature.set(State::SAMPLE_MASK);
}
if (state.alphaToCoverageEnable != State::DEFAULT.alphaToCoverageEnable) {
signature.set(State::ALPHA_TO_COVERAGE_ENABLE);
}
if (state.blendFunction != State::DEFAULT.blendFunction) {
signature.set(State::BLEND_FUNCTION);
}
if (state.colorWriteMask != State::DEFAULT.colorWriteMask) {
signature.set(State::COLOR_WRITE_MASK);
}
return signature;
}
State::State(const Data& values) :
_values(values) {
_signature = evalSignature(_values);
}
}
if (state.alphaToCoverageEnable != State::DEFAULT.alphaToCoverageEnable) {
signature.set(State::ALPHA_TO_COVERAGE_ENABLE);
}
if (state.blendFunction != State::DEFAULT.blendFunction) {
signature.set(State::BLEND_FUNCTION);
}
if (state.colorWriteMask != State::DEFAULT.colorWriteMask) {
signature.set(State::COLOR_WRITE_MASK);
}
return signature;
}
State::State(const Data& values) :
_values(values) {
_signature = evalSignature(_values);
}

View file

@ -249,7 +249,7 @@ public:
uint8 colorWriteMask = WRITE_ALL;
bool frontFaceClockwise : 1;
bool depthClipEnable : 1;
bool depthClampEnable : 1;
bool scissorEnable : 1;
bool multisampleEnable : 1;
bool antialisedLineEnable : 1;
@ -257,7 +257,7 @@ public:
Data() :
frontFaceClockwise(false),
depthClipEnable(false),
depthClampEnable(false),
scissorEnable(false),
multisampleEnable(false),
antialisedLineEnable(false),
@ -276,8 +276,8 @@ public:
void setFrontFaceClockwise(bool isClockwise) { SET_FIELD(FRONT_FACE_CLOCKWISE, DEFAULT.frontFaceClockwise, isClockwise, _values.frontFaceClockwise); }
bool isFrontFaceClockwise() const { return _values.frontFaceClockwise; }
void setDepthClipEnable(bool enable) { SET_FIELD(DEPTH_CLIP_ENABLE, DEFAULT.depthClipEnable, enable, _values.depthClipEnable); }
bool isDepthClipEnable() const { return _values.depthClipEnable; }
void setDepthClampEnable(bool enable) { SET_FIELD(DEPTH_CLAMP_ENABLE, DEFAULT.depthClampEnable, enable, _values.depthClampEnable); }
bool isDepthClampEnable() const { return _values.depthClampEnable; }
void setScissorEnable(bool enable) { SET_FIELD(SCISSOR_ENABLE, DEFAULT.scissorEnable, enable, _values.scissorEnable); }
bool isScissorEnable() const { return _values.scissorEnable; }
@ -341,6 +341,7 @@ public:
// Color write mask
void setColorWriteMask(uint8 mask) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, mask, _values.colorWriteMask); }
void setColorWriteMask(bool red, bool green, bool blue, bool alpha) { uint32 value = ((WRITE_RED * red) | (WRITE_GREEN * green) | (WRITE_BLUE * blue) | (WRITE_ALPHA * alpha)); SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, value, _values.colorWriteMask); }
uint8 getColorWriteMask() const { return _values.colorWriteMask; }
// All the possible fields
@ -348,7 +349,7 @@ public:
FILL_MODE,
CULL_MODE,
FRONT_FACE_CLOCKWISE,
DEPTH_CLIP_ENABLE,
DEPTH_CLAMP_ENABLE,
SCISSOR_ENABLE,
MULTISAMPLE_ENABLE,
ANTIALISED_LINE_ENABLE,

View file

@ -86,6 +86,7 @@ TransformCamera getTransformCamera() {
return camera;
}
uniform mat4 transformObject_model;
uniform mat4 transformCamera_viewInverse;
uniform vec4 transformCamera_viewport;
@ -130,6 +131,16 @@ uniform vec4 transformCamera_viewport;
<@endif@>
<@endfunc@>
<@func transformModelToWorldPos(objectTransform, modelPos, worldPos)@>
<@if GPU_TRANSFORM_PROFILE == GPU_CORE@>
{ // transformModelToWorldPos
<$worldPos$> = (<$objectTransform$>._model * <$modelPos$>);
}
<@else@>
<$worldPos$> = (transformObject_model * <$modelPos$>);
<@endif@>
<@endfunc@>
<@func transformModelToEyeDir(cameraTransform, objectTransform, modelDir, eyeDir)@>
<@if GPU_TRANSFORM_PROFILE == GPU_CORE@>
{ // transformModelToEyeDir

View file

@ -113,6 +113,8 @@ public:
// Generate a BufferStream on the mesh vertices and attributes
const gpu::BufferStream makeBufferStream() const;
static gpu::Primitive topologyToPrimitive(Topology topo) { return static_cast<gpu::Primitive>(topo); }
protected:
gpu::Stream::FormatPointer _vertexFormat;

View file

@ -77,18 +77,27 @@ void Light::setMaximumRadius(float radius) {
editSchema()._attenuation = Vec4(surfaceRadius, 1.0f/surfaceRadius, CutOffIntensityRatio, radius);
}
#include <math.h>
void Light::setSpotAngle(float angle) {
if (angle <= 0.f) {
angle = 0.0f;
double dangle = angle;
if (dangle <= 0.0) {
dangle = 0.0;
}
editSchema()._spot.x = cos(angle);
editSchema()._spot.y = sin(angle);
editSchema()._spot.z = angle;
if (dangle > glm::half_pi<double>()) {
dangle = glm::half_pi<double>();
}
auto cosAngle = cos(dangle);
auto sinAngle = sin(dangle);
editSchema()._spot.x = (float) std::abs(cosAngle);
editSchema()._spot.y = (float) std::abs(sinAngle);
editSchema()._spot.z = (float) angle;
}
void Light::setSpotExponent(float exponent) {
if (exponent <= 0.f) {
exponent = 1.0f;
exponent = 0.0f;
}
editSchema()._spot.w = exponent;
}

View file

@ -81,6 +81,7 @@ public:
bool isSpot() const { return getType() == SPOT; }
void setSpotAngle(float angle);
float getSpotAngle() const { return getSchema()._spot.z; }
glm::vec2 getSpotAngleCosSin() const { return glm::vec2(getSchema()._spot.x, getSchema()._spot.y); }
void setSpotExponent(float exponent);
float getSpotExponent() const { return getSchema()._spot.w; }
@ -107,7 +108,7 @@ public:
Color _color{1.0f};
float _intensity{1.0f};
Vec4 _attenuation{1.0f};
Vec4 _spot{0.0f, 0.0f, 0.0f, 3.0f};
Vec4 _spot{0.0f, 0.0f, 0.0f, 0.0f};
Vec4 _shadow{0.0f};
Vec4 _control{0.0f, 0.0f, 0.0f, 0.0f};

View file

@ -51,7 +51,7 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky
static gpu::BufferPointer theBuffer;
static gpu::Stream::FormatPointer theFormat;
static gpu::BufferPointer theConstants;
int SKYBOX_CONSTANTS_SLOT = 0; // need to be defined by the compilation of the shader
static int SKYBOX_CONSTANTS_SLOT = 0; // need to be defined by the compilation of the shader
if (!thePipeline) {
auto skyVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(Skybox_vert)));
auto skyFS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(Skybox_frag)));

View file

@ -248,7 +248,7 @@ bool EntityMotionState::remoteSimulationOutOfSync(uint32_t simulationStep) {
btTransform xform = _body->getWorldTransform();
_serverPosition = bulletToGLM(xform.getOrigin());
_serverRotation = bulletToGLM(xform.getRotation());
_serverVelocity = bulletToGLM(_body->getLinearVelocity());
_serverVelocity = getBodyLinearVelocity();
_serverAngularVelocity = bulletToGLM(_body->getAngularVelocity());
_lastStep = simulationStep;
_serverActionData = _entity->getActionData();
@ -536,7 +536,7 @@ void EntityMotionState::bump(quint8 priority) {
void EntityMotionState::resetMeasuredBodyAcceleration() {
_lastMeasureStep = ObjectMotionState::getWorldSimulationStep();
if (_body) {
_lastVelocity = bulletToGLM(_body->getLinearVelocity());
_lastVelocity = getBodyLinearVelocity();
} else {
_lastVelocity = glm::vec3(0.0f);
}
@ -555,7 +555,7 @@ void EntityMotionState::measureBodyAcceleration() {
// Note: the integration equation for velocity uses damping: v1 = (v0 + a * dt) * (1 - D)^dt
// hence the equation for acceleration is: a = (v1 / (1 - D)^dt - v0) / dt
glm::vec3 velocity = bulletToGLM(_body->getLinearVelocity());
glm::vec3 velocity = getBodyLinearVelocity();
_measuredAcceleration = (velocity / powf(1.0f - _body->getLinearDamping(), dt) - _lastVelocity) * invDt;
_lastVelocity = velocity;
if (numSubsteps > PHYSICS_ENGINE_MAX_NUM_SUBSTEPS) {

View file

@ -13,9 +13,9 @@
#include "ObjectAction.h"
ObjectAction::ObjectAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) :
ObjectAction::ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) :
btActionInterface(),
_id(id),
EntityActionInterface(type, id),
_active(false),
_ownerEntity(ownerEntity) {
}
@ -24,15 +24,15 @@ ObjectAction::~ObjectAction() {
}
void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
if (!_active) {
return;
}
if (_ownerEntity.expired()) {
qDebug() << "warning -- action with no entity removing self from btCollisionWorld.";
btDynamicsWorld* dynamicsWorld = static_cast<btDynamicsWorld*>(collisionWorld);
dynamicsWorld->removeAction(this);
return;
}
if (!_active) {
return;
}
updateActionWorker(deltaTimeStep);
}
@ -129,11 +129,3 @@ void ObjectAction::setAngularVelocity(glm::vec3 angularVelocity) {
rigidBody->activate();
}
QByteArray ObjectAction::serialize() {
assert(false);
return QByteArray();
}
void ObjectAction::deserialize(QByteArray serializedArguments) {
assert(false);
}

View file

@ -17,8 +17,6 @@
#include <btBulletDynamicsCommon.h>
#include <EntityItem.h>
#include "ObjectMotionState.h"
#include "BulletUtil.h"
#include "EntityActionInterface.h"
@ -26,31 +24,25 @@
class ObjectAction : public btActionInterface, public EntityActionInterface {
public:
ObjectAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity);
ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity);
virtual ~ObjectAction();
const QUuid& getID() const { return _id; }
virtual EntityActionType getType() { assert(false); return ACTION_TYPE_NONE; }
virtual void removeFromSimulation(EntitySimulation* simulation) const;
virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; }
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; }
virtual bool updateArguments(QVariantMap arguments) { return false; }
virtual QVariantMap getArguments() { return QVariantMap(); }
virtual bool updateArguments(QVariantMap arguments) = 0;
virtual QVariantMap getArguments() = 0;
// this is called from updateAction and should be overridden by subclasses
virtual void updateActionWorker(float deltaTimeStep) {}
virtual void updateActionWorker(float deltaTimeStep) = 0;
// these are from btActionInterface
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
virtual void debugDraw(btIDebugDraw* debugDrawer);
virtual QByteArray serialize();
virtual void deserialize(QByteArray serializedArguments);
private:
QUuid _id;
QReadWriteLock _lock;
virtual QByteArray serialize() const = 0;
virtual void deserialize(QByteArray serializedArguments) = 0;
protected:
@ -70,6 +62,10 @@ protected:
bool tryLockForWrite() { return _lock.tryLockForWrite(); }
void unlock() { _lock.unlock(); }
private:
QReadWriteLock _lock;
protected:
bool _active;
EntityItemWeakPointer _ownerEntity;
};

View file

@ -15,8 +15,12 @@
const uint16_t ObjectActionOffset::offsetVersion = 1;
ObjectActionOffset::ObjectActionOffset(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) :
ObjectAction(type, id, ownerEntity) {
ObjectActionOffset::ObjectActionOffset(const QUuid& id, EntityItemPointer ownerEntity) :
ObjectAction(ACTION_TYPE_OFFSET, id, ownerEntity),
_pointToOffsetFrom(0.0f),
_linearDistance(0.0f),
_linearTimeScale(FLT_MAX),
_positionalTargetSet(false) {
#if WANT_DEBUG
qDebug() << "ObjectActionOffset::ObjectActionOffset";
#endif
@ -44,6 +48,7 @@ void ObjectActionOffset::updateActionWorker(btScalar deltaTimeStep) {
unlock();
return;
}
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
btRigidBody* rigidBody = motionState->getRigidBody();
if (!rigidBody) {
@ -52,21 +57,32 @@ void ObjectActionOffset::updateActionWorker(btScalar deltaTimeStep) {
return;
}
if (_positionalTargetSet) {
glm::vec3 offset = _pointToOffsetFrom - bulletToGLM(rigidBody->getCenterOfMassPosition());
float offsetLength = glm::length(offset);
float offsetError = _linearDistance - offsetLength;
// if (glm::abs(offsetError) > IGNORE_POSITION_DELTA) {
if (glm::abs(offsetError) > 0.0f) {
float offsetErrorAbs = glm::abs(offsetError);
float offsetErrorDirection = - offsetError / offsetErrorAbs;
glm::vec3 previousVelocity = bulletToGLM(rigidBody->getLinearVelocity());
glm::vec3 velocityAdjustment = glm::normalize(offset) * offsetErrorDirection * offsetErrorAbs / _linearTimeScale;
rigidBody->setLinearVelocity(glmToBullet(previousVelocity + velocityAdjustment));
// rigidBody->setLinearVelocity(glmToBullet(velocityAdjustment));
const float MAX_LINEAR_TIMESCALE = 600.0f; // 10 minutes is a long time
if (_positionalTargetSet && _linearTimeScale < MAX_LINEAR_TIMESCALE) {
if (_needsActivation) {
rigidBody->activate();
_needsActivation = false;
}
glm::vec3 objectPosition = bulletToGLM(rigidBody->getCenterOfMassPosition());
glm::vec3 springAxis = objectPosition - _pointToOffsetFrom; // from anchor to object
float distance = glm::length(springAxis);
if (distance > FLT_EPSILON) {
springAxis /= distance; // normalize springAxis
// compute (critically damped) target velocity of spring relaxation
glm::vec3 offset = (distance - _linearDistance) * springAxis;
glm::vec3 targetVelocity = (-1.0f / _linearTimeScale) * offset;
// compute current velocity and its parallel component
glm::vec3 currentVelocity = bulletToGLM(rigidBody->getLinearVelocity());
glm::vec3 parallelVelocity = glm::dot(currentVelocity, springAxis) * springAxis;
// we blend the parallel component with the spring's target velocity to get the new velocity
float blend = deltaTimeStep / _linearTimeScale;
if (blend > 1.0f) {
blend = 1.0f;
}
rigidBody->setLinearVelocity(glmToBullet(currentVelocity + blend * (targetVelocity - parallelVelocity)));
}
}
@ -75,45 +91,45 @@ void ObjectActionOffset::updateActionWorker(btScalar deltaTimeStep) {
bool ObjectActionOffset::updateArguments(QVariantMap arguments) {
bool pOk0 = true;
bool ok = true;
glm::vec3 pointToOffsetFrom =
EntityActionInterface::extractVec3Argument("offset action", arguments, "pointToOffsetFrom", pOk0, true);
EntityActionInterface::extractVec3Argument("offset action", arguments, "pointToOffsetFrom", ok, true);
if (!ok) {
return false;
}
bool pOk1 = true;
ok = true;
float linearTimeScale =
EntityActionInterface::extractFloatArgument("offset action", arguments, "linearTimeScale", pOk1, false);
EntityActionInterface::extractFloatArgument("offset action", arguments, "linearTimeScale", ok, false);
if (ok) {
if (linearTimeScale <= 0.0f) {
qDebug() << "offset action -- linearTimeScale must be greater than zero.";
return false;
}
} else {
linearTimeScale = 0.1f;
}
bool pOk2 = true;
ok = true;
float linearDistance =
EntityActionInterface::extractFloatArgument("offset action", arguments, "linearDistance", pOk2, false);
if (!pOk0) {
return false;
}
if (pOk1 && linearTimeScale <= 0.0f) {
qDebug() << "offset action -- linearTimeScale must be greater than zero.";
return false;
EntityActionInterface::extractFloatArgument("offset action", arguments, "linearDistance", ok, false);
if (!ok) {
linearDistance = 0.0f;
}
lockForWrite();
_pointToOffsetFrom = pointToOffsetFrom;
_positionalTargetSet = true;
if (pOk1) {
// only change stuff if something actually changed
if (_pointToOffsetFrom != pointToOffsetFrom
|| _linearTimeScale != linearTimeScale
|| _linearDistance != linearDistance) {
lockForWrite();
_pointToOffsetFrom = pointToOffsetFrom;
_linearTimeScale = linearTimeScale;
} else {
_linearTimeScale = 0.1f;
}
if (pOk2) {
_linearDistance = linearDistance;
} else {
_linearDistance = 1.0f;
_positionalTargetSet = true;
_active = true;
_needsActivation = true;
unlock();
}
_active = true;
unlock();
return true;
}
@ -127,7 +143,7 @@ QVariantMap ObjectActionOffset::getArguments() {
return arguments;
}
QByteArray ObjectActionOffset::serialize() {
QByteArray ObjectActionOffset::serialize() const {
QByteArray ba;
QDataStream dataStream(&ba, QIODevice::WriteOnly);
dataStream << getType();
@ -146,13 +162,14 @@ void ObjectActionOffset::deserialize(QByteArray serializedArguments) {
QDataStream dataStream(serializedArguments);
EntityActionType type;
QUuid id;
uint16_t serializationVersion;
dataStream >> type;
assert(type == getType());
QUuid id;
dataStream >> id;
assert(id == getID());
uint16_t serializationVersion;
dataStream >> serializationVersion;
if (serializationVersion != ObjectActionOffset::offsetVersion) {
return;

View file

@ -19,17 +19,15 @@
class ObjectActionOffset : public ObjectAction {
public:
ObjectActionOffset(EntityActionType type, QUuid id, EntityItemPointer ownerEntity);
ObjectActionOffset(const QUuid& id, EntityItemPointer ownerEntity);
virtual ~ObjectActionOffset();
virtual EntityActionType getType() { return ACTION_TYPE_OFFSET; }
virtual bool updateArguments(QVariantMap arguments);
virtual QVariantMap getArguments();
virtual void updateActionWorker(float deltaTimeStep);
virtual QByteArray serialize();
virtual QByteArray serialize() const;
virtual void deserialize(QByteArray serializedArguments);
private:
@ -38,6 +36,7 @@ public:
float _linearDistance;
float _linearTimeScale;
bool _positionalTargetSet;
bool _needsActivation = true;
};
#endif // hifi_ObjectActionOffset_h

View file

@ -17,8 +17,8 @@ const float SPRING_MAX_SPEED = 10.0f;
const uint16_t ObjectActionSpring::springVersion = 1;
ObjectActionSpring::ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) :
ObjectAction(type, id, ownerEntity),
ObjectActionSpring::ObjectActionSpring(const QUuid& id, EntityItemPointer ownerEntity) :
ObjectAction(ACTION_TYPE_SPRING, id, ownerEntity),
_positionalTarget(glm::vec3(0.0f)),
_linearTimeScale(0.2f),
_positionalTargetSet(false),
@ -206,7 +206,7 @@ QVariantMap ObjectActionSpring::getArguments() {
return arguments;
}
QByteArray ObjectActionSpring::serialize() {
QByteArray ObjectActionSpring::serialize() const {
QByteArray serializedActionArguments;
QDataStream dataStream(&serializedActionArguments, QIODevice::WriteOnly);
@ -229,13 +229,14 @@ void ObjectActionSpring::deserialize(QByteArray serializedArguments) {
QDataStream dataStream(serializedArguments);
EntityActionType type;
QUuid id;
uint16_t serializationVersion;
dataStream >> type;
assert(type == getType());
QUuid id;
dataStream >> id;
assert(id == getID());
uint16_t serializationVersion;
dataStream >> serializationVersion;
if (serializationVersion != ObjectActionSpring::springVersion) {
return;

View file

@ -12,24 +12,19 @@
#ifndef hifi_ObjectActionSpring_h
#define hifi_ObjectActionSpring_h
#include <QUuid>
#include <EntityItem.h>
#include "ObjectAction.h"
class ObjectActionSpring : public ObjectAction {
public:
ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity);
ObjectActionSpring(const QUuid& id, EntityItemPointer ownerEntity);
virtual ~ObjectActionSpring();
virtual EntityActionType getType() { return ACTION_TYPE_SPRING; }
virtual bool updateArguments(QVariantMap arguments);
virtual QVariantMap getArguments();
virtual void updateActionWorker(float deltaTimeStep);
virtual QByteArray serialize();
virtual QByteArray serialize() const;
virtual void deserialize(QByteArray serializedArguments);
protected:

View file

@ -80,8 +80,19 @@ void ObjectMotionState::setBodyGravity(const glm::vec3& gravity) const {
}
glm::vec3 ObjectMotionState::getBodyLinearVelocity() const {
return bulletToGLM(_body->getLinearVelocity());
// returns the body's velocity unless it is moving too slow in which case returns zero
btVector3 velocity = _body->getLinearVelocity();
// NOTE: the threshold to use here relates to the linear displacement threshold (dX) for sending updates
// to objects that are tracked server-side (e.g. entities which use dX = 2mm). Hence an object moving
// just under this velocity threshold would trigger an update about V/dX times per second.
const float MIN_LINEAR_SPEED_SQUARED = 0.0036f; // 6 mm/sec
if (velocity.length2() < MIN_LINEAR_SPEED_SQUARED) {
velocity *= 0.0f;
}
return bulletToGLM(velocity);
}
glm::vec3 ObjectMotionState::getObjectLinearVelocityChange() const {
return glm::vec3(0.0f); // Subclasses override where meaningful.
}

View file

@ -25,6 +25,7 @@
#include "gpu/Batch.h"
#include "gpu/GLBackend.h"
#include "gpu/StandardShaderLib.h"
#include "simple_vert.h"
#include "simple_textured_frag.h"
@ -32,6 +33,7 @@
#include "deferred_light_vert.h"
#include "deferred_light_limited_vert.h"
#include "deferred_light_spot_vert.h"
#include "directional_light_frag.h"
#include "directional_light_shadow_map_frag.h"
@ -90,26 +92,40 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) {
gpu::Shader::makeProgram(*_emissiveShader, slotBindings);
_viewState = viewState;
loadLightProgram(directional_light_frag, false, _directionalLight, _directionalLightLocations);
loadLightProgram(directional_light_shadow_map_frag, false, _directionalLightShadowMap,
loadLightProgram(deferred_light_vert, directional_light_frag, false, _directionalLight, _directionalLightLocations);
loadLightProgram(deferred_light_vert, directional_light_shadow_map_frag, false, _directionalLightShadowMap,
_directionalLightShadowMapLocations);
loadLightProgram(directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap,
loadLightProgram(deferred_light_vert, directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap,
_directionalLightCascadedShadowMapLocations);
loadLightProgram(directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations);
loadLightProgram(directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap,
loadLightProgram(deferred_light_vert, directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations);
loadLightProgram(deferred_light_vert, directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap,
_directionalAmbientSphereLightShadowMapLocations);
loadLightProgram(directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap,
loadLightProgram(deferred_light_vert, directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap,
_directionalAmbientSphereLightCascadedShadowMapLocations);
loadLightProgram(directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations);
loadLightProgram(directional_skybox_light_shadow_map_frag, false, _directionalSkyboxLightShadowMap,
loadLightProgram(deferred_light_vert, directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations);
loadLightProgram(deferred_light_vert, directional_skybox_light_shadow_map_frag, false, _directionalSkyboxLightShadowMap,
_directionalSkyboxLightShadowMapLocations);
loadLightProgram(directional_skybox_light_cascaded_shadow_map_frag, false, _directionalSkyboxLightCascadedShadowMap,
loadLightProgram(deferred_light_vert, directional_skybox_light_cascaded_shadow_map_frag, false, _directionalSkyboxLightCascadedShadowMap,
_directionalSkyboxLightCascadedShadowMapLocations);
loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations);
loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations);
loadLightProgram(deferred_light_limited_vert, point_light_frag, true, _pointLight, _pointLightLocations);
loadLightProgram(deferred_light_spot_vert, spot_light_frag, true, _spotLight, _spotLightLocations);
{
auto VSFS = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS();
auto PSBlit = gpu::StandardShaderLib::getDrawTexturePS();
auto blitProgram = gpu::ShaderPointer(gpu::Shader::createProgram(VSFS, PSBlit));
gpu::Shader::makeProgram(*blitProgram);
gpu::StatePointer blitState = gpu::StatePointer(new gpu::State());
blitState->setBlendFunction(true,
gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA,
gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
blitState->setColorWriteMask(true, true, true, false);
_blitLightBuffer = gpu::PipelinePointer(gpu::Pipeline::create(blitProgram, blitState));
}
// Allocate a global light representing the Global Directional light casting shadow (the sun) and the ambient light
_globalLights.push_back(0);
@ -201,51 +217,40 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu
}
void DeferredLightingEffect::prepare(RenderArgs* args) {
// clear the normal and specular buffers
auto textureCache = DependencyManager::get<TextureCache>();
textureCache->setPrimaryDrawBuffers(false, true, false);
glClear(GL_COLOR_BUFFER_BIT);
textureCache->setPrimaryDrawBuffers(false, false, true);
// clearing to zero alpha for specular causes problems on my Nvidia card; clear to lowest non-zero value instead
gpu::Batch batch;
// clear the normal and specular buffers
batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLOR1, glm::vec4(0.0f, 0.0f, 0.0f, 0.0f));
const float MAX_SPECULAR_EXPONENT = 128.0f;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f / MAX_SPECULAR_EXPONENT);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
textureCache->setPrimaryDrawBuffers(true, false, false);
batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLOR2, glm::vec4(0.0f, 0.0f, 0.0f, 1.0f / MAX_SPECULAR_EXPONENT));
args->_context->syncCache();
args->_context->render(batch);
}
void DeferredLightingEffect::render(RenderArgs* args) {
// perform deferred lighting, rendering to free fbo
glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glDepthMask(false);
gpu::Batch batch;
// perform deferred lighting, rendering to free fbo
auto textureCache = DependencyManager::get<TextureCache>();
glBindFramebuffer(GL_FRAMEBUFFER, 0 );
QSize framebufferSize = textureCache->getFrameBufferSize();
// binding the first framebuffer
auto freeFBO = DependencyManager::get<GlowEffect>()->getFreeFramebuffer();
glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(freeFBO));
batch.setFramebuffer(freeFBO);
glClear(GL_COLOR_BUFFER_BIT);
// glEnable(GL_FRAMEBUFFER_SRGB);
batch.clearColorFramebuffer(freeFBO->getBufferMask(), glm::vec4(0.0f, 0.0f, 0.0f, 0.0f));
batch.setUniformTexture(0, textureCache->getPrimaryColorTexture());
// glBindTexture(GL_TEXTURE_2D, primaryFBO->texture());
glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryColorTextureID());
batch.setUniformTexture(1, textureCache->getPrimaryNormalTexture());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryNormalTextureID());
batch.setUniformTexture(2, textureCache->getPrimarySpecularTexture());
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, textureCache->getPrimarySpecularTextureID());
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryDepthTextureID());
batch.setUniformTexture(3, textureCache->getPrimaryDepthTexture());
// get the viewport side (left, right, both)
int viewport[4];
@ -266,53 +271,51 @@ void DeferredLightingEffect::render(RenderArgs* args) {
glm::mat4 invViewMat;
_viewState->getViewTransform().getMatrix(invViewMat);
ProgramObject* program = &_directionalLight;
auto& program = _directionalLight;
const LightLocations* locations = &_directionalLightLocations;
bool shadowsEnabled = _viewState->getShadowsEnabled();
if (shadowsEnabled) {
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, textureCache->getShadowDepthTextureID());
batch.setUniformTexture(4, textureCache->getShadowFramebuffer()->getDepthStencilBuffer());
program = &_directionalLightShadowMap;
program = _directionalLightShadowMap;
locations = &_directionalLightShadowMapLocations;
if (_viewState->getCascadeShadowsEnabled()) {
program = &_directionalLightCascadedShadowMap;
program = _directionalLightCascadedShadowMap;
locations = &_directionalLightCascadedShadowMapLocations;
if (useSkyboxCubemap) {
program = &_directionalSkyboxLightCascadedShadowMap;
program = _directionalSkyboxLightCascadedShadowMap;
locations = &_directionalSkyboxLightCascadedShadowMapLocations;
} else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLightCascadedShadowMap;
program = _directionalAmbientSphereLightCascadedShadowMap;
locations = &_directionalAmbientSphereLightCascadedShadowMapLocations;
}
program->bind();
program->setUniform(locations->shadowDistances, _viewState->getShadowDistances());
batch.setPipeline(program);
batch._glUniform3fv(locations->shadowDistances, 1, (const GLfloat*) &_viewState->getShadowDistances());
} else {
if (useSkyboxCubemap) {
program = &_directionalSkyboxLightShadowMap;
program = _directionalSkyboxLightShadowMap;
locations = &_directionalSkyboxLightShadowMapLocations;
} else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLightShadowMap;
program = _directionalAmbientSphereLightShadowMap;
locations = &_directionalAmbientSphereLightShadowMapLocations;
}
program->bind();
batch.setPipeline(program);
}
program->setUniformValue(locations->shadowScale,
1.0f / textureCache->getShadowFramebuffer()->getWidth());
batch._glUniform1f(locations->shadowScale, 1.0f / textureCache->getShadowFramebuffer()->getWidth());
} else {
if (useSkyboxCubemap) {
program = &_directionalSkyboxLight;
program = _directionalSkyboxLight;
locations = &_directionalSkyboxLightLocations;
} else if (_ambientLightMode > -1) {
program = &_directionalAmbientSphereLight;
program = _directionalAmbientSphereLight;
locations = &_directionalAmbientSphereLightLocations;
}
program->bind();
batch.setPipeline(program);
}
{
{ // Setup the global lighting
auto globalLight = _allocatedLights[_globalLights.front()];
if (locations->ambientSphere >= 0) {
@ -321,70 +324,74 @@ void DeferredLightingEffect::render(RenderArgs* args) {
sh = (*_skybox->getCubemap()->getIrradiance());
}
for (int i =0; i <gpu::SphericalHarmonics::NUM_COEFFICIENTS; i++) {
program->setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i));
batch._glUniform4fv(locations->ambientSphere + i, 1, (const GLfloat*) (&sh) + i * 4);
}
}
if (useSkyboxCubemap) {
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_CUBE_MAP, gpu::GLBackend::getTextureID(_skybox->getCubemap()));
batch.setUniformTexture(5, _skybox->getCubemap());
}
if (locations->lightBufferUnit >= 0) {
gpu::Batch batch;
batch.setUniformBuffer(locations->lightBufferUnit, globalLight->getSchemaBuffer());
gpu::GLBackend::renderBatch(batch);
}
if (_atmosphere && (locations->atmosphereBufferUnit >= 0)) {
gpu::Batch batch;
batch.setUniformBuffer(locations->atmosphereBufferUnit, _atmosphere->getDataBuffer());
gpu::GLBackend::renderBatch(batch);
}
glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
batch._glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
}
float left, right, bottom, top, nearVal, farVal;
glm::vec4 nearClipPlane, farClipPlane;
_viewState->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane);
program->setUniformValue(locations->nearLocation, nearVal);
batch._glUniform1f(locations->nearLocation, nearVal);
float depthScale = (farVal - nearVal) / farVal;
program->setUniformValue(locations->depthScale, depthScale);
batch._glUniform1f(locations->depthScale, depthScale);
float nearScale = -1.0f / nearVal;
float depthTexCoordScaleS = (right - left) * nearScale / sWidth;
float depthTexCoordScaleT = (top - bottom) * nearScale / tHeight;
float depthTexCoordOffsetS = left * nearScale - sMin * depthTexCoordScaleS;
float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT;
program->setUniformValue(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
program->setUniformValue(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
batch._glUniform2f(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
batch._glUniform2f(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight);
program->release();
{
Transform model;
model.setTranslation(glm::vec3(sMin, tMin, 0.0));
model.setScale(glm::vec3(sWidth, tHeight, 1.0));
batch.setModelTransform(model);
batch.setProjectionTransform(glm::mat4());
batch.setViewTransform(Transform());
glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec2 topLeft(-1.0f, -1.0f);
glm::vec2 bottomRight(1.0f, 1.0f);
glm::vec2 texCoordTopLeft(sMin, tMin);
glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight);
DependencyManager::get<GeometryCache>()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color);
}
if (useSkyboxCubemap) {
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
if (!shadowsEnabled) {
glActiveTexture(GL_TEXTURE3);
}
batch.setUniformTexture(5, nullptr);
}
if (shadowsEnabled) {
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE3);
batch.setUniformTexture(4, nullptr);
}
// additive blending
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_CULL_FACE);
glm::vec4 sCoefficients(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f);
glm::vec4 tCoefficients(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f);
glTexGenfv(GL_S, GL_OBJECT_PLANE, (const GLfloat*)&sCoefficients);
glTexGenfv(GL_T, GL_OBJECT_PLANE, (const GLfloat*)&tCoefficients);
auto texcoordMat = glm::mat4();
texcoordMat[0] = glm::vec4(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f);
texcoordMat[1] = glm::vec4(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f);
texcoordMat[2] = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
texcoordMat[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
// enlarge the scales slightly to account for tesselation
const float SCALE_EXPANSION = 0.05f;
@ -393,148 +400,162 @@ void DeferredLightingEffect::render(RenderArgs* args) {
auto geometryCache = DependencyManager::get<GeometryCache>();
glm::mat4 projMat;
Transform viewMat;
args->_viewFrustum->evalProjectionMatrix(projMat);
args->_viewFrustum->evalViewTransform(viewMat);
if (args->_renderMode == RenderArgs::MIRROR_RENDER_MODE) {
viewMat.postScale(glm::vec3(-1.0f, 1.0f, 1.0f));
}
batch.setProjectionTransform(projMat);
batch.setViewTransform(viewMat);
if (!_pointLights.empty()) {
_pointLight.bind();
_pointLight.setUniformValue(_pointLightLocations.nearLocation, nearVal);
_pointLight.setUniformValue(_pointLightLocations.depthScale, depthScale);
_pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
_pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
batch.setPipeline(_pointLight);
batch._glUniform1f(_pointLightLocations.nearLocation, nearVal);
batch._glUniform1f(_pointLightLocations.depthScale, depthScale);
batch._glUniform2f(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
batch._glUniform2f(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
batch._glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
batch._glUniformMatrix4fv(_pointLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat));
for (auto lightID : _pointLights) {
auto light = _allocatedLights[lightID];
auto& light = _allocatedLights[lightID];
// IN DEBUG: light->setShowContour(true);
if (_pointLightLocations.lightBufferUnit >= 0) {
gpu::Batch batch;
batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer());
gpu::GLBackend::renderBatch(batch);
}
glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
glPushMatrix();
float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION);
// TODO: We shouldn;t have to do that test and use a different volume geometry for when inside the vlight volume,
// we should be able to draw thre same geometry use DepthClamp but for unknown reason it's s not working...
if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) {
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -1.0f);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
renderFullscreenQuad();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Transform model;
model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f));
batch.setModelTransform(model);
batch.setViewTransform(Transform());
batch.setProjectionTransform(glm::mat4());
glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec2 topLeft(-1.0f, -1.0f);
glm::vec2 bottomRight(1.0f, 1.0f);
glm::vec2 texCoordTopLeft(sMin, tMin);
glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight);
DependencyManager::get<GeometryCache>()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color);
batch.setProjectionTransform(projMat);
batch.setViewTransform(viewMat);
} else {
glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z);
geometryCache->renderSphere(expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
Transform model;
model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z));
batch.setModelTransform(model);
geometryCache->renderSphere(batch, expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
}
glPopMatrix();
}
_pointLights.clear();
_pointLight.release();
}
if (!_spotLights.empty()) {
_spotLight.bind();
_spotLight.setUniformValue(_spotLightLocations.nearLocation, nearVal);
_spotLight.setUniformValue(_spotLightLocations.depthScale, depthScale);
_spotLight.setUniformValue(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
_spotLight.setUniformValue(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
batch.setPipeline(_spotLight);
batch._glUniform1f(_spotLightLocations.nearLocation, nearVal);
batch._glUniform1f(_spotLightLocations.depthScale, depthScale);
batch._glUniform2f(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT);
batch._glUniform2f(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT);
batch._glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
batch._glUniformMatrix4fv(_spotLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat));
for (auto lightID : _spotLights) {
auto light = _allocatedLights[lightID];
// IN DEBUG: light->setShowContour(true);
if (_spotLightLocations.lightBufferUnit >= 0) {
gpu::Batch batch;
batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer());
gpu::GLBackend::renderBatch(batch);
}
glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat));
glPushMatrix();
batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer());
auto eyeLightPos = eyePoint - light->getPosition();
auto eyeHalfPlaneDistance = glm::dot(eyeLightPos, light->getDirection());
const float TANGENT_LENGTH_SCALE = 0.666f;
glm::vec4 coneParam(light->getSpotAngleCosSin(), TANGENT_LENGTH_SCALE * tan(0.5 * light->getSpotAngle()), 1.0f);
float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION);
float edgeRadius = expandedRadius / glm::cos(light->getSpotAngle());
if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) {
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -1.0f);
// TODO: We shouldn;t have to do that test and use a different volume geometry for when inside the vlight volume,
// we should be able to draw thre same geometry use DepthClamp but for unknown reason it's s not working...
if ((eyeHalfPlaneDistance > -nearRadius) && (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius)) {
coneParam.w = 0.0f;
batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const GLfloat* >(&coneParam));
Transform model;
model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f));
batch.setModelTransform(model);
batch.setViewTransform(Transform());
batch.setProjectionTransform(glm::mat4());
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
renderFullscreenQuad();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec2 topLeft(-1.0f, -1.0f);
glm::vec2 bottomRight(1.0f, 1.0f);
glm::vec2 texCoordTopLeft(sMin, tMin);
glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight);
DependencyManager::get<GeometryCache>()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color);
batch.setProjectionTransform(projMat);
batch.setViewTransform(viewMat);
} else {
glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z);
glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection());
glm::vec3 axis = glm::axis(spotRotation);
glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z);
glTranslatef(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f));
geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotAngle()),
expandedRadius, 32, 1);
coneParam.w = 1.0f;
batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const GLfloat* >(&coneParam));
Transform model;
model.setTranslation(light->getPosition());
model.postRotate(light->getOrientation());
model.postScale(glm::vec3(expandedRadius, expandedRadius, expandedRadius));
batch.setModelTransform(model);
auto mesh = getSpotLightMesh();
batch.setIndexBuffer(mesh->getIndexBuffer());
batch.setInputBuffer(0, mesh->getVertexBuffer());
batch.setInputFormat(mesh->getVertexFormat());
auto& part = mesh->getPartBuffer().get<model::Mesh::Part>();
batch.drawIndexed(model::Mesh::topologyToPrimitive(part._topology), part._numIndices, part._startIndex);
}
glPopMatrix();
}
_spotLights.clear();
_spotLight.release();
}
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
// glDisable(GL_FRAMEBUFFER_SRGB);
// Probably not necessary in the long run because the gpu layer would unbound this texture if used as render target
batch.setUniformTexture(0, nullptr);
batch.setUniformTexture(1, nullptr);
batch.setUniformTexture(2, nullptr);
batch.setUniformTexture(3, nullptr);
args->_context->syncCache();
args->_context->render(batch);
// End of the Lighting pass
}
void DeferredLightingEffect::copyBack(RenderArgs* args) {
gpu::Batch batch;
auto textureCache = DependencyManager::get<TextureCache>();
QSize framebufferSize = textureCache->getFrameBufferSize();
auto freeFBO = DependencyManager::get<GlowEffect>()->getFreeFramebuffer();
//freeFBO->release();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
batch.setFramebuffer(textureCache->getPrimaryFramebuffer());
batch.setPipeline(_blitLightBuffer);
batch.setUniformTexture(0, freeFBO->getRenderBuffer(0));
glDisable(GL_CULL_FACE);
// now transfer the lit region to the primary fbo
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);
glColorMask(true, true, true, false);
auto primaryFBO = gpu::GLBackend::getFramebufferID(textureCache->getPrimaryFramebuffer());
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, primaryFBO);
batch.setProjectionTransform(glm::mat4());
batch.setViewTransform(Transform());
//primaryFBO->bind();
glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(freeFBO->getRenderBuffer(0)));
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
const int VIEWPORT_X_INDEX = 0;
@ -547,21 +568,19 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) {
float tMin = viewport[VIEWPORT_Y_INDEX] / (float)framebufferSize.height();
float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)framebufferSize.height();
renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glColorMask(true, true, true, true);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glDepthMask(true);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
Transform model;
model.setTranslation(glm::vec3(sMin, tMin, 0.0));
model.setScale(glm::vec3(sWidth, tHeight, 1.0));
batch.setModelTransform(model);
batch.setViewportTransform(glm::ivec4(viewport[0], viewport[1], viewport[2], viewport[3]));
batch.draw(gpu::TRIANGLE_STRIP, 4);
args->_context->syncCache();
args->_context->render(batch);
}
void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferUnit) {
@ -569,67 +588,62 @@ void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferU
args->_batch->setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer());
}
void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations) {
program.addShaderFromSourceCode(QGLShader::Vertex, (limited ? deferred_light_limited_vert : deferred_light_vert));
program.addShaderFromSourceCode(QGLShader::Fragment, fragSource);
program.link();
void DeferredLightingEffect::loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocations& locations) {
auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(vertSource)));
auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(fragSource)));
program.bind();
program.setUniformValue("diffuseMap", 0);
program.setUniformValue("normalMap", 1);
program.setUniformValue("specularMap", 2);
program.setUniformValue("depthMap", 3);
program.setUniformValue("shadowMap", 4);
program.setUniformValue("skyboxMap", 5);
locations.shadowDistances = program.uniformLocation("shadowDistances");
locations.shadowScale = program.uniformLocation("shadowScale");
locations.nearLocation = program.uniformLocation("near");
locations.depthScale = program.uniformLocation("depthScale");
locations.depthTexCoordOffset = program.uniformLocation("depthTexCoordOffset");
locations.depthTexCoordScale = program.uniformLocation("depthTexCoordScale");
locations.radius = program.uniformLocation("radius");
locations.ambientSphere = program.uniformLocation("ambientSphere.L00");
locations.invViewMat = program.uniformLocation("invViewMat");
gpu::ShaderPointer program = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PS));
GLint loc = -1;
#if (GPU_FEATURE_PROFILE == GPU_CORE)
gpu::Shader::BindingSet slotBindings;
slotBindings.insert(gpu::Shader::Binding(std::string("diffuseMap"), 0));
slotBindings.insert(gpu::Shader::Binding(std::string("normalMap"), 1));
slotBindings.insert(gpu::Shader::Binding(std::string("specularMap"), 2));
slotBindings.insert(gpu::Shader::Binding(std::string("depthMap"), 3));
slotBindings.insert(gpu::Shader::Binding(std::string("shadowMap"), 4));
slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), 5));
const GLint LIGHT_GPU_SLOT = 3;
loc = glGetUniformBlockIndex(program.programId(), "lightBuffer");
if (loc >= 0) {
glUniformBlockBinding(program.programId(), loc, LIGHT_GPU_SLOT);
locations.lightBufferUnit = LIGHT_GPU_SLOT;
} else {
locations.lightBufferUnit = -1;
}
#else
loc = program.uniformLocation("lightBuffer");
if (loc >= 0) {
locations.lightBufferUnit = loc;
} else {
locations.lightBufferUnit = -1;
}
#endif
slotBindings.insert(gpu::Shader::Binding(std::string("lightBuffer"), LIGHT_GPU_SLOT));
const GLint ATMOSPHERE_GPU_SLOT = 4;
slotBindings.insert(gpu::Shader::Binding(std::string("atmosphereBufferUnit"), ATMOSPHERE_GPU_SLOT));
gpu::Shader::makeProgram(*program, slotBindings);
locations.shadowDistances = program->getUniforms().findLocation("shadowDistances");
locations.shadowScale = program->getUniforms().findLocation("shadowScale");
locations.nearLocation = program->getUniforms().findLocation("near");
locations.depthScale = program->getUniforms().findLocation("depthScale");
locations.depthTexCoordOffset = program->getUniforms().findLocation("depthTexCoordOffset");
locations.depthTexCoordScale = program->getUniforms().findLocation("depthTexCoordScale");
locations.radius = program->getUniforms().findLocation("radius");
locations.ambientSphere = program->getUniforms().findLocation("ambientSphere.L00");
locations.invViewMat = program->getUniforms().findLocation("invViewMat");
locations.texcoordMat = program->getUniforms().findLocation("texcoordMat");
locations.coneParam = program->getUniforms().findLocation("coneParam");
#if (GPU_FEATURE_PROFILE == GPU_CORE)
const GLint ATMOSPHERE_GPU_SLOT = 4;
loc = glGetUniformBlockIndex(program.programId(), "atmosphereBufferUnit");
if (loc >= 0) {
glUniformBlockBinding(program.programId(), loc, ATMOSPHERE_GPU_SLOT);
locations.atmosphereBufferUnit = ATMOSPHERE_GPU_SLOT;
} else {
locations.atmosphereBufferUnit = -1;
}
locations.lightBufferUnit = program->getBuffers().findLocation("lightBuffer");
locations.atmosphereBufferUnit = program->getBuffers().findLocation("atmosphereBufferUnit");
#else
loc = program.uniformLocation("atmosphereBufferUnit");
if (loc >= 0) {
locations.atmosphereBufferUnit = loc;
} else {
locations.atmosphereBufferUnit = -1;
}
locations.lightBufferUnit = program->getUniforms().findLocation("lightBuffer");
locations.atmosphereBufferUnit = program->getUniforms().findLocation("atmosphereBufferUnit");
#endif
program.release();
gpu::StatePointer state = gpu::StatePointer(new gpu::State());
if (lightVolume) {
state->setCullMode(gpu::State::CULL_BACK);
// No need for z test since the depth buffer is not bound state->setDepthTest(true, false, gpu::LESS_EQUAL);
// TODO: We should bind the true depth buffer both as RT and texture for the depth test
// TODO: We should use DepthClamp and avoid changing geometry for inside /outside cases
state->setDepthClampEnable(true);
// additive blending
state->setBlendFunction(true, gpu::State::ONE, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
} else {
state->setCullMode(gpu::State::CULL_BACK);
}
pipeline.reset(gpu::Pipeline::create(program, state));
}
void DeferredLightingEffect::setAmbientLightMode(int preset) {
@ -654,3 +668,100 @@ void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const gl
void DeferredLightingEffect::setGlobalSkybox(const model::SkyboxPointer& skybox) {
_skybox = skybox;
}
model::MeshPointer DeferredLightingEffect::getSpotLightMesh() {
if (!_spotLightMesh) {
_spotLightMesh.reset(new model::Mesh());
int slices = 32;
int rings = 3;
int vertices = 2 + rings * slices;
int originVertex = vertices - 2;
int capVertex = vertices - 1;
int verticesSize = vertices * 3 * sizeof(float);
int indices = 3 * slices * (1 + 1 + 2 * (rings -1));
int ringFloatOffset = slices * 3;
GLfloat* vertexData = new GLfloat[verticesSize];
GLfloat* vertexRing0 = vertexData;
GLfloat* vertexRing1 = vertexRing0 + ringFloatOffset;
GLfloat* vertexRing2 = vertexRing1 + ringFloatOffset;
for (int i = 0; i < slices; i++) {
float theta = TWO_PI * i / slices;
auto cosin = glm::vec2(cosf(theta), sinf(theta));
*(vertexRing0++) = cosin.x;
*(vertexRing0++) = cosin.y;
*(vertexRing0++) = 0.0f;
*(vertexRing1++) = cosin.x;
*(vertexRing1++) = cosin.y;
*(vertexRing1++) = 0.33f;
*(vertexRing2++) = cosin.x;
*(vertexRing2++) = cosin.y;
*(vertexRing2++) = 0.66f;
}
*(vertexRing2++) = 0.0f;
*(vertexRing2++) = 0.0f;
*(vertexRing2++) = -1.0f;
*(vertexRing2++) = 0.0f;
*(vertexRing2++) = 0.0f;
*(vertexRing2++) = 1.0f;
_spotLightMesh->setVertexBuffer(gpu::BufferView(new gpu::Buffer(verticesSize, (gpu::Byte*) vertexData), gpu::Element::VEC3F_XYZ));
delete[] vertexData;
gpu::uint16* indexData = new gpu::uint16[indices];
gpu::uint16* index = indexData;
for (int i = 0; i < slices; i++) {
*(index++) = originVertex;
int s0 = i;
int s1 = ((i + 1) % slices);
*(index++) = s0;
*(index++) = s1;
int s2 = s0 + slices;
int s3 = s1 + slices;
*(index++) = s1;
*(index++) = s0;
*(index++) = s2;
*(index++) = s1;
*(index++) = s2;
*(index++) = s3;
int s4 = s2 + slices;
int s5 = s3 + slices;
*(index++) = s3;
*(index++) = s2;
*(index++) = s4;
*(index++) = s3;
*(index++) = s4;
*(index++) = s5;
*(index++) = s5;
*(index++) = s4;
*(index++) = capVertex;
}
_spotLightMesh->setIndexBuffer(gpu::BufferView(new gpu::Buffer(sizeof(GLushort) * indices, (gpu::Byte*) indexData), gpu::Element::INDEX_UINT16));
delete[] indexData;
model::Mesh::Part part(0, indices, 0, model::Mesh::TRIANGLES);
//DEBUG: model::Mesh::Part part(0, indices, 0, model::Mesh::LINE_STRIP);
_spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(sizeof(part), (gpu::Byte*) &part), gpu::Element::PART_DRAWCALL));
_spotLightMesh->makeBufferStream();
}
return _spotLightMesh;
}

View file

@ -21,6 +21,7 @@
#include "model/Light.h"
#include "model/Stage.h"
#include "model/Geometry.h"
class AbstractViewStateInterface;
class RenderArgs;
@ -94,41 +95,49 @@ private:
int lightBufferUnit;
int atmosphereBufferUnit;
int invViewMat;
int texcoordMat;
int coneParam;
};
static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations);
model::MeshPointer _spotLightMesh;
model::MeshPointer getSpotLightMesh();
static void loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& program, LightLocations& locations);
gpu::PipelinePointer getPipeline(SimpleProgramKey config);
gpu::ShaderPointer _simpleShader;
gpu::ShaderPointer _emissiveShader;
QHash<SimpleProgramKey, gpu::PipelinePointer> _simplePrograms;
ProgramObject _directionalSkyboxLight;
gpu::PipelinePointer _blitLightBuffer;
gpu::PipelinePointer _directionalSkyboxLight;
LightLocations _directionalSkyboxLightLocations;
ProgramObject _directionalSkyboxLightShadowMap;
gpu::PipelinePointer _directionalSkyboxLightShadowMap;
LightLocations _directionalSkyboxLightShadowMapLocations;
ProgramObject _directionalSkyboxLightCascadedShadowMap;
gpu::PipelinePointer _directionalSkyboxLightCascadedShadowMap;
LightLocations _directionalSkyboxLightCascadedShadowMapLocations;
ProgramObject _directionalAmbientSphereLight;
gpu::PipelinePointer _directionalAmbientSphereLight;
LightLocations _directionalAmbientSphereLightLocations;
ProgramObject _directionalAmbientSphereLightShadowMap;
gpu::PipelinePointer _directionalAmbientSphereLightShadowMap;
LightLocations _directionalAmbientSphereLightShadowMapLocations;
ProgramObject _directionalAmbientSphereLightCascadedShadowMap;
gpu::PipelinePointer _directionalAmbientSphereLightCascadedShadowMap;
LightLocations _directionalAmbientSphereLightCascadedShadowMapLocations;
ProgramObject _directionalLight;
gpu::PipelinePointer _directionalLight;
LightLocations _directionalLightLocations;
ProgramObject _directionalLightShadowMap;
gpu::PipelinePointer _directionalLightShadowMap;
LightLocations _directionalLightShadowMapLocations;
ProgramObject _directionalLightCascadedShadowMap;
gpu::PipelinePointer _directionalLightCascadedShadowMap;
LightLocations _directionalLightCascadedShadowMapLocations;
ProgramObject _pointLight;
gpu::PipelinePointer _pointLight;
LightLocations _pointLightLocations;
ProgramObject _spotLight;
gpu::PipelinePointer _spotLight;
LightLocations _spotLightLocations;
class PointLight {
public:
glm::vec4 position;

View file

@ -401,7 +401,6 @@ void GeometryCache::renderCone(float base, float height, int slices, int stacks)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void GeometryCache::renderGrid(int xDivisions, int yDivisions, const glm::vec4& color) {
gpu::Batch batch;
renderGrid(batch, xDivisions, yDivisions, color);

View file

@ -301,6 +301,7 @@ private:
};
QHash<IntPair, VerticesIndices> _coneVBOs;
int _nextID;
QHash<int, Vec3PairVec4Pair> _lastRegisteredQuad3DTexture;

View file

@ -417,6 +417,7 @@ void Model::reset() {
}
bool Model::updateGeometry() {
PROFILE_RANGE(__FUNCTION__);
bool needFullUpdate = false;
bool needToRebuild = false;
@ -690,6 +691,7 @@ void Model::recalculateMeshPartOffsets() {
// entity-scripts to call. I think it would be best to do the picking once-per-frame (in cpu, or gpu if possible)
// and then the calls use the most recent such result.
void Model::recalculateMeshBoxes(bool pickAgainstTriangles) {
PROFILE_RANGE(__FUNCTION__);
bool calculatedMeshTrianglesNeeded = pickAgainstTriangles && !_calculatedMeshTrianglesValid;
if (!_calculatedMeshBoxesValid || calculatedMeshTrianglesNeeded || (!_calculatedMeshPartBoxesValid && pickAgainstTriangles) ) {
@ -1281,6 +1283,7 @@ Blender::Blender(Model* model, int blendNumber, const QWeakPointer<NetworkGeomet
}
void Blender::run() {
PROFILE_RANGE(__FUNCTION__);
QVector<glm::vec3> vertices, normals;
if (!_model.isNull()) {
int offset = 0;
@ -1392,6 +1395,7 @@ void Model::snapToRegistrationPoint() {
}
void Model::simulate(float deltaTime, bool fullUpdate) {
PROFILE_RANGE(__FUNCTION__);
fullUpdate = updateGeometry() || fullUpdate || (_scaleToFit && !_scaledToFit)
|| (_snapModelToRegistrationPoint && !_snappedToRegistrationPoint);
@ -1829,6 +1833,7 @@ void Model::setupBatchTransform(gpu::Batch& batch, RenderArgs* args) {
}
AABox Model::getPartBounds(int meshIndex, int partIndex) {
if (meshIndex < _meshStates.size()) {
const MeshState& state = _meshStates.at(meshIndex);
bool isSkinned = state.clusterMatrices.size() > 1;
@ -1859,6 +1864,7 @@ AABox Model::getPartBounds(int meshIndex, int partIndex) {
}
void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool translucent) {
// PROFILE_RANGE(__FUNCTION__);
PerformanceTimer perfTimer("Model::renderPart");
if (!_readyWhenAdded) {
return; // bail asap
@ -1933,7 +1939,9 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran
pickPrograms(batch, mode, translucentMesh, alphaThreshold, hasLightmap, hasTangents, hasSpecular, isSkinned, wireframe,
args, locations);
updateVisibleJointStates();
{
updateVisibleJointStates();
}
// if our index is ever out of range for either meshes or networkMeshes, then skip it, and set our _meshGroupsKnown
// to false to rebuild out mesh groups.
@ -2076,9 +2084,13 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran
}
}
_mutex.lock();
qint64 offset = _calculatedMeshPartOffset[QPair<int,int>(meshIndex, partIndex)];
_mutex.unlock();
qint64 offset;
{
// FIXME_STUTTER: We should n't have any lock here
_mutex.lock();
offset = _calculatedMeshPartOffset[QPair<int,int>(meshIndex, partIndex)];
_mutex.unlock();
}
if (part.quadIndices.size() > 0) {
batch.drawIndexed(gpu::QUADS, part.quadIndices.size(), offset);

View file

@ -40,8 +40,9 @@ void ResolveDeferred::run(const SceneContextPointer& sceneContext, const RenderC
}
RenderDeferredTask::RenderDeferredTask() : Task() {
_jobs.push_back(Job(new PrepareDeferred::JobModel("PrepareDeferred")));
_jobs.push_back(Job(new DrawBackground::JobModel("DrawBackground")));
_jobs.push_back(Job(new PrepareDeferred::JobModel("PrepareDeferred")));
_jobs.push_back(Job(new FetchItems::JobModel("FetchOpaque",
FetchItems(
[] (const RenderContextPointer& context, int count) {
@ -118,7 +119,7 @@ void DrawOpaqueDeferred::run(const SceneContextPointer& sceneContext, const Rend
args->_viewFrustum->evalProjectionMatrix(projMat);
args->_viewFrustum->evalViewTransform(viewMat);
if (args->_renderMode == RenderArgs::MIRROR_RENDER_MODE) {
viewMat.postScale(glm::vec3(-1.0f, 1.0f, 1.0f));
viewMat.preScale(glm::vec3(-1.0f, 1.0f, 1.0f));
}
batch.setProjectionTransform(projMat);
batch.setViewTransform(viewMat);

View file

@ -343,7 +343,7 @@ void Font3D::setupGPU() {
gpu::StatePointer state = gpu::StatePointer(new gpu::State());
state->setCullMode(gpu::State::CULL_BACK);
state->setDepthTest(true, true, gpu::LESS_EQUAL);
state->setBlendFunction(false,
state->setBlendFunction(true,
gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA,
gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
_pipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, state));

View file

@ -12,9 +12,19 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
uniform mat4 texcoordMat;
void main(void) {
gl_Position = ftransform();
// standard transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
<$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$>;
vec4 projected = gl_Position / gl_Position.w;
gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w,
dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w);
gl_TexCoord[0] = vec4(dot(projected, texcoordMat[0]) * gl_Position.w,
dot(projected, texcoordMat[1]) * gl_Position.w, 0.0, gl_Position.w);
}

View file

@ -0,0 +1,47 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// deferred_light_spot.vert
// vertex shader
//
// Created by Sam Gateau on 7/8/15.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
uniform mat4 texcoordMat;
uniform vec4 coneParam;
void main(void) {
vec4 coneVertex = gl_Vertex;
if (coneParam.w != 0.0) {
if(coneVertex.z >= 0.0) {
// Evaluate the true position of the spot volume
vec2 dir = float(coneVertex.z < 0.5f) * (coneParam.xy
+ vec2(coneParam.y, -coneParam.x) * coneParam.z * float(coneVertex.z > 0.0f))
+ float(coneVertex.z > 0.5f) * (vec2(1.0, 0.0)
+ vec2(0.0, coneParam.z) * float(coneVertex.z < 1.0f));
coneVertex.xy *= dir.y;
coneVertex.z = -dir.x;
} else {
coneVertex.z = 0.0;
}
}
// standard transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
<$transformModelToClipPos(cam, obj, coneVertex, gl_Position)$>;
vec4 projected = gl_Position / gl_Position.w;
gl_TexCoord[0] = vec4(dot(projected, texcoordMat[0]) * gl_Position.w,
dot(projected, texcoordMat[1]) * gl_Position.w, 0.0, gl_Position.w);
}

View file

@ -18,7 +18,7 @@ uniform vec4 Color;
varying vec4 interpolatedNormal;
const float gamma = 2.2;
const float smoothing = 64.0;
const float smoothing = 256.0;
const float interiorCutoff = 0.8;
const float outlineExpansion = 0.2;

View file

@ -28,7 +28,7 @@ void main(void) {
// Grab the fragment data from the uv
vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q;
DeferredFragment frag = unpackDeferredFragment(texCoord);
// Kill if in front of the light volume
float depth = frag.depthVal;
if (depth < gl_FragCoord.z) {

View file

@ -9,4 +9,16 @@ add_dependency_external_projects(glm)
find_package(GLM REQUIRED)
target_include_directories(${TARGET_NAME} PUBLIC ${GLM_INCLUDE_DIRS})
link_hifi_libraries(shared gpu model)
link_hifi_libraries(shared gpu model)
if (WIN32)
if (USE_NSIGHT)
# try to find the Nsight package and add it to the build if we find it
find_package(NSIGHT)
if (NSIGHT_FOUND)
include_directories(${NSIGHT_INCLUDE_DIRS})
add_definitions(-DNSIGHT_FOUND)
target_link_libraries(${TARGET_NAME} "${NSIGHT_LIBRARIES}")
endif ()
endif()
endif (WIN32)

View file

@ -11,6 +11,7 @@
#include "Scene.h"
#include <numeric>
#include "gpu/Batch.h"
using namespace render;
@ -167,6 +168,7 @@ void consolidateChangeQueue(PendingChangesQueue& queue, PendingChanges& singleBa
}
void Scene::processPendingChangesQueue() {
PROFILE_RANGE(__FUNCTION__);
_changeQueueMutex.lock();
PendingChanges consolidatedPendingChanges;
consolidateChangeQueue(_changeQueue, consolidatedPendingChanges);

View file

@ -22,10 +22,10 @@ uniform vec3 inBoundPos;
uniform vec3 inBoundDim;
uniform ivec4 inStatus;
vec3 paintRainbow(float nv) {
float v = nv * 5.f;
vec3 paintRainbow(float normalizedHue) {
float v = normalizedHue * 6.f;
if (v < 0.f) {
return vec3(0.f, 0.f, 0.f);
return vec3(1.f, 0.f, 0.f);
} else if (v < 1.f) {
return vec3(1.f, v, 0.f);
} else if (v < 2.f) {
@ -36,8 +36,10 @@ vec3 paintRainbow(float nv) {
return vec3(0.f, 1.f - (v-3.f), 1.f );
} else if (v < 5.f) {
return vec3((v-4.f), 0.f, 1.f );
} else if (v < 6.f) {
return vec3(1.f, 0.f, 1.f - (v-5.f));
} else {
return vec3(1.f, 1.f, 1.f);
return vec3(1.f, 0.f, 0.f);
}
}

View file

@ -493,7 +493,7 @@ void ScriptEngine::evaluate() {
QScriptValue result = evaluate(_scriptContents);
// TODO: why do we check this twice? It seems like the call to clearExcpetions() in the lower level evaluate call
// TODO: why do we check this twice? It seems like the call to clearExceptions() in the lower level evaluate call
// will cause this code to never actually run...
if (hasUncaughtException()) {
int line = uncaughtExceptionLineNumber();
@ -710,7 +710,13 @@ void ScriptEngine::run() {
// since we're in non-threaded mode, call process so that the packets are sent
if (!entityScriptingInterface->getEntityPacketSender()->isThreaded()) {
entityScriptingInterface->getEntityPacketSender()->process();
// wait here till the edit packet sender is completely done sending
while (entityScriptingInterface->getEntityPacketSender()->hasPacketsToSend()) {
entityScriptingInterface->getEntityPacketSender()->process();
QCoreApplication::processEvents();
}
} else {
// FIXME - do we need to have a similar "wait here" loop for non-threaded packet senders?
}
}

View file

@ -0,0 +1,122 @@
//
// Created by Bradley Austin Davis on 2015/07/08
// Copyright 2013-2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once
#ifndef hifi_BufferParser_h
#define hifi_BufferParser_h
#include <cstdint>
#include <QUuid>
#include <QtEndian>
#include "GLMHelpers.h"
#include "ByteCountCoding.h"
#include "PropertyFlags.h"
class BufferParser {
public:
BufferParser(const uint8_t* data, size_t size, size_t offset = 0) :
_offset(offset), _data(data), _size(size) {
}
template<typename T>
inline void readValue(T& result) {
Q_ASSERT(remaining() >= sizeof(T));
memcpy(&result, _data + _offset, sizeof(T));
_offset += sizeof(T);
}
inline void readUuid(QUuid& result) {
readValue(result.data1);
readValue(result.data2);
readValue(result.data3);
readValue(result.data4);
result.data1 = qFromBigEndian<quint32>(result.data1);
result.data2 = qFromBigEndian<quint16>(result.data2);
result.data3 = qFromBigEndian<quint16>(result.data3);
}
template <typename T>
inline void readFlags(PropertyFlags<T>& result) {
// FIXME doing heap allocation
QByteArray encoded((const char*)(_data + _offset), remaining());
result.decode(encoded);
_offset += result.getEncodedLength();
}
template<typename T>
inline void readCompressedCount(T& result) {
// FIXME switch to a heapless implementation as soon as Brad provides it.
ByteCountCoded<T> codec;
_offset += codec.decode(reinterpret_cast<const char*>(_data + _offset), remaining());
result = codec.data;
}
inline size_t remaining() const {
return _size - _offset;
}
inline size_t offset() const {
return _offset;
}
inline const uint8_t* data() const {
return _data;
}
private:
size_t _offset{ 0 };
const uint8_t* const _data;
const size_t _size;
};
template<>
inline void BufferParser::readValue<quat>(quat& result) {
size_t advance = unpackOrientationQuatFromBytes(_data + _offset, result);
_offset += advance;
}
template<>
inline void BufferParser::readValue(QString& result) {
uint16_t length; readValue(length);
result = QString((const char*)_data + _offset);
}
template<>
inline void BufferParser::readValue(QUuid& result) {
uint16_t length; readValue(length);
Q_ASSERT(16 == length);
readUuid(result);
}
template<>
inline void BufferParser::readValue(xColor& result) {
readValue(result.red);
readValue(result.blue);
readValue(result.green);
}
template<>
inline void BufferParser::readValue(QVector<glm::vec3>& result) {
uint16_t length; readValue(length);
result.resize(length);
memcpy(result.data(), _data + _offset, sizeof(glm::vec3) * length);
_offset += sizeof(glm::vec3) * length;
}
template<>
inline void BufferParser::readValue(QByteArray& result) {
uint16_t length; readValue(length);
result = QByteArray((char*)_data + _offset, (int)length);
_offset += length;
}
#endif

View file

@ -21,9 +21,13 @@
#include <climits>
#include <limits>
#include <QDebug>
#include <QBitArray>
#include <QByteArray>
#include "SharedUtil.h"
#include "NumericalConstants.h"
template<typename T> class ByteCountCoded {
@ -37,7 +41,8 @@ public:
ByteCountCoded(const QByteArray& fromEncoded) : data(0) { decode(fromEncoded); }
QByteArray encode() const;
void decode(const QByteArray& fromEncoded);
size_t decode(const QByteArray& fromEncoded);
size_t decode(const char* encodedBuffer, int encodedSize);
bool operator==(const ByteCountCoded& other) const { return data == other.data; }
bool operator!=(const ByteCountCoded& other) const { return data != other.data; }
@ -110,52 +115,63 @@ template<typename T> inline QByteArray ByteCountCoded<T>::encode() const {
return output;
}
template<typename T> inline void ByteCountCoded<T>::decode(const QByteArray& fromEncodedBytes) {
template<typename T> inline size_t ByteCountCoded<T>::decode(const QByteArray& fromEncodedBytes) {
return decode(fromEncodedBytes.constData(), fromEncodedBytes.size());
}
// first convert the ByteArray into a BitArray...
QBitArray encodedBits;
int bitCount = BITS_IN_BYTE * fromEncodedBytes.count();
encodedBits.resize(bitCount);
template<typename T> inline size_t ByteCountCoded<T>::decode(const char* encodedBuffer, int encodedSize) {
data = 0; // reset data
size_t bytesConsumed = 0;
int bitCount = BITS_IN_BYTE * encodedSize;
int encodedByteCount = 1; // there is at least 1 byte (after the leadBits)
int leadBits = 1; // there is always at least 1 lead bit
bool inLeadBits = true;
int bitAt = 0;
int expectedBitCount; // unknown at this point
int lastValueBit;
T bitValue = 1;
for(int byte = 0; byte < fromEncodedBytes.count(); byte++) {
char originalByte = fromEncodedBytes.at(byte);
for(int byte = 0; byte < encodedSize; byte++) {
char originalByte = encodedBuffer[byte];
bytesConsumed++;
unsigned char maskBit = 128; // LEFT MOST BIT set
for(int bit = 0; bit < BITS_IN_BYTE; bit++) {
int shiftBy = BITS_IN_BYTE - (bit + 1);
char maskBit = ( 1 << shiftBy);
bool bitValue = originalByte & maskBit;
encodedBits.setBit(byte * BITS_IN_BYTE + bit, bitValue);
bool bitIsSet = originalByte & maskBit;
// Processing of the lead bits
if (inLeadBits) {
if (bitIsSet) {
encodedByteCount++;
leadBits++;
} else {
inLeadBits = false; // once we hit our first 0, we know we're out of the lead bits
expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits;
lastValueBit = expectedBitCount + bitAt;
// check to see if the remainder of our buffer is sufficient
if (expectedBitCount > (bitCount - leadBits)) {
break;
}
}
} else {
if (bitAt > lastValueBit) {
break;
}
if(bitIsSet) {
data += bitValue;
}
bitValue *= 2;
}
bitAt++;
maskBit = maskBit >> 1;
}
}
// next, read the leading bits to determine the correct number of bytes to decode (may not match the QByteArray)
int encodedByteCount = 0;
int leadBits = 1;
int bitAt;
for (bitAt = 0; bitAt < bitCount; bitAt++) {
if (encodedBits.at(bitAt)) {
encodedByteCount++;
leadBits++;
} else {
if (!inLeadBits && bitAt > lastValueBit) {
break;
}
}
encodedByteCount++; // always at least one byte
int expectedBitCount = encodedByteCount * BITS_IN_BYTE;
T value = 0;
if (expectedBitCount <= (encodedBits.size() - leadBits)) {
// Now, keep reading...
int valueStartsAt = bitAt + 1;
T bitValue = 1;
for (bitAt = valueStartsAt; bitAt < expectedBitCount; bitAt++) {
if(encodedBits.at(bitAt)) {
value += bitValue;
}
bitValue *= 2;
}
}
data = value;
return bytesConsumed;
}
#endif // hifi_ByteCountCoding_h

View file

@ -0,0 +1,72 @@
//
// Created by Bradley Austin Davis 2015/07/08.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once
#ifndef hifi_GenericQueueThread_h
#define hifi_GenericQueueThread_h
#include <stdint.h>
#include <QQueue>
#include <QMutex>
#include <QWaitCondition>
#include "GenericThread.h"
#include "NumericalConstants.h"
template <typename T>
class GenericQueueThread : public GenericThread {
public:
using Queue = QQueue<T>;
GenericQueueThread(QObject* parent = nullptr)
: GenericThread(parent) {}
virtual ~GenericQueueThread() {}
void queueItem(const T& t) {
lock();
queueItemInternal(t);
unlock();
_hasItems.wakeAll();
}
protected:
virtual void queueItemInternal(const T& t) {
_items.push_back(t);
}
virtual uint32_t getMaxWait() {
return MSECS_PER_SECOND;
}
virtual bool process() {
if (!_items.size()) {
_hasItemsMutex.lock();
_hasItems.wait(&_hasItemsMutex, getMaxWait());
_hasItemsMutex.unlock();
}
if (!_items.size()) {
return isStillRunning();
}
Queue processItems;
lock();
processItems.swap(_items);
unlock();
return processQueueItems(processItems);
}
virtual bool processQueueItems(const Queue& items) = 0;
Queue _items;
QWaitCondition _hasItems;
QMutex _hasItemsMutex;
};
#endif // hifi_GenericQueueThread_h

View file

@ -14,7 +14,8 @@
#include "GenericThread.h"
GenericThread::GenericThread() :
GenericThread::GenericThread(QObject* parent) :
QObject(parent),
_stopThread(false),
_isThreaded(false) // assume non-threaded, must call initialize()
{
@ -27,13 +28,14 @@ GenericThread::~GenericThread() {
}
}
void GenericThread::initialize(bool isThreaded) {
void GenericThread::initialize(bool isThreaded, QThread::Priority priority) {
_isThreaded = isThreaded;
if (_isThreaded) {
_thread = new QThread(this);
// match the thread name to our object name
_thread->setObjectName(objectName());
_thread->setPriority(priority);
// when the worker thread is started, call our engine's run..
connect(_thread, SIGNAL(started()), this, SLOT(threadRoutine()));

View file

@ -23,12 +23,12 @@
class GenericThread : public QObject {
Q_OBJECT
public:
GenericThread();
GenericThread(QObject* parent = nullptr);
virtual ~GenericThread();
/// Call to start the thread.
/// \param bool isThreaded true by default. false for non-threaded mode and caller must call threadRoutine() regularly.
void initialize(bool isThreaded = true);
void initialize(bool isThreaded = true, QThread::Priority priority = QThread::NormalPriority);
/// Call to stop the thread
void terminate();

View file

@ -20,7 +20,7 @@ QVariantList glmToQList(const glm::quat& g) {
return QVariantList() << g.x << g.y << g.z << g.w;
}
QVariantList rgbColorToQList(rgbColor& v) {
QVariantList rgbColorToQList(const rgbColor& v) {
return QVariantList() << (int)(v[0]) << (int)(v[1]) << (int)(v[2]);
}
@ -42,12 +42,12 @@ QVariantMap glmToQMap(const glm::quat& glmQuat) {
}
glm::vec3 qListToGlmVec3(const QVariant q) {
glm::vec3 qListToGlmVec3(const QVariant& q) {
QVariantList qList = q.toList();
return glm::vec3(qList[RED_INDEX].toFloat(), qList[GREEN_INDEX].toFloat(), qList[BLUE_INDEX].toFloat());
}
glm::quat qListToGlmQuat(const QVariant q) {
glm::quat qListToGlmQuat(const QVariant& q) {
QVariantList qList = q.toList();
float x = qList[0].toFloat();
float y = qList[1].toFloat();
@ -56,7 +56,7 @@ glm::quat qListToGlmQuat(const QVariant q) {
return glm::quat(w, x, y, z);
}
void qListtoRgbColor(const QVariant q, rgbColor returnValue) {
void qListtoRgbColor(const QVariant& q, rgbColor& returnValue) {
QVariantList qList = q.toList();
returnValue[RED_INDEX] = qList[RED_INDEX].toInt();
returnValue[GREEN_INDEX] = qList[GREEN_INDEX].toInt();

View file

@ -19,11 +19,11 @@
QVariantList glmToQList(const glm::vec3& g);
QVariantList glmToQList(const glm::quat& g);
QVariantList rgbColorToQList(rgbColor& v);
QVariantList rgbColorToQList(const rgbColor& v);
QVariantMap glmToQMap(const glm::vec3& glmVector);
QVariantMap glmToQMap(const glm::quat& glmQuat);
glm::vec3 qListToGlmVec3(const QVariant q);
glm::quat qListToGlmQuat(const QVariant q);
void qListtoRgbColor(const QVariant q, rgbColor returnValue);
glm::vec3 qListToGlmVec3(const QVariant& q);
glm::quat qListToGlmQuat(const QVariant& q);
void qListtoRgbColor(const QVariant& q, rgbColor& returnValue);

View file

@ -97,6 +97,8 @@ public:
const Vec3& getScale() const;
void setScale(float scale);
void setScale(const Vec3& scale); // [new this] = [this.translation] * [this.rotation] * [scale]
void preScale(float scale);
void preScale(const Vec3& scale);
void postScale(float scale); // [new this] = [this] * [scale] equivalent to glScale
void postScale(const Vec3& scale); // [new this] = [this] * [scale] equivalent to glScale
@ -322,6 +324,14 @@ inline void Transform::setScale(const Vec3& scale) {
}
}
inline void Transform::preScale(float scale) {
setScale(getScale() * scale);
}
inline void Transform::preScale(const Vec3& scale) {
setScale(getScale() * scale);
}
inline void Transform::postScale(float scale) {
if (!isValidScale(scale) || scale == 1.0f) {
return;