glDrawArrays出现OpenGL错误1280

时间:2018-08-21 17:33:21

标签: c++ opengl

OpenGL错误1280出现在glDrawArrays()调用中。 当我在互联网上搜索时,我发现它可能是由无效的枚举引起的。

GL_ENUM对我来说似乎还不错。另外,在绘制调用之前,我已经启用了着色器以及顶点数组。

屏幕上什么都没有画。

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]);
    }
}

};

顶点着色器:

#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);  
}

片段着色器:

#version 400

out vec4 color;

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

1 个答案:

答案 0 :(得分:3)

GL_INVALID_ENUM错误发生在glGetProgramiv中,其中第二个参数必须是GL_LINK_STATUS而不是GL_COMPILE_STATUS

glGetProgramiv(program, GL_LINK_STATUS, &status);