即使顶点着色器正在积极使用glAttribLocation,它也会返回-1

时间:2018-08-21 15:37:07

标签: c++ opengl

在堆栈溢出中搜索了类似的问题,以寻找解决方案,但似乎并没有解决。

White triangle rendered with errors in console window

main.cpp:

#include"reader.h"
#include"window.h"
#include"shader.h"
int main() {

float vertices[] = {
    -0.5f, -0.5f, 0.0f ,
    0.5f, -0.5f, 0.0f , 
    0.0f,  0.5f, 0.0f 
};

Window window;

GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

Shader shader;
shader.addShader("./src/shaders/basic.vtx",GL_VERTEX_SHADER);
shader.addShader("./src/shaders/basic.frg", GL_FRAGMENT_SHADER);
shader.compile();
shader.enable();


GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,GL_STATIC_DRAW);

GLint pos_in = glGetAttribLocation(shader.getProgram(), "pos_in");
if (pos_in < 0) {
    std::cout << "pos_in not found\n";
}
glVertexAttribPointer(pos_in, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glEnableVertexAttribArray(pos_in);


while (!window.closed()) {
    window.update();
    glDrawArrays(GL_TRIANGLES,0,3);
}
return 0;
}

shader.h:

#pragma once
#include<glad/glad.h>
#include<iostream>
#include<vector>
#include"reader.h"

class Shader {
std::vector<GLuint*> shaders;
GLuint program;

public :

GLuint& getProgram() { 
    return program; 
}

Shader() {
    program = glCreateProgram();
}
void addShader(const char * path, GLenum type) {

    std::string data = ShaderReader(path).read_shader();
    const char * chardata = data.c_str();
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &chardata , nullptr);
    glCompileShader(shader);

    int success;
    char buffer[512];
    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(shader, 512, NULL, buffer);
        std::cout << buffer << std::endl;
        return;
    }
    std::cout << "shader inserted into vector\n";

    shaders.push_back(&shader);
}

void compile(){
    for (int i = 0; i != shaders.size();i++) {
        glAttachShader(program, *shaders[i]);
    }
    glLinkProgram(program);
    glValidateProgram(program);
    glUseProgram(program);
    int status;
    glGetProgramiv(program, GL_COMPILE_STATUS, &status);

    char buffer[512];
    if (!status) {
        glGetProgramInfoLog(program,512,NULL,buffer);
        std::cout << buffer << std::endl;
        return;
    }
    std::cout << "shader compilation successful\n";
}
void enable() {
    glUseProgram(program);
}
void disable() {
    glUseProgram(0);
}
~Shader() {
    for (int i = 0; i != shaders.size();i++) {
        glDeleteShader(*shaders[i]);
    }
}

};

用basic.vtx编写的顶点着色器:

#version 400 

layout (location = 0 ) in vec3 pos_in ;

void main(){
gl_Position = vec4(pos_in.x , pos_in.y , pos_in.z , 1.0f);  
}

以basic.frg编写的片段着色器:

#version 400

out vec4 color;

void main(){
color = vec4(0.0f, 0.5f , 0.5f , 1.0f);
}

在调用glGetAttribLocation时,顶点着色器正在使用pos_in attrib来设置gl_Position,但它返回-1。 同样,调用glGetAttribLocation()时不渲染三角形;或使用直接属性指针值(如0,1)将其呈现白色,并且出现openGL 1281错误。

1 个答案:

答案 0 :(得分:3)

shaders.push_back(&shader)绝对是无效的一件事:

void addShader(const char * path, GLenum type) {
    // ....

    GLuint shader = glCreateShader(type);

    // ....

    shaders.push_back(&shader);
}

使用shaders.push_back(&shader)将局部变量的地址推回shaders向量。因此glAttachShader(program, *shaders[i]);将导致未定义的行为。

shader仅包含一个数字ID,因此无需获取指向它的指针,只需将std::vector<GLuint*> shaders更改为std::vector<GLuint> shaders,使用shaders.push_back(shader)并替换所有{ {1}}与*shaders[i]

之所以没有出现链接错误,很可能是因为在进行shaders[i]之前,从&shader获得的地址中的内容没有被覆盖,并且着色器矢量拥有相同的堆栈地址。结果是每次在相同的ID上调用glAttachShader(program, *shaders[i]),因此您仅将片段着色器绑定到程序。