PDA

View Full Version : vertex buffer object won't render to screen



zee2200
05-18-2016, 06:49 PM
I have a sprite class that is supposed to initialize an array of vertices and then draw them when the draw method is called however when i do call the draw method nothing is rendered to the screen. This is the source code for it. just in case you need this info the window size is 700x700 and i have my glOrtho set up as such
glOrtho(0, _width, _height, 0, 0, 1);

void Sprite::init(int x, int y, int vertNum, float r, Shape shape) {
_vertNum = vertNum;
if (_vboID == 0) {
glGenBuffers(1, &_vboID);
}
std::vector<Vertex> vertexData(vertNum);
if (shape == Shape::CIRCLE) {
for (int i = 0; i < vertNum; i++) {
float angle = 2.0 * M_PI * i / _vertNum;
vertexData[i].setPosition(r*cos(angle), r*sin(angle));
}
}
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void Sprite::draw() {
//bind buffer array
glBindBuffer(GL_ARRAY_BUFFER, _vboID);

//use first vertex attrib array
glEnableVertexAttribArray(0);

//pointer to vertices location
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
//pointer to vertices color
//glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));

//Draw the 6 vertices to the screen
glDrawArrays(GL_POLYGON, 0, _vertNum);

//Disable the vertex attrib array. This is not optional.
glDisableVertexAttribArray(0);

//Unbind the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

giordi
05-19-2016, 06:57 AM
Are you sure this is getting called?

if (_vboID == 0) {
glGenBuffers(1, &_vboID);

are you manually initializing hte _vboID to 0 or relying on the compiler to do so?
Also I think you are not copying the data properly. sizeof(vertex_data) is not the size of your data, but the size of the vector class, the actually array data is somewhere else in memory, try with sizeof(float)*vertexData.size()

zee2200
05-20-2016, 09:17 PM
Are you sure this is getting called?

if (_vboID == 0) {
glGenBuffers(1, &_vboID);

are you manually initializing hte _vboID to 0 or relying on the compiler to do so?
Also I think you are not copying the data properly. sizeof(vertex_data) is not the size of your data, but the size of the vector class, the actually array data is somewhere else in memory, try with sizeof(float)*vertexData.size()

sorry for the late response but thank you so much it's fixed now! the problem was actually with the size of vertexData, however i am a noob and would like to understand what this means. what does sizeof give me? and why does sizeof(float)*vertexData work?

john_connor
05-21-2016, 12:45 AM
sorry for the late response but thank you so much it's fixed now! the problem was actually with the size of vertexData, however i am a noob and would like to understand what this means. what does sizeof give me? and why does sizeof(float)*vertexData work?


that has nothing todo with opengl ... there you can find basic c/c++ stuff: http://www.cplusplus.com/



// random struct
struct ABC // total size = 128bit
{
int x; // size = 32bit
float y; // size = 32bit
double z; // size = 64bit
};

showsize(const ABC* pointertomyarray)
{
cout << sizeof(pointertomyarray) << endl; // wil print 32bit on 32bit systems (or 64bit om 64bit systems)
}

somefunction()
{
ABC myarray[100]; // array size = 128bit * 10 = 1280bit
showsize(myarray);


cout << sizeof(myarray) << endl; // this will print the actual size: 1280bit, because its invoked in same scope {...}
}



... a std::vector<sometype> is a container / dynamically sized array that
knows (stores) its own size, raw pointer dont know the size of the memory
block they point to ...

pointer in general are only adresses, theitr size is dependent on the system architecture you're programming for: 4 byte on 32bit systems, 8 byte on 64bit