Merge local branch

This commit is contained in:
Alexander Bock
2014-11-29 14:05:42 +01:00
5 changed files with 165 additions and 105 deletions

View File

@@ -37,7 +37,6 @@ namespace openspace {
class RenderableStars : public Renderable {
public:
RenderableStars(const ghoul::Dictionary& dictionary);
~RenderableStars();
bool initialize() override;
bool deinitialize() override;
@@ -50,7 +49,8 @@ public:
private:
enum ColorOption {
Color = 0,
Velocity = 1
Velocity = 1,
Speed = 2
};
void createDataSlice(ColorOption option);
@@ -60,28 +60,26 @@ private:
bool loadCachedFile(const std::string& file);
bool saveCachedFile(const std::string& file) const;
void generateBufferObjects(const void* data);
properties::StringProperty _colorTexturePath;
ghoul::opengl::Texture* _texture;
bool _textureIsDirty;
properties::OptionProperty _colorOption;
bool _dataIsDirty;
ghoul::opengl::ProgramObject* _haloProgram;
properties::FloatProperty _spriteSize;
ghoul::opengl::ProgramObject* _program;
bool _programIsDirty;
ghoul::opengl::Texture* _texture;
std::string _speckPath;
std::string _speckFile;
std::vector<float> _slicedData;
std::vector<float> _fullData;
int _nValuesPerStar;
GLuint _vboID;
GLuint _vaoID;
GLuint _vao;
GLuint _vbo;
};
} // namespace openspace

View File

@@ -27,6 +27,7 @@
// keep in sync with renderablestars.h:ColorOption enum
const int COLOROPTION_COLOR = 0;
const int COLOROPTION_VELOCITY = 1;
const int COLOROPTION_SPEED = 2;
uniform sampler2D texture1;
uniform vec3 Color;
@@ -36,7 +37,8 @@ uniform int colorOption;
layout(location = 0) in vec4 vs_position;
layout(location = 1) in vec3 ge_brightness;
layout(location = 2) in vec3 ge_velocity;
layout(location = 3) in vec2 texCoord;
layout(location = 3) in float ge_speed;
layout(location = 4) in vec2 texCoord;
@@ -80,7 +82,10 @@ void main(void)
break;
case COLOROPTION_VELOCITY:
color = vec4(abs(ge_velocity), 0.5);
//color = vec4(1.0, 0.0, 0.0, 1.0);
break;
case COLOROPTION_SPEED:
// @TODO Include a transfer function here ---abock
color = vec4(vec3(ge_speed), 0.5);
break;
}

View File

@@ -40,25 +40,28 @@ layout(triangle_strip, max_vertices = 4) out;
layout(location = 0) in vec4 psc_position[];
layout(location = 1) in vec3 vs_brightness[];
layout(location = 2) in vec3 vs_velocity[];
layout(location = 3) in vec4 cam_position[];
layout(location = 3) in float vs_speed[];
layout(location = 4) in vec4 cam_position[];
layout(location = 0) out vec4 vs_position;
layout(location = 1) out vec3 ge_brightness;
layout(location = 2) out vec3 ge_velocity;
layout(location = 3) out vec2 texCoord;
layout(location = 3) out float ge_speed;
layout(location = 4) out vec2 texCoord;
uniform mat4 projection; // we do this after distance computation.
float spriteSize = 0.0000005f; // set here for now.
uniform float spriteSize;
void main(){
ge_brightness = vs_brightness[0]; // pass on to fragment shader.
void main() {
ge_brightness = vs_brightness[0];
ge_velocity = vs_velocity[0];
ge_speed = vs_speed[0];
/// --- distance modulus --- NOT OPTIMIZED YET.
// float M = vs_brightness[0][0]; // get ABSOLUTE magnitude (x param)
float M = vs_brightness[0][2]; // if NOT running test-target.
float M = vs_brightness[0].z; // if NOT running test-target.
vec4 cam = vec4(-cam_position[0].xyz, cam_position[0].w); // get negative camera position
vec4 pos = psc_position[0]; // get OK star position
@@ -68,9 +71,10 @@ void main(){
y = result[1];
z = result[2];
w = result[3];
// I dont trust the legnth function at this point
vec2 pc = vec2(sqrt(x*x +y*y + z*z), result[3]); // form vec2
vec2 pc = vec2(length(result.xyz), result[3]);
// @Check conversion is also done in the cpp file ---abock
pc[0] *= 0.324077929f; // convert meters -> parsecs
pc[1] += -18.0f;
@@ -82,16 +86,16 @@ void main(){
float weight = 0.00001f; // otherwise this takes over.
float depth = pc[0] * pow(10, pc[1]);
depth *= pow(apparent,3);
//if(round(apparent) > 10)
spriteSize += (depth*weight);
float modifiedSpriteSize = spriteSize + (depth*weight);
// EMIT QUAD
for(int i = 0; i < 4; i++){
vec4 p1 = P;
p1.xy += spriteSize *(corners[i] - vec2(0.5));
p1.xy += modifiedSpriteSize *(corners[i] - vec2(0.5));
vs_position = p1;
gl_Position = projection * p1;
// gl_Position = z_normalization(projection * p1); // projection here
// gl_Position = z_normalization(projection * p1);
texCoord = corners[i];
EmitVertex();
}

View File

@@ -24,9 +24,6 @@
#version __CONTEXT__
uniform mat4 ViewProjection;
uniform mat4 ModelTransform;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
@@ -34,11 +31,13 @@ uniform mat4 projection;
layout(location = 0) in vec4 in_position;
layout(location = 1) in vec3 in_brightness;
layout(location = 2) in vec3 in_velocity;
layout(location = 3) in float in_speed;
layout(location = 0) out vec4 psc_position;
layout(location = 1) out vec3 vs_brightness;
layout(location = 2) out vec3 vs_velocity;
layout(location = 3) out vec4 cam_position;
layout(location = 3) out float vs_speed;
layout(location = 4) out vec4 cam_position;
#include "PowerScaling/powerScaling_vs.hglsl"
@@ -46,12 +45,14 @@ void main() {
psc_position = in_position;
vs_brightness = in_brightness;
vs_velocity = in_velocity;
vs_speed = in_speed;
cam_position = campos;
vec4 tmp = in_position;
vec4 position = pscTransform(tmp, ModelTransform);
vec4 position = pscTransform(tmp, mat4(1.0));
// psc_position = tmp;
position = view * model * position;
position = model * view * position;
// position = ViewProjection * ModelTransform * position;
// gl_Position = z_normalization(position);
gl_Position = position;
}

View File

@@ -59,6 +59,16 @@ namespace {
float vy; // v_y
float vz; // v_z
};
struct SpeedVBOLayout {
std::array<float, 4> position; // (x,y,z,e)
float bvColor; // B-V color value
float luminance;
float absoluteMagnitude;
float speed;
};
}
namespace openspace {
@@ -66,14 +76,17 @@ namespace openspace {
RenderableStars::RenderableStars(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _colorTexturePath("colorTexture", "Color Texture")
, _colorOption("colorOption", "Color Option")
, _haloProgram(nullptr)
, _programIsDirty(false)
, _texture(nullptr)
, _textureIsDirty(true)
, _colorOption("colorOption", "Color Option")
, _dataIsDirty(true)
, _spriteSize("spriteSize", "Sprite Size", 0.0000005f, 0.f, 1.f)
, _program(nullptr)
, _programIsDirty(false)
, _speckFile("")
, _nValuesPerStar(0)
, _vaoID(0)
, _vboID(0)
, _vao(0)
, _vbo(0)
{
std::string texturePath = "";
if (dictionary.hasKey(constants::renderablestars::keyTexture)) {
@@ -81,42 +94,40 @@ RenderableStars::RenderableStars(const ghoul::Dictionary& dictionary)
_colorTexturePath = absPath(texturePath);
}
bool success = dictionary.getValue(constants::renderablestars::keyFile, _speckPath);
bool success = dictionary.getValue(constants::renderablestars::keyFile, _speckFile);
if (!success) {
LERROR("SpeckDataSource did not contain key '" <<
constants::renderablestars::keyFile << "'");
return;
}
_speckPath = absPath(_speckPath);
_speckFile = absPath(_speckFile);
_colorOption.addOption({ColorOption::Color, "Color"});
_colorOption.addOption({ColorOption::Velocity, "Velocity"});
_colorOption.addOption({ColorOption::Speed, "Speed"});
addProperty(_colorOption);
_colorOption.onChange([&]{ _dataIsDirty = true;});
addProperty(_spriteSize);
addProperty(_colorTexturePath);
_colorTexturePath.onChange([&]{ _textureIsDirty = true;});
}
RenderableStars::~RenderableStars() {
}
bool RenderableStars::isReady() const {
return (_haloProgram != nullptr) && (_fullData.size() > 0);
return (_program != nullptr) && (_fullData.size() > 0);
}
bool RenderableStars::initialize() {
bool completeSuccess = true;
// Star program
_haloProgram = ghoul::opengl::ProgramObject::Build("Star",
"${SHADERS}/modules/stars/star_vs.glsl",
"${SHADERS}/modules/stars/star_fs.glsl",
"${SHADERS}/modules/stars/star_ge.glsl",
_program = ghoul::opengl::ProgramObject::Build("Star",
"${SHADERS}/star_vs.glsl",
"${SHADERS}/star_fs.glsl",
"${SHADERS}/star_ge.glsl",
[&](ghoul::opengl::ProgramObject*){ _programIsDirty = true;});
completeSuccess = (_haloProgram != nullptr);
completeSuccess = (_program != nullptr);
completeSuccess &= loadData();
completeSuccess &= (_texture != nullptr);
@@ -124,68 +135,60 @@ bool RenderableStars::initialize() {
}
bool RenderableStars::deinitialize() {
glDeleteBuffers(1, &_vboID);
_vboID = 0;
glDeleteVertexArrays(1, &_vaoID);
_vaoID = 0;
glDeleteBuffers(1, &_vbo);
_vbo = 0;
glDeleteVertexArrays(1, &_vao);
_vao = 0;
delete _texture;
_texture = nullptr;
delete _program;
_program = nullptr;
return true;
}
void RenderableStars::render(const RenderData& data) {
if(!_haloProgram)
return;
if(!_texture)
return;
assert(_haloProgram);
_haloProgram->activate();
_program->activate();
// @Check overwriting the scaling from the camera; error as parsec->meter conversion
// is done twice? ---abock
glm::vec2 scaling = glm::vec2(1, -19);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE);
glm::mat4 modelMatrix = data.camera.modelMatrix();
glm::mat4 viewMatrix = data.camera.viewMatrix();
glm::mat4 projectionMatrix = data.camera.projectionMatrix();
_haloProgram->setUniform("model", modelMatrix);
_haloProgram->setUniform("view", viewMatrix);
_haloProgram->setUniform("projection", projectionMatrix);
_program->setUniform("model", modelMatrix);
_program->setUniform("view", viewMatrix);
_program->setUniform("projection", projectionMatrix);
_haloProgram->setUniform("colorOption", _colorOption.value());
_program->setUniform("colorOption", _colorOption);
//_haloProgram->setUniform("ViewProjection", camera->viewProjectionMatrix());
_haloProgram->setUniform("ModelTransform", glm::mat4(1));
setPscUniforms(_haloProgram, &data.camera, data.position);
_haloProgram->setUniform("scaling", scaling);
setPscUniforms(_program, &data.camera, data.position);
_program->setUniform("scaling", scaling);
_program->setUniform("spriteSize", _spriteSize);
ghoul::opengl::TextureUnit unit;
unit.activate();
_texture->bind();
_haloProgram->setIgnoreUniformLocationError(true);
_haloProgram->setUniform("texture1", unit);
_haloProgram->setIgnoreUniformLocationError(false);
_program->setIgnoreUniformLocationError(true);
_program->setUniform("texture1", unit);
_program->setIgnoreUniformLocationError(false);
glBindVertexArray(_vaoID);
glBindVertexArray(_vao);
const GLsizei nStars = static_cast<GLsizei>(_fullData.size() / _nValuesPerStar);
glDrawArrays(GL_POINTS, 0, nStars);
glBindVertexArray(0);
_haloProgram->deactivate();
glDisable(GL_BLEND);
_program->deactivate();
}
void RenderableStars::update(const UpdateData& data) {
if (_programIsDirty) {
_haloProgram->rebuildFromFile();
_program->rebuildFromFile();
_dataIsDirty = true;
_programIsDirty = false;
}
if (_dataIsDirty) {
@@ -196,16 +199,23 @@ void RenderableStars::update(const UpdateData& data) {
int size = static_cast<int>(_slicedData.size());
if (_vaoID == 0)
glGenVertexArrays(1, &_vaoID);
if (_vboID == 0)
glGenBuffers(1, &_vboID);
glBindVertexArray(_vaoID);
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, size*sizeof(GLfloat), &_slicedData[0], GL_STATIC_DRAW);
if (_vao == 0) {
glGenVertexArrays(1, &_vao);
LDEBUG("Generating Vertex Array id '" << _vao << "'");
}
if (_vbo == 0) {
glGenBuffers(1, &_vbo);
LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'");
}
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER,
size*sizeof(GLfloat),
&_slicedData[0],
GL_STATIC_DRAW);
GLint positionAttrib = _haloProgram->attributeLocation("in_position");
GLint brightnessDataAttrib = _haloProgram->attributeLocation("in_brightness");
GLint positionAttrib = _program->attributeLocation("in_position");
GLint brightnessDataAttrib = _program->attributeLocation("in_brightness");
const size_t nStars = _fullData.size() / _nValuesPerStar;
const size_t nValues = _slicedData.size() / nStars;
@@ -217,20 +227,38 @@ void RenderableStars::update(const UpdateData& data) {
const int colorOption = _colorOption;
switch (colorOption) {
case ColorOption::Color:
glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<void*>(offsetof(ColorVBOLayout, position)));
glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<void*>(offsetof(ColorVBOLayout, bvColor)));
glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride,
reinterpret_cast<void*>(offsetof(ColorVBOLayout, position)));
glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride,
reinterpret_cast<void*>(offsetof(ColorVBOLayout, bvColor)));
break;
case ColorOption::Velocity:
{
glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<void*>(offsetof(VelocityVBOLayout, position)));
glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<void*>(offsetof(VelocityVBOLayout, bvColor)));
glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride,
reinterpret_cast<void*>(offsetof(VelocityVBOLayout, position)));
glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride,
reinterpret_cast<void*>(offsetof(VelocityVBOLayout, bvColor)));
GLint velocityAttrib = _program->attributeLocation("in_velocity");
glEnableVertexAttribArray(velocityAttrib);
glVertexAttribPointer(velocityAttrib, 3, GL_FLOAT, GL_TRUE, stride,
reinterpret_cast<void*>(offsetof(VelocityVBOLayout, vx)));
break;
}
case ColorOption::Speed:
{
glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride,
reinterpret_cast<void*>(offsetof(SpeedVBOLayout, position)));
glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride,
reinterpret_cast<void*>(offsetof(SpeedVBOLayout, bvColor)));
GLint speedAttrib = _program->attributeLocation("in_speed");
glEnableVertexAttribArray(speedAttrib);
glVertexAttribPointer(speedAttrib, 1, GL_FLOAT, GL_TRUE, stride,
reinterpret_cast<void*>(offsetof(SpeedVBOLayout, speed)));
GLint velocityAttrib = _haloProgram->attributeLocation("in_velocity");
glEnableVertexAttribArray(velocityAttrib);
glVertexAttribPointer(velocityAttrib, 3, GL_FLOAT, GL_TRUE, stride, reinterpret_cast<void*>(offsetof(VelocityVBOLayout, vx)));
break;
}
}
@@ -241,7 +269,7 @@ void RenderableStars::update(const UpdateData& data) {
}
if (_textureIsDirty) {
LDEBUG("Reloading texture due to changed texture");
LDEBUG("Reloading texture");
delete _texture;
_texture = nullptr;
if (_colorTexturePath.value() != "") {
@@ -256,7 +284,7 @@ void RenderableStars::update(const UpdateData& data) {
}
bool RenderableStars::loadData() {
std::string _file = _speckPath;
std::string _file = _speckFile;
std::string cachedFile = "";
FileSys.cacheManager()->getCachedFile(_file, cachedFile, true);
@@ -288,7 +316,7 @@ bool RenderableStars::loadData() {
}
bool RenderableStars::readSpeckFile() {
std::string _file = _speckPath;
std::string _file = _speckFile;
std::ifstream file(_file);
if (!file.good()) {
LERROR("Failed to open Speck file '" << _file << "'");
@@ -367,7 +395,8 @@ bool RenderableStars::loadCachedFile(const std::string& file) {
fileStream.read(reinterpret_cast<char*>(&_nValuesPerStar), sizeof(int32_t));
_fullData.resize(nValues);
fileStream.read(reinterpret_cast<char*>(&_fullData[0]), nValues * sizeof(_fullData[0]));
fileStream.read(reinterpret_cast<char*>(&_fullData[0]),
nValues * sizeof(_fullData[0]));
bool success = fileStream.good();
return success;
@@ -381,7 +410,8 @@ bool RenderableStars::loadCachedFile(const std::string& file) {
bool RenderableStars::saveCachedFile(const std::string& file) const {
std::ofstream fileStream(file, std::ofstream::binary);
if (fileStream.good()) {
fileStream.write(reinterpret_cast<const char*>(&CurrentCacheVersion), sizeof(int8_t));
fileStream.write(reinterpret_cast<const char*>(&CurrentCacheVersion),
sizeof(int8_t));
int32_t nValues = static_cast<int32_t>(_fullData.size());
if (nValues == 0) {
@@ -461,6 +491,28 @@ void RenderableStars::createDataSlice(ColorOption option) {
layout.value.vy = _fullData[i + 13];
layout.value.vz = _fullData[i + 14];
_slicedData.insert(_slicedData.end(),
layout.data.begin(),
layout.data.end());
break;
}
case ColorOption::Speed:
{
union {
SpeedVBOLayout value;
std::array<float, sizeof(SpeedVBOLayout)> data;
} layout;
layout.value.position = { {
position[0], position[1], position[2], position[3]
} };
layout.value.bvColor = _fullData[i + 3];
layout.value.luminance = _fullData[i + 4];
layout.value.absoluteMagnitude = _fullData[i + 5];
layout.value.speed = _fullData[i + 15];
_slicedData.insert(_slicedData.end(),
layout.data.begin(),
layout.data.end());