OpenGL片段着色器VS DirectX片段着色器

时间:2013-01-16 18:09:46

标签: opengl directx shader

在DirectX中我知道我可以做这样的事情。

struct GBufferVertexOut
{
    float4 position     : POSITION0;
    float4 normal       : TEXCOORD0;
    float2 texCoord     : TEXCOORD1;
};

GBufferFragOut GBuffersFS(GBufferVertexOut inVert)
{
    GBufferFragOut buffOut;
    buffOut.normal = normalize(inVert.normal);
    buffOut.normal = ( buffOut.normal + 1 ) * .5;
    buffOut.diffuse = tex2D( diffuseSampler, inVert.texCoord );
    buffOut.diffuse.a = tex2D( ambientSampler, inVert.texCoord ).r;
    buffOut.specular = float4( 1, 1, 1, 1 );

    return buffOut;
}

所以我的片段结果是一组信息 我正在尝试将openGL着色器转换为类似的东西

但你能做到吗?我之前从未返回任何其他内容,除了vec4为“片段颜色”之外的OpenGL片段着色器,我似乎找不到任何告诉我可以返回更多的信息。

1 个答案:

答案 0 :(得分:2)

在opengl中,您可以在main函数之外定义输出变量,然后在该函数中设置它们的值。所以你在那里的hlsl代码的等价物将是这样的(片段着色器)

layout (location = 0) out vec4 diffuse;     //these are the outputs
layout (location = 1) out vec4 normal;
layout (location = 2) out vec4 specular;

in vec4 in_position;    //these are the inputs from the vertex shader
in vec4 in_normal;
in vec2 in_tex_coord;

void main()
{
    normal = normalize(in_normal);
    normal = (normal + vec4(1.0, 1.0, 1.0, 1.0)) * 0.5; 
    diffuse = texture(diffuseSampler, in_tex_coord);
    ambient = texture(ambientSampler, in_tex_coord);
    specular = vec4(1.0, 1.0, 1.0, 1.0);
}

然后在您的应用程序中,您需要确保绑定了一个framebufferobject,它具有要写入的正确数量和类型的附件。

相关问题