c++ - 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
- Linux
- Qt 5.11.1
- OpenGL ES
Setup
- The scene is 1M x 1M
- 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
- An attempt was made to fit the entire scene inside the window.
- The result was that only 75 out of the 900 triangles are visible in the window.
Here is a screenshot of the triangles demo.
Triangles demo source:
- The scene.cpp module contains the
paintGL
method. - The triangles.cpp module is where the shader program is setup and the triangles are constructed.
- The full source to the triangles demo is on github.
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:
- The full source to the squares demo is on github.
解决方案
我发现了这个错误。我在triangles.cpp中分配 VBO。我使用了错误的字节数。
改变:
_vertexBuffer.allocate(_vertexes, _vertexCount);
至:
_vertexBuffer.allocate(_vertexes, _vertexCount * sizeof(QVector3D));
解决了这个问题。
推荐阅读
- jsonnet - 如何正确格式化此 jsonnet 数组输出?
- r - 为什么 my.function$ 不打开我的返回值以在 R 中选择
- javafx - 如何在 JavaFX 的 Anchorpane 中找到 VBox 的 XY 坐标?
- java - 无法为每个循环使用整数类型
- jquery - 如何使用 jquery 获取 div 的样式
- python - 如何解决 AttributeError:模块 'tensorflow.compat.v2' 没有属性 'py_func'
- javascript - 未捕获的 TypeError:children 不是 react.js 中 Draggable 的函数
- ios - UIView.animate 将矩形从左下角扩展到右上角,而不是垂直向上
- azure - 需要在对 Terraform 服务主体的整个订阅中授予 Azure RBAC 角色“ConfigureFactoryRepo”以启动数据工厂
- firebase - 如何在 Flutter 中为 Firebase 执行此操作