From 834612469cfacf33cd0247d91058fe4b9d5cbcfa Mon Sep 17 00:00:00 2001 From: Hans-Christian Helltegen Date: Mon, 1 Dec 2014 16:27:30 +0100 Subject: [PATCH] Finalized the billboard rendering. Code cleanup --- .../rendering/renderablefieldlines.h | 14 +--- shaders/fieldline_fs.glsl | 10 +-- shaders/fieldline_gs.glsl | 74 +++++-------------- src/rendering/renderablefieldlines.cpp | 73 ++---------------- src/util/kameleonwrapper.cpp | 22 +++--- 5 files changed, 43 insertions(+), 150 deletions(-) diff --git a/include/openspace/rendering/renderablefieldlines.h b/include/openspace/rendering/renderablefieldlines.h index 2d986fd146..8a32010215 100644 --- a/include/openspace/rendering/renderablefieldlines.h +++ b/include/openspace/rendering/renderablefieldlines.h @@ -27,17 +27,9 @@ // open space includes #include -#include // ghoul includes #include -#include - -namespace ghoul { - namespace opengl { - class Texture; - } -} namespace openspace { struct LinePoint; @@ -60,14 +52,10 @@ private: std::vector _seedPoints; ghoul::opengl::ProgramObject* _shader; - GLuint _fieldlineVAO, _seedpointVAO; + GLuint _fieldlineVAO; std::vector _lineStart; std::vector _lineCount; - - //TEST - ghoul::opengl::Texture* _texture; - void loadTexture(std::string path); }; } // namespace openspace diff --git a/shaders/fieldline_fs.glsl b/shaders/fieldline_fs.glsl index c7f8f0d173..26d5dc3440 100644 --- a/shaders/fieldline_fs.glsl +++ b/shaders/fieldline_fs.glsl @@ -24,23 +24,17 @@ #version __CONTEXT__ -uniform vec3 cameraViewDir; -uniform sampler2D texture1; - in vec4 gs_color; in vec4 gs_position; in vec3 gs_normal; -in vec2 gs_texcoord; #include "ABuffer/abufferStruct.hglsl" #include "ABuffer/abufferAddToBuffer.hglsl" #include "PowerScaling/powerScaling_fs.hglsl" -float EARTH_RADIUS = 6371000.0; - void main() { - // vec4 fragColor = vec4(gs_texcoord.x, gs_texcoord.x , gs_texcoord.x, 1)*gs_color; - vec4 fragColor = texture(texture1, gs_texcoord)*gs_color; + float alpha = 1-length(gs_normal)*length(gs_normal); + vec4 fragColor = vec4(gs_color.rgb, alpha); float depth = pscDepth(gs_position); ABufferStruct_t frag = createGeometryFragment(fragColor, gs_position, depth); diff --git a/shaders/fieldline_gs.glsl b/shaders/fieldline_gs.glsl index 968fba25dd..cdeca1641c 100644 --- a/shaders/fieldline_gs.glsl +++ b/shaders/fieldline_gs.glsl @@ -32,7 +32,6 @@ in vec4 vs_color[]; out vec4 gs_color; out vec4 gs_position; out vec3 gs_normal; -out vec2 gs_texcoord; #include "PowerScaling/powerScaling_vs.hglsl" @@ -40,8 +39,6 @@ layout(lines_adjacency) in; layout(triangle_strip, max_vertices = 4) out; vec4 prismoid[4]; -vec2 texcoords[4]; -float EARTH_RADIUS = 6371000.0; // Calculate the correct powerscaled position and depth for the ABuffer void ABufferEmitVertex(vec4 pos) { @@ -56,21 +53,11 @@ void ABufferEmitVertex(vec4 pos) { EmitVertex(); } -void emitFace(int a, int b, int c, int d) { - gs_texcoord = texcoords[a]; - ABufferEmitVertex(prismoid[a]); - gs_texcoord = texcoords[b]; - ABufferEmitVertex(prismoid[b]); - gs_texcoord = texcoords[c]; - ABufferEmitVertex(prismoid[c]); - gs_texcoord = texcoords[d]; - ABufferEmitVertex(prismoid[d]); - EndPrimitive(); -} - // Original code from http://prideout.net/blog/?p=61 void main() { gs_color = vs_color[0]; + + // Get the current and adjacent vertex positions and calculate help vectors u and v vec3 p0, p1, p2, p3; p0 = gl_in[0].gl_Position.xyz; p1 = gl_in[1].gl_Position.xyz; p2 = gl_in[2].gl_Position.xyz; p3 = gl_in[3].gl_Position.xyz; @@ -80,54 +67,33 @@ void main() { vec3 u = normalize(n0+n1); vec3 v = normalize(n1+n2); - // Declare scratch variables for basis vectors: - float width = 0.15*EARTH_RADIUS; - vec3 normals[2]; + float EARTH_RADIUS = 6371000.0; + float width = 0.1*EARTH_RADIUS; + + // Calculate normals for all 4 new vertices + vec3 normals[4]; normals[0] = normalize(cross(cameraViewDir,u)); - normals[1] = normalize(cross(cameraViewDir,v)); - - texcoords[0] = vec2(1,1); - texcoords[1] = vec2(1,0); - texcoords[2] = vec2(0,0); - texcoords[3] = vec2(0,1); + normals[1] = -normals[0]; + normals[3] = normalize(cross(cameraViewDir,v)); + normals[2] = -normals[3]; + // Calculate positions for the new vertices prismoid[0] = vec4(p1 + normals[0]*width, 0); - prismoid[1] = vec4(p1 - normals[0]*width, 0); - prismoid[2] = vec4(p2 - normals[1]*width, 0); - prismoid[3] = vec4(p2 + normals[1]*width, 0); + prismoid[1] = vec4(p1 + normals[1]*width, 0); + prismoid[2] = vec4(p2 + normals[2]*width, 0); + prismoid[3] = vec4(p2 + normals[3]*width, 0); - gs_normal = n1; - gs_texcoord = texcoords[0]; + // Send normals and verticies to fragment shader + gs_normal = normals[0]; ABufferEmitVertex(prismoid[0]); - gs_texcoord = texcoords[1]; + gs_normal = normals[1]; ABufferEmitVertex(prismoid[1]); - gs_normal = n2; - gs_texcoord = texcoords[3]; + gs_normal = normals[3]; ABufferEmitVertex(prismoid[3]); - gs_texcoord = texcoords[2]; + gs_normal = normals[2]; ABufferEmitVertex(prismoid[2]); - EndPrimitive(); - - // vec3 i,j,k; float r = 0.05*EARTH_RADIUS; - // j = u; i = normal; k = cross(i, j); i *= r; k *= r; - - // // Compute face 1 of 2: - // prismoid[0] = vec4(p1 + i + k, 0); - // prismoid[1] = vec4(p1 + i - k, 0); - // prismoid[2] = vec4(p1 - i - k, 0); - // prismoid[3] = vec4(p1 - i + k, 0); - - // // Compute face 2 of 2: - // prismoid[4] = vec4(p2 + i + k, 0); - // prismoid[5] = vec4(p2 + i - k, 0); - // prismoid[6] = vec4(p2 - i - k, 0); - // prismoid[7] = vec4(p2 - i + k, 0); - - // // Emit the six faces of the prismoid: - // emitFace(0,1,3,2); emitFace(5,4,6,7); - // emitFace(4,5,0,1); emitFace(3,2,7,6); - // emitFace(0,3,4,7); emitFace(2,1,6,5); + EndPrimitive(); } \ No newline at end of file diff --git a/src/rendering/renderablefieldlines.cpp b/src/rendering/renderablefieldlines.cpp index db3662e007..73d7120576 100644 --- a/src/rendering/renderablefieldlines.cpp +++ b/src/rendering/renderablefieldlines.cpp @@ -26,12 +26,9 @@ #include #include #include - #include -#include -#include -#include +#include namespace { const std::string _loggerCat = "RenderableFieldlines"; @@ -47,12 +44,12 @@ RenderableFieldlines::RenderableFieldlines(const ghoul::Dictionary& dictionary) : Renderable(dictionary) , _fieldlineVAO(0) , _shader(nullptr) - , _texture(nullptr) { std::string name; bool success = dictionary.getValue(constants::scenegraphnode::keyName, name); assert(success); + // Read fieldlines module into dictionary ghoul::Dictionary fieldlines; success = dictionary.getValue(keyFieldlines, fieldlines); if (!success) { @@ -96,23 +93,20 @@ bool RenderableFieldlines::initialize() { assert(_hintsDictionaries.size() != 0); int prevEnd = 0; - std::vector vertexData, seedPointsData; + std::vector vertexData; std::vector > fieldlinesData; - glm::vec4 seedPointsColor = glm::vec4(1.0, 0.5, 0.0, 1.0); + // Read data from fieldlines dictionary for (int i = 0; i < _filenames.size(); ++i) { fieldlinesData = getFieldlinesData(_filenames[i], _hintsDictionaries[i]); + // Arrange data for glMultiDrawArrays for (int j = 0; j < fieldlinesData.size(); ++j) { _lineStart.push_back(prevEnd); _lineCount.push_back(fieldlinesData[j].size()); prevEnd = prevEnd + fieldlinesData[j].size(); vertexData.insert( vertexData.end(), fieldlinesData[j].begin(), fieldlinesData[j].end()); } - // Give seedpoints a color for visualizing as GL_POINTS - for (glm::vec3 seedPoint : _seedPoints) { - seedPointsData.push_back(LinePoint(seedPoint, seedPointsColor)); - } } LDEBUG("Number of vertices : " << vertexData.size()); @@ -137,30 +131,9 @@ bool RenderableFieldlines::initialize() { glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind buffer glBindVertexArray(0); //unbind array - // ------ SEEDPOINTS ----------------- - GLuint seedpointPositionBuffer; - glGenVertexArrays(1, &_seedpointVAO); // generate array - glBindVertexArray(_seedpointVAO); // bind array - glGenBuffers(1, &seedpointPositionBuffer); // generate buffer - glBindBuffer(GL_ARRAY_BUFFER, seedpointPositionBuffer); // bind buffer - glBufferData(GL_ARRAY_BUFFER, seedPointsData.size()*sizeof(LinePoint), &seedPointsData.front(), GL_STATIC_DRAW); - - // Vertex positions - glEnableVertexAttribArray(vertexLocation); - glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(LinePoint), reinterpret_cast(0)); - - // 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 - OsEng.ref().configurationManager().getValue("FieldlineProgram", _shader); assert(_shader); - loadTexture("C:/openspace/openspace-data/test.png"); - return true; } @@ -178,35 +151,21 @@ void RenderableFieldlines::render(const RenderData& data) { _shader->setUniform("cameraViewDir", data.camera.viewDirection()); setPscUniforms(_shader, &data.camera, data.position); - ghoul::opengl::TextureUnit unit; - unit.activate(); - _texture->bind(); - _shader->setUniform("texture1", unit); - // ------ DRAW FIELDLINES ----------------- - glEnable(GL_POLYGON_SMOOTH); - glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); glBindVertexArray(_fieldlineVAO); glMultiDrawArrays(GL_LINE_STRIP_ADJACENCY, &_lineStart[0], &_lineCount[0], _lineStart.size()); - //// ------ DRAW SEEDPOINTS ----------------- - //glBindVertexArray(_seedpointVAO); - //glMultiDrawArrays(GL_LINE_STRIP_ADJACENCY, &_lineStart[0], &_lineCount[0], _seedPoints.size()); - - glDisable(GL_POLYGON_SMOOTH); glBindVertexArray(0); _shader->deactivate(); } std::vector > RenderableFieldlines::getFieldlinesData(std::string filename, ghoul::Dictionary hintsDictionary) { - std::string modelString; - float stepSize = 0.5; // default if no stepsize is specified in hints - std::string xVariable, yVariable, zVariable; + std::string modelString, xVariable, yVariable, zVariable; KameleonWrapper::Model model; std::vector > fieldlinesData; - bool classification = false, lorentz = false; glm::vec4 fieldlineColor = glm::vec4(1.0, 1.0, 1.0, 1.0); // default color if no color or classification is specified + float stepSize = 0.5; // default if no stepsize is specified in hints if (hintsDictionary.hasKey("Model") && hintsDictionary.getValue("Model", modelString)) { // ------ MODEL ----------------- @@ -278,22 +237,4 @@ std::vector > RenderableFieldlines::getFieldlinesData(std return fieldlinesData; } -// TEST -void RenderableFieldlines::loadTexture(std::string path) { - if (path != "") { - ghoul::opengl::Texture* texture = ghoul::opengl::loadTexture(path); - if (texture) { - LDEBUG("Loaded texture from '" << path << "'"); - texture->uploadTexture(); - - // Textures of planets looks much smoother with AnisotropicMipMap rather than linear - texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap); - - if (_texture) - delete _texture; - _texture = texture; - } - } -} - } // namespace openspace diff --git a/src/util/kameleonwrapper.cpp b/src/util/kameleonwrapper.cpp index f8abc9c3b7..4be16d2ebf 100644 --- a/src/util/kameleonwrapper.cpp +++ b/src/util/kameleonwrapper.cpp @@ -46,6 +46,7 @@ namespace openspace { std::string _loggerCat = "KameleonWrapper"; +const float RE_TO_METER = 6371000; KameleonWrapper::KameleonWrapper() : _kameleon(nullptr) @@ -395,8 +396,6 @@ 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,10 +407,10 @@ KameleonWrapper::Fieldlines KameleonWrapper::getClassifiedFieldLines( // classify color = classifyFieldline(forwardEnd, backEnd); - // write colors + // write colors and convert positions to meter std::vector line; for (glm::vec3 position : bLine) { - line.push_back(LinePoint(ReToMeter*position, color)); + line.push_back(LinePoint(RE_TO_METER*position, color)); } fieldLines.push_back(line); @@ -446,10 +445,10 @@ KameleonWrapper::Fieldlines KameleonWrapper::getFieldLines( bLine.erase(bLine.begin()); bLine.insert(bLine.begin(), fLine.rbegin(), fLine.rend()); - // write colors + // write colors and convert positions to meter std::vector line; for (glm::vec3 position : bLine) { - line.push_back(LinePoint(position, color)); + line.push_back(LinePoint(RE_TO_METER*position, color)); } fieldLines.push_back(line); @@ -475,12 +474,17 @@ KameleonWrapper::Fieldlines KameleonWrapper::getLorentzTrajectories( plusTraj = traceLorentzTrajectory(seedPoint, stepsize, 1.0); minusTraj = traceLorentzTrajectory(seedPoint, stepsize, -1.0); + //minusTraj.erase(minusTraj.begin()); + int plusNum = plusTraj.size(); minusTraj.insert(minusTraj.begin(), plusTraj.rbegin(), plusTraj.rend()); - // write colors + // write colors and convert positions to meter std::vector trajectory; - for (glm::vec3 position : minusTraj) { - trajectory.push_back(LinePoint(position, color)); + for (glm::vec3 position : minusTraj) { + if (trajectory.size() < plusNum) // set positive trajectory to pink + trajectory.push_back(LinePoint(RE_TO_METER*position, glm::vec4(1, 0, 1, 1))); + else // set negative trajectory to cyan + trajectory.push_back(LinePoint(RE_TO_METER*position, glm::vec4(0, 1, 1, 1))); } trajectories.push_back(trajectory); }