簡體   English   中英

頂點着色器接收輸入但不生成輸出

[英]Vertex shader recieving input but no output is generated

我有這個頂點着色器,它只是傳遞給定的位置並將 UV 和顏色傳遞給片段着色器:

#version 330 core                                                   
layout (location = 0) in vec2 in_pos;                            
layout (location = 1) in vec2 in_texUV;                             
layout (location = 2) in vec4 in_color;                             
out vec2 ex_texUV;                                                  
out vec4 ex_color;                                                  
                                                                              
uniform mat4 projection;                                            
                                                                    
void main()                                                         
{                                                                   
    gl_Position = vec4(in_pos, 0.0, 1.0) * projection;           
    ex_texUV    = in_texUV;                                         
    ex_color    = in_color;                                         
}

編輯:此處顯示了片段着色器,並且所有制服都已正確設置:

#version 330 core                                               
in vec2 in_texUV;                                               
in vec4 in_color;                                               
out vec4 out_color;                                             
                                                                
uniform vec2 screenSize;                                        
uniform vec3 transparentColour;                                 
uniform sampler2D sprite;                                       
                                                                
uniform sampler2D palette;                                      
uniform int paletteLines[0x100];                                
                                                                
void main()                                                     
{                                                               
    if (in_color.a == 0.0) {                                    
        vec4 coord = gl_FragCoord - 0.5;                        
        vec2 screenPos;                                         
        screenPos.x = coord.x * screenSize.x;                   
        screenPos.y = coord.y * screenSize.y;                   
                                                                
        int id = paletteLines[int(screenPos.y)];                
                                                                
        int index = int(texture2D(sprite, in_texUV).r * 255);   
                                                                
        if (index == 0)                                         
            discard;                                            
                                                                
        vec2 palvec;                                            
        palvec.x = index;                                       
        palvec.y = id;                                          
                                                                
        out_color = texture(palette, palvec);                   
    }                                                           
}

(投影變量已正確設置,使用 NVIDIA Nsight 顯示。

頂點着色器和片段着色器都被編輯為簡單的傳遞(甚至將片段着色器設置為常量vec4(1.0, 1.0, 1.0, 1.0) ,)但它始終沒有顯示。

為了設置着色器,我首先設置 VAO 和 VBO 以從DrawVertex列表中DrawVertex

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(2, &GFXVBO);
glBindBuffer(GL_ARRAY_BUFFER, GFXVBO);
glVertexAttribPointer(0, 2, GL_SHORT, GL_FALSE, sizeof(DrawVertex), 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_SHORT, GL_FALSE, sizeof(DrawVertex), (void *)(sizeof(short) * 2));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(DrawVertex), (void *)(sizeof(short) * 4));
glEnableVertexAttribArray(2);

然后使用下面的代碼進行繪制(保證VAO和VBO綁定, gfxShader只是一個使用程序的幫手):

gfxShader.use();

// [setup the program, change uniforms as necessary]

// lastRenderCount is how many to render
// gfxPolyList is the list of DrawVertex
glBufferData(GL_ARRAY_BUFFER, lastRenderCount * sizeof(DrawVertex), gfxPolyList, GL_DYNAMIC_DRAW);

glDrawArrays(GL_TRIANGLES, 0, lastRenderCount);

gfxShader.stop();

然而,盡管如此,雖然RenderDoc 顯示輸入正在通過,輸出根本沒有顯示任何內容。 最重要的是,NVIDIA Nsight 表示沒有繪制任何片段。 我哪里會出錯?

對於上下文,這里是 struct DrawVertex

struct DrawVertex {
    short x;
    short y;
    short u;
    short v;

    Color color = 0; //0xRRGGBBAA
};
gl_Position = vec4(in_pos, 0.0, 1.0) * projection;

這是錯誤的。 矩陣乘法不是可交換的。 應該:

gl_Position = projection * vec4(in_pos, 0.0, 1.0);

如果你不相信我,試試這個:

glm::mat4 proj = {
    1.0f, 0.0f, 3.0f, 5.0f,
    0.0f, 1.0f, 3.0f, 6.0f,
    0.0f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 3.0f, 1.0f
};
glm::vec4 vec = { 0.0f, 1.0f, 0.0f, 1.0f };
glm::vec4 result1 = proj * vec;
glm::vec4 result2 = vec * proj;
std::cout << "result1: " << result1.x << result1.y << result1.z << result1.w << '\n';
std::cout << "result2: " << result2.x << result2.y << result2.z << result2.w << '\n';

// Output:
result1: 0167
result2: 5701

https://learnopengl.com/Getting-started/Transformations

編輯:您的片段着色器僅在in_color.a == 0.0運行? 你確定這是正確的嗎? 也許您打算改用!=

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM