我試圖呈現一個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));
}
}
請更具體一點:究竟你有什麼問題?地圖沒有顯示出來嗎?它渲染不正確?如果是這樣,什麼 - 它看起來像什麼 - 它應該是什麼樣子?恐怕僅僅發佈幾十行代碼並期望人們解決一個模糊的問題是不合理的。謝謝。 – 2010-12-08 22:06:47