diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index ddeb8b2..68be535 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -4,10 +4,10 @@ out vec4 FragColor; in vec3 color; -uniform bool drawPoints; +uniform bool drawPoints = false; void main() { - if (drawPoints){ + if (drawPoints == true){ vec2 coord = gl_PointCoord - vec2(0.5); if (length(coord) > 0.5) discard; @@ -18,5 +18,4 @@ void main() { } else { FragColor = vec4(color, 1); } - } diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 4b938ec..7b314e6 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; +uniform bool drawPoints = false; uniform float screenSizePixels; uniform float screenSizeMeters; uniform float depthOffset; @@ -14,9 +14,9 @@ uniform float depthOffset; out vec3 color; void main() { - if (drawPoints){ + if (drawPoints == true){ gl_Position = vec4(VP * vPos, 1.0); - gl_Position.z -= depthOffset * 0.25; + gl_Position.z -= depthOffset * 0.5; gl_PointSize = vMassRadius / screenSizeMeters * screenSizePixels * 2; } else { gl_Position = vec4(VP * vPos, 1.0); diff --git a/src/GLWidget.cpp b/src/GLWidget.cpp index a2eb981..7d2f11a 100644 --- a/src/GLWidget.cpp +++ b/src/GLWidget.cpp @@ -37,7 +37,7 @@ void GLWidget::initializeGL() { glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); - glDepthMask(GL_TRUE); + glClearDepth(1); glClearColor(.15, .15, .15, 1); std::vector empty; @@ -45,12 +45,12 @@ void GLWidget::initializeGL() { } void GLWidget::paintGL() { - auto p = new QPainter(this); - p->setRenderHint(QPainter::Antialiasing); - p->beginNativePainting(); + QPixmap img(size()); + auto p = new QPainter(&img); // Native OpenGL { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); program->bind(); @@ -75,7 +75,6 @@ void GLWidget::paintGL() { glBindVertexArray(0); } - p->endNativePainting(); // FPS { @@ -105,7 +104,7 @@ bool GLWidget::AnyDialogOpen() { void GLWidget::initGPUMemory(const std::vector *pendula) { - int segmentCount = 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; }, [](const Pendulum * p){ return p->X.size(); @@ -118,8 +117,9 @@ void GLWidget::initGPUMemory(const std::vector *pendula) { if (!pendula->empty()){ - float depth = 1.f - 1.f / float(segmentCount); - depthOffset = 1.f * 2 / float(segmentCount); + float margin = 0.1f; + depthOffset = 2 * (1.f - margin) / float(pointCount); + float depth = (1.f - margin) - (1.f - margin) / float(pointCount) + depthOffset; GLuint index = 0; for (const auto p : *pendula){ @@ -127,8 +127,7 @@ void GLWidget::initGPUMemory(const std::vector *pendula) { // Origin point positions.push_back(0); positions.push_back(0); - positions.push_back(depth); - depth -= depthOffset; + positions.push_back(depth -= depthOffset); colors.resize(colors.size() + 3); float * red = &colors.back() - 2; @@ -143,8 +142,7 @@ void GLWidget::initGPUMemory(const std::vector *pendula) { Vector pos = p->X[segment]; positions.push_back(float(pos.x)); positions.push_back(float(pos.y)); - positions.push_back(depth); - depth -= depthOffset; + positions.push_back(depth -= depthOffset); colors.resize(colors.size() + 3); red = &colors.back() - 2; diff --git a/src/main.cpp b/src/main.cpp index dba4834..501f7aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,15 +8,15 @@ using namespace std::chrono; int main(int argc, char* argv[]) { - QApplication app(argc, argv); - QSurfaceFormat fmt; fmt.setSamples(4); - fmt.setDepthBufferSize(24); - //fmt.setVersion(3, 3); - //fmt.setProfile(QSurfaceFormat::CoreProfile); + fmt.setDepthBufferSize(32); + fmt.setVersion(3, 3); + fmt.setProfile(QSurfaceFormat::CoreProfile); QSurfaceFormat::setDefaultFormat(fmt); + QApplication app(argc, argv); + MainWindow w; w.show();