minor optimizations

main
Benjamin Kraft 1 year ago
parent 71474f3cb6
commit b62c326894
  1. 4
      shaders/fragment.glsl
  2. 7
      shaders/vertex.glsl
  3. 33
      src/GLWidget.cpp
  4. 2
      src/GLWidget.h

@ -4,10 +4,10 @@ out vec4 FragColor;
in vec3 color; in vec3 color;
uniform bool drawPoints = false; uniform bool drawPoints;
void main() { void main() {
if (drawPoints == true){ if (drawPoints){
vec2 coord = gl_PointCoord - vec2(0.5); vec2 coord = gl_PointCoord - vec2(0.5);
if (length(coord) > 0.5) if (length(coord) > 0.5)
discard; discard;

@ -6,7 +6,7 @@ layout (location = 2) in float vMassRadius;
uniform mat3 VP; uniform mat3 VP;
uniform bool drawPoints = false; uniform bool drawPoints;
uniform float screenSizePixels; uniform float screenSizePixels;
uniform float screenSizeMeters; uniform float screenSizeMeters;
uniform float depthOffset; uniform float depthOffset;
@ -14,12 +14,11 @@ uniform float depthOffset;
out vec3 color; out vec3 color;
void main() { 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_Position.z -= depthOffset * 0.5;
gl_PointSize = vMassRadius / screenSizeMeters * screenSizePixels * 2; gl_PointSize = vMassRadius / screenSizeMeters * screenSizePixels * 2;
} else { } else {
gl_Position = vec4(VP * vPos, 1.0);
color = vColor; color = vColor;
} }
} }

@ -13,7 +13,7 @@ GLWidget::GLWidget(Simulation * simulation) : simulation(simulation) {
startTimer(1000 / 144); startTimer(1000 / 144);
fps = new FPS; fps = new FPS;
fps->setUpdateInterval(500); fps->setUpdateInterval(500);
connect(simulation, &Simulation::layoutChanged, this, &GLWidget::initGPUMemory); connect(simulation, &Simulation::layoutChanged, this, &GLWidget::uploadStaticDataToGPU);
connect(simulation, &Simulation::positionChanged, this, &GLWidget::changePosition); connect(simulation, &Simulation::positionChanged, this, &GLWidget::changePosition);
} }
@ -41,13 +41,10 @@ void GLWidget::initializeGL() {
glClearColor(.15, .15, .15, 1); glClearColor(.15, .15, .15, 1);
std::vector<Pendulum*> empty; std::vector<Pendulum*> empty;
initGPUMemory(&empty); uploadStaticDataToGPU(&empty);
} }
void GLWidget::paintGL() { void GLWidget::paintGL() {
QPixmap img(size());
auto p = new QPainter(&img);
// Native OpenGL // Native OpenGL
{ {
@ -76,6 +73,8 @@ void GLWidget::paintGL() {
glBindVertexArray(0); glBindVertexArray(0);
} }
QPixmap img(size());
auto p = new QPainter(&img);
// FPS // FPS
{ {
p->setPen(Qt::white); p->setPen(Qt::white);
@ -102,7 +101,7 @@ bool GLWidget::AnyDialogOpen() {
return false; return false;
} }
void GLWidget::initGPUMemory(const std::vector<Pendulum *> *pendula) { void GLWidget::uploadStaticDataToGPU(const std::vector<Pendulum *> *pendula) {
int pointCount = std::transform_reduce(pendula->begin(), pendula->end(), 0, [](int prev, int curr){ int pointCount = std::transform_reduce(pendula->begin(), pendula->end(), 0, [](int prev, int curr){
return prev + curr + 1; return prev + curr + 1;
@ -183,29 +182,23 @@ void GLWidget::initGPUMemory(const std::vector<Pendulum *> *pendula) {
glBindVertexArray(0); glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
simulation->pendulaMutex.unlock(); simulation->pendulaMutex.unlock();
} }
void GLWidget::changePosition(const std::vector<Pendulum *> *pendula) { void GLWidget::changePosition(const std::vector<Pendulum *> *pendula) {
glBindBuffer(GL_ARRAY_BUFFER, positionVBO); glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
auto positions = (GLfloat*) glMapBufferRange(GL_ARRAY_BUFFER, 0, GLsizeiptr(positionCount * sizeof(float)), GL_MAP_WRITE_BIT); auto positions = (GLfloat*) glMapBufferRange(GL_ARRAY_BUFFER, 0, GLsizeiptr(positionCount * sizeof(float)), GL_MAP_WRITE_BIT);
if (positions){ size_t index = 0;
size_t index = 0; for (const auto p : *pendula){
for (const auto p : *pendula){ index += 3;
index += 3; for (auto x : p->X){
for (auto x : p->X){ positions[index++] = float(x.x);
positions[index++] = float(x.x); positions[index++] = float(x.y);
positions[index++] = float(x.y); index++;
index++;
}
} }
} }
simulation->pendulaMutex.unlock();
glUnmapBuffer(GL_ARRAY_BUFFER); glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0); simulation->pendulaMutex.unlock();
} }
void GLWidget::resizeGL(int w, int h) { void GLWidget::resizeGL(int w, int h) {

@ -20,7 +20,7 @@ protected:
public slots: public slots:
void showMassesChanged(int state); void showMassesChanged(int state);
private slots: private slots:
void initGPUMemory(const std::vector<Pendulum *> *pendula); void uploadStaticDataToGPU(const std::vector<Pendulum *> *pendula);
void changePosition(const std::vector<Pendulum *> *pendula); void changePosition(const std::vector<Pendulum *> *pendula);
private: private:
QOpenGLShaderProgram * program; QOpenGLShaderProgram * program;

Loading…
Cancel
Save