Sign in to follow this  

VBO Problem

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm trying to implement VBOs in my demo, but something is not right in my code as i keep getting messed up Cube, so i'm seeking your help again guys :) here's the traditional working code
glVertexPointer(3, GL_FLOAT, sizeof(Cube_Vertices), &cube.vertices[0].VBO_Position);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS, 0, 24);

and here's the messed up VBO implementation
glGenBuffersARB(1, &VBO_Vertices);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO_Vertices);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(Cube_Vertices)*3*sizeof(float), &cube.vertices[0].VBO_Position, GL_STATIC_DRAW_ARB);
glVertexPointer(3, GL_FLOAT, sizeof(Cube_Vertices), BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS, 0, 24);

so what am i doing wrong

Share this post


Link to post
Share on other sites
I'm curious about this line
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(Cube_Vertices)*3*sizeof(float), &cube.vertices[0].VBO_Position, GL_STATIC_DRAW_ARB);


Why are you determining the size of your VBO like that? Shouldn't you be allocating a buffer of size
sizeof(Cube_Vertices) * VertexCountInCube

Share this post


Link to post
Share on other sites
Ok here some more code

Cube.h


#ifndef _CUBE_H
#define _CUBE_H

#include <gl/gl.h>
#include "Vector.h"


class Cube_Vertices
{
public:
Vector Position; float VBO_Position[3];
TexCoord TexCoord; float VBO_TexCoord[2];
Vector Tangent;
Vector Normal;
Vector Binormal;
Vector TangentMatrixLight; float VBO_TangentMatrixLight[3];
};


class Cube
{
public:
Cube();
~Cube();

bool InitCube();

Cube_Vertices * vertices;
};


#endif



and here's Cube.cpp


#include <windows.h>
#include "Cube.h"


Cube::Cube()
{
InitCube();
}


Cube::~Cube()
{
if(vertices)
delete [] vertices;
vertices=NULL;
}



bool Cube::InitCube()
{
vertices = new Cube_Vertices[26];
if(!vertices)
{
MessageBox(NULL, "Unable to allocate memory for Cube vertices\n", "Memory Error", MB_OK | MB_ICONEXCLAMATION);
return false;
}
return true;
}

Share this post


Link to post
Share on other sites
The only way it produced correct result for me, is when i enter

sizeof(Cube_Vertices)*9*sizeof(float) or any value greater than 9

as the size of my VBO in the command : glBufferDataARB

Can somebody explain why this is happening?

Share this post


Link to post
Share on other sites
Quote:
Original post by L0rdDiabl0
The only way it produced correct result for me, is when i enter

sizeof(Cube_Vertices)*9*sizeof(float) or any value greater than 9

as the size of my VBO in the command : glBufferDataARB

Can somebody explain why this is happening?


I think it's because you're determining the size of the buffer wrong as MikeMJH suggested.

How about trying to replace:
sizeof(Cube_Vertices)*9*sizeof(float)
with:
size0f(Cube_Vertices)*26
Where the "26" is your hardcoded value for the number of vertices that i saw in the cube initialisation. It might be that the 9*sizeof(float) is just allocating a large enough buffer by luck.

Share this post


Link to post
Share on other sites
It worked and produced correct results, thanks NineYearCycle
but how did that happen, i read in the VBO extension that the size should refer to Vertex Count In Object (sizeof(Cube_Vertices)) so whay i have to multiply it by 26 to work right?

Share this post


Link to post
Share on other sites

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this