I'm trying to use a texture atlas in a game program I'm writing in C#/OpenGl with the OpenTK library.
I loaded my texture atlas as a texture in OpenGL (dimension is 256x256) and each tile is 32x32.
To get the first tile of the atlas to display, I used this code:
GL.Begin(BeginMode.Quads);
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
GL.TexCoord2(0.125f, 0); GL.Vertex2((32 * zoom), 0);
GL.TexCoord2(0.125f, 0.125f); GL.Vertex2((32 * zoom), (32 * zoom));
GL.TexCoord2(0, 0.125f); GL.Vertex2(0, (32 * zoom));
GL.End();
0.125 was computed by dividing 1/8, 8 being the number of tiles in a row/column.
I've got no idea how to compute the second tile's coordinates this way! I tried using 0.125 and 0.25 in place of 0 and 0.125 respectively, but this renders nothing. I'm guessing you aren't allowed to use a value greater than zero for (EDIT)the first (0) texture coordinates?
If anyone could help or provide a better way of doing this, it would be much appreciated!
Give this a shot:
int col = 0;
int row = 0;
float tex_w = 256;
float tex_h = 256;
float tile_w = 32;
float tile_h = 32;
float w_factor = tile_w / tex_w;
float h_factor = tile_h / tex_h;
float x_tex_beg = w_factor*(col+0);
float x_tex_end = w_factor*(col+1);
float y_tex_beg = h_factor*(row+0);
float y_tex_end = h_factor*(row+1);
look through the 8x8 tiles in a nested loop like this:
GL.Scale(zoom*32,zoom*32,1);
GL.Begin(BeginMode.Quads);
for (int i=0; i<8; i++)
for (int j=0; j<8; j++)
{
var x = i/8.0; // texture scale
var y = j/8.0;
GL.TexCoord2(x, y); GL.Vertex2(i, j);
GL.TexCoord2(x+0.125f, y); GL.Vertex2(i+1, j);
GL.TexCoord2(x+0.125f, y+0.125f); GL.Vertex2(i+1, j+1);
GL.TexCoord2(x, y+0.125f); GL.Vertex2(i, j+1);
}
GL.End();
GL.Scale(1.0/(zoom*32),1.0/(zoom*32),1); // unzoom
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With