OpenGL ES 2.0 多个程序或多着色器还是什么?它是如何运作的?

发布于 2025-01-03 22:55:42 字数 1380 浏览 2 评论 0原文

问题 (TL;DR)

我的问题从根本上来说是我不知道 OpenGL ES 2.0 期望我如何编写和使用多个着色器;或者如果甚至建议/期望一个人会这样做。

这里的基本问题是:如果我有一个苹果、一块发光的岩石和一个模糊网格,它们都在同一个 3D 世界中,最好用不同的着色器程序绘制,但使用相同的 mvpMatrix,那么我将如何在相同的 OpenGL 渲染,以便它们都使用我编写的最合适的着色器?

我做了什么

所以我为我的 Android 游戏编写了一个基本的 OpenGL ES 2.0 程序,该程序运行良好,可以在屏幕上绘制对象的轮廓。但它没有别的作用;几乎是因为着色器看起来像这样:

顶点着色器

uniform mat4 uMVPMatrix;
attribute vec4 aPosition;

void main() {
    gl_Position = uMVPMatrix * aPosition;
}

片段着色器

void main() {
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

现在它们非常基本。我没有更进一步的原因是因为我无法弄清楚是否应该编写一个着色器来应用于所有不同的对象,或者是否应该使用多个着色器。如果我应该使用多个着色器来绘制多个不同的对象,那么我该如何有效地做到这一点?

我觉得这对任何日复一日使用 OpenGL ES 2.0 的人来说都是基本知识,所以我希望有人能回答我的问题或为我指出正确的方向。

我:

  • 看过多个教程;除了最基本的着色器之外,它们都不使用任何东西。
  • 阅读整个 OpenGL ES 2.0 GLSL 规范(其中都没有提到它的用途;它只是关于一切的作用,而不是它们如何组合在一起)。
  • 尝试稍微修改我的着色器。

所以我希望我已经接近了解 OpenGL 工作流程,但我似乎还没有做到这一点。

编辑:我后来发现这一点很好:

如果您的应用程序是为 OpenGL ES 2.0 编写的,请不要创建具有大量开关和条件的单个着色器来执行应用程序渲染场景所需的每项任务。相反,编译多个着色器程序,每个程序执行一个特定的、集中的任务。

这是来自 iOS OpenGL ES 2.0 指南。

The Problem (TL;DR)

My problem, fundamentally, is that I do not know how OpenGL ES 2.0 expects me to write and use multiple shaders; or if it is even advisable/expected that a person will do so.

The fundamental question here is: if I have an apple, a glowing rock and a fuzzy mesh, all in the same 3D world, all best drawn with different shader programs but using the same mvpMatrix then how would I go about using all of them in the same OpenGL render such that they all use their most appropriate shaders that I have written?

What Have I done

So I have written a basic OpenGL ES 2.0 program for my Android Game that works perfectly in that it can draw the outline of the objects to the screen. But it does nothing else; pretty much because the shaders look like this:

Vertex Shader

uniform mat4 uMVPMatrix;
attribute vec4 aPosition;

void main() {
    gl_Position = uMVPMatrix * aPosition;
}

Fragment Shader

void main() {
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

Now they are pretty basic. The reason that I have not gone further is because I cannot figure out if I am supposed to write one shader to apply to all of my different objects or if I am supposed to use multiple shaders. And if I am supposed to use multiple shaders to draw multiple different objects then how do I go about doing that in an efficient way?

I get the feeling that this must be basic knowledge to anybody that does OpenGL ES 2.0 day in and day out so I am hoping that somebody can answer my question or point me in the right direction.

I have:

  • Looked at multiple tutorials; none of which use anything but the most basic shaders.
  • Read the entire OpenGL ES 2.0 GLSL Spec (none of which mentioned how it was intended to be used; it was just about what everything did rather than how it fits together).
  • Tried to modify my shaders a bit.

So I'm hoping that I am close to understanding the OpenGL workflow but I don't seem to be there yet.

Edit: I found this well afterwards:

If your application is written for OpenGL ES 2.0, do not create a single shader with lots of switches and conditionals that performs every task your application needs to render the scene. Instead, compile multiple shader programs that each perform a specific, focused task.

That is from the iOS OpenGL ES 2.0 guidelines.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2025-01-10 22:55:42

您可以使用多个着色器,但在它们之间切换可能会非常昂贵,因此建议的做法是绘制着色器的每个对象,然后切换到下一个着色器并使用该着色器绘制所有对象,依此类推。
要在着色器之间切换,请调用 glUseProgram()

You can use multiple shaders, but to switch between them can be quite costly so the recommended practise is to draw every object of a shader, then switch to the next shader and draw all the objects using that one and so on.
To switch between shaders, you call glUseProgram().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文