Jump to content
  • Advertisement
Sign in to follow this  
NukeCorr

OpenGL VBO problem/weird rendering

This topic is 1076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)?

Edited by NukeCorr

Share this post


Link to post
Share on other sites
Advertisement

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.

Edited by tonemgub

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!