opengl with vbo gives error 0x501

久未见 提交于 2019-12-12 10:10:48

问题


I'm trying to display a texture with height-field (think kinect here). What I got gives an 0x501 error, no idea why.

So the questions are: why am I getting this 0x501 error? And the rest of the code: should this work?

// void EC(void) is an inline function that checks for OpenGL errors and shows them
// url_width / url_height: dimensions of the frame and by that the dimensions of the texture/height field
// texture_id: id for the coordinates of each point in the texture
// vector_id: id for the vectors which set the x/y and height
// texture_color_id: id for the frame bitmap which is used for colors

// malloc
vertex_coords  = new GLfloat[url_width * url_height * 6 * 3];
texture_coords = new GLfloat[url_width * url_height * 6 * 2];

// calculate x/y offsets in texture for each point
int index = 0;
for (int hMapX = 0; hMapX < url_width; hMapX++)
{
       for (int hMapY = 0; hMapY < url_height; hMapY++)
       {
               for (int nTri = 0; nTri < 6; nTri++)
               {
                       int x = (float)hMapX + ((nTri == 1 || nTri == 2 || nTri == 5) ? 1 : 0);
                       int y = (float)hMapY + ((nTri == 2 || nTri == 4 || nTri == 5) ? 1 : 0);
                       texture_coords[index++] = (double)x / (double)url_width;
                       texture_coords[index++] = (double)y / (double)url_height;
               }
       }
}

// copy texture pointers into videocard
glBindBuffer(GL_ARRAY_BUFFER, texture_id); EC();
int texture_bytes = url_width * url_height * 6 * 2 * sizeof(GLfloat);
glBufferData(GL_ARRAY_BUFFER, texture_bytes, NULL, GL_STATIC_DRAW); EC();
glBufferSubData(GL_ARRAY_BUFFER, 0, texture_bytes, texture_coords); EC();

// pre-initialize vertex structures
glBindBuffer(GL_ARRAY_BUFFER, vertex_id); EC();
int vertex_bytes = url_width * url_height * 6 * 3 * sizeof(GLfloat);
glBufferData(GL_ARRAY_BUFFER, vertex_bytes, NULL, GL_STATIC_DRAW); EC();

// draw function()
/////... fill vertex array with x,y and height in z
//

// copy vertexes to card
int vertex_bytes = url_width * url_height * 6 * 3 * sizeof(GLfloat);
glBindBuffer(GL_ARRAY_BUFFER, vertex_id); EC();

//
// ======> THIS OPENGL CALL FAILS WITH THE 0x501 ERROR <======
//
glBufferSubData(GL_ARRAY_BUFFER, 0, vertex_bytes, vertex_coords); EC();

// copy texture picture into card
glEnable(GL_TEXTURE_2D); EC();
glBindTexture(GL_TEXTURE_2D, texture_colors_id); EC();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bg -> w, bg -> h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bg -> pixels); EC();
glDisable(GL_TEXTURE_2D); EC();

glEnable(GL_TEXTURE_2D); EC();
glBindTexture(GL_TEXTURE_2D, texture); EC();

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    // Verts
    glBindBuffer(GL_ARRAY_BUFFER, vertex_id);
    glVertexPointer(3, GL_FLOAT, 0, 0);

    // Tex coords
    glBindBuffer(GL_ARRAY_BUFFER, texture_id);
    glTexCoordPointer(2, GL_FLOAT, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertex_id);
    // not sure if i should use quads for this
    glDrawElements(GL_QUADS, 6, GL_FLOAT, 0);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    glDisable(GL_TEXTURE_2D); EC();

来源:https://stackoverflow.com/questions/9415111/opengl-with-vbo-gives-error-0x501

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!