简体   繁体   English

将1d数组的一部分渲染为2D?

[英]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: 我有一个一维数组,其大小为10000(100x100),并包含纹理ID:

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

The texture file contains 512x512 pixels, with 16x16 pixels for each image. 纹理文件包含512x512像素,每个图像有16x16像素。 So, that is 32x32 images in total. 因此,总共是32x32图像。 And the texture ids correspond by going left to right starting from 0: 纹理ID从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. 给定800x600像素的屏幕分辨率,我只想渲染适合此分辨率的四边形的一个子集,因此我不想从我的1D数组中绘制所有10000个四边形。

To draw from the (0,0) tile, this is what I have: 从(0,0)磁贴绘制,这就是我所拥有的:

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. 问题是quadIndex不正确。 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. 我敢肯定是因为我可以错误地计算quadIndex ,但是我不知道解决方案是什么。

Also, as an added bonus, how would I specify rendering from any (a,b) offset instead of always from (0,0)? 另外,作为额外的好处,我如何指定从任何(a,b)偏移而不是始终从(0,0)进行渲染? That is, with my 1D array, I want to draw from (4,4) onto my 800x600 resolution. 也就是说,对于我的一维数组,我想从(4,4)绘制到我的800x600分辨率上。

You should use width of source array , not screen width. 您应该使用源数组的宽度,而不是屏幕宽度。 As far as I understand, it is width of mapping = 100? 据我了解,它的mapping宽度= 100吗?

int quadIndex = j * 100 + i;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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