[英]Render to texture for mini map, dirextX
我正在嘗試將3d環境的鳥瞰圖渲染為紋理,並將該紋理繪制為HUD來表示迷你地圖,就好像它是一台照相機從上面向下看游戲一樣。 我是3D編程和DirextX API的新手,因此我需要幫助。 通過一些示例和教程,這是我所擁有的,但仍然沒有成功。
幾乎我不知道如何將紋理作為HUD正確地繪制到屏幕上。 發生的情況是,引擎先運行beginProjectScene()和endProjectScene()方法,然后運行beginSuperImpose()方法。
beginProjectScene()創建要繪制的紋理,創建適當的視圖和投影矩陣以及后緩沖區以及一個新的視口,並在備份當前視口后進行設置。 它還設置適當的渲染目標。
endProjectScene()還原備份的矩陣,視口,后備緩沖區。
beginSuperImpose()應該在屏幕上繪制紋理(texH)。
通過顯示設備(d3dd)調用D3D API方法還有一些其他方法可以實現此目的。 但這似乎不起作用,實際上沒有任何可能發生,我不確定為什么。 以前,在將渲染目標還原到endProjectScene()中的備份后備緩沖區之前,屏幕只是變黑了(出於明顯的原因)。 我相信我的大部分問題都在使用多個視口,而且很多概念相對較新,我仍在努力完全掌握。
同樣,在還原后緩沖區后調用SetTexture(0,texH)時,我的白色球體變為藍色。
我對此有所提及: http : //www.two-kings.de/tutorials/dxgraphics/dxgraphics16.html
主機循環:
int Engine::run() {
....
// update the model components
design->update(rightNow);
Viewing->update(rightNow);
audio->update(rightNow);
lighting->update();
hud->update(rightNow);
//NEW CODE FOR HUD BEGIN
// create the projection here - this part is new
display->beginProjectScene();
// draw the opaque scene objects
scene->draw(true);
// then the translucent/transparent objects
display->alphaBlendOn();
scene->draw(false);
display->alphaBlendOff();
display->endProjectScene();
//NEW CODE FOR HUD END
// draw the scene
display->beginDraw();
// draw the opaque scene objects
scene->draw(true);
// then the translucent/transparent objects
display->alphaBlendOn();
scene->draw(false);
display->alphaBlendOff();
// draw the HUD
display->beginSuperimpose();
hud->draw();
display->endDraw();
....
}
顯示:
void Display::beginProjectScene() {
// create the texture COM object on which to draw
// if the texture COM object does not yet exist
//
if (!texH && FAILED(D3DXCreateTexture(d3dd, TEXTURE_WIDTH,
TEXTURE_HEIGHT, 0, D3DUSAGE_RENDERTARGET | D3DUSAGE_AUTOGENMIPMAP,
D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &texH)))
error(L"Projection::11 Failed to create projection texture");
// get the interface to the surface for the texture - GetSurfaceLevel
// increases the reference count by 1 so we will have
// to Release the interface when done
//
if (texH && SUCCEEDED(texH->GetSurfaceLevel(0, &textureSurface))) {
// build the view matrix
//
// define position, heading, and up direction of camera and
// create a view matrix and set the view transformationf
//TEMPORTY VALUES
Vector up(0,1,0);
Vector heading(0,0,0);
Vector position(0.5,1,0.5);
Vector view(1,0,0);
// the look at point from the virtual camera
Vector lookAt = position + heading;
Matrix viewH =
::view(view, position+heading, up);
// build the projection matrix
// ...TEST VALUES
Matrix projectionProjection =
::projection(FIELD_OF_VIEW, aspect, NEAR_CLIPPING,
FAR_CLIPPING);
// back up the projection matrix and the viewport, and back buffer
d3dd->GetTransform(D3DTS_PROJECTION, &projBak);
d3dd->GetViewport(&viewportBak);
d3dd->GetRenderTarget(0, &pBackBuffer);
// associate the backbuffer with the texture surface
d3dd->SetRenderTarget(0, textureSurface);
// project the scene onto the texture
d3dd->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_ARGB(100, 100, 100, alpha), 1.0f, 0);
d3dd->BeginScene();
//d3dd->SetTexture(0, texH);
d3dd->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&viewH);
d3dd->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&projectionProjection);
// define the viewport for the texture
D3DVIEWPORT9 viewport;
viewport.X = 0;
viewport.Y = 0;
viewport.Width = TEXTURE_WIDTH;
viewport.Height = TEXTURE_HEIGHT;
viewport.MinZ = 0.0f;
viewport.MaxZ = 1.0f;
d3dd->SetViewport(&viewport);
//d3dd->EndScene();
}
}
void Display::endProjectScene() {
d3dd->SetRenderTarget(0, pBackBuffer);
d3dd->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_ARGB(100, 100, 100, alpha), 1.0f, 0);
//d3dd->BeginScene();
//d3dd->SetTexture(0, texH);
// restore the projection and viewport
d3dd->SetTransform(D3DTS_PROJECTION, &projBak);
d3dd->SetViewport(&viewportBak);
d3dd->EndScene();
// release the backbuffer associated with the texture
textureSurface->Release();
pBackBuffer->Release();
//texH->Release();
}
void Display::beginSuperimpose() {
// prepare to draw the hud
//
if (spriteManager_){
// start the sprite manager
spriteManager_->Begin(D3DXSPRITE_ALPHABLEND);
//NEW CODE FOR HUD
Vector topRight(width() * 0.01f, height() * 0.01f, 0);
spriteManager_->Draw(texH, NULL, NULL, (D3DXVECTOR3*)&topRight,
D3DCOLOR_RGBA(SPRITEH_R, SPRITEH_G, SPRITEH_B, 1));
}
}
繪制場景后應繪制HUD,否則場景將覆蓋HUD。
嘗試在Draw調用中將1更改為255。 D3DCOLOR_RGBA的取值范圍為0到255。alpha的值1幾乎是透明的。
更改
Vector topRight(width() * 0.01f, height() * 0.01f, 0);
至:
Vector topRight(0.5f, 0.5f, 0);
看看它是否渲染。 這應該顯示精靈,其左上角從屏幕的ay的3/4處開始,一直到屏幕的1/4處。
我懷疑您的寬度計算正在將其顯示在屏幕之外。 DirectX渲染空間的x和y從-1變為1。 因此,如果您的寬度為1024,則乘以0.01f將得到1.024的值,該值不在屏幕的右側。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.