QImage的坐标系
(0,0)------- | | | | | | | | |--------(1,1)
int width = 20, height = 10; int size = width * height; unsigned char* buffer = (unsigned char*)malloc(size); if (!buffer) { return; } // 0黑色 255白色 memset(buffer, 0, size); // 第一象限灰色 for (int i = 0;i < height / 2;i++) { for (int j = 0;j < width / 2;j++) { buffer[i * width + j] = 100; } } view->showA8Texture(width, height, buffer, size); free(buffer);
可以看到第一象限在左上角,符合坐标系的预期。
FT_Bitmap
FT_Bitmap的结构
typedef struct FT_Bitmap_ { unsigned int rows; unsigned int width; int pitch; unsigned char* buffer; unsigned short num_grays; unsigned char pixel_mode; unsigned char palette_mode; void* palette; } FT_Bitmap;
buffer指向的就是纹理内存,这个纹理内存的坐标系和QImage一致,都是左上角是(0,0)
cocos
cocos2dx 中对bitmap的纹理有个转换
// 绘制的起始坐标 int iX = posX; int iY = posY; for (long y = 0; y < bitmapHeight; ++y){ long bitmap_y = y * bitmapWidth; for (int x = 0; x < bitmapWidth; ++x){ unsigned char cTemp = bitmap[bitmap_y + x]; // the final pixel int dest_idx = (iX + (iY * FontAtlas::CacheTextureWidth)); dest[dest_idx] = cTemp; CCLOG("bitmap:%d => dest:%d", bitmap_y + x, dest_idx); iX += 1; } iX = posX; iY += 1; }
width:43, height:86 bitmap:0 => dest:513 = (1 + (1*512)) bitmap:1 => dest:514 bitmap:2 => dest:515 bitmap:3 => dest:516 bitmap:4 => dest:517 bitmap:5 => dest:518 bitmap:6 => dest:519 bitmap:7 => dest:520 bitmap:8 => dest:521 bitmap:9 => dest:522 bitmap:10 => dest:523 bitmap:11 => dest:524 bitmap:12 => dest:525 bitmap:13 => dest:526 bitmap:14 => dest:527 bitmap:15 => dest:528
把纹理copy到了大纹理里面,并且有(1,1)
的偏移,从下图看的更加清晰:
使用到QImage去查看纹理数据
不要被OpenGL的坐标系迷惑,engine会自动处理坐标系问题