VBO Problem

Started by
10 comments, last by Ysaneya 19 years, 7 months ago
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
Advertisement
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
Yes you're right, thats what i'm supposed to use, but somehow for an unknown reason what i'am doing seem to produce better result (less messed up cube)
Hmm. Think you can post a bit more code (like Cube_Vertices declaration)?
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;}
This isn't going to help your problem, but I'm curious why you have two positions.
It's old but I didn't update the code recently( i was having trouble with glBufferDataARB not accepting Vector Position)
So no idea what could be wrong?
So... any other ideas what could be wrong?
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?

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.

"Ars longa, vita brevis, occasio praeceps, experimentum periculosum, iudicium difficile"

"Life is short, [the] craft long, opportunity fleeting, experiment treacherous, judgement difficult."

This topic is closed to new replies.

Advertisement