ToyVolume: Fix bugs, clean up and add example asset

This commit is contained in:
Emil Axelsson
2018-05-04 18:04:47 +02:00
parent 20ff81017c
commit 97b2311cb3
7 changed files with 76 additions and 37 deletions

View File

@@ -0,0 +1,22 @@
-- This asset requires OpenSpace to be built with the OPENSPACE_MODULE_TOYVOLUME enabled
local assetHelper = asset.require("util/asset_helper")
local transforms = asset.require("scene/solarsystem/sun/transforms")
local ToyVolume = {
Identifier = "RenderableToyVolume",
Parent = transforms.SolarSystemBarycenter.Identifier,
Renderable = {
Type = "RenderableToyVolume",
Size = {5, 5, 5},
ScalingExponent = 11,
StepSize = 0.01,
Color = {1, 0, 0, 1}
},
GUI = {
Path = "/Examples"
}
}
local objects = { ToyVolume }
assetHelper.registerSceneGraphNodesAndExport(asset, objects)

View File

@@ -34,9 +34,9 @@
#include <ghoul/opengl/ghoul_gl.h>
namespace {
static const openspace::properties::Property::PropertyInfo ScalingInfo = {
"Scaling",
"Scaling",
static const openspace::properties::Property::PropertyInfo SizeInfo = {
"Size",
"Size",
"" // @TODO Missing documentation
};
@@ -75,7 +75,7 @@ namespace openspace {
RenderableToyVolume::RenderableToyVolume(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _scaling(ScalingInfo, glm::vec3(1.f, 1.f, 1.f), glm::vec3(0.f), glm::vec3(10.f))
, _size(SizeInfo, glm::vec3(1.f, 1.f, 1.f), glm::vec3(0.f), glm::vec3(10.f))
, _scalingExponent(ScalingExponentInfo, 1, -10, 20)
, _stepSize(StepSizeInfo, 0.02f, 0.01f, 1.f )
, _translation(TranslationInfo, glm::vec3(0.f), glm::vec3(0.f), glm::vec3(10.f))
@@ -83,24 +83,24 @@ RenderableToyVolume::RenderableToyVolume(const ghoul::Dictionary& dictionary)
, _color(ColorInfo, glm::vec4(1.f, 0.f, 0.f, 0.1f), glm::vec4(0.f), glm::vec4(1.f))
{
float stepSize, scalingExponent;
glm::vec3 scaling, translation, rotation;
glm::vec3 size, translation, rotation;
glm::vec4 color;
if (dictionary.getValue("ScalingExponent", scalingExponent)) {
if (dictionary.getValue(ScalingExponentInfo.identifier, scalingExponent)) {
_scalingExponent = static_cast<int>(scalingExponent);
}
if (dictionary.getValue("Scaling", scaling)) {
_scaling = scaling;
if (dictionary.getValue(SizeInfo.identifier, size)) {
_size = size;
}
if (dictionary.getValue("Translation", translation)) {
if (dictionary.getValue(TranslationInfo.identifier, translation)) {
_translation = translation;
}
if (dictionary.getValue("Rotation", rotation)) {
if (dictionary.getValue(RotationInfo.identifier, rotation)) {
_rotation = rotation;
}
if (dictionary.getValue("Color", color)) {
if (dictionary.getValue(ColorInfo.identifier, color)) {
_color = color;
}
if (dictionary.getValue("StepSize", stepSize)) {
if (dictionary.getValue(StepSizeInfo.identifier, stepSize)) {
_stepSize = stepSize;
}
}
@@ -124,7 +124,7 @@ void RenderableToyVolume::initializeGL() {
onEnabledChange(onChange);
addProperty(_scaling);
addProperty(_size);
addProperty(_scalingExponent);
addProperty(_stepSize);
addProperty(_translation);
@@ -158,7 +158,7 @@ void RenderableToyVolume::update(const UpdateData& data) {
transform = glm::scale(
transform,
static_cast<glm::vec3>(_scaling) *
static_cast<glm::vec3>(_size) *
std::pow(10.0f, static_cast<float>(_scalingExponent))
);

View File

@@ -51,7 +51,7 @@ public:
void update(const UpdateData& data) override;
private:
properties::Vec3Property _scaling;
properties::Vec3Property _size;
properties::IntProperty _scalingExponent;
properties::FloatProperty _stepSize;
properties::Vec3Property _translation;

View File

@@ -57,9 +57,8 @@ void ToyVolumeRaycaster::deinitialize() {
void ToyVolumeRaycaster::renderEntryPoints(const RenderData& data,
ghoul::opengl::ProgramObject& program)
{
program.setUniform("modelTransform", _modelTransform);
program.setUniform("modelViewTransform", glm::mat4(modelViewTransform(data)));
program.setUniform("viewProjection", data.camera.viewProjectionMatrix());
Renderable::setPscUniforms(program, data.camera, data.position);
// Cull back face
glEnable(GL_CULL_FACE);
@@ -73,9 +72,8 @@ void ToyVolumeRaycaster::renderExitPoints(const RenderData& data,
ghoul::opengl::ProgramObject& program)
{
// Uniforms
program.setUniform("modelTransform", _modelTransform);
program.setUniform("modelViewTransform", glm::mat4(modelViewTransform(data)));
program.setUniform("viewProjection", data.camera.viewProjectionMatrix());
Renderable::setPscUniforms(program, data.camera, data.position);
// Cull front face
glEnable(GL_CULL_FACE);
@@ -88,6 +86,16 @@ void ToyVolumeRaycaster::renderExitPoints(const RenderData& data,
glCullFace(GL_BACK);
}
glm::dmat4 ToyVolumeRaycaster::modelViewTransform(const RenderData& data) {
glm::dmat4 modelTransform =
glm::translate(glm::dmat4(1.0), data.modelTransform.translation) *
glm::dmat4(data.modelTransform.rotation) *
glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale)) *
glm::dmat4(_modelTransform);
return data.camera.combinedViewMatrix() * modelTransform;
}
void ToyVolumeRaycaster::preRaycast(const RaycastData& data,
ghoul::opengl::ProgramObject& program)
{
@@ -103,6 +111,19 @@ void ToyVolumeRaycaster::postRaycast(const RaycastData&, ghoul::opengl::ProgramO
// For example: release texture units
}
bool ToyVolumeRaycaster::cameraIsInside(const RenderData& data,
glm::vec3& localPosition)
{
glm::vec4 modelPos =
glm::inverse(modelViewTransform(data)) * glm::vec4(0.0, 0.0, 0.0, 1.0);
localPosition = (glm::vec3(modelPos) + glm::vec3(0.5));
return (localPosition.x > 0 && localPosition.x < 1 &&
localPosition.y > 0 && localPosition.y < 1 &&
localPosition.z > 0 && localPosition.z < 1);
}
std::string ToyVolumeRaycaster::getBoundsVsPath() const {
return GlslBoundsVsPath;
}

View File

@@ -58,6 +58,7 @@ public:
ghoul::opengl::ProgramObject& program) override;
void postRaycast(const RaycastData& data,
ghoul::opengl::ProgramObject& program) override;
bool cameraIsInside(const RenderData& data, glm::vec3& localPosition) override;
std::string getBoundsVsPath() const override;
std::string getBoundsFsPath() const override;
@@ -69,6 +70,8 @@ public:
void setTime(double time);
void setStepSize(float time);
private:
glm::dmat4 modelViewTransform(const RenderData& data);
BoxGeometry _boundingBox;
glm::vec4 _color;
glm::mat4 _modelTransform;

View File

@@ -22,18 +22,15 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include "PowerScaling/powerScaling_fs.hglsl"
#include "floatoperations.glsl"
#include "fragment.glsl"
in vec3 vPosition;
in vec4 worldPosition;
in vec3 modelPosition;
in vec4 viewPosition;
Fragment getFragment() {
vec4 position = worldPosition;
Fragment frag;
frag.color = vec4(vPosition + 0.5, 1.0);
frag.depth = pscDepth(position);
frag.color = vec4(modelPosition + 0.5, 1.0);
frag.depth = safeLength(viewPosition);
return frag;
}

View File

@@ -24,24 +24,20 @@
#version __CONTEXT__
#include "PowerScaling/powerScaling_vs.hglsl"
layout(location = 0) in vec4 vertPosition;
out vec3 vPosition;
out vec4 worldPosition;
out vec3 modelPosition;
out vec4 viewPosition;
uniform mat4 viewProjection;
uniform mat4 modelTransform;
uniform mat4 modelViewTransform;
void main() {
vPosition = vertPosition.xyz;
worldPosition = modelTransform*vertPosition;
vec4 position = pscTransform(worldPosition, mat4(1.0));
modelPosition = vertPosition.xyz;
viewPosition = modelViewTransform*vertPosition;
// project the position to view space
gl_Position = viewProjection * position;
gl_Position = viewProjection * viewPosition;
gl_Position.z = 1.0;
}