简体   繁体   English

在单通道OpenGL中绘制立方体贴图

[英]Draw a cubemap in a single pass OpenGL

I'm trying to draw to a cubemap in a single pass using a geometry shade in OpenGL. 我试图在OpenGL中使用几何阴影在单个通道中绘制立方体贴图。 Basically need I do this to copy the content of a cubemap into another cubemap, and the may not have the same resolution and pixel layout. 基本上我需要这样做才能将立方体贴图的内容复制到另一个立方体贴图中,并且可能没有相同的分辨率和像素布局。

I'm trying to achieve the result I want feeding a single point to the vertex shader and then, from the geometry shader, select each layer (face of the cubemap) and emit a quad and texture coordinates. 我正在尝试实现我想要将单个点提供给顶点着色器的结果,然后从几何着色器中选择每个图层(立方体贴图的面)并发出四边形和纹理坐标。

So far I've tried this method emitting only two of the cubemap faces (positive and negative X) to see if it could work, but it doesn't. 到目前为止,我已经尝试过这种方法只发出两个立方体贴图面(正面和负面X)来查看它是否可以工作,但事实并非如此。

Using NSight I can see that there is something wrong. 使用NSight,我可以看到出现了问题。

This is the source cubemap: 这是源立方体贴图: 在此输入图像描述

And this is the result cubemap: 这是立方体贴图的结果: 在此输入图像描述

The only face that's being drawn to is the positive X and still it's not correct. 被吸引的唯一面孔是正X,但仍然不正确。

This is my geometry shader: 这是我的几何着色器:

#version 330 core

layout(points) in;
layout(triangle_strip, max_vertices = 8) out;

in vec3 pos[];
out vec3 frag_textureCoord;

void main()
{
    const vec4 positions[4] = vec4[4] ( vec4(-1.0, -1.0, 0.0, 0.0),
                                        vec4( 1.0, -1.0, 0.0, 0.0),
                                        vec4(-1.0,  1.0, 0.0, 0.0),
                                        vec4( 1.0,  1.0, 0.0, 0.0) );

    // Positive X
    gl_Layer = 0;

    gl_Position = positions[0];
    frag_textureCoord = vec3(1.0, -1.0, -1.0);
    EmitVertex();

    gl_Position = positions[1];
    frag_textureCoord = vec3(1.0, -1.0,  1.0);
    EmitVertex();

    gl_Position = positions[2];
    frag_textureCoord = vec3(1.0,  1.0, -1.0);
    EmitVertex();

    gl_Position = positions[3];
    frag_textureCoord = vec3(1.0,  1.0,  1.0);
    EmitVertex();                    
    EndPrimitive();

    // Negative X
    gl_Layer = 1;

    gl_Position = positions[0];
    frag_textureCoord = vec3(-1.0, -1.0,  1.0);
    EmitVertex();

    gl_Position = positions[1];
    frag_textureCoord = vec3(-1.0, -1.0, -1.0);
    EmitVertex();

    gl_Position = positions[2];
    frag_textureCoord = vec3(-1.0,  1.0,  1.0);
    EmitVertex();

    gl_Position = positions[3];
    frag_textureCoord = vec3(-1.0,  1.0, -1.0);
    EmitVertex();                    
    EndPrimitive();
}

And this is my fragment shader: 这是我的片段着色器:

#version 150 core

uniform samplerCube AtmosphereMap;

in vec3 frag_textureCoord;

out vec4 FragColor;

void main()
{
    FragColor = texture(AtmosphereMap, frag_textureCoord) * 1.0f;
}

UPDATE Further debugging with NSight shows that for the positive x face every fragment gets a value of frag_textureCoord of vec3(~1.0, ~0.0, ~0.0) (I've used ~ since the values are not exactly those but approximated). 更新使用NSight的进一步调试显示,对于正x面,每个片段都获得frag_textureCoordvec3(~1.0, ~0.0, ~0.0)vec3(~1.0, ~0.0, ~0.0) (我已经使用过~因为这些值不是那些但是近似的值)。 The negative x face instead never reaches the fragment shader stage. 负x面反而永远不会到达片段着色器阶段。


UPDATE Changing the definition of my vertex position from vec4(x, y, z, 0.0) to vec4(x, y, z, 1.0) makes my shader render correctly the positive X face, but the negative is still wrong, even if debugging the fragment shader I see that the right color is selected and applied, but then it becomes black. 更新我的顶点位置的定义从vec4(x, y, z, 0.0)更改为vec4(x, y, z, 1.0)使我的着色器正确渲染正X面,但负面仍然是错误的,即使调试片段着色器我看到选择并应用了正确的颜色,但随后变为黑色。

gl_Layer = 0;

This is a Geometry Shader output . 这是几何着色器输出 Calling EmitVertex will cause the value of all output variables to become undefined. 调用EmitVertex将导致所有输出变量的值变为未定义。 Therefore, you must always set each output for each vertex to which that output applies. 因此,您必须始终为该输出应用的每个顶点设置每个输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM