mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-02-25 14:29:03 -06:00
Fixed depth, position, scale and shader issues with fieldlines. Added passthrough geometry shader
This commit is contained in:
@@ -45,7 +45,6 @@ public:
|
||||
bool deinitialize();
|
||||
|
||||
void render(const RenderData& data) override;
|
||||
void update(const UpdateData& data) override;
|
||||
|
||||
private:
|
||||
std::vector<std::vector<LinePoint> > getFieldlinesData(std::string filename, ghoul::Dictionary hintsDictionary);
|
||||
@@ -54,18 +53,11 @@ private:
|
||||
std::vector<std::string> _filenames;
|
||||
std::vector<glm::vec3> _seedPoints;
|
||||
|
||||
ghoul::opengl::ProgramObject *_fieldlinesProgram;
|
||||
GLuint _VAO, _seedpointVAO;
|
||||
|
||||
ghoul::filesystem::File* _vertexSourceFile;
|
||||
ghoul::filesystem::File* _fragmentSourceFile;
|
||||
ghoul::opengl::ProgramObject* _shader;
|
||||
GLuint _fieldlineVAO, _seedpointVAO;
|
||||
|
||||
std::vector<GLint> _lineStart;
|
||||
std::vector<GLsizei> _lineCount;
|
||||
|
||||
bool _programUpdateOnSave;
|
||||
bool _update;
|
||||
void safeShaderCompilation();
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
40
shaders/fieldline_fs.glsl
Normal file
40
shaders/fieldline_fs.glsl
Normal file
@@ -0,0 +1,40 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#version __CONTEXT__
|
||||
|
||||
in vec4 gs_color;
|
||||
in vec4 gs_position;
|
||||
|
||||
#include "ABuffer/abufferStruct.hglsl"
|
||||
#include "ABuffer/abufferAddToBuffer.hglsl"
|
||||
#include "PowerScaling/powerScaling_fs.hglsl"
|
||||
|
||||
void main() {
|
||||
vec4 fragColor = gs_color;
|
||||
|
||||
float depth = pscDepth(gs_position);
|
||||
ABufferStruct_t frag = createGeometryFragment(fragColor, gs_position, depth);
|
||||
addToBuffer(frag);
|
||||
}
|
||||
90
shaders/fieldline_gs.glsl
Normal file
90
shaders/fieldline_gs.glsl
Normal file
@@ -0,0 +1,90 @@
|
||||
|
||||
#version __CONTEXT__
|
||||
|
||||
uniform mat4 modelViewProjection;
|
||||
uniform mat4 modelTransform;
|
||||
|
||||
layout(lines) in;
|
||||
layout(line_strip, max_vertices = 2) out;
|
||||
|
||||
in vec4 vs_color[];
|
||||
in vec4 vs_position[];
|
||||
out vec4 gs_color;
|
||||
out vec4 gs_position;
|
||||
|
||||
#include "PowerScaling/powerScaling_vs.hglsl"
|
||||
|
||||
void main() {
|
||||
gs_color = vs_color[0];
|
||||
for(int i = 0; i < 2; i++) {
|
||||
// gs_position = gl_in[i].gl_Position;
|
||||
// gl_Position = gl_in[i].gl_Position;
|
||||
|
||||
// calculate psc position
|
||||
vec4 tmp = gl_in[i].gl_Position;
|
||||
vec4 position = pscTransform(tmp, modelTransform);
|
||||
gs_position = tmp;
|
||||
|
||||
// project the position to view space
|
||||
position = modelViewProjection * position;
|
||||
gl_Position = z_normalization(position);
|
||||
EmitVertex();
|
||||
}
|
||||
EndPrimitive();
|
||||
}
|
||||
|
||||
// uniform mat4 ModelviewProjection;
|
||||
// layout(lines_adjacency) in;
|
||||
// layout(location = 0) in vec3 vPosition[4]; // Four inputs since we're using GL_LINE_STRIP_ADJACENCY
|
||||
// // layout(location = 2) in vec3 vNormal[4]; // Orientation vectors are necessary for consistent alignment
|
||||
// layout(triangle_strip, max_vertices = 24) out;
|
||||
|
||||
// out vec4 in_color;
|
||||
|
||||
// vec4 prismoid[8]; // Scratch space for the eight corners of the prismoid
|
||||
|
||||
// void emit(int a, int b, int c, int d) {
|
||||
// gl_Position = prismoid[a]; EmitVertex();
|
||||
// gl_Position = prismoid[b]; EmitVertex();
|
||||
// gl_Position = prismoid[c]; EmitVertex();
|
||||
// gl_Position = prismoid[d]; EmitVertex();
|
||||
// EndPrimitive();
|
||||
// }
|
||||
|
||||
// void main() {
|
||||
// // Compute orientation vectors for the two connecting faces:
|
||||
// vec3 vNormal = vec3(0, 0, 1);
|
||||
|
||||
// vec3 p0, p1, p2, p3;
|
||||
// p0 = vPosition[0]; p1 = vPosition[1];
|
||||
// p2 = vPosition[2]; p3 = vPosition[3];
|
||||
// vec3 n0 = normalize(p1-p0);
|
||||
// vec3 n1 = normalize(p2-p1);
|
||||
// vec3 n2 = normalize(p3-p2);
|
||||
// vec3 u = normalize(n0+n1);
|
||||
// vec3 v = normalize(n1+n2);
|
||||
|
||||
// // Declare scratch variables for basis vectors:
|
||||
// vec3 i,j,k; float r = 0.1;
|
||||
|
||||
// // Compute face 1 of 2:
|
||||
// // j = u; i = vNormal[1]; k = cross(i, j); i *= r; k *= r;
|
||||
// j = u; i = vNormal; k = cross(i, j); i *= r; k *= r;
|
||||
// prismoid[0] = ModelviewProjection * vec4(p1 + i + k, 1);
|
||||
// prismoid[1] = ModelviewProjection * vec4(p1 + i - k, 1);
|
||||
// prismoid[2] = ModelviewProjection * vec4(p1 - i - k, 1);
|
||||
// prismoid[3] = ModelviewProjection * vec4(p1 - i + k, 1);
|
||||
|
||||
// // Compute face 2 of 2:
|
||||
// // j = v; i = vNormal[2]; k = cross(i, j); i *= r; k *= r;
|
||||
// j = v; i = vNormal; k = cross(i, j); i *= r; k *= r;
|
||||
// prismoid[4] = ModelviewProjection * vec4(p2 + i + k, 1);
|
||||
// prismoid[5] = ModelviewProjection * vec4(p2 + i - k, 1);
|
||||
// prismoid[6] = ModelviewProjection * vec4(p2 - i - k, 1);
|
||||
// prismoid[7] = ModelviewProjection * vec4(p2 - i + k, 1);
|
||||
|
||||
// // Emit the six faces of the prismoid:
|
||||
// emit(0,1,3,2); emit(5,4,6,7);
|
||||
// emit(4,5,0,1); emit(3,2,7,6);
|
||||
// emit(0,3,4,7); emit(2,1,6,5);
|
||||
// }
|
||||
51
shaders/fieldline_vs.glsl
Normal file
51
shaders/fieldline_vs.glsl
Normal file
@@ -0,0 +1,51 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#version __CONTEXT__
|
||||
|
||||
uniform mat4 modelViewProjection;
|
||||
uniform mat4 modelTransform;
|
||||
|
||||
layout(location = 0) in vec3 in_position;
|
||||
layout(location = 1) in vec4 in_color;
|
||||
|
||||
out vec4 vs_color;
|
||||
// out vec4 vs_position;
|
||||
|
||||
#include "PowerScaling/powerScaling_vs.hglsl"
|
||||
|
||||
void main() {
|
||||
vs_color = in_color;
|
||||
|
||||
// // calculate psc position
|
||||
// vec4 tmp = vec4(in_position, 0);
|
||||
// vec4 position = pscTransform(tmp, modelTransform);
|
||||
// // vs_position = tmp;
|
||||
|
||||
// // project the position to view space
|
||||
// position = modelViewProjection * position;
|
||||
// gl_Position = z_normalization(position);
|
||||
|
||||
gl_Position = vec4(in_position, 0);
|
||||
}
|
||||
@@ -35,18 +35,14 @@ namespace {
|
||||
const std::string keyFieldlines = "Fieldlines";
|
||||
const std::string keyFilename = "File";
|
||||
const std::string keyHints = "Hints";
|
||||
const std::string keyShaders = "Shaders";
|
||||
const std::string keyVertexShader = "VertexShader";
|
||||
const std::string keyFragmentShader = "FragmentShader";
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
RenderableFieldlines::RenderableFieldlines(const ghoul::Dictionary& dictionary)
|
||||
: Renderable(dictionary)
|
||||
, _VAO(0)
|
||||
, _programUpdateOnSave(false)
|
||||
, _update(false)
|
||||
, _fieldlineVAO(0)
|
||||
, _shader(nullptr)
|
||||
{
|
||||
std::string name;
|
||||
bool success = dictionary.getValue(constants::scenegraphnode::keyName, name);
|
||||
@@ -84,41 +80,7 @@ RenderableFieldlines::RenderableFieldlines(const ghoul::Dictionary& dictionary)
|
||||
}
|
||||
}
|
||||
|
||||
ghoul::Dictionary shaderDictionary;
|
||||
success = dictionary.getValue(keyShaders, shaderDictionary);
|
||||
if (!success) {
|
||||
LERROR("RenderableFieldlines '" << name << "' does not contain a '" <<
|
||||
keyShaders << "' table");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string vshaderpath;
|
||||
success = shaderDictionary.getValue(keyVertexShader, vshaderpath);
|
||||
if (!success) {
|
||||
LERROR("RenderableFieldlines '" << name << "' does not have a '" <<
|
||||
keyVertexShader << "'");
|
||||
return;
|
||||
}
|
||||
vshaderpath = findPath(vshaderpath);
|
||||
|
||||
std::string fshaderpath;
|
||||
success = shaderDictionary.getValue(keyFragmentShader, fshaderpath);
|
||||
if (!success) {
|
||||
LERROR("RenderableFieldlines '" << name << "' does not have a '" <<
|
||||
keyFragmentShader << "'");
|
||||
return;
|
||||
}
|
||||
fshaderpath = findPath(fshaderpath);
|
||||
|
||||
_vertexSourceFile = new ghoul::filesystem::File(vshaderpath, false);
|
||||
_fragmentSourceFile = new ghoul::filesystem::File(fshaderpath, false);
|
||||
|
||||
|
||||
_fieldlinesProgram = ghoul::opengl::ProgramObject::Build("FieldlinesProgram", vshaderpath, fshaderpath);
|
||||
|
||||
dictionary.getValue("UpdateOnSave", _programUpdateOnSave);
|
||||
|
||||
setBoundingSphere(PowerScaledScalar::CreatePSS(5)); // FIXME a non-magic number perhaps
|
||||
setBoundingSphere(PowerScaledScalar::CreatePSS(250.f*6371000.f)); // FIXME a non-magic number perhaps
|
||||
}
|
||||
|
||||
RenderableFieldlines::~RenderableFieldlines() {
|
||||
@@ -151,8 +113,8 @@ bool RenderableFieldlines::initialize() {
|
||||
|
||||
// ------ FIELDLINES -----------------
|
||||
GLuint vertexPositionBuffer;
|
||||
glGenVertexArrays(1, &_VAO); // generate array
|
||||
glBindVertexArray(_VAO); // bind array
|
||||
glGenVertexArrays(1, &_fieldlineVAO); // generate array
|
||||
glBindVertexArray(_fieldlineVAO); // bind array
|
||||
glGenBuffers(1, &vertexPositionBuffer); // generate buffer
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBuffer); // bind buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, vertexData.size()*sizeof(LinePoint), &vertexData.front(), GL_STATIC_DRAW);
|
||||
@@ -162,10 +124,10 @@ bool RenderableFieldlines::initialize() {
|
||||
glEnableVertexAttribArray(vertexLocation);
|
||||
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(LinePoint), reinterpret_cast<void*>(0));
|
||||
|
||||
// Texture coordinates
|
||||
GLuint texcoordLocation = 1;
|
||||
glEnableVertexAttribArray(texcoordLocation);
|
||||
glVertexAttribPointer(texcoordLocation, 4, GL_FLOAT, GL_FALSE, sizeof(LinePoint), (void*)(sizeof(glm::vec3)));
|
||||
// Vertex colors
|
||||
GLuint colorLocation = 1;
|
||||
glEnableVertexAttribArray(colorLocation);
|
||||
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(LinePoint), (void*)(sizeof(glm::vec3)));
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind buffer
|
||||
glBindVertexArray(0); //unbind array
|
||||
@@ -182,26 +144,21 @@ bool RenderableFieldlines::initialize() {
|
||||
glEnableVertexAttribArray(vertexLocation);
|
||||
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(LinePoint), reinterpret_cast<void*>(0));
|
||||
|
||||
// Texture coordinates
|
||||
glEnableVertexAttribArray(texcoordLocation);
|
||||
glVertexAttribPointer(texcoordLocation, 4, GL_FLOAT, GL_FALSE, sizeof(LinePoint), (void*)(3*sizeof(float)));
|
||||
// Vertex colors
|
||||
glEnableVertexAttribArray(colorLocation);
|
||||
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(LinePoint), (void*)(sizeof(glm::vec3)));
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind buffer
|
||||
glBindVertexArray(0); //unbind array
|
||||
|
||||
glPointSize(5); // size of seedpoints
|
||||
//glEnable(GL_LINE_SMOOTH);
|
||||
//glEnable(GL_POLYGON_SMOOTH);
|
||||
//glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
||||
//glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
|
||||
|
||||
// ------ SETUP SHADERS -----------------
|
||||
auto privateCallback = [this](const ghoul::filesystem::File& file) {
|
||||
_update = true;
|
||||
};
|
||||
if(_programUpdateOnSave) {
|
||||
_vertexSourceFile->setCallback(privateCallback);
|
||||
_fragmentSourceFile->setCallback(privateCallback);
|
||||
}
|
||||
|
||||
_fieldlinesProgram->compileShaderObjects();
|
||||
_fieldlinesProgram->linkProgramObject();
|
||||
OsEng.ref().configurationManager().getValue("FieldlineProgram", _shader);
|
||||
assert(_shader);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -211,46 +168,24 @@ bool RenderableFieldlines::deinitialize() {
|
||||
}
|
||||
|
||||
void RenderableFieldlines::render(const RenderData& data) {
|
||||
if(_update) {
|
||||
_update = false;
|
||||
safeShaderCompilation();
|
||||
}
|
||||
if (!_shader)
|
||||
return;
|
||||
|
||||
glm::mat4 transform = data.camera.viewProjectionMatrix();
|
||||
glm::mat4 camTransform = data.camera.viewRotationMatrix();
|
||||
//psc relative = data.position - data.camera.position();
|
||||
_shader->activate();
|
||||
_shader->setUniform("modelViewProjection", data.camera.viewProjectionMatrix());
|
||||
_shader->setUniform("modelTransform", glm::mat4(1.0));
|
||||
setPscUniforms(_shader, &data.camera, data.position);
|
||||
|
||||
transform = transform*camTransform;
|
||||
transform = glm::mat4(1.0);
|
||||
transform = glm::scale(transform, glm::vec3(0.01));
|
||||
|
||||
// Activate shader
|
||||
_fieldlinesProgram->activate();
|
||||
|
||||
_fieldlinesProgram->setUniform("modelViewProjection", data.camera.viewProjectionMatrix());
|
||||
_fieldlinesProgram->setUniform("modelTransform", transform);
|
||||
setPscUniforms(_fieldlinesProgram, &data.camera, data.position);
|
||||
|
||||
// ------ FIELDLINES -----------------
|
||||
glBindVertexArray(_VAO);
|
||||
// ------ DRAW FIELDLINES -----------------
|
||||
glBindVertexArray(_fieldlineVAO);
|
||||
glMultiDrawArrays(GL_LINE_STRIP, &_lineStart[0], &_lineCount[0], _lineStart.size());
|
||||
|
||||
// // ------ SEEDPOINTS -----------------
|
||||
// // ------ DRAW SEEDPOINTS -----------------
|
||||
// glBindVertexArray(_seedpointVAO);
|
||||
// glMultiDrawArrays(GL_POINTS, &_lineStart[0], &_lineCount[0], _seedPoints.size());
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
// Deactivate shader
|
||||
_fieldlinesProgram->deactivate();
|
||||
}
|
||||
|
||||
void RenderableFieldlines::update(const UpdateData& data) {
|
||||
}
|
||||
|
||||
void RenderableFieldlines::safeShaderCompilation() {
|
||||
_fieldlinesProgram->rebuildFromFile();
|
||||
_fieldlinesProgram->compileShaderObjects();
|
||||
_fieldlinesProgram->linkProgramObject();
|
||||
_shader->deactivate();
|
||||
}
|
||||
|
||||
std::vector<std::vector<LinePoint> > RenderableFieldlines::getFieldlinesData(std::string filename, ghoul::Dictionary hintsDictionary) {
|
||||
|
||||
@@ -231,6 +231,16 @@ bool SceneGraph::initialize()
|
||||
_programs.push_back(tmpProgram);
|
||||
OsEng.ref().configurationManager().setValue("PlaneProgram", tmpProgram);
|
||||
|
||||
// Fieldline program
|
||||
tmpProgram = ProgramObject::Build("Fieldline",
|
||||
"${SHADERS}/fieldline_vs.glsl",
|
||||
"${SHADERS}/fieldline_fs.glsl",
|
||||
"${SHADERS}/fieldline_gs.glsl",
|
||||
cb);
|
||||
if (!tmpProgram) return false;
|
||||
_programs.push_back(tmpProgram);
|
||||
OsEng.ref().configurationManager().setValue("FieldlineProgram", tmpProgram);
|
||||
|
||||
// Done building shaders
|
||||
double elapsed = std::chrono::duration_cast<second_>(clock_::now()-beginning).count();
|
||||
LINFO("Time to load shaders: " << elapsed);
|
||||
|
||||
@@ -395,6 +395,8 @@ KameleonWrapper::Fieldlines KameleonWrapper::getClassifiedFieldLines(
|
||||
glm::vec4 color;
|
||||
FieldlineEnd forwardEnd, backEnd;
|
||||
|
||||
float ReToMeter = 6371000;
|
||||
|
||||
if (_type == Model::BATSRUS) {
|
||||
for (glm::vec3 seedPoint : seedPoints) {
|
||||
fLine = traceCartesianFieldline(xVar, yVar, zVar, seedPoint, stepSize, TraceDirection::FORWARD, forwardEnd);
|
||||
@@ -408,7 +410,7 @@ KameleonWrapper::Fieldlines KameleonWrapper::getClassifiedFieldLines(
|
||||
// write colors
|
||||
std::vector<LinePoint> line;
|
||||
for (glm::vec3 position : bLine) {
|
||||
line.push_back(LinePoint(position, color));
|
||||
line.push_back(LinePoint(ReToMeter*position, color));
|
||||
}
|
||||
|
||||
fieldLines.push_back(line);
|
||||
|
||||
Reference in New Issue
Block a user