VBO error

Started by
13 comments, last by MrSimon 11 years, 11 months ago
Hi!

Besides, glDrawElements wants to know the format of the index buffer, so instead of GL_FLOAT you need either GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT.
Currently you use int in the sPolygons struct, so you should probably change that to unsigned int and then pick GL_UNSIGNED_INT in glDrawElements.

Hope that helps! :)
Advertisement
And now it works.

Thank you guys.

I should be quiet for a couple of days while I try to work out loading textures! :)

Thanks again guys. You make a difficult process (for me anyway - it's only been a week, so far) a whole lot easier.
OK. I have one more. After this, I promise, I'll leave you all alone.

VBO works, colours work, IBO works. Texture doesn't work?

Any ideas what's wrong?


struct float3

{

float x, y, z;

};

struct float2

{

float x, y;

};
struct sVertex
{

float3 Position, colours, normals;

float2 Texture;
};

struct sPolygons

{

int x, y, z;

//float Normal;

};
class cModel
{

private:

GLuint TextureID;

GLuint VBOID;

GLuint IBOID;

int nVertices, nPolygons;

unsigned char* TextureData;

public:

cModel(int nVertices, sVertex Vertices[], int nPolygons, sPolygons Polygons[], const char * TextureFile, int TextureWidth, int
TextureHeight);

void Update();

void Render();

};
cModel::cModel(int numVertices, sVertex Vertices[], int numPolygons, sPolygons Polygons[], const char * TextureFile, int TextureWidth, int
TextureHeight)
{
nVertices = numVertices;

glGenBuffers(1, &VBOID);

glGenBuffers(1, &IBOID);

glBindBuffer(GL_ARRAY_BUFFER, VBOID);

glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(sVertex), Vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);

nPolygons = numPolygons;

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOID);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, numPolygons*sizeof(sPolygons), Polygons, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

FILE * file = NULL;
file = fopen(TextureFile, "rb");
TextureData = new unsigned char [TextureWidth * TextureHeight * 3];

fread(TextureData, TextureWidth * TextureHeight * 3, 1, file);

fclose(file);

glGenTextures(1, &TextureID);

glBindTexture(GL_TEXTURE_2D, TextureID);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureWidth, TextureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureData);

};
void cModel::Render()
{
glBindBuffer(GL_ARRAY_BUFFER, VBOID);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOID);

glBindTexture(GL_TEXTURE_2D, TextureID);

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_COLOR_ARRAY);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 11*sizeof(float), (char*)NULL+0);

glColorPointer(3, GL_FLOAT, 11*sizeof(float), (char*)NULL+3*sizeof(float));

glTexCoordPointer(2, GL_FLOAT,11*sizeof(float), ((char*)NULL)+9*sizeof(float));

glDrawElements(GL_TRIANGLES, nPolygons*3, GL_UNSIGNED_INT, 0);

//glDrawArrays(GL_TRIANGLES, 0, nVertices);

glDisableClientState(GL_VERTEX_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
};
sVertex Vertices[3] =
{
{{0.0f, 5.0f, -1.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.5f, 1.0f}},

{{5.0f, -5.0f, -1.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f}},

{{-5.0f, -5.0f, -1.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}
};
sPolygons Array2[1] =
{
{0, 1, 2}
};


I really promise, this will be the last thing for a while (a little while anyway).
You probably have to set filters for the texture.
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


Oh, and texturing should be enabled of course. smile.png
glEnable(GL_TEXTURE_2D);

Best regards
And that did it.

As promised, I will bugger off for a while.

Thanks guys. You really are brilliant.

This topic is closed to new replies.

Advertisement