标题
[directx]手工创建纹理并手工填充颜色
clq
浏览(0) +
2010-08-11 13:01:15 发表
编辑
关键字:
我对这个不熟,代码来自网上的七拼八凑.不过网上确实没有直接的代码,两个 for 循环的位置让我很迷惑,不是 i,j 顺序而是 j,i 顺序,我在操作 opencv 的图像时也发现有这个问题,奇怪原理何在? ///------------------------------------- ///创建纹理 ///------------------------------------- void GraphEngine::CreateGTexture() { //D3DXCreateTextureFromFile(DXUTGetD3DDevice(), (L"Direct3D_2D7.jpg"), &g_Texture);return; //-------------------------------------------------- int width = 100; int height = 100; //D3DXCreateTexture(device, width, height, 1, 0, D3DFMT_DXT1, D3DPOOL_SYSTEMMEM, &texture); Texture texture; HRESULT hr = 0; //hr = D3DXCreateTexture(DXUTGetD3DDevice(), 200, 200, 0, 0, D3DFMT_A8L8, D3DPOOL_MANAGED, &texture); //hr = D3DXCreateTexture(DXUTGetD3DDevice(), 1024, 1024, 0, 0, D3DFMT_R8G8B8, D3DPOOL_SYSTEMMEM, &texture); //hr = D3DXCreateTexture(DXUTGetD3DDevice(), 1024, 1024, 0, 0, D3DFMT_A8R8G8B8/*D3DFMT_R8G8B8*/, D3DPOOL_SYSTEMMEM, &texture); hr = D3DXCreateTexture(DXUTGetD3DDevice(), width, height, 0, 0, D3DFMT_R8G8B8, D3DPOOL_MANAGED, &texture); if(FAILED(hr)) { return; } Texture _tex; //_tex = TexturePool[whichone]; _tex = texture; int t = _tex->Format; printf("%d\r\n", t); D3DLOCKED_RECT lockedRect; _tex->LockRect(0, &lockedRect,0, 0);//D3DPOOL_DEFAULT 创建的在显存中不能 lock //使用类型要对应(unsigned short --- D3DFMT_A8L8) unsigned char* imageData = (unsigned char*)lockedRect.pBits; //D3DCOLOR* pColor = (D3DCOLOR*)lockedRect.pBits; unsigned char* pColor = (unsigned char*)lockedRect.pBits; //for (int i=0; i<_tex->Width; i++) for (int row=0; row<width; row++) { //for (int j=0; j<_tex->Height; j++) for (int col=0; col<height; col++) { //int index = i * lockedRect.Pitch /3/*D3DFMT_A8L8 二字节*/+ j; //imageData[index] = 0x0000ff; //int index = i*200*3 + j*3; //int index = i * lockedRect.Pitch + j*3; //unsigned char * p = &imageData[index]; //p[0] = 0; //p[1] = 0; //p[2] = 200; //int index = i * lockedRect.Pitch/4 + j; //int index = i * lockedRect.Pitch + j*3;//D3DFMT_R8G8B8 一样是每个像素占 4 个字节,可能是为了字节对齐 //int index = i * lockedRect.Pitch + j*4; int index = col* lockedRect.Pitch + row*4; //pColor[index] = D3DCOLOR_XRGB(0xff,0,0); unsigned char * p = (unsigned char *)(&pColor[index]); p[0] = 0;//b p[1] = 0;//g p[2] = 0xff;//r //p[3] = 0xff;//r } } _tex->UnlockRect(0); D3DXSaveTextureToFileA("c:\\1.jpg", D3DXIFF_JPG, _tex, NULL); g_Texture = _tex; }
clq
要注意的是每个像素的大小,d3d 会改动它的. -------------------------------------------------- http://topic.csdn.net/u/20090909/21/2cf1f92b-cebf-427c-b0e9-0a900ab1fd52.html Pitch大小不等于每行的字节数(1280*3),那它的用途在什么地方呢 是等于每行字节数阿,一行2048个像素,每个像素4字节。 因为D3DXCreateTexture函数把你的纹理从1280扩充到了2048
NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.