简体   繁体   中英

VertexArray glVertexAttribPointer GL_INVALID_VALUE 0x0501

Writing an OpenGL program to draw a square on screen, but running into an invalid value error. The error occurs on the call MyGLCall(glVertexAttribPointer(i, . MyGLCall is an abstraction of GLCALL to I can have the error code printed to the console as below.

Not sure value is invalid here. I believe all the data types are correct for glVertexAttribPointer , bu missing something here.

Console output

Rendering...
OpenGL version 3.3.0 NVIDIA 388.13
GL Extension Wrangler version 2.1.0
[!!! OpenGL ERROR!!!] (0x0501)
glVertexAttribPointer(i, element.elements_per_vertex, element.type, element.normalized, layout.get_stride(), (const void*) offset)

Layout and Element Data Types

在此处输入图像描述

Source code

// bind vertex buffer and set memory layout
void VertexArray::add_buffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 
{
    VertexArray::bind();
    vb.bind();
    // auto is std::vector<VertexBufferLayoutElement>
    const auto& elements = layout.get_elements();
    unsigned int offset = 0;
    for (unsigned int i = 0; i < elements.size(); i++) 
    {       
        const auto& element = elements[i];      
        // GL_INVALID_VALUE 0x0501
        MyGLCall(glEnableVertexAttribArray(i));
        MyGLCall(glVertexAttribPointer(i,
            element.elements_per_vertex,
            element.type,
            element.normalized,
            layout.get_stride(),
            (const void*) offset)); // heckin, what is this (const void*)???
        offset += element.elements_per_vertex * VertexBufferLayoutElement::get_size_of_type(element.type);
    }   
}

struct for VertexBufferLayoutElement in my VertextBufferLayout Header file

struct VertexBufferLayoutElement
{
    // nice mem alignment bro, mem size on most machines is
    unsigned int elements_per_vertex;   // 4 bytes
    unsigned int type;                  // 4 bytes
    unsigned char normalized;           // 1 byte
    static unsigned int get_size_of_type(unsigned int type)
    {
        switch (type)
        {
            case GL_FLOAT:          return 4;
            case GL_UNSIGNED_INT:   return 4;
            case GL_UNSIGNED_BYTE:  return 1;
        }
        ASSERT(false);
        return 0;
    }
};

Error was in this header file above

Did not follow the correct parameter order of the the first c spec parameter list for the arrtrib pointer function . GL_FLOAT and elements_per_vertex were swapped causing the error, the screenshot below is the correct order.

在此处输入图像描述

The 2nd parameter of glVertexAttribPointer is the tuple size and has to be 1, 2, 3, 4 or GL_BGRA . The 3rd parameter is the type (eg GL_UNSIGNED_BYTE , GL_FLOAT ).

You pass element.elements_per_vertex to the 2nd and element.type to the 3rd parameter which seems to be correct. But the values of the parameters are swapped:

 elements_per_vertex: 5126 type: 3

Note, 5126 is 0x1406 and this is the value of the enumerator constant GL_FLOAT .

Set

elements_per_vertex = 3
type = GL_FLOAT

to solve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM