diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 68be535..626550d 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -4,10 +4,10 @@ out vec4 FragColor; in vec3 color; -uniform bool drawPoints = false; +uniform bool drawPoints; void main() { - if (drawPoints == true){ + if (drawPoints){ vec2 coord = gl_PointCoord - vec2(0.5); if (length(coord) > 0.5) discard; diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 7b314e6..544fdaa 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -6,7 +6,7 @@ layout (location = 2) in float vMassRadius; uniform mat3 VP; -uniform bool drawPoints = false; +uniform bool drawPoints; uniform float screenSizePixels; uniform float screenSizeMeters; uniform float depthOffset; @@ -14,12 +14,11 @@ uniform float depthOffset; out vec3 color; void main() { - if (drawPoints == true){ - gl_Position = vec4(VP * vPos, 1.0); + gl_Position = vec4(VP * vPos, 1.0); + if (drawPoints){ gl_Position.z -= depthOffset * 0.5; gl_PointSize = vMassRadius / screenSizeMeters * screenSizePixels * 2; } else { - gl_Position = vec4(VP * vPos, 1.0); color = vColor; } } diff --git a/src/GLWidget.cpp b/src/GLWidget.cpp index 7d2f11a..dca7c64 100644 --- a/src/GLWidget.cpp +++ b/src/GLWidget.cpp @@ -13,7 +13,7 @@ GLWidget::GLWidget(Simulation * simulation) : simulation(simulation) { startTimer(1000 / 144); fps = new FPS; fps->setUpdateInterval(500); - connect(simulation, &Simulation::layoutChanged, this, &GLWidget::initGPUMemory); + connect(simulation, &Simulation::layoutChanged, this, &GLWidget::uploadStaticDataToGPU); connect(simulation, &Simulation::positionChanged, this, &GLWidget::changePosition); } @@ -41,13 +41,10 @@ void GLWidget::initializeGL() { glClearColor(.15, .15, .15, 1); std::vector empty; - initGPUMemory(&empty); + uploadStaticDataToGPU(&empty); } void GLWidget::paintGL() { - QPixmap img(size()); - auto p = new QPainter(&img); - // Native OpenGL { @@ -76,6 +73,8 @@ void GLWidget::paintGL() { glBindVertexArray(0); } + QPixmap img(size()); + auto p = new QPainter(&img); // FPS { p->setPen(Qt::white); @@ -102,7 +101,7 @@ bool GLWidget::AnyDialogOpen() { return false; } -void GLWidget::initGPUMemory(const std::vector *pendula) { +void GLWidget::uploadStaticDataToGPU(const std::vector *pendula) { int pointCount = std::transform_reduce(pendula->begin(), pendula->end(), 0, [](int prev, int curr){ return prev + curr + 1; @@ -183,29 +182,23 @@ void GLWidget::initGPUMemory(const std::vector *pendula) { glBindVertexArray(0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - simulation->pendulaMutex.unlock(); } void GLWidget::changePosition(const std::vector *pendula) { glBindBuffer(GL_ARRAY_BUFFER, positionVBO); auto positions = (GLfloat*) glMapBufferRange(GL_ARRAY_BUFFER, 0, GLsizeiptr(positionCount * sizeof(float)), GL_MAP_WRITE_BIT); - if (positions){ - size_t index = 0; - for (const auto p : *pendula){ - index += 3; - for (auto x : p->X){ - positions[index++] = float(x.x); - positions[index++] = float(x.y); - index++; - } + size_t index = 0; + for (const auto p : *pendula){ + index += 3; + for (auto x : p->X){ + positions[index++] = float(x.x); + positions[index++] = float(x.y); + index++; } } - simulation->pendulaMutex.unlock(); glUnmapBuffer(GL_ARRAY_BUFFER); - glBindBuffer(GL_ARRAY_BUFFER, 0); + simulation->pendulaMutex.unlock(); } void GLWidget::resizeGL(int w, int h) { diff --git a/src/GLWidget.h b/src/GLWidget.h index 7629a57..a440bb0 100644 --- a/src/GLWidget.h +++ b/src/GLWidget.h @@ -20,7 +20,7 @@ protected: public slots: void showMassesChanged(int state); private slots: - void initGPUMemory(const std::vector *pendula); + void uploadStaticDataToGPU(const std::vector *pendula); void changePosition(const std::vector *pendula); private: QOpenGLShaderProgram * program;