Skip to main content
GameDev.net gamedev.net
🔒 Locked

VBO problem/weird rendering

Started by NukeCorr Jan 5, 2016 at 12:42 PM 1 replies 2.5k views
Original Post
NukeCorr
NukeCorr

Hey.

I set up a simple VBO to render a tilemap array, everything renders as it's supposed to render but for some reason there is weird grid rendered at 0,0,0. I noticed this after changing from 2D to 3D perspective. Using C++, OpenGL and SFML

I marked the weirdness with red circle.

CIa4Sep.png

Here's a zoomed in image of it

wQEPXi0.png

which looks like a 16x16 grid.

Here is the relevant code:


#define TILES_X 64
#define TILES_Y 128

#define TILE_SIZE_W 16
#define TILE_SIZE_H 16

GLuint vboId;
std::vector<GLfloat> verts;
...
int Tiles[TILES_X * TILES_Y];

Init:


   for(int i=0;i!=TILES_X;++i)
      for(int k=0;k!=TILES_Y;++k)
      {
         int t = k * TILES_X + i;
         int tex = Map.GetTile(t);

         if(tex > -1)
	 {
	 int x = i * TILE_SIZE_W;
	 int y = k * TILE_SIZE_H;
         
         // vertices
	 verts.push_back(x);
	 verts.push_back(y);
	 verts.push_back(x + TILE_SIZE_W);
	 verts.push_back(y);
	 verts.push_back(x + TILE_SIZE_W);
	 verts.push_back(y + TILE_SIZE_H);
	 verts.push_back(x);
	 verts.push_back(y + TILE_SIZE_H);

	 GLfloat offset = ((tex * 16.0f) * 1.0f) / (TILE_MAX * 16.0f);
	 GLfloat zsize = (TILE_MAX * 16.0f) / TILE_MAX / 16.0f / TILE_MAX;
         
         // texture coords
	 verts.push_back(0);
	 verts.push_back(offset);
	 verts.push_back(1);
	 verts.push_back(offset);
	 verts.push_back(1);
	 verts.push_back(offset+zsize);
	 verts.push_back(0);
	 verts.push_back(offset+zsize);
	 }
      }

glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(GLfloat), verts.data(), GL_STATIC_DRAW);

Render:


glPushMatrix();
glBindBuffer(GL_ARRAY_BUFFER,vboId);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,Tile_Texture.Texture);

glRotatef(180.0f,1.0f,0.0f,0.0f);
glTranslatef(-Camera.x,-Camera.y,0);

glTexCoordPointer(2,GL_FLOAT,0,(void*)(sizeof(GLfloat)*8));
glVertexPointer(2,GL_FLOAT,0,0);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glDrawArrays(GL_QUADS, 0, verts.size());

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glPopMatrix();

Someone see what's wrong with the code (except deprecated OpenGL functions, unless they cause it)?

What the h*ll are you?
tonemgub
tonemgub

I'm not sure if this is the problem but you are storing 4 vertex coordinates of a quad followed by 4 texture coordinates in your vertex buffer. Instead, you should store the texture coordinate corresponding to each vertex immediately after the vertex coordinates of that vertex. I think part of your texture coordinates are being drawn as the vertices which form that weird grid. Your vertex format declaration might also be bad.

NukeCorr
NukeCorr

you are storing 4 vertex coordinates of a quad followed by 4 texture coordinates in your vertex buffer. Instead, you should store the texture coordinate corresponding to each vertex immediately after the vertex coordinates of that vertex.

Thanks for the quick reply, that was the problem. It's fixed now

What the h*ll are you?

Topic Locked

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

Sign in to reply to this topic.