GLSL片段位置 [英] GLSL Fragment Position

查看:78
本文介绍了GLSL片段位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我将重写问题,希望清楚.

Sorry I will rewrite the question hoping to be clear.

在我的.cpp代码中,我创建了一个四边形列表,其中一些有一个标志,在像素着色器中,我检查是否设置了该标志,如果未设置标志,则四边形变为红色例如,如果设置了标志,则我想确定每个像素的颜色,因此,如果我需要将标记的四边形的一半用红色上色,而另一半用蓝色上色,我可以简单地执行以下操作:

In my .cpp code I create a list of quads, a few of them have a flag, in the pixel shader I check if this flag is set or not, if the flag is not set, the quad gets colored in red for example, if the flag is set, I want to decide the color of every single pixel, so if I need to colour half of the flagged quad in red and the other half in blue I can simply do something like :

if coordinate in quad < something color = red
else colour = blue; 

通过这种方式,我可以得到四边形的一半为蓝色,另一半为红色,或者我可以决定将红色放置在何处或将蓝色放置在何处.

In this way I can get half of the quad colored in blue and another half colored in red, or I can decide where to put the red color or where to put the blue one.

想象一下,我有一个四边形50x50像素

Imagine I've got a quad 50x50 pixels

[frag]

if(quad.flag == 1)
  {    
    if(Pixel_coordinate.x<25 ) gl_fragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else gl_fragColor = vec4(0.0, 1.0, 0.0, 1.0);
  }
else
  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);

在这种情况下,我希望设置了标志的四边形将在每个面上获得两种颜色. 我希望我现在更加具体.

In this case I would expect that a quad with the flag set will get two colors per face. I hope I have been more specific now.

谢谢.

只需添加一些我不能使用的纹理即可.

Just to add something I can't use any texture.

好吧,我现在这样做:

每个四边形都有4个纹理,分别协调为(0,0),(0,1),(1,1),(1,0);

Every quad has 4 textures coordinated (0,0), (0,1), (1,1), (1,0);

我使用:

glTexCoordPointer(2, GL_SHORT, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 7));

[vert]

varying vec2 texCoord;

main()
{
    texCoord = gl_MultiTexCoord0.xy;
} 

[frag]

varying vec2 texCoord;

main()
{
    float x1 = texCoord.s;
    float x2 = texCoord.t;

    gl_FragColor = vec4(x1, x2, 0.0, 1.0);
}

我总是得到黄色,所以x1 = 1和x2 = 1几乎总是黄色,有些四边形是黄色/绿色.

I get always the yellow color so x1 =1 and x2 = 1 almost always and some quad is yellow/green.

我希望片段着色器中的纹理坐标发生变化,所以我应该得到一个渐变,对吗?

I would expect that the texture coordinates change in the fragment shader and so I should get a gradient, am I wrong?

推荐答案

如果您想了解四边形内 的坐标,则需要自己计算.为此,您需要创建一个新的插值器(将其称为vec2 quadCoord之类),并为每个顶点进行适当设置,这意味着您可能还需要将其添加为属性并传递给它.您的顶点着色器.例如:

If you want to know the coordinate within the quad, you need to calculate it yourself. In order to that, you'll need to create a new interpolant (call it something like vec2 quadCoord), and set it appropriately for each vertex, which means you'll likely also need to add it as an attribute and pass it through your vertex shader. eg:

// in the vertex shader
attribute vec2 quadCoordIn;
varying vec2 quadCoord;

main() {
    quadCoord = quadCoordIn;
              :

绘制四边形时,需要在绘制代码中输入此属性.对于每个四边形,顶点的quadCoordIn值可能分别为(0,0),(0,1),(1,1)和(1,0)-您可以根据需要使用其他坐标系,但这是最简单的.

You'll need to feed in this attribute in your drawing code when drawing your quads. For each quad, the vertexes will have likely have quadCoordIn values of (0,0), (0,1), (1,1) and (1,0) -- you could use some other coordinate system if you prefer, but this is the easiest.

然后,在片段程序中,您可以访问quadCoord.xy以确定您在四边形中的位置.

Then, in your fragment program, you can access quadCoord.xy to determine where in the quad you are.

这篇关于GLSL片段位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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