如何在 OpenGL ES 中绘制纯色曲面?

发布于 2024-11-25 23:09:31 字数 753 浏览 2 评论 0原文

我有一个由多个三角形面定义的曲面。整个表面为单色。

到目前为止,我正在分别绘制每个三角形。对于每个三角形,我传递了 3 个顶点、3 个索引、3 个颜色值(相同颜色)和 3 个法线值(全部等于三角形的表面法线)。然后我会将所有这些数组推送到 VBO 并渲染曲面。

最近我意识到,与纹理渲染方法相比,这是相当浪费的。现在,我用 2x2 像素阵列创建纹理(所有颜色与我想要的曲面颜色相同)。然后我计算曲面上所有顶点的纹理坐标 (s,t)。因此,我有效地拉伸 2x2 像素纹理以适应我的曲面。对于某些表面来说,这非常有效。此外,这还需要更小的缓冲区(因为现在顶点只有单个法线值和颜色值,因此只需提及一次)。然而,通过这种方法,有时曲面根本不渲染。我的怀疑在于我计算纹理坐标的方式。如果表面稍微弯曲是可以的,但是当表面绕轴旋转并形成闭合边界时,纹理坐标似乎会混淆显卡。

我的问题是,OpenGL ES 中使用单一颜色绘制表面的推荐方法是什么?

更新 - 接受答案的说明

我花了一段时间才理解 genpfault 的答案是如何工作的。所以这里有一个解释。上面我描述了绘制纯色表面的两种方法 - 1. 使用 rgba 颜色值,2. 使用单色纹理。 genpfault 的答案解释了如何有效地执行方法 1。

我通过多次传递每个顶点相同的 rgba 颜色值来实现第一种方法。因此,我在顶点着色器代码中读取此颜色值作为属性。在genpfault的方法中,我们在调用drawArray/Elements之前将颜色设置为统一变量。这将需要更改顶点着色器以从统一变量而不是属性中读取值。

I have a curved surface defined by a number of triangular faces. The entire surface has single color.

So far I was drawing each triangle separately. For each triangle I was passing 3 vertices, 3 indices, 3 color values (of same color) and 3 normal values (all equal to surface normal of triangle). Then I would push all these arrays to VBO and render the curved surface.

Recently I realized this is pretty wasteful compared to the texture method of rendering. Now I create a texture out of a 2x2 array of pixels (all of the same color that I want for the curved surface). Then I calculate texture coordinates (s,t) for all the vertices on the curved surface. Thus I effectively stretch the 2x2 pixel texture to fit to my curved surface. This works pretty OK, for some surfaces. Also this requires much smaller size buffers (because now the vertices have only single normal and color value, therefore they have to be mentioned only once). However by this method, sometimes the curved surface doesn't render at all. My suspicion is in the way I calculate the texture coordinates. It's alright if a surface is slightly curved, but when the surface revolves around an axis and forms a closed boundary then the texture coordinates seem to confuse the graphic card.

My question is, what's a recommended way in OpenGL ES, of drawing surfaces with a single color?

UPDATE - Explanation on accepting the answer

It took me a while to understand how genpfault's answer will work. So here's an explanation. Above I have described two methods of drawing plain colored surfaces - 1. using rgba color values, 2. using textures that have single color. genpfault's answer explains how to do method 1 efficiently.

I had implemented first method by passing the same rgba color values per vertex multiple times. Therefore I was reading this color value in the vertex shader code as an attribute. In genpfault's method, we set the color as a uniform variable before calling drawArray/Elements. This will require change in vertex shader to read the value from a uniform variable, instead of an attribute.

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

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

发布评论

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

评论(1

清风挽心 2024-12-02 23:09:31

只需通过 glColor() 在渲染每个补丁之前。

这样您就不必传入每个顶点的颜色分量。

Just set the current color via glColor() before rendering each patch.

That way you don't have to pass in per-vertex color components.

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