首页 > 解决方案 > How do I fit a 2D scene into a QOpenGLWidget window using a shader program?

问题描述

I am trying to fit an entire 2D scene of triangles into a window. I am using a shader program to handle drawing the triangles. I only a fraction of the 900 triangles I expect to see. However, when I use the deprecated OpenGL API to draw squares in a similar scene, it works as expected.

It must have somethig to do with the MVP matrix that I am passing into the vertex shader. See the paintGL() method in the scene.cpp module. That is where I setup the glViewport and the mvpMatrix before drawing the triangles.

I have outlined what I have done below.

Tools

Setup

  1. The scene is 1M x 1M
  2. 900 triangles are added to the scene using a shader program
    • It is a grid of 30 x 30 triangles evenly distributed in the scene

Problem

Here is a screenshot of the triangles demo.

Triangles demo source:

What worked

I found that using the deprecated OpenGL API, drawing squares using GL_QUADS, does what I am looking for. However, I would like to be using shaders.

Here is a screenshot of the squares demo. It is a grid of 10 x 10 squares in a 1M x 1M scene.

Squares demo source:

标签: c++openglglslqt5shader

解决方案


我发现了这个错误。我在triangles.cpp中分配 VBO。我使用了错误的字节数。

改变:

_vertexBuffer.allocate(_vertexes, _vertexCount);

至:

_vertexBuffer.allocate(_vertexes, _vertexCount * sizeof(QVector3D));

解决了这个问题。


推荐阅读