c++ - 为什么顶点数组对象会导致错误?

标签 c++ opengl sdl vao

我有一个可以运行的 opengl 程序...直到我尝试使用顶点数组对象。我已经尝试来回更改代码好几天了,但找不到问题所在。

这是代码的一部分。 glCall(...) 是一个用于错误检查的辅助函数。

    static struct {
    GLint color;
    GLuint vertices;
    GLuint mvpMatrix;

    const char * vertexCode =
        R"___(
        #version 330 core

        in vec4 vPosition;

        uniform vec4 uColor;
        uniform  mat4    mvp_matrix;
        out vec4 fColor;
        void main() {
           gl_Position = mvp_matrix * vPosition;
           fColor = uColor;
        }
    )___";


    const char * fragmentCode =
        R"__(
        #version 330 core
        in vec4 fColor;
        out vec4 FragColor;

        void main() {
          gl_FragColor = fColor;
        })__";

} program1;



static Matrix<float> mvpMatrix;
static double screenWidth, screenHeight;
static ShaderProgram squareShaderProgram;

static GLuint vertexArray;
static GLuint vertexBuffer;


//Square
static const vector<float> squareVertices = { 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f };
static const vector<float> squareColors = {.8, .8, 1., 1};



static void setDimensions(double width, double height){
    screenWidth = width;
    screenHeight = height;
}


static bool initDrawModule(double width, double height) {
    //GLuint vao;  <-----------------------  //This seems to be the problem
    //glCall(glGenVertexArrays(1, &vao));
    //glCall(glBindVertexArray(vao));



    squareShaderProgram.initProgram(program1.vertexCode, program1.fragmentCode);


    if (!squareShaderProgram.getProgram()) {
        cerr << "Could not create shader program in " << __FILE__ << ":" << __LINE__ << endl;
        return false;
    }


    glCall(squareShaderProgram.use());

    program1.vertices = squareShaderProgram.getAttribute("vPosition");
    program1.color = squareShaderProgram.getUniform("uColor");
    program1.mvpMatrix = squareShaderProgram.getUniform("mvp_matrix");



    setDimensions(width, height);

    return false;
}


static void drawSquare(Vec p, double a, double sx, double sy){
    squareShaderProgram.use();

    {
        mvpMatrix = mvpMatrix.RotationZ(a / 180.);
        mvpMatrix.scale(sx, sy, 1);

        mvpMatrix.scale(1. / screenWidth, 1. / screenHeight, 1);
        mvpMatrix.setTranslation(
            p.x / screenWidth * 2 - 1.,
            p.y / screenHeight * 2 - 1.,
            p.z
        );

        glCall(glUniformMatrix4fv(program1.mvpMatrix, 1, GL_FALSE, mvpMatrix));
    }


    glCall(glEnableVertexAttribArray(program1.vertices));
    glCall(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer))
    glCall(glVertexAttribPointer( //<----- this fails
            program1.vertices,
            2,
            GL_FLOAT,
            GL_FALSE,
            0,
            &squareVertices[0]));

    glCall(glUniform4fv(program1.color, 1, &squareColors[0]));
    glCall(glDrawArrays(GL_TRIANGLE_FAN, 0, 4));
    glDisableVertexAttribArray(program1.vertices);
}



static void render() {
    static float x = 0;

    drawSquare(Vec(.1 + x, 50, 1), 20 + x * 2, 100,100);
    x += 20;
}




bool init() {
    return initDrawModule(512, 512);
}





void die(string message);
void checkSDLError(int line = -1);



int main(int argc, char *argv[])
{
    SDL_Window *mainwindow; 
    SDL_GLContext maincontext; 

    // Create our window centered at 512x512 resolution
    mainwindow = SDL_CreateWindow("sdl-window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (!mainwindow) {
        die("Unable to create window");
    }

    checkSDLError(__LINE__);


    maincontext = SDL_GL_CreateContext(mainwindow);
    checkSDLError(__LINE__);

    init();

    SDL_GL_SetSwapInterval(1);

    for (int i = 0; i < 10; ++i) {
        glClearColor ( 0.1 * i, 0.0, 0.0, 1.0 );
        glClear ( GL_COLOR_BUFFER_BIT );
        render();
        SDL_GL_SwapWindow(mainwindow);
        SDL_Delay(200);
    }

    SDL_GL_DeleteContext(maincontext);
    SDL_DestroyWindow(mainwindow);
    SDL_Quit();

    return 0;
}

我唯一知道的是,当我取消注释 initDrawModule() 开头的行时,glVertexAttribPointer(在代码中标记)失败并显示“GL_INVALID_OPERATION”。这是怎么回事?

最佳答案

GL_INVALID_OPERATION是由glVertexAttribPointer的最后一个参数引起的,指向数据的指针。

在兼容性配置文件中,如果绑定(bind)了顶点缓冲区对象0,则glVertexAttribPointer的最后一个参数被视为指向顶点数组数据的指针。

如果绑定(bind)了命名顶点缓冲区对象,则 glVertexAttribPointer 的最后一个参数是该缓冲区内的偏移量。

您混合了两种情况,绑定(bind)了一个命名缓冲区对象,但也传递了一个指向顶点数组数据的指针:

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(program1.vertices, 2, GL_FLOAT, GL_FALSE, 0, &squareVertices[0]);

如果你想使用顶点缓冲对象,那么你必须使用 glBufferData创建并初始化缓冲区对象的数据存储:

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
GLsizeiptr size_in_bytes = ...; // size of the buffer data in bytes
glBufferData(GL_ARRAY_BUFFER, size_in_bytes, &squareVertices[0], GL_STATIC_DRAW);

命名顶点缓冲区可用于定义通用顶点属性数据数组:

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(program1.vertices, 2, GL_FLOAT, GL_FALSE, 0, NULL);

另请参阅Vertex Buffer Object .

关于c++ - 为什么顶点数组对象会导致错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219673/

相关文章:

c++ - 使用 OpenGL 写入文件而不是在屏幕上绘制

c++ - SDL 自身和其他窗口崩溃

c++ - 为什么 VS2013 告诉我使用 scanf_s?

c++ - 如何使异步并行程序代码易于管理(例如在 C++ 中)

c++ - 操作队列

opengl - 如何在 GLSL 中编写 "Texture Breathing"着色器?

c++ - 我们需要序列化 ​​VAO 和 VBO

c++ - 使用构造函数参数将仿函数传递给 std::thread。是否可以?

c# - 如何检查点是否在四面体中?

c++ - 梯度生成产生奇怪的伪像