This is ultimately going to go back into the class it's failing to work in. It took me a day to wok out that the this bit of code is what is wrong with it. It was a long day.
The draw call glDrawElements is used, when you additionaly use an index buffer (GL_ELEMENT_ARRAY_BUFFER). In your case, you’d rather use glDrawArrays(GL_TRIANGLES, 0, numVertices);
You probably did it to shorten the code you show us, but in practice it’s better to create resources once at startup (glGenBuffers), not every draw call.
You probably did it to shorten the code you show us, but in practice it’s better to create resources once at startup (glGenBuffers), not every draw call.
I do normally. It was all nicely arranged in a good long class, which I then had to butcher whilst trying to find what was wrong!
numVertices is probably 3 coming in here, as you have 3 verts (each of which is 3 floats), so you're not allocating enough buffer storage for them. The data parameter to glBufferData is a total size in bytes, so you need numVertices * 3 * sizeof (float) instead. It's probably better to use a struct for your vertex type rather than a raw array of floats as it can help catch this kind of thing.
If numVertices is actually 9 then this is the wrong value to use for glDrawArrays - you need to be using 3 instead. Again, using a struct will make this clearer and easier.
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.
numVertices is probably 3 coming in here, as you have 3 verts (each of which is 3 floats), so you're not allocating enough buffer storage for them. The data parameter to glBufferData is a total size in bytes, so you need numVertices * 3 * sizeof (float) instead. It's probably better to use a struct for your vertex type rather than a raw array of floats as it can help catch this kind of thing.
If numVertices is actually 9 then this is the wrong value to use for glDrawArrays - you need to be using 3 instead. Again, using a struct will make this clearer and easier.
It is the second case. I'm working on the structs, but I'm not so good with them. Hopefully a little more practice...
If I remove the structs and enter the information as I did in the opening post, it works and I get a little grey triangle. I am assuming the problem is with the structs, or with my trying to get the information from the struct into the buffer.
Would I have to take the vertex information out of the struct, and put it into an array within the class before putting it into the buffer? That seems a bit excessive, but I guess the glVertexArray may be having a problem with being given an array of structs, each of which contains another struct.
Any ideas? All help is appreciated.
Edit: OK. For a start, I think I'm initialising the array of structs wrong. I'll have a look and see what that does later.
What should(?) happen is that each vertices should be a different colour.
What is happening, is that the whole triangle renders black. If I shift the stride and offset values I can get one corner blue or one corner yellow, or one corner red, or one corner green.