简体   繁体   中英

Render a portion of 1d array as 2D?

I am trying to render a bunch of quads on a screen, but I cannot get it to render correctly.

I have a 1D array that is 10000 (100x100) in size and holds texture ids:

mapping = { 1, 22, 55, 28, 95, 105, ...} 

The texture file contains 512x512 pixels, with 16x16 pixels for each image. So, that is 32x32 images in total. And the texture ids correspond by going left to right starting from 0:

 0  1  2  3 ... 31
32 33 34 35 ... 63

............... 1023

Given a screen resolution of 800x600 pixels, I want to render only a subset of my quads that will fit on this resolution, so I don't want to draw all 10000 quads from my 1D array.

To draw from the (0,0) tile, this is what I have:

for (int j = 0; j < 37; j++) {     // 600/16 = 37
    for(int i = 0; i < 50; i++) {  // 800/16 = 50
        int quadIndex = j*800/16 + i;
        int textureID = mapping[quadIndex];
        int x = (textureID % 512) * 16;
        int y = (textureID / 512) * 16;

        // Take image from texture starting at (x,y) and draw on screen at (i, j)
        draw(i, j, x, y);    
    }
}

The problem with this is the quadIndex is not accurate. It draws the first row correctly, but the second row is just a continuation of the first row instead of the actual second row. Basically the first row is overflowing onto the second row, and it's throwing everything off.

I am sure it is because I can calculating the quadIndex incorrectly, but I don't know what the solution is.

Also, as an added bonus, how would I specify rendering from any (a,b) offset instead of always from (0,0)? That is, with my 1D array, I want to draw from (4,4) onto my 800x600 resolution.

You should use width of source array , not screen width. As far as I understand, it is width of mapping = 100?

int quadIndex = j * 100 + i;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM