OpenGL 渲染多个三角形条会导致间隙
我正在第一次尝试 OpenGL,使用 C++ 和 Qt 作为支持。
在相当于我在主应用程序之外实现功能的游乐场中,我试图从二维浮点数组中绘制相当于网格表面的东西。该二维数组的索引用于 x 和 z 坐标,值是 y 坐标。
目前我的实现有问题。没有形成坚固的表面,而是出现了明显的黑色间隙。
快速谷歌向我展示了http://marc.blog.at Purpose.com/2009/10/24/programatically-generate-a-rectangle-mesh-using-single-gl_triangle_strip/ 这与我的渲染方法非常相似。但是,在绘制一排三角形并在末尾开始一个新的带后,我将终止我的三角形带。
下面是一些示例调试输出,显示了我正在创建的顶点:
glBegin
drawing vertex ( 0 , 2.76401 , 3 )
drawing vertex ( 0 , 1.82236 , 4 )
drawing vertex ( 1 , 1.55657 , 3 )
drawing vertex ( 1 , 3.82013 , 4 )
drawing vertex ( 2 , 3.1032 , 3 )
drawing vertex ( 2 , 2.77563 , 4 )
drawing vertex ( 3 , 2.2032 , 3 )
drawing vertex ( 3 , 4.49317 , 4 )
drawing vertex ( 4 , 0.469515 , 3 )
drawing vertex ( 4 , 1.48398 , 4 )
drawing vertex ( 5 , 0.301526 , 3 )
drawing vertex ( 5 , 0.606281 , 4 )
drawing vertex ( 6 , 1.12292 , 3 )
drawing vertex ( 6 , 1.98806 , 4 )
glEnd/glBegin
drawing vertex ( 0 , 4.31023 , 2 )
drawing vertex ( 0 , 2.76401 , 3 )
drawing vertex ( 1 , 3.64741 , 2 )
drawing vertex ( 1 , 1.55657 , 3 )
drawing vertex ( 2 , 2.48897 , 2 )
drawing vertex ( 2 , 3.1032 , 3 )
drawing vertex ( 3 , 4.54997 , 2 )
drawing vertex ( 3 , 2.2032 , 3 )
drawing vertex ( 4 , 0.11039 , 2 )
drawing vertex ( 4 , 0.469515 , 3 )
drawing vertex ( 5 , 1.12043 , 2 )
drawing vertex ( 5 , 0.301526 , 3 )
drawing vertex ( 6 , 4.27371 , 2 )
drawing vertex ( 6 , 1.12292 , 3 )
glEnd/glBegin
drawing vertex ( 0 , 0.818045 , 1 )
drawing vertex ( 0 , 4.31023 , 2 )
drawing vertex ( 1 , 0.886993 , 1 )
drawing vertex ( 1 , 3.64741 , 2 )
drawing vertex ( 2 , 0.612948 , 1 )
drawing vertex ( 2 , 2.48897 , 2 )
drawing vertex ( 3 , 4.69348 , 1 )
drawing vertex ( 3 , 4.54997 , 2 )
drawing vertex ( 4 , 3.56293 , 1 )
drawing vertex ( 4 , 0.11039 , 2 )
drawing vertex ( 5 , 2.22925 , 1 )
drawing vertex ( 5 , 1.12043 , 2 )
drawing vertex ( 6 , 0.15616 , 1 )
drawing vertex ( 6 , 4.27371 , 2 )
glEnd/glBegin
drawing vertex ( 0 , 1.84323 , 0 )
drawing vertex ( 0 , 0.818045 , 1 )
drawing vertex ( 1 , 2.28323 , 0 )
drawing vertex ( 1 , 0.886993 , 1 )
drawing vertex ( 2 , 1.1108 , 0 )
drawing vertex ( 2 , 0.612948 , 1 )
drawing vertex ( 3 , 3.72698 , 0 )
drawing vertex ( 3 , 4.69348 , 1 )
drawing vertex ( 4 , 4.41645 , 0 )
drawing vertex ( 4 , 3.56293 , 1 )
drawing vertex ( 5 , 4.69124 , 0 )
drawing vertex ( 5 , 2.22925 , 1 )
drawing vertex ( 6 , 3.11876 , 0 )
drawing vertex ( 6 , 0.15616 , 1 )
glEnd
以及我所看到的屏幕截图(不同的顶点值):
我正在寻找任何线索或见解来了解我可能做错了什么。正如我所说,我对 OpenGL 非常陌生。我没有使用单三角带网格方法,因为我相信这会给最终应用带来问题。
更新:我已经尝试过使用我的 y 值,并注意到只有当我使用从
qrand() / (float)(RAND_MAX) * 5.0f
使用常量值 (y = 1.0f)、
y = x
或类似 y = z % 2 == 0 ? x : xMax - x 。
I am in the midst of my first foray into OpenGL using C++ and Qt as the supporting cast.
In what amounts to my playground for implementing a feature outside of the main application, I am attempting to draw what amounts to a mesh surface from a two-dimensional array of floats. The indices of this 2d array are used for the x and z coordinates and the value is the y coordinate.
Currently my implementation is bugged. Instead of creating a solid surface there are noticeable black gaps.
A quick Google showed me http://marc.blog.atpurpose.com/2009/10/24/programatically-generating-a-rectangular-mesh-using-single-gl_triangle_strip/ which is very similar to my rendering method. However, I am terminating my triangle strip after drawing one row of triangles and starting a new strip at the end.
Below is some sample debug output showing the vertices I am creating:
glBegin
drawing vertex ( 0 , 2.76401 , 3 )
drawing vertex ( 0 , 1.82236 , 4 )
drawing vertex ( 1 , 1.55657 , 3 )
drawing vertex ( 1 , 3.82013 , 4 )
drawing vertex ( 2 , 3.1032 , 3 )
drawing vertex ( 2 , 2.77563 , 4 )
drawing vertex ( 3 , 2.2032 , 3 )
drawing vertex ( 3 , 4.49317 , 4 )
drawing vertex ( 4 , 0.469515 , 3 )
drawing vertex ( 4 , 1.48398 , 4 )
drawing vertex ( 5 , 0.301526 , 3 )
drawing vertex ( 5 , 0.606281 , 4 )
drawing vertex ( 6 , 1.12292 , 3 )
drawing vertex ( 6 , 1.98806 , 4 )
glEnd/glBegin
drawing vertex ( 0 , 4.31023 , 2 )
drawing vertex ( 0 , 2.76401 , 3 )
drawing vertex ( 1 , 3.64741 , 2 )
drawing vertex ( 1 , 1.55657 , 3 )
drawing vertex ( 2 , 2.48897 , 2 )
drawing vertex ( 2 , 3.1032 , 3 )
drawing vertex ( 3 , 4.54997 , 2 )
drawing vertex ( 3 , 2.2032 , 3 )
drawing vertex ( 4 , 0.11039 , 2 )
drawing vertex ( 4 , 0.469515 , 3 )
drawing vertex ( 5 , 1.12043 , 2 )
drawing vertex ( 5 , 0.301526 , 3 )
drawing vertex ( 6 , 4.27371 , 2 )
drawing vertex ( 6 , 1.12292 , 3 )
glEnd/glBegin
drawing vertex ( 0 , 0.818045 , 1 )
drawing vertex ( 0 , 4.31023 , 2 )
drawing vertex ( 1 , 0.886993 , 1 )
drawing vertex ( 1 , 3.64741 , 2 )
drawing vertex ( 2 , 0.612948 , 1 )
drawing vertex ( 2 , 2.48897 , 2 )
drawing vertex ( 3 , 4.69348 , 1 )
drawing vertex ( 3 , 4.54997 , 2 )
drawing vertex ( 4 , 3.56293 , 1 )
drawing vertex ( 4 , 0.11039 , 2 )
drawing vertex ( 5 , 2.22925 , 1 )
drawing vertex ( 5 , 1.12043 , 2 )
drawing vertex ( 6 , 0.15616 , 1 )
drawing vertex ( 6 , 4.27371 , 2 )
glEnd/glBegin
drawing vertex ( 0 , 1.84323 , 0 )
drawing vertex ( 0 , 0.818045 , 1 )
drawing vertex ( 1 , 2.28323 , 0 )
drawing vertex ( 1 , 0.886993 , 1 )
drawing vertex ( 2 , 1.1108 , 0 )
drawing vertex ( 2 , 0.612948 , 1 )
drawing vertex ( 3 , 3.72698 , 0 )
drawing vertex ( 3 , 4.69348 , 1 )
drawing vertex ( 4 , 4.41645 , 0 )
drawing vertex ( 4 , 3.56293 , 1 )
drawing vertex ( 5 , 4.69124 , 0 )
drawing vertex ( 5 , 2.22925 , 1 )
drawing vertex ( 6 , 3.11876 , 0 )
drawing vertex ( 6 , 0.15616 , 1 )
glEnd
And a screenshot of what I am seeing (different vertex values):
I am looking for any clues or insights into what I may be doing wrong. As I stated, I am very new to OpenGL. I am not using the single triangle strip mesh method because I believe it will prove problematic for the final application.
Update: I have played around with my y-values and noticed that rendering only seems problematic when I am using random numbers for y generated from
qrand() / (float)(RAND_MAX) * 5.0f
When using constant values (y = 1.0f
), y = x
, or something like y = z % 2 == 0 ? x : xMax - x
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
猜测:
你启用了面部剔除功能吗?向 openGL 发送顶点的顺序决定三角形是正面还是背面。您可以使用 glFrontFace 控制将哪个方向 CW 或 CCW(CCW 是默认值)视为正面。
尝试禁用面部剔除; glDisable(GL_CULL_FACE);看看它是否可以修复您的形象。
如果这确实是您的问题,您应该保持启用面剔除,但修复将顶点发送到 openGL 的顺序。
A guess:
Do you have face culling enabled? The order that you send vertices to openGL determine if the triangle is a front face or a back face. You can control which direction, CW or CCW (CCW is the default) is considered the front face using glFrontFace.
Try disabling face culling; glDisable(GL_CULL_FACE); and see if it fixes your image.
If that does turn out to be your problem, you should keep face culling enabled, but fix the order you send your vertices to openGL.
对我来说,自从 openGl 以来已经有一段时间了(然后,它是 iPod 上的 OpenGL-ez)。但渲染看起来条带不在 X、Z 方向上的规则网格上。我会减小 Y 的范围(甚至将其设置为 0)来测试您的三角形条带。
另外,所有内容都以浮动形式发送吗?我有点怀疑调试输出显示 1,2 等,而不是 1.0、2.0。
It's been a while since openGl for me (and then, it was OpenGL-ez on iPod). But the render looks like the strips are not on a regular grid in X,Z. I would reduce the range of the Y (or even set it to 0) to test your triangle strips.
Also, is everything sent as float? I am kinda suspicious of the debug output showing 1,2, etc and not 1.0, 2.0.
我最终放弃了渲染并使用了一个提供我需要的库(一旦我让它工作)。对于那些感兴趣的人来说,该库是QwtPlot3D。它需要一些严重的性能改进来满足我的需求,但它让我绕过了这个最初的障碍。
I ended up scrapping my rendering and using a library that provided what I needed (once I got it working). The library, for those interested, is QwtPlot3D. It needs some serious performance improvements for my needs, but it has allowed me to bypass this initial hurdle.