Fixed issue 675.

This commit is contained in:
Jonathas Costa
2018-07-31 14:50:10 -04:00
parent 335beb8dd3
commit 4e30d9c3b8
2 changed files with 75 additions and 106 deletions

View File

@@ -524,15 +524,17 @@ void RenderablePlanesCloud::initializeGL() {
}
void RenderablePlanesCloud::deleteDataGPU() {
for (RenderingPlane& renderingPlane : _renderingPlanesArray) {
glDeleteVertexArrays(1, &renderingPlane.vao);
glDeleteBuffers(1, &renderingPlane.vbo);
void RenderablePlanesCloud::deleteDataGPUAndCPU() {
for (std::pair<int, PlaneAggregate> pAMapItem : _planesMap) {
glDeleteBuffers(1, &pAMapItem.second.vbo);
glDeleteVertexArrays(1, &pAMapItem.second.vao);
pAMapItem.second.planesCoordinates.clear();
}
_planesMap.clear();
}
void RenderablePlanesCloud::deinitializeGL() {
deleteDataGPU();
deleteDataGPUAndCPU();
DigitalUniverseModule::ProgramObjectManager.releaseProgramObject(
ProgramObjectName,
@@ -590,56 +592,21 @@ void RenderablePlanesCloud::renderPlanes(const RenderData&,
unit.activate();
_program->setUniform(_uniformCache.galaxyTexture, unit);
int currentTextureIndex = -1;
for (const RenderingPlane& renderingPlane : _renderingPlanesArray) {
for (const std::pair<int, PlaneAggregate> pAMapItem : _planesMap) {
// For planes with undefined textures references
if (renderingPlane.planeIndex == -1) {
if (pAMapItem.first == 30) {
continue;
}
const glm::dvec4 vertex0 = modelViewProjectionMatrix * glm::dvec4(
renderingPlane.vertexData[0],
renderingPlane.vertexData[1],
renderingPlane.vertexData[2],
renderingPlane.vertexData[3]
);
const glm::dvec4 vertex1 = modelViewProjectionMatrix * glm::dvec4(
renderingPlane.vertexData[6],
renderingPlane.vertexData[7],
renderingPlane.vertexData[8],
renderingPlane.vertexData[9]
);
// Testing size:
glm::vec4 topRight = vertex1 / vertex1.w;
topRight = ((topRight + glm::vec4(1.0)) / glm::vec4(2.0)) *
glm::vec4(viewport[2], viewport[3], 1.0, 1.0);
glm::vec4 bottomLeft = vertex0 / vertex0.w;
bottomLeft = ((bottomLeft + glm::vec4(1.0)) / glm::vec4(2.0)) *
glm::vec4(viewport[2], viewport[3], 1.0, 1.0);
const float lengthY = std::abs(topRight.y - bottomLeft.y);
const float lengthX = std::abs(topRight.x - bottomLeft.x);
const float lengthXY = glm::length(glm::vec2(topRight) - glm::vec2(bottomLeft));
const float biggestAxis = lengthY > lengthX ?
(lengthY > lengthXY ? lengthY : lengthXY) :
(lengthX > lengthXY ? lengthX : lengthXY);
if (biggestAxis < _planeMinSize) {
continue;
if (currentTextureIndex != pAMapItem.first) {
_textureMap[pAMapItem.first]->bind();
currentTextureIndex = pAMapItem.first;
}
if (currentTextureIndex != renderingPlane.planeIndex) {
_textureMap[renderingPlane.planeIndex]->bind();
currentTextureIndex = renderingPlane.planeIndex;
}
glBindVertexArray(renderingPlane.vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(pAMapItem.second.vao);
glDrawArrays(GL_TRIANGLES, 0, 6 * pAMapItem.second.numberOfPlanes);
}
//if (additiveBlending) {
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// //glDepthMask(true);
//}
glBindVertexArray(0);
_program->deactivate();
@@ -785,7 +752,7 @@ void RenderablePlanesCloud::render(const RenderData& data, RendererTasks&) {
void RenderablePlanesCloud::update(const UpdateData&) {
if (_dataIsDirty && _hasSpeckFile) {
deleteDataGPU();
deleteDataGPUAndCPU();
createPlanes();
_dataIsDirty = false;
}
@@ -1239,7 +1206,7 @@ bool RenderablePlanesCloud::saveCachedFile(const std::string& file) const {
void RenderablePlanesCloud::createPlanes() {
if (_dataIsDirty && _hasSpeckFile) {
LDEBUG("Creating planes");
LDEBUG("Creating planes...");
float maxSize = 0.f;
for (size_t p = 0; p < _fullData.size(); p += _nValuesPerAstronomicalObject) {
const glm::vec4 transformedPos = glm::vec4(
@@ -1282,17 +1249,6 @@ void RenderablePlanesCloud::createPlanes() {
u *= _scaleFactor;
v *= _scaleFactor;
RenderingPlane plane;
plane.planeIndex = static_cast<int>(_fullData[p + _textureVariableIndex]);
// JCC: Ask Abbott about these points refeering to a non-existing texture.
if (plane.planeIndex == 30) {
plane.planeIndex = -1;
}
glGenVertexArrays(1, &plane.vao);
glGenBuffers(1, &plane.vbo);
glm::vec4 vertex0 = transformedPos - u - v; // same as 3
glm::vec4 vertex1 = transformedPos + u + v; // same as 5
glm::vec4 vertex2 = transformedPos - u + v;
@@ -1300,27 +1256,27 @@ void RenderablePlanesCloud::createPlanes() {
float scale = 0.f;
switch (_unit) {
case Meter:
scale = 1.f;
break;
case Kilometer:
scale = 1e3f;
break;
case Parsec:
scale = static_cast<float>(PARSEC);
break;
case Kiloparsec:
scale = static_cast<float>(1e3 * PARSEC);
break;
case Megaparsec:
scale = static_cast<float>(1e6 * PARSEC);
break;
case Gigaparsec:
scale = static_cast<float>(1e9 * PARSEC);
break;
case GigalightYears:
scale = static_cast<float>(306391534.73091 * PARSEC);
break;
case Meter:
scale = 1.f;
break;
case Kilometer:
scale = 1e3f;
break;
case Parsec:
scale = static_cast<float>(PARSEC);
break;
case Kiloparsec:
scale = static_cast<float>(1e3 * PARSEC);
break;
case Megaparsec:
scale = static_cast<float>(1e6 * PARSEC);
break;
case Gigaparsec:
scale = static_cast<float>(1e9 * PARSEC);
break;
case GigalightYears:
scale = static_cast<float>(306391534.73091 * PARSEC);
break;
}
for (int i = 0; i < 3; ++i) {
@@ -1336,7 +1292,7 @@ void RenderablePlanesCloud::createPlanes() {
vertex4 *= scale;
GLfloat vertexData[] = {
// x y z w s t
// x y z w s t
vertex0.x, vertex0.y, vertex0.z, 1.f, 0.f, 0.f,
vertex1.x, vertex1.y, vertex1.z, 1.f, 1.f, 1.f,
vertex2.x, vertex2.y, vertex2.z, 1.f, 0.f, 1.f,
@@ -1345,14 +1301,36 @@ void RenderablePlanesCloud::createPlanes() {
vertex1.x, vertex1.y, vertex1.z, 1.f, 1.f, 1.f,
};
std::memcpy(plane.vertexData, vertexData, sizeof(vertexData));
int textureIndex = static_cast<int>(_fullData[p + _textureVariableIndex]);
std::unordered_map<int, PlaneAggregate>::iterator found =
_planesMap.find(textureIndex);
if (found != _planesMap.end()) {
for (int i = 0; i < PLANES_VERTEX_DATA_SIZE; ++i) {
found->second.planesCoordinates.push_back(vertexData[i]);
}
found->second.numberOfPlanes++;
}
else {
PlaneAggregate pA;
pA.textureIndex = textureIndex;
glGenVertexArrays(1, &pA.vao);
glGenBuffers(1, &pA.vbo);
pA.numberOfPlanes = 1;
for (int i = 0; i < PLANES_VERTEX_DATA_SIZE; ++i) {
pA.planesCoordinates.push_back(vertexData[i]);
}
_planesMap.insert(std::pair<int, PlaneAggregate>(textureIndex, pA));
}
}
glBindVertexArray(plane.vao);
glBindBuffer(GL_ARRAY_BUFFER, plane.vbo);
// Send data to GPU
for (const std::pair<int, PlaneAggregate> pAMapItem : _planesMap) {
glBindVertexArray(pAMapItem.second.vao);
glBindBuffer(GL_ARRAY_BUFFER, pAMapItem.second.vbo);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(plane.vertexData),
plane.vertexData,
sizeof(GLfloat) * PLANES_VERTEX_DATA_SIZE * pAMapItem.second.numberOfPlanes,
&pAMapItem.second.planesCoordinates[0],
GL_STATIC_DRAW
);
// in_position
@@ -1377,10 +1355,8 @@ void RenderablePlanesCloud::createPlanes() {
reinterpret_cast<GLvoid*>(sizeof(GLfloat) * 4)
);
_renderingPlanesArray.push_back(plane);
}
glBindVertexArray(0);
glBindVertexArray(0);
}
_dataIsDirty = false;
@@ -1390,13 +1366,6 @@ void RenderablePlanesCloud::createPlanes() {
if (_hasLabel && _labelDataIsDirty) {
_labelDataIsDirty = false;
}
// Sort planes by texture index
std::sort(_renderingPlanesArray.begin(), _renderingPlanesArray.end(),
[](const RenderingPlane& planeA, const RenderingPlane& planeB) {
return planeA.planeIndex < planeB.planeIndex;
}
);
}
} // namespace openspace

View File

@@ -82,14 +82,15 @@ private:
GigalightYears = 6
};
struct RenderingPlane {
int planeIndex;
struct PlaneAggregate {
int textureIndex;
int numberOfPlanes;
GLuint vao;
GLuint vbo;
GLfloat vertexData[PLANES_VERTEX_DATA_SIZE];
std::vector<GLfloat> planesCoordinates;
};
void deleteDataGPU();
void deleteDataGPUAndCPU();
void createPlanes();
void renderPlanes(const RenderData& data, const glm::dmat4& modelViewMatrix,
const glm::dmat4& projectionMatrix, float fadeInVariable);
@@ -134,6 +135,7 @@ private:
std::shared_ptr<ghoul::fontrendering::Font> _font = nullptr;
std::unordered_map<int, std::unique_ptr<ghoul::opengl::Texture>> _textureMap;
std::unordered_map<int, std::string> _textureFileMap;
std::unordered_map<int, PlaneAggregate> _planesMap;
std::string _speckFile;
std::string _labelFile;
@@ -151,8 +153,6 @@ private:
float _sluminosity = 1.f;
glm::dmat4 _transformationMatrix = glm::dmat4(1.0);
std::vector<RenderingPlane> _renderingPlanesArray;
};