Reputation: 47
This code works like it's supposed to work, renders correctly (didn't post every piece of related code since I think something's wrong with these parts):
std::vector<GLuint> vboId;
std::vector< std::vector<GLfloat> > verts;
...init:
verts[num].push_back(x);
verts[num].push_back(y);
//verts[num].push_back(0);
// texture offset
verts[num].push_back(0);
verts[num].push_back(offset);
verts[num].push_back(x + TILE_SIZE);
verts[num].push_back(y);
//verts[num].push_back(0);
// texture offset
verts[num].push_back(1);
verts[num].push_back(offset);
verts[num].push_back(x + TILE_SIZE);
verts[num].push_back(y + TILE_SIZE);
//verts[num].push_back(0);
// texture offset
verts[num].push_back(1);
verts[num].push_back(offset + zsize);
verts[num].push_back(x);
verts[num].push_back(y + TILE_SIZE);
//verts[num].push_back(0);
// texture offset
verts[num].push_back(0);
verts[num].push_back(offset + zsize);
...
glBindBuffer(GL_ARRAY_BUFFER, vboId[num]);
glBufferData(GL_ARRAY_BUFFER, verts[num].size() * sizeof(GLfloat), verts[num].data(), GL_STATIC_DRAW);
...render:
glBindBuffer(GL_ARRAY_BUFFER,vboId[num]);
glTexCoordPointer(2,GL_FLOAT,sizeof(verts[num]),(void*)(sizeof(GLfloat)*2));
glVertexPointer(2,GL_FLOAT,sizeof(verts[num]),0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS, 0, verts[num].size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
But when I try to add Z value by modifying the code, everything renders messed up:
...init
verts[num].push_back(x);
verts[num].push_back(y);
verts[num].push_back(0);
// texture offset
verts[num].push_back(0);
verts[num].push_back(offset);
verts[num].push_back(x + TILE_SIZE);
verts[num].push_back(y);
verts[num].push_back(0);
// texture offset
verts[num].push_back(1);
verts[num].push_back(offset);
verts[num].push_back(x + TILE_SIZE);
verts[num].push_back(y + TILE_SIZE);
verts[num].push_back(0);
// texture offset
verts[num].push_back(1);
verts[num].push_back(offset + zsize);
verts[num].push_back(x);
verts[num].push_back(y + TILE_SIZE);
verts[num].push_back(0);
// texture offset
verts[num].push_back(0);
verts[num].push_back(offset + zsize);
...
glBindBuffer(GL_ARRAY_BUFFER, vboId[num]);
glBufferData(GL_ARRAY_BUFFER, verts[num].size() * sizeof(GLfloat), verts[num].data(), GL_STATIC_DRAW);
...render:
glBindBuffer(GL_ARRAY_BUFFER,vboId[num]);
glTexCoordPointer(2,GL_FLOAT,sizeof(verts[num]),(void*)(sizeof(GLfloat)*3));
glVertexPointer(3,GL_FLOAT,sizeof(verts[num]),0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS, 0, verts[num].size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Can someone explain what I'm doing wrong here? Why do the first codes work but the last ones don't?
Upvotes: 1
Views: 106
Reputation: 47
I guess I solved it.. this code renders it like it's supposed to:
glTexCoordPointer(2,GL_FLOAT,sizeof(GLfloat) * 5,(void*)(sizeof(GLfloat) * 3));
glVertexPointer(3,GL_FLOAT,sizeof(GLfloat) * 5,(void*)0);
...
glDrawArrays(GL_QUADS, 0, verts[num].size() / 5);
But I'm unsure if it's really correct, I don't see any artifacts but don't really know what's happening behind the scenes.. Thanks for help, if anyone can confirm the code is correct please do
But what I understand here is that glTexCoordPointer(* 5, * 3) starts from third array index (the first texture coordinate) and increases by 5 (index * 5) elements forward each time. And glVertexPointer(* 5, 0) starts from array index 0 and increases also 5 each time. Therefore:
verts[num].push_back(x); // index 0
verts[num].push_back(y); // index 1
verts[num].push_back(0); // index 2
// texture offset
verts[num].push_back(0); // index 3
verts[num].push_back(offset); // index 4
I can get back to add more stuff heh https://i.sstatic.net/gNvyP.png
Upvotes: 1
Reputation: 196
I believe your original one is only rendering correctly because the verticies happen to land on relevant co-ordinates. I think the issue is with your stride and initial locations.
Since your array looks like
x-y-z-u-v
The verticies will have stride as well, as they have to jump two floats to reach the next co-ordinate. The texture co-ordinates will also have an initial pointer location.
glTexCoordPointer(2, GL_FLOAT, sizeof(GLfloat) * 3, sizeof(GLfloat) * 3);
glVertexPointer(3, GL_FLOAT, sizeof(GLfloat) * 2, 0);
This is to say, for the textures, start at index 3 (u), read two elements (u and v), then move ahead three elements, which will be the next u, as it skips x, y and z.
For the verticies, start at 0 (x), read three elements, x, y and z, then skip two to start at the next x.
Upvotes: 0