OpenGL大项目,VAO等 [英] OpenGL big projects, VAO-s and more

查看:234
本文介绍了OpenGL大项目,VAO等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在 https://open.gl/ 学习OpenGL 3.3,我真的很困惑一些东西。

So I've been learning OpenGL 3.3 on https://open.gl/ and I got really confused about some stuff.


  1. VAO。根据我的理解,它们用于存储 glVertexAttribPointer 调用。

  2. VBO-s。他们存储vertecies。所以如果我正在做的东西与多个对象,我需要一个VBO为每个对象?

  3. 着色器程序 - 为什么我们需要多个,它们做什么?

  4. 这行代码是什么:glBindFragDataLocation(shaderProgram,0 ,outColor);

  1. VAO-s. By my understanding they are used to store the glVertexAttribPointer calls.
  2. VBO-s. They store vertecies. So if I am making something with multiple objects do I need a VBO for every object?
  3. Shader Programs - Why do we need multiple ones and what exactly do they do ?
  4. What exactly does this line do : glBindFragDataLocation(shaderProgram, 0, "outColor");

最重要的是所有这些都适合大程序吗?为什么使用VAO-s?大多数教程只是覆盖刚刚绘制一个立方体或2的硬编码顶点的东西,那么如何管理具有很多对象的场景?我阅读了线程,并得到了一点了解如何场景管理发生,所有,但仍然我不能弄清楚如何连接OpenGL的东西,这一切。

The most important thing is how does all of this fit into a big program? For what exactly are used the VAO-s? Most tutorials just cover the things just to drawing a cube or 2 with hard coded vertices, so how would one go to managing scenes with a lot of objects? I've read this thread and got a little bit of understanding on how the scene management happens and all but still I can't figure out how to connect the OpenGL stuff to it all.

推荐答案

1 - 是。 VAOs 存储顶点数组绑定。当你看到你正在做许多调用来启用,禁用和改变GPU状态时,你可以在程序的某个早期做所有这些调用,然后使用VAO来获取一个快照,什么是绑定和什么不是,在那个时间点。稍后,在你的实际绘制调用期间,你所需要做的是再次绑定 VAO 将所有顶点状态设置为它们当时的状态。就像 VBOs 比立即模式更快,因为他们立即发送所有顶点, VAOs 通过改变许多顶点

1-Yes. VAOs store vertex array bindings in general. When you see that you're doing lots of calls that does enabling, disabling and changing of GPU states, you can do all that at some early point in the program and then use VAOs to take a "snapshot" ,of what is bound and what isn't, at that point in time. Later, during your actual draw calls, all you need to do is bind that VAO again to set all the vertex states to what they were then. Just like how VBOs are faster that immediate mode because they send all vertices at once, VAOs work faster by changing many vertex states at once.

2-VBO只是另一种发送您的 glPosition glColor ..等坐标到GPU在屏幕上呈现。这个想法是,不同于立即模式,其中您通过 gl * Attribute * 调用逐个发送顶点数据,是预先将所有顶点上传到GPU;检索其位置作为ID。在渲染时,你只需要指向GPU(你将 VBO id绑定到 GL_ARRAY_BUFFER ,并使用 glVertexAttribPointer 指定如何存储顶点数据的详细信息)到该位置并发出您的订单进行渲染。

2-VBOs are just another way to send your glPosition, glColor..etc coordinates to the GPU to render on screen. The idea is, unlike with immediate mode where you send your vertex data one by one with the gl*Attribute* calls, is to upload all your vertices to the GPU in advance and retrieve their location as an ID. At time of rendering, you're only going to point the GPU (you bind the VBO id to something like GL_ARRAY_BUFFER, and use glVertexAttribPointer to specify details of how you stored the vertices data) to that location and issue your order to render. That obviously saves lots of time by doing things overhead, and so it's much faster.

至于是否应该有一个 VBO 每个对象或者甚至一个 VBO 对于所有的对象是由程序员和他们想要呈现的对象的结构。毕竟, VBOs 自己只是一堆存储在GPU中的数据,你告诉计算机如何使用 glVertexAttribPointer 调用。

As for whether one should have one VBO per object or even one VBO for all the objects is up to the programmer and the structure of the objects they want to render. After all, VBOs themselves are just a bunch of data you stored in the GPU, and you tell the computer how they're arranged using the glVertexAttribPointer calls.

3着色器用于定义一个管道 - 一个例程 - 顶点,颜色,法线它们已经被发送到GPU,直到它们被呈现为屏幕上的片段或像素。当您将顶点发送到GPU时,它们通常仍然是3D坐标,但屏幕是一张2D图像像素。仍然有根据ProjectionModelView矩阵(顶点着色器的作业)重新定位这些顶点,然后将3D几何(几何着色器)平面化或光栅化为2D平面的过程。然后,它着色平坦的2D场景(片段着色器),最后照亮屏幕上的像素。在OpenGL版本1.5核心及以下,你对这些阶段没有太多的控制,因为它们都是固定(因此术语固定管道) 。只要想想你可以在这些着色阶段做什么,你会看到有很多真棒的东西,你可以做他们。例如,在片段着色器中,在将片段颜色发送到GPU之前,取消颜色的符号,并添加1以使用该着色器呈现的对象的颜色反转!

3-Shaders are used to define a pipeline - a routine - of what happens to the vertices, colors, normals..etc after they've been sent to the GPU until they're rendered as fragments or pixels on the screen. When you send vertices over to the GPU, they're often still 3D coordinates, but the screen is a 2D sheet of pixels. There still comes the process of re-positioning these vertices according to the ProjectionModelView matrices (job of vertex shader) and then "flattening" or rasterizing the 3D geometry (geometry shader) into a 2D plane. Then it follows with coloring the flattened 2D scene (fragment shader) and finally lighting the pixels on your screen accordingly. In OpenGL versions 1.5 core and below, you didn't have much control over those stages as it was all fixed (hence the term fixed pipeline). Just think about what you could do in any of these shader stages and you will see that there is a lot of awesome things you can do with them. For example, in the fragment shader, just before you send the fragment color to the GPU, negate the sign of the color and add 1 to have colors of objects rendered with that shader inverted!

至于需要使用多少着色器,由程序员决定是否拥有多个着色器。他们可以将他们需要的所有功能合并到一个大的着色器(uber shader)中,并用布尔 uniforms (通常被认为是坏习惯)打开和关闭这些功能,或者让每个着色器根据需要绑定正确的一个。

As for how many shaders one needs to use, again, it's up to the programmer to decide whether to have many or not. They could merge all the functionalities they need into one big giant shader (uber shader) and switch these functionalities on and off with boolean uniforms (very often considered as a bad practice), or have every shader do a certain thing and bind the right one according to what they need.


这行代码是什么:
glBindFragDataLocation(shaderProgram,0,outColor);

意味着在片段着色器执行结束时在 out 声明的变量outColor中存储的任何内容将作为最终主片段颜色发送到GPU。

It means that whatever is stored in the out declared variable "outColor" at the end of the fragment shader execution will be sent to the GPU as the final primary fragment color.


最重要的是,所有这些都适合大的
程序吗?为什么使用VAO-s?大多数教程只是
包括只是绘制一个立方体的东西或2与硬编码的顶点,
,那么如何去管理与很多对象的场景?我有
阅读这个线程,并有一点了解如何
场景管理发生,所有,但仍然我无法弄清楚如何
连接OpenGL的东西,它所有。

The most important thing is how does all of this fit into a big program? For what exactly are used the VAO-s? Most tutorials just cover the things just to drawing a cube or 2 with hard coded vertices, so how would one go to managing scenes with a lot of objects? I've read this thread and got a little bit of understanding on how the scene management happens and all but still I can't figure out how to connect the OpenGL stuff to it all.

他们都在一起工作,在屏幕上绘制漂亮的彩色形状。 VBO s是存储场景顶点的结构(都以丑陋方式对齐), VertexAttribPointer 调用告诉GPU如何安排 VBO 中的数据, VAO s来存储所有这些 VertexAttribPointer 提前发送指令,并在主循环中渲染期间立即发送所有指令,并在渲染过程中给予更多的控制权。

They all work together to draw your nice colored shapes on the screen. VBOs are the structures where the vertices of your scene are stored (all aligned in an ugly fashion), VertexAttribPointer calls to tell the GPU how the data in the VBO is arranged, VAOs to store all these VertexAttribPointer instructions ahead of time and send them all at once with simply binding one during rendering in your main loop, and shaders to give you more control during the process of drawing your scene on the screen.

这一切首先听起来不可思议,但练习你会习惯。

All of this can sound overwhelming at first, but with practice you will get used to it.

这篇关于OpenGL大项目,VAO等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆