为 OpenGL 实现细节层次算法
我正在尝试实现以下算法(分解为小三角形),但我在网上找不到任何正确解释它的教程, 我发现的大多数内容都从理论上解释了它,并且示例太复杂而难以理解,因为它们包含许多其他内容。
如果您能指出我是如何完成的或类似的事情,我将非常感激。
I'm trying to implement the following algorithm (breaking to painting to small triangles) but I couldn't find any tutorial over the net that explains it properly,
Most of the things I've found explain it theoretically and the samples are way too complicated to understand since they contain many other things.
If you can point me how it's done or something like that I would be more than thankful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的评论来看,您似乎正在调用 glVertex 一百万次,这意味着您正在使用已弃用的 OpenGL 函数,这可能就是您的程序运行如此缓慢的原因。
回到过去(不是我真的知道,我才 20 岁),您使用 glVertex 指定顶点,一次一个,每帧一次。这称为立即模式。这会将顶点信息每帧每个顶点传递到 OpenGL 内存(通常是显卡)一次。因此,如果你有 200k 个顶点,正如你所说,你每帧至少要执行 200k 次(如果你使用索引,你可以将其减少到恰好 200k,但是你必须传递一些其他东西)。
我怀疑所有这些顶点每一帧都在改变。我敢打赌,它们中的许多甚至全部在多个帧中都保持不变。所以你能做的就是把它们放入一个叫做 VBO(顶点缓冲对象)的东西中,这意味着你将所有这些顶点信息存储在 OpenGL 内存中(同样,如果你有的话,可能是 gfx 卡),然后你就不需要了不必每帧都传输所有这些内容。
一开始有点难以理解。但本质上调用 glVertex 一百万次就像在每一帧都说:“这是我需要你绘制的一堆信息。”你每一帧都说同样的话。使用顶点缓冲区对象就像说一次:“这里有一堆信息”,然后每帧说一次:“还记得我不久前给你的信息吗?画出来。”
这显然要快得多,因为您不必每帧都转发信息。
一个缺点是,如果您需要更改顶点,则会有点棘手,因为数据不再由您控制。在这种情况下,您必须获取 vbo 内容的内存映射并对其进行修改,而不是传递新数据。或者您始终可以删除它并重新生成它,但如果您每帧都这样做,那么在立即模式上使用 vbo 就没有意义了。
我不会发布任何有关 VBO 的代码,因为这样这篇文章的长度将比现在长 4 倍。我想我已经给了你很多关键词,你可以用谷歌搜索来找到更多信息。我建议从以下几个方面开始了解该主题(单独搜索它们):
顶点缓冲区对象、索引、固定函数管道、着色器
如果您在实现我提到的任何内容时遇到任何问题,那么我建议您提出了一个新的特定问题。
祝你好运!
From your comment, it looks like you are calling glVertex a million times which means you're using deprecated OpenGL functions, which is probably why your program is running so slowly.
Back in the day (not that I would really know, I'm 20), you specify vertices using glVertex, one at a time, once per frame. This is called the immediate mode. This passes the vertex information to OpenGL memory (usually graphics card) once per vertex per frame. So if you've got 200k vertices, as you said, you're doing this at least 200k times per frame (you can cut it down to exactly 200k if you use indices, but then you have to pass some other stuff to).
I doubt all of these vertices are changing every frame. I'd bet that many or even all of them stay the same over multiple frames. So what you can do is put them into something called a VBO (vertex buffer object), which means you store all of this vertex information in OpenGL memory (again, probably gfx card if you've got one), and then you don't have to transfer all of that stuff every frame.
It's kind of hard to wrap your head around at first. But essentially calling glVertex a million times is like saying this every frame: "Here's a bunch of information that I need you to draw." And you say the same thing every frame. Using Vertex Buffer Objects are like saying this once: "Here's a bunch of information", and then saying this once per frame: "Remember that information I gave you a while back? Draw it."
This is obviously way faster because you don't have to relay the information every frame.
One downside is that if you need to change the vertices, it's a little trickier because the data is no longer controlled by you. In this case, you'd have to get a memory map to the vbo's contents and modify it, rather than passing new data. Or you can always delete it and regenerate it, but if you do this every frame then there's no point in using vbo's over the immediate mode.
I won't post any code regarding VBO's because then this post would get 4 times longer than it already is. I think I've given you many keywords that you can google to find out more information. Here's a few that I would suggest starting with just to learn about the subject (search for them separately):
vertex buffer objects, indices, fixed function pipeline, shaders
If you have any problems implementing any of the stuff that I mentioned, then I suggest you open a new specific question.
Good luck!