fixed sorting, wont use qpainter(this), need to use pixmap with texture sampling

main
Benjamin Kraft 1 year ago
parent c2f109e6a8
commit 71474f3cb6
  1. 5
      shaders/fragment.glsl
  2. 6
      shaders/vertex.glsl
  3. 22
      src/GLWidget.cpp
  4. 10
      src/main.cpp

@ -4,10 +4,10 @@ out vec4 FragColor;
in vec3 color; in vec3 color;
uniform bool drawPoints; uniform bool drawPoints = false;
void main() { void main() {
if (drawPoints){ if (drawPoints == true){
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;
@ -18,5 +18,4 @@ void main() {
} else { } else {
FragColor = vec4(color, 1); FragColor = vec4(color, 1);
} }
} }

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

@ -37,7 +37,7 @@ void GLWidget::initializeGL() {
glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX); glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE); glClearDepth(1);
glClearColor(.15, .15, .15, 1); glClearColor(.15, .15, .15, 1);
std::vector<Pendulum*> empty; std::vector<Pendulum*> empty;
@ -45,12 +45,12 @@ void GLWidget::initializeGL() {
} }
void GLWidget::paintGL() { void GLWidget::paintGL() {
auto p = new QPainter(this); QPixmap img(size());
p->setRenderHint(QPainter::Antialiasing); auto p = new QPainter(&img);
p->beginNativePainting();
// Native OpenGL // Native OpenGL
{ {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program->bind(); program->bind();
@ -75,7 +75,6 @@ void GLWidget::paintGL() {
glBindVertexArray(0); glBindVertexArray(0);
} }
p->endNativePainting();
// FPS // FPS
{ {
@ -105,7 +104,7 @@ bool GLWidget::AnyDialogOpen() {
void GLWidget::initGPUMemory(const std::vector<Pendulum *> *pendula) { void GLWidget::initGPUMemory(const std::vector<Pendulum *> *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; return prev + curr + 1;
}, [](const Pendulum * p){ }, [](const Pendulum * p){
return p->X.size(); return p->X.size();
@ -118,8 +117,9 @@ void GLWidget::initGPUMemory(const std::vector<Pendulum *> *pendula) {
if (!pendula->empty()){ if (!pendula->empty()){
float depth = 1.f - 1.f / float(segmentCount); float margin = 0.1f;
depthOffset = 1.f * 2 / float(segmentCount); depthOffset = 2 * (1.f - margin) / float(pointCount);
float depth = (1.f - margin) - (1.f - margin) / float(pointCount) + depthOffset;
GLuint index = 0; GLuint index = 0;
for (const auto p : *pendula){ for (const auto p : *pendula){
@ -127,8 +127,7 @@ void GLWidget::initGPUMemory(const std::vector<Pendulum *> *pendula) {
// Origin point // Origin point
positions.push_back(0); positions.push_back(0);
positions.push_back(0); positions.push_back(0);
positions.push_back(depth); positions.push_back(depth -= depthOffset);
depth -= depthOffset;
colors.resize(colors.size() + 3); colors.resize(colors.size() + 3);
float * red = &colors.back() - 2; float * red = &colors.back() - 2;
@ -143,8 +142,7 @@ void GLWidget::initGPUMemory(const std::vector<Pendulum *> *pendula) {
Vector pos = p->X[segment]; Vector pos = p->X[segment];
positions.push_back(float(pos.x)); positions.push_back(float(pos.x));
positions.push_back(float(pos.y)); positions.push_back(float(pos.y));
positions.push_back(depth); positions.push_back(depth -= depthOffset);
depth -= depthOffset;
colors.resize(colors.size() + 3); colors.resize(colors.size() + 3);
red = &colors.back() - 2; red = &colors.back() - 2;

@ -8,15 +8,15 @@
using namespace std::chrono; using namespace std::chrono;
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QSurfaceFormat fmt; QSurfaceFormat fmt;
fmt.setSamples(4); fmt.setSamples(4);
fmt.setDepthBufferSize(24); fmt.setDepthBufferSize(32);
//fmt.setVersion(3, 3); fmt.setVersion(3, 3);
//fmt.setProfile(QSurfaceFormat::CoreProfile); fmt.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(fmt); QSurfaceFormat::setDefaultFormat(fmt);
QApplication app(argc, argv);
MainWindow w; MainWindow w;
w.show(); w.show();

Loading…
Cancel
Save