如何在iOS OpenGL ES 2.0中画星 [英] How to draw a star in iOS OpenGL ES 2.0

查看:92
本文介绍了如何在iOS OpenGL ES 2.0中画星的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我之前搜索过的,但是几年前才问过.答案总是使用纹理映射,但是我真正想做的是将恒星表示为单个顶点-您可能认为我正在使用一种简单的方法来解决问题,但实际上,单点光源实际上看起来还不错和现实.但是我想用高斯模糊之类的东西来处理那个光点,在放大或寻找更明亮的恒星时也要给它更多一点的身体.我本来要对高斯模糊图像进行纹理贴图,但是如果我对事情的理解正确,那么我将不得不用4个顶点绘制每个星星.也许没有那么困难,但是如果我只能处理一个顶点,我就不想去那里.顶点着色器会这样做吗? GLKBaseEffects可以带我到那里吗?有什么建议吗?

This question has been asked before but the quite a few years ago in my searches. The answer was always to use texture mapping but what I really want to do is represent the star as a single vertex - you may think I'm copping out with a simplistic method but in fact, a single point source of light actually looks pretty good and realistic. But I want to process that point of light with something like a gaussian blur too give it a little more body when zooming in or for brighter stars. I was going to texture map a gaussian blur image but if I understand things correctly I would then have to draw each star with 4 vertexes. Maybe not so difficult but I don't want to go there if I can just process a single vertex. Would a vertex-shader do this? Can GLKBaseEffects get me there? Any suggestions?

谢谢.

推荐答案

您可以使用点精灵.

您使用包含星星图像的纹理,并使用典型设置绑定纹理,将其绑定到着色器中均匀的采样器等.

You use a texture containing the image of the star, and use the typical setup to bind a texture, bind it to a sampler uniform in the shader, etc.

您为每个星星绘制一个顶点,并以GL_POINTS作为基本类型作为第一个参数传递给glDrawArrays()/glDrawElements().不需要纹理坐标.

You draw a single vertex for each star, with GL_POINTS as the primitive type passed as the first argument to glDrawArrays()/glDrawElements(). No texture coordinates are needed.

在顶点着色器中,您可以照常变换顶点,并设置内置的gl_PointSize变量:

In the vertex shader, you transform the vertex as you normally would, and also set the built-in gl_PointSize variable:

uniform float PointSize;
attribute vec4 Position;

void main() {
    gl_Position = ...;  // Transform Position attribute;
    gl_PointSize = PointSize;
}

在该示例中,我为点大小使用了统一的大小,这意味着所有恒星的大小将相同.根据所需的效果,您还可以根据距离计算大小,或使用其他顶点属性为每颗恒星指定不同的大小.

For the example, I used a uniform for the point size, which means that all stars will have the same size. Depending on the desired effect, you could also calculate the size based on the distance, or use an additional vertex attribute to specify a different size for each star.

在片段着色器中,您现在可以访问内置的gl_PointCoord变量以获取点精灵内片段的相对坐标.如果您的点精灵是简单的纹理图像,则可以直接将其用作纹理坐标.

In the fragment shader, you can now access the built-in gl_PointCoord variable to get the relative coordinates of the fragment within the point sprite. If your point sprite is a simple texture image, you can use it directly as the texture coordinates.

uniform sampler2D SpriteTex;

void main() {
    gl_FragColor = texture2D(SpriteTex, gl_PointCoord);
}

其他材料

我在这里回答了一个类似的问题:在现代OpenGL中渲染大圆形点.由于它是用于桌面OpenGL的,而不是用于纹理Sprite的,因此这似乎值得一个单独的答案.但是其中一些步骤是共享的,可能在其他答案中有更详细的说明.

Additional Material

I answered a somewhat similar question here: Render large circular points in modern OpenGL. Since it was for desktop OpenGL, and not for a textured sprite, this seemed worth a separate answer. But some of the steps are shared, and might be explained in more detail in the other answer.

这篇关于如何在iOS OpenGL ES 2.0中画星的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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