2017-10-18 74 views
-1

我想創建將RGB轉換爲YCbCr的程序,所以我需要設置變換矩陣和向量爲統一。當我多次呼叫glUniformglGetAttribLocation返回-1。這是我的代碼。glGetAttribLocation在設置了多個uniform後返回-1

片段着色器

#version 410 
precision highp float; 

uniform sampler2D u_texture; 
uniform mat3 u_ycbcr_matrix; 
uniform mat3 u_ycbcr_vector; 

out vec4 color; 

in vec2 v_coord; 

void main() { 
    color = texture(u_texture, v_coord) + vec4(u_ycbcr_matrix[0], 1); 
} 

C++代碼

void ScreenQuad::createProgram(ShaderProgram * program, string vsPath, string fsPath){ 
shaderProgram = *program; 
shaderProgram.loadVertexShader(vsPath); 
shaderProgram.loadFragmentShader(fsPath); 
shaderProgram.link(); 

GLint attr = glGetAttribLocation(program -> getShaderProgram(), "position"); 
if (attr < 0) { 
    fprintf(stdout, "Nieprawidlowy parametr: position"); 
} 

glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0); 
glEnableVertexAttribArray(attr); 

shaderProgram.use(); 
program -> setUniforms(); 
glUniform1i(glGetUniformLocation(shaderProgram.getShaderProgram(), "u_texture"), 0); 

int total = -1; 
glGetProgramiv(shaderProgram.getShaderProgram(), GL_ACTIVE_UNIFORMS, &total); 
for(int i=0; i<total; ++i) { 
    int name_len=-1, num=-1; 
    GLenum type = GL_ZERO; 
    char name[100]; 
    glGetActiveUniform(shaderProgram.getShaderProgram(), GLuint(i), sizeof(name)-1, 
         &name_len, &num, &type, name); 
    name[name_len] = 0; 
    GLuint location = glGetUniformLocation(shaderProgram.getShaderProgram(), name); 
    fprintf(stdout, "\n --- %s : %d---", name, location); 
} 

shaderPrograms.push_back(shaderProgram);} 


ShaderProgram RGB2YCbCr::setUniforms() { 
    GLuint program = getShaderProgram(); 
    fprintf(stdout, "\n+++ %d +++ \n", glGetUniformLocation(program, "u_ycbcr_matrix")); 
    glUniformMatrix3fv(glGetUniformLocation(program, "u_ycbcr_matrix"), 1, GL_FALSE, ycbcr_matrix); 

    fprintf(stdout, "\n+++ %d +++ \n", glGetUniformLocation(program, "u_ycbcr_vector")); 
    glUniform3fv(glGetUniformLocation(program, "u_ycbcr_vector"), 1, ycbcr_vector); 
}; 
+4

您確定'shaderProgram = * program;'正常工作嗎?這似乎是對象上的副本分配調用,應該可能是不可複製的。而且你還沒有調用過'glGetError'來檢查錯誤代碼。 – VTT

+0

'GLenum a = glGetError(); fprintf(stdout,「GL_GET_ERROR%d」,a);'打印代碼1282 –

回答

1

您的着色器u_ycbcr_vector均勻,不使用它。這是由GLSL編譯器和鏈接器完成的某種優化,它將爲已聲明但未使用的制服丟棄無效的位置。因此,如果您沒有使用它,然後再試一次,請從着色器中移除該制服。

相關問題