简体   繁体   English

OpenGL VBO故障

[英]OpenGL VBO Troubles

Okay, so I am trying to learn OpenGL. 好的,我正在尝试学习OpenGL。 I have successfully followed an example to render a triangle. 我已经成功地遵循了渲染三角形的示例。 So I tried to move on, and instead of using the vertices provided in example, I tried to read in the vertices from a file (arbitrary number of vertices). 因此,我尝试继续前进,而不是使用示例中提供的顶点,而是尝试从文件(任意数量的顶点)中读取顶点。 Because it is arbitrary, I read into a std::vector and then try and assign that to an array in order to pass to glBufferData . 因为它是任意的,所以我读入std::vector ,然后尝试将其分配给数组以传递给glBufferData FWIW, I want to draw GL_LINE_STRIP , instead of GL_TRIANGLES . FWIW,我想绘制GL_LINE_STRIP而不是GL_TRIANGLES My program runs for a second, then a pop-up window with an error displays: 我的程序运行了一秒钟,然后显示一个带有错误的弹出窗口:

Debug assertion failed... then it says ...Expression: vector subscript out of range Debug assertion failed...然后它说...Expression: vector subscript out of range

The code I am about to post is the example program that renders a triangle, however, I have commented out my attempt at reading in from file (the code that is giving me the error). 我要发布的代码是呈现三角形的示例程序,但是,我已经注释掉了我从文件中读取的尝试(给出错误的代码)。 So you can see where it is going wrong. 这样您可以看到问题出在哪里。 The only things that really changed in MY version is change hard-coded vertices in array to be read from input, and then in the drawArray function I change GL_TRIANGLES to be GL_LINE_STRIP . 在我的版本,真正唯一改变的是改变硬编码的顶点数组从输入读取,然后在drawArray功能更改GL_TRIANGLESGL_LINE_STRIP I know I am doing something very silly and I am missing it, I am just so confused with non-immediate mode in OpenGL (extreme noob I know). 我知道我做的事情很愚蠢,我很想念它,我对OpenGL中的非立即模式(我知道的极端菜鸟)感到困惑。 ANYWAY, here is the code: 无论如何,这是代码:

void CreateVBO(void)
{
/*
string line;
int count = 0;
vector<GLfloat> vertices;
vector<GLfloat> colors;

ifstream myfile("C:\\Users\\Andrew\\Documents\\Visual Studio 2013\\Projects\\Control\\Control\\bin\\Debug\\GeoTemp.test");
if (myfile.is_open())
{
    while (getline(myfile, line))
    {
        float temp = (float)atof(line.c_str());

        if (count == 3)
        {
            vertices.push_back(1.0);
            colors.push_back(1.0);
            count = 0;
        }
        else
        {
            vertices.push_back(temp);
            colors.push_back(0.0);
            count++;
        }

        //cout << line << '\n';
    }
    myfile.close();
}

GLfloat* Vertices = &vertices[0];
GLfloat* Colors = &colors[0];
*/

GLfloat Vertices[] = {
    -0.8f, -0.8f, 0.0f, 1.0f,
    0.0f, 0.8f, 0.0f, 1.0f,
    0.8f, -0.8f, 0.0f, 1.0f
};

GLfloat Colors[] = {
    1.0f, 0.0f, 0.0f, 1.0f,
    0.0f, 1.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f, 1.0f
};


GLenum ErrorCheckValue = glGetError();

glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);

glGenBuffers(1, &VboId);
glBindBuffer(GL_ARRAY_BUFFER, VboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
//glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

glGenBuffers(1, &ColorBufferId);
glBindBuffer(GL_ARRAY_BUFFER, ColorBufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
//glBufferData(GL_ARRAY_BUFFER, colors.size()*sizeof(GLfloat), &colors[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);

ErrorCheckValue = glGetError();
if (ErrorCheckValue != GL_NO_ERROR)
{
    fprintf(
        stderr,
        "ERROR: Could not create a VBO: %s \n",
        gluErrorString(ErrorCheckValue)
        );

    exit(-1);
}
}

I suspect that your vertices / colors vectors are empty when you hit these lines: 我怀疑当您按下这些行时,您的vertices / colors向量为空:

GLfloat* Vertices = &vertices[0];
GLfloat* Colors = &colors[0];

Add a size check after your file reading code: 在文件读取代码之后添加大小检查:

if( vertices.empty() || colors.empty() )
{
    exit( -1 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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