diff --git a/examples/grab.js b/examples/grab.js index 552a194100..b81c70c541 100644 --- a/examples/grab.js +++ b/examples/grab.js @@ -13,6 +13,7 @@ var isGrabbing = false; var grabbedEntity = null; +var lineEntityID = null; var prevMouse = {}; var deltaMouse = { z: 0 @@ -86,6 +87,17 @@ function mousePressEvent(event) { gravity: {x: 0, y: 0, z: 0} }); + + lineEntityID = Entities.addEntity({ + type: "Line", + position: MyAvatar.position, + // dimensions: {x:5, y:5, z:5}, + dimensions: Vec3.subtract(targetPosition, MyAvatar.position), + color: { red: 0, green: 255, blue: 0 } + // lifetime: 10 + }); + + Audio.playSound(grabSound, { position: props.position, volume: 0.4 @@ -133,6 +145,9 @@ function mouseReleaseEvent() { visible: false }); targetPosition = null; + + Entities.deleteEntity(lineEntityID); + Audio.playSound(grabSound, { position: entityProps.position, volume: 0.25 diff --git a/libraries/entities-renderer/src/RenderableLineEntityItem.cpp b/libraries/entities-renderer/src/RenderableLineEntityItem.cpp new file mode 100644 index 0000000000..6f8aed930c --- /dev/null +++ b/libraries/entities-renderer/src/RenderableLineEntityItem.cpp @@ -0,0 +1,56 @@ +// +// RenderableLineEntityItem.cpp +// libraries/entities-renderer/src/ +// +// Created by Seth Alves on 5/11/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 + +#include + +#include +#include + +#include "RenderableLineEntityItem.h" + +EntityItem* RenderableLineEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) { + return new RenderableLineEntityItem(entityID, properties); +} + +void RenderableLineEntityItem::render(RenderArgs* args) { + PerformanceTimer perfTimer("RenderableLineEntityItem::render"); + assert(getType() == EntityTypes::Line); + glm::vec3 position = getPosition(); + // glm::vec3 center = getCenter(); + glm::vec3 dimensions = getDimensions(); + glm::quat rotation = getRotation(); + + const float MAX_COLOR = 255.0f; + + glm::vec4 lineColor(getColor()[RED_INDEX] / MAX_COLOR, getColor()[GREEN_INDEX] / MAX_COLOR, + getColor()[BLUE_INDEX] / MAX_COLOR, getLocalRenderAlpha()); + + glPushMatrix(); + glTranslatef(position.x, position.y, position.z); + glm::vec3 axis = glm::axis(rotation); + glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); + // glPushMatrix(); + // glm::vec3 positionToCenter = center - position; + // glTranslatef(positionToCenter.x, positionToCenter.y, positionToCenter.z); + // glScalef(dimensions.x, dimensions.y, dimensions.z); + + glm::vec3 p1 = {0.0f, 0.0f, 0.0f}; + glm::vec3& p2 = dimensions; + + DependencyManager::get()->renderLine(p1, p2, lineColor, lineColor); + + // glPopMatrix(); + glPopMatrix(); + + RenderableDebugableEntityItem::render(this, args); +}; diff --git a/libraries/entities-renderer/src/RenderableLineEntityItem.h b/libraries/entities-renderer/src/RenderableLineEntityItem.h new file mode 100644 index 0000000000..0de7cd43ae --- /dev/null +++ b/libraries/entities-renderer/src/RenderableLineEntityItem.h @@ -0,0 +1,29 @@ +// +// RenderableLineEntityItem.h +// libraries/entities-renderer/src/ +// +// Created by Seth Alves on 5/11/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 +// + +#ifndef hifi_RenderableLineEntityItem_h +#define hifi_RenderableLineEntityItem_h + +#include +#include "RenderableDebugableEntityItem.h" + +class RenderableLineEntityItem : public LineEntityItem { +public: + static EntityItem* factory(const EntityItemID& entityID, const EntityItemProperties& properties); + + RenderableLineEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) : + LineEntityItem(entityItemID, properties) { } + + virtual void render(RenderArgs* args); +}; + + +#endif // hifi_RenderableLineEntityItem_h diff --git a/libraries/entities/src/LineEntityItem.cpp b/libraries/entities/src/LineEntityItem.cpp new file mode 100644 index 0000000000..61c9542834 --- /dev/null +++ b/libraries/entities/src/LineEntityItem.cpp @@ -0,0 +1,108 @@ +// +// LineEntityItem.cpp +// libraries/entities/src +// +// Created by Seth Alves on 5/11/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 + +#include + +#include "LineEntityItem.h" +#include "EntityTree.h" +#include "EntitiesLogging.h" +#include "EntityTreeElement.h" + + +EntityItem* LineEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) { + EntityItem* result = new LineEntityItem(entityID, properties); + return result; +} + +LineEntityItem::LineEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) : + EntityItem(entityItemID) +{ + _type = EntityTypes::Line; + _created = properties.getCreated(); + setProperties(properties); +} + +EntityItemProperties LineEntityItem::getProperties() const { + EntityItemProperties properties = EntityItem::getProperties(); // get the properties from our base class + + properties._color = getXColor(); + properties._colorChanged = false; + + properties._glowLevel = getGlowLevel(); + properties._glowLevelChanged = false; + + return properties; +} + +bool LineEntityItem::setProperties(const EntityItemProperties& properties) { + bool somethingChanged = false; + somethingChanged = EntityItem::setProperties(properties); // set the properties in our base class + + SET_ENTITY_PROPERTY_FROM_PROPERTIES(color, setColor); + + if (somethingChanged) { + bool wantDebug = false; + if (wantDebug) { + uint64_t now = usecTimestampNow(); + int elapsed = now - getLastEdited(); + qCDebug(entities) << "LineEntityItem::setProperties() AFTER update... edited AGO=" << elapsed << + "now=" << now << " getLastEdited()=" << getLastEdited(); + } + setLastEdited(properties._lastEdited); + } + return somethingChanged; +} + +int LineEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead, + ReadBitstreamToTreeParams& args, + EntityPropertyFlags& propertyFlags, bool overwriteLocalData) { + + int bytesRead = 0; + const unsigned char* dataAt = data; + + READ_ENTITY_PROPERTY_COLOR(PROP_COLOR, _color); + + return bytesRead; +} + + +// TODO: eventually only include properties changed since the params.lastViewFrustumSent time +EntityPropertyFlags LineEntityItem::getEntityProperties(EncodeBitstreamParams& params) const { + EntityPropertyFlags requestedProperties = EntityItem::getEntityProperties(params); + requestedProperties += PROP_COLOR; + return requestedProperties; +} + +void LineEntityItem::appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params, + EntityTreeElementExtraEncodeData* modelTreeElementExtraEncodeData, + EntityPropertyFlags& requestedProperties, + EntityPropertyFlags& propertyFlags, + EntityPropertyFlags& propertiesDidntFit, + int& propertyCount, + OctreeElement::AppendState& appendState) const { + + bool successPropertyFits = true; + + APPEND_ENTITY_PROPERTY(PROP_COLOR, appendColor, getColor()); +} + +void LineEntityItem::debugDump() const { + quint64 now = usecTimestampNow(); + qCDebug(entities) << " LINE EntityItem id:" << getEntityItemID() << "---------------------------------------------"; + qCDebug(entities) << " color:" << _color[0] << "," << _color[1] << "," << _color[2]; + qCDebug(entities) << " position:" << debugTreeVector(_position); + qCDebug(entities) << " dimensions:" << debugTreeVector(_dimensions); + qCDebug(entities) << " getLastEdited:" << debugTime(getLastEdited(), now); +} + diff --git a/libraries/entities/src/LineEntityItem.h b/libraries/entities/src/LineEntityItem.h new file mode 100644 index 0000000000..a834fee816 --- /dev/null +++ b/libraries/entities/src/LineEntityItem.h @@ -0,0 +1,62 @@ +// +// LineEntityItem.h +// libraries/entities/src +// +// Created by Seth Alves on 5/11/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 +// + +#ifndef hifi_LineEntityItem_h +#define hifi_LineEntityItem_h + +#include "EntityItem.h" + +class LineEntityItem : public EntityItem { + public: + static EntityItem* factory(const EntityItemID& entityID, const EntityItemProperties& properties); + + LineEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties); + + ALLOW_INSTANTIATION // This class can be instantiated + + // methods for getting/setting all properties of an entity + virtual EntityItemProperties getProperties() const; + virtual bool setProperties(const EntityItemProperties& properties); + + // TODO: eventually only include properties changed since the params.lastViewFrustumSent time + virtual EntityPropertyFlags getEntityProperties(EncodeBitstreamParams& params) const; + + virtual void appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params, + EntityTreeElementExtraEncodeData* modelTreeElementExtraEncodeData, + EntityPropertyFlags& requestedProperties, + EntityPropertyFlags& propertyFlags, + EntityPropertyFlags& propertiesDidntFit, + int& propertyCount, + OctreeElement::AppendState& appendState) const; + + virtual int readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead, + ReadBitstreamToTreeParams& args, + EntityPropertyFlags& propertyFlags, bool overwriteLocalData); + + const rgbColor& getColor() const { return _color; } + xColor getXColor() const { xColor color = { _color[RED_INDEX], _color[GREEN_INDEX], _color[BLUE_INDEX] }; return color; } + + void setColor(const rgbColor& value) { memcpy(_color, value, sizeof(_color)); } + void setColor(const xColor& value) { + _color[RED_INDEX] = value.red; + _color[GREEN_INDEX] = value.green; + _color[BLUE_INDEX] = value.blue; + } + + virtual ShapeType getShapeType() const { return SHAPE_TYPE_LINE; } + + virtual void debugDump() const; + + protected: + rgbColor _color; +}; + +#endif // hifi_LineEntityItem_h diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index ed24f089d6..4dd31ae92c 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -129,6 +129,20 @@ void DeferredLightingEffect::renderWireCube(float size, const glm::vec4& color) releaseSimpleProgram(); } +void DeferredLightingEffect::renderLine(const glm::vec3& p1, const glm::vec3& p2, + const glm::vec4& color1, const glm::vec4& color2) { + + qDebug() << "LINE -- " + << p1[0] << p1[1] << p1[2] + << "," + << p2[0] << p2[1] << p2[2]; + + bindSimpleProgram(); + DependencyManager::get()->renderLine(p1, p2, color1, color2); + releaseSimpleProgram(); +} + + void DeferredLightingEffect::renderSolidCone(float base, float height, int slices, int stacks) { bindSimpleProgram(); DependencyManager::get()->renderCone(base, height, slices, stacks); @@ -288,21 +302,21 @@ void DeferredLightingEffect::render() { program->setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); } } - + if (useSkyboxCubemap) { glActiveTexture(GL_TEXTURE5); glBindTexture(GL_TEXTURE_CUBE_MAP, gpu::GLBackend::getTextureID(_skybox->getCubemap())); - } - - if (locations->lightBufferUnit >= 0) { - gpu::Batch batch; - batch.setUniformBuffer(locations->lightBufferUnit, globalLight->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + } + + 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::Batch batch; + batch.setUniformBuffer(locations->atmosphereBufferUnit, _atmosphere->getDataBuffer()); gpu::GLBackend::renderBatch(batch); } glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); @@ -366,11 +380,11 @@ void DeferredLightingEffect::render() { for (auto lightID : _pointLights) { auto light = _allocatedLights[lightID]; - - if (_pointLightLocations.lightBufferUnit >= 0) { - gpu::Batch batch; - batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + + 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)); @@ -412,10 +426,10 @@ void DeferredLightingEffect::render() { for (auto lightID : _spotLights) { auto light = _allocatedLights[lightID]; - if (_spotLightLocations.lightBufferUnit >= 0) { - gpu::Batch batch; - batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + 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)); @@ -535,42 +549,42 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit locations.ambientSphere = program.uniformLocation("ambientSphere.L00"); locations.invViewMat = program.uniformLocation("invViewMat"); - GLint loc = -1; - -#if (GPU_FEATURE_PROFILE == GPU_CORE) - 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; - } + GLint loc = -1; + +#if (GPU_FEATURE_PROFILE == GPU_CORE) + 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 -#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; - } -#else - loc = program.uniformLocation("atmosphereBufferUnit"); - if (loc >= 0) { - locations.atmosphereBufferUnit = loc; - } else { - locations.atmosphereBufferUnit = -1; - } +#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; + } +#else + loc = program.uniformLocation("atmosphereBufferUnit"); + if (loc >= 0) { + locations.atmosphereBufferUnit = loc; + } else { + locations.atmosphereBufferUnit = -1; + } #endif program.release(); @@ -597,4 +611,4 @@ void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const gl void DeferredLightingEffect::setGlobalSkybox(const model::SkyboxPointer& skybox) { _skybox = skybox; -} \ No newline at end of file +} diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 66cc6f59e9..fa37d48221 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -54,6 +54,10 @@ public: //// Renders a wireframe cube with the simple program. void renderWireCube(float size, const glm::vec4& color); + //// Renders a line with the simple program. + void renderLine(const glm::vec3& p1, const glm::vec3& p2, + const glm::vec4& color1, const glm::vec4& color2); + //// Renders a solid cone with the simple program. void renderSolidCone(float base, float height, int slices, int stacks);