简体   繁体   中英

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. 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.

Using NSight I can see that there is something wrong.

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.

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). The negative x face instead never reaches the fragment shader stage.


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.

gl_Layer = 0;

This is a Geometry Shader output . Calling EmitVertex will cause the value of all output variables to become undefined. Therefore, you must always set each output for each vertex to which that output applies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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