Skip to main content
gamedev.net
🔒 Locked

VBO Problem

Started by L0rdDiabl0 Aug 28, 2004 at 1:30 PM 10 replies 1.7k views
Original Post
L0rdDiabl0
L0rdDiabl0
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
NLDEV
NLDEV
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
L0rdDiabl0
L0rdDiabl0
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)
NLDEV
NLDEV
Hmm. Think you can post a bit more code (like Cube_Vertices declaration)?
L0rdDiabl0
L0rdDiabl0
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;}
NLDEV
NLDEV
This isn't going to help your problem, but I'm curious why you have two positions.
L0rdDiabl0
L0rdDiabl0
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?
L0rdDiabl0
L0rdDiabl0
So... any other ideas what could be wrong?
L0rdDiabl0
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?

NineYearCycle
NineYearCycle
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."
L0rdDiabl0
L0rdDiabl0
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?
Ysaneya
Ysaneya
You probably didn't read well - it's in amount of bytes to allocate.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.