【问题标题】:OpenGL Fragment Shader VS DirectX Fragment ShaderOpenGL 片段着色器 VS DirectX 片段着色器
【发布时间】:2013-01-16 18:09:46
【问题描述】:

在 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 片段着色器,我之前从未返回过任何其他东西,而且我似乎找不到任何可以告诉我可以返回更多信息的信息。

【问题讨论】:

    标签: opengl directx shader


    【解决方案1】:

    在 opengl 中,您在主函数之外定义输出变量,然后在该函数中设置它们的值。所以相当于你拥有的 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);
    }
    

    然后在您的应用程序中,您需要确保您已绑定一个帧缓冲区对象,该对象具有正确数量和类型的要写入的附件。

    【讨论】:

    • 抱歉。是的,我的顶点着色器中确实有这些变量。你是说我应该操纵那里的信息?因为我试图获得每个像素的“正常”数据或跟踪像素位置。
    • 因为我正在跟踪图像。我确实绑定了我的帧缓冲区。我写信给它。但是在 directX 中,您的片段着色器会输出您告诉它丢弃的所有信息。
    • 你的意思是当我使用 glFramebufferTexture2D 时? void glFramebufferTexture2D(GLenum 目标,GLenum 附件,GLenum textarget,GLuint 纹理,GLint 级别);我应该操纵附件位置吗?我一直在使用 GL_COLOR_ATTACHMENT0
    • 是的,您需要为每个输出提供单独的纹理。所以你需要附加到 GL_COLOR_ATTACHMENT1 和 GL_COLOR_ATTACHMENT2 的纹理。附件编号对应着色器中的布局编号
    • 好的,所以基本上我会使用多个渲染目标?我将需要像(normalRenderTarget,colorRenderTarget)ext ..将它们全部附加到ATTACHMENT [x],如果我正在做3个数据,只需使用只有“示例”3“out vec4”变量的片段着色器进行我的正常渲染过程收藏?
    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多