简体   繁体   中英

OpenGL VBO Troubles

Okay, so I am trying to learn 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 . FWIW, I want to draw GL_LINE_STRIP , instead of 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

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 . 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). 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:

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

Add a size check after your file reading code:

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

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