Direct3D 7 Zeichnet nicht

LJSilver

Grünschnabel
Moinsen...

Ich bin zihmlich neu in cpp, hab aber vorher ne lange Zeit vb programmiert(Auch DX apps) und wollte mich jetzt mal mit cpp und DX probieren.

Mein problem besteht z.Z. darin, das sich D3D nicht dazu überreden lässt auf minen Bachbuffer zu zeichnen. Das clear, blit, usw klappt aber wunderbar.


LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_ACTIVATE: {
return (0);

} case WM_SYSCOMMAND: {
switch (wParam) {
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return (0);
}
break;

} case WM_CLOSE: {
PostQuitMessage(0);
return (0);
} case WM_KEYDOWN: {
PostQuitMessage(0);
return (0);
}
}

return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}



bool clsMt3d::clsDriver::clsGraphic::createWindow(clsMt3d *Mt3d) {
/****************************************************************************
** Creates a new window with the size of the new resolution.
** The window has no border, is in the foreground, focused
** and on pupop state.
*/


hWnd = NULL;

DWORD dwExStyle; // Contains the window style
DWORD dwStyle; // and the extandet window style

RECT rctWindow; // Represent the area of the window
rctWindow.left = (long)0; // Set left value to 0
rctWindow.right = (long)Mt3d->Options.Graphic.iXRes; // Set right value to the resulotion width
rctWindow.top = (long)0; // Set top value to 0
rctWindow.bottom = (long)Mt3d->Options.Graphic.iYRes; // Set bottom value to the resolution height

hInstance = GetModuleHandle (NULL); // Get the window handle

WNDCLASS wcWindow; // Contains the window message infos
wcWindow.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcWindow.lpfnWndProc = (WNDPROC) WndProc;
wcWindow.cbClsExtra = 0;
wcWindow.cbWndExtra = 0;
wcWindow.hInstance = hInstance;
wcWindow.hIcon = LoadIcon (NULL, IDI_WINLOGO);
wcWindow.hCursor = LoadCursor (NULL, IDC_ARROW);
wcWindow.hbrBackground = NULL;
wcWindow.lpszMenuName = NULL;
wcWindow.lpszClassName = WNDCLASSNAME;

if ( ! RegisterClass(&wcWindow)) { // Try to register the window class
MessageBox(NULL, "ERROR: Register window class!", "ERROR!", MB_OK | MB_ICONEXCLAMATION);
return (false);
}

dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_POPUP;

ShowCursor(false); // Hide the cursor

AdjustWindowRectEx(&rctWindow, dwStyle, FALSE, dwExStyle); // Resize window

if ( ! (hWnd = CreateWindowEx(dwExStyle, // Try to create the window
WNDCLASSNAME, // The window class name
VERSION, // The window title
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
0, 0, // The window position
rctWindow.right, rctWindow.bottom, // The window width / height
NULL, // No parent window
NULL, // No menu
hInstance,
NULL))) { // Don‘t pass anything to WM_CREATE

MessageBox(NULL, "ERROR: Create window!", "ERROR!", MB_OK | MB_ICONEXCLAMATION);
return (false);
}

ShowWindow(hWnd, SW_SHOW); // Make the window to the absolutly current one
SetForegroundWindow(hWnd); // ...
SetFocus(hWnd); // ...

return (true);
}



bool clsMt3d::clsDriver::clsGraphic::deleteWindow(clsMt3d *Mt3d) {
/****************************************************************************
** Destroys the window.
*/

ShowCursor(true); // Unhide the cursor

if(this->hWnd) // Destroy the window
DestroyWindow(this->hWnd);
this->hWnd = NULL;

if(this->hInstance) // Release the WindowClass
UnregisterClass(WNDCLASSNAME, this->hInstance);
this->hInstance = NULL;

return (true);
}



bool clsMt3d::clsDriver::clsGraphic::initDirectDraw(clsMt3d *Mt3d) {
/****************************************************************************
** Init all the stuff for the DirectDraw part of the engine.
*/

Mt3d->Driver.ErrorCode = DirectDrawCreateEx(NULL, // Try to create the DirectDraw object
(VOID**)&this->DDraw,
IID_IDirectDraw7,
NULL);
_("DirectDrawcreateEx");

Mt3d->Driver.ErrorCode = this->DDraw->SetCooperativeLevel(this->hWnd, // Try to set the cooperative level of the engine
DDSCL_EXCLUSIVE | // Set it to exclusive handle for window messages
DDSCL_FULLSCREEN | // Fullscreen mode for the window
DDSCL_NORMAL |
DDSCL_ALLOWREBOOT); // And you ar allowed to reboot during this app runs
_("DDraw->SetCooperativeLevel");

Mt3d->Driver.ErrorCode = this->DDraw->SetDisplayMode(Mt3d->Options.Graphic.iXRes, // Set display mode to window size
Mt3d->Options.Graphic.iYRes,
Mt3d->Options.Graphic.iBpp,
Mt3d->Options.Graphic.iRef,
0); // With extended vga modes
_("DDrwa->SetDisplayMode");

DDSURFACEDESC2 ddSurfaceDesc; // Create a new surface description
ZeroMemory(&ddSurfaceDesc, sizeof(ddSurfaceDesc)); // Allocate the memory
ddSurfaceDesc.dwSize = sizeof(ddSurfaceDesc);
ddSurfaceDesc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; // Enable the Caps and the backbuffer counter
ddSurfaceDesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | // It‘s a primary surface
DDSCAPS_FLIP | // You can flip it
DDSCAPS_3DDEVICE | // You can render to this surface
DDSCAPS_COMPLEX; // And it‘s complex, what ever it means :)
ddSurfaceDesc.dwBackBufferCount = 1; // Numbers of backbuffers

Mt3d->Driver.ErrorCode = this->DDraw->CreateSurface(&ddSurfaceDesc, &this->ddsPrimaryBuffer, 0); // Create the primary surface
_("DDraw->CreateSurface");

ddSurfaceDesc.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; // Set the description to the backbuffer
Mt3d->Driver.ErrorCode = this->ddsPrimaryBuffer->GetAttachedSurface(&ddSurfaceDesc.ddsCaps, &this->ddsBackBuffer); // And create the backbuffer
_("DDraw->GetAttachedSurface");

return (true);
}



bool clsMt3d::clsDriver::clsGraphic::destDirectDraw(clsMt3d *Mt3d) {
/****************************************************************************
** Destroys the DirectDraw object an all objects around it.
*/

if(this->ddsBackBuffer) {
this->ddsBackBuffer->Release(); // Destry the backbuffer
this->ddsBackBuffer = NULL;
}

if(this->ddsPrimaryBuffer) {
this->ddsPrimaryBuffer->Release(); // Destroy the primary buffer
this->ddsPrimaryBuffer = NULL;
}

if(this->DDraw) {
this->DDraw->RestoreDisplayMode(); // Change to the old display mode
this->DDraw->Release(); // Destry the Direct draw object
this->DDraw = NULL;
}

return (true);
}



bool clsMt3d::clsDriver::clsGraphic::initDirect3D(clsMt3d *Mt3d) {
/****************************************************************************
** Init the Direct3D parts of the engine
*/

Mt3d->Driver.ErrorCode = this->DDraw->QueryInterface(IID_IDirect3D7, (void **)&this->D3D); // Get the Direct3D object
_("DDraw->QueryInterface");

Mt3d->Driver.ErrorCode = this->D3D->CreateDevice(IID_IDirect3DHALDevice, // Create the Direct3DDevice object
this->ddsBackBuffer,
&this->D3DDevice);
_("D3D->CreateDevice");

this->D3DViewport.dwX = 0; // Config the viewport
this->D3DViewport.dwY = 0;
this->D3DViewport.dwWidth = Mt3d->Options.Graphic.iXRes;
this->D3DViewport.dwHeight = Mt3d->Options.Graphic.iYRes;
this->D3DViewport.dvMinZ = 0.0f;
this->D3DViewport.dvMaxZ = 1.0f;

Mt3d->Driver.ErrorCode = this->D3DDevice->SetViewport(&this->D3DViewport); // Set the viewort
_("D3DDevice->SetViewport");

this->D3DDevice->SetRenderTarget(this->ddsBackBuffer, 0);

this->matWorld = new D3DMATRIX();
this->matProj = new D3DMATRIX();
this->matView = new D3DMATRIX();


identityMatrix(this->matWorld);
this->D3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, this->matWorld);
_("D3DDevice->SetTransform - World");


D3DUtil_SetViewMatrix(*this->matView, makeVector(0.0f, 0.0f, -10.0f), makeVector(0.0f, 0.0f, 0.0f), makeVector(0.0f, 0.0f, 1.0f));
this->D3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, this->matView);
_("D3DDevice->SetTransform - View");


D3DUtil_SetProjectionMatrix(*this->matProj);
this->D3DDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, this->matProj);
_("D3DDevice->SetTransform - Projection");

return (true);
}



bool clsMt3d::clsDriver::clsGraphic::destDirect3D(clsMt3d *Mt3d) {

return (true);
}



bool clsMt3d::clsDriver::clsGraphic::showGraphic(clsMt3d *Mt3d) {
/****************************************************************************
** Copy everything from the backbuffer to the primary surface
*/

this->DDraw->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, 0); // Wait for a new refresh sequece
this->ddsPrimaryBuffer->Flip(0, DDFLIP_WAIT); // Copy the stuff

return (true);
}



bool clsMt3d::clsDriver::clsGraphic::clear(clsMt3d *Mt3d) {
/****************************************************************************
** Clear the backbuffer for new drawing on it
*/

/*DDBLTFX clsBltFX; // Create a new blit FX object
ZeroMemory(&clsBltFX, sizeof(clsBltFX)); // Alloate memory
clsBltFX.dwSize = sizeof(clsBltFX);
this->ddsBackBuffer->Blt(0, // Blit with no target rect (means all)
0, // No source surface
0, // No source rect
DDBLT_COLORFILL | // Should fill it with a color
DDBLT_WAIT, // And wait to its end
&clsBltFX); // Add the effects*/

this->D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, 16711680, 0.0f, 0);

return (true);
}

///////////////////////////////////////////////////////////////////

MSG msg;

this->bRunning = true;
this->bError = true;
this->Driver.ErrorCode = 0;

if (this->bError) // Load the options
this->Options.load(this);

if (this->bError) // Init the log file
this->System.Console.Log.openFile(this, "log.txt");

this->System.Console.Log.write(this, "This is a test");

if (this->bError) // Create the window
this->bError = this->Driver.Graphic.createWindow(this);
if (this->bError) // init DirectDraw
this->bError = this->Driver.Graphic.initDirectDraw(this);
if (this->bError) // init Direct3D
this->bError = this->Driver.Graphic.initDirect3D(this);

if (this->bError) { // Start the game loop

D3DVERTEX vtxTest[4];

vtxTest[0] = createD3DVertex( 1, 1, 0, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
vtxTest[1] = createD3DVertex( 1, -1, 0, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
vtxTest[2] = createD3DVertex(-1, 1, 0, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
vtxTest[3] = createD3DVertex(-1, -1, 0, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);

while (this->bRunning) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // Is there a message
if (msg.message == WM_QUIT) { // Would you quit
this->bRunning = false;
} else { // Forwarde the messages to the messagle handler
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} else { // Could i draw the picture
this->Driver.Graphic.clear(this); // Clear the backbuffer

this->Driver.ErrorCode = this->Driver.Graphic.D3DDevice->BeginScene();

this->Driver.Graphic.D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, D3DFVF_VERTEX, &vtxTest, 4, D3DDP_WAIT);

this->Driver.ErrorCode = this->Driver.Graphic.D3DDevice->EndScene();

this->Driver.Graphic.showGraphic(this); // Show the backbuffer(flip it to the primary buffer)
}
}
}


der rest läuft aber wunderbar.

Danke für ‘ne Antwort schonmal im Vorraus

cu

P.S.: Im Anhang nochmal das Ganze Projekt
 
Prüfe mal die Parameter von DrawPrimitive, bei denen verhaut man sich so leicht.

Mir fällt auf, dass du zwar eine Triangle-Liste rendern willst, aber nur vier Vertices hast. Da sollte aber dann mindestens ein Triangle rauskommen. Eventuell tut DirectX bzw. der Treiber aber da gar nichts, da die Anzahl der Vertices nicht durch 3 teilbar ist.

Ich vermute, dass du ein Quad rendern willst; wenn du nur vier Vertices angeben willst, musst du einen Triangle-Fan oder Triangle-Strip verwenden. Bei einer Triangle-Liste müsstest du zwei Vertices doppelt ins Array packen.
 
Ich hab meine Vertexe falsch initialisiert. Jetzt klappts aber.
Trotzdem noma Danke

cu

------------------------------------------------------------------------------------------------------------------------------------

Ich hab das Zeichnen zwar hinbekommen, aber mein z-Buffer noch net so wirklich. Will Meinen: Er schon, aber nicht richtig. Dh.: mein Vertex wird zerhackstückelt.


HTML:
  static HRESULT WINAPI EnumZBufferCallback(DDPIXELFORMAT* pddpf, VOID* pddpfDesired) {
        if (pddpf->dwFlags == DDPF_ZBUFFER) {
            memcpy(pddpfDesired, pddpf, sizeof(DDPIXELFORMAT));
            return D3DENUMRET_CANCEL;
      }
      return D3DENUMRET_OK;
    }
    
    
    ...
   DDSURFACEDESC2 ddSurfaceDesc; // Create a new surface description
   ZeroMemory(&ddSurfaceDesc, sizeof(ddSurfaceDesc)); // Allocate the memory
        ddSurfaceDesc.dwSize = sizeof(ddSurfaceDesc);
   ddSurfaceDesc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT; // Enable the Caps and the backbuffer counter
        ddSurfaceDesc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY;
        ddSurfaceDesc.dwWidth = Mt3d->Options.Graphic.iXRes;
        ddSurfaceDesc.dwHeight = Mt3d->Options.Graphic.iYRes;
    
        DDPIXELFORMAT ddpfZBuffer;
      Mt3d->Driver.ErrorCode = this->D3D->EnumZBufferFormats(IID_IDirect3DHALDevice, 
                                                                    EnumZBufferCallback,
                                                                    (VOID*)&ddpfZBuffer );
        _("Enumerate the z buffer pixel format");
    
        memcpy(&ddSurfaceDesc.ddpfPixelFormat, &ddpfZBuffer, sizeof(DDPIXELFORMAT));
    
        Mt3d->Driver.ErrorCode = this->DDraw->CreateSurface(&ddSurfaceDesc, &this->ddsZBuffer, 0);
        _("DDraw->CreateSurface as depth buffer surface");
    
        Mt3d->Driver.ErrorCode = this->ddsPrimaryBuffer->AddAttachedSurface(this->ddsZBuffer);
        _("DDraw->AddAttachedSurface as depth buffer surface to back bufer surface");
    
        Mt3d->Driver.ErrorCode = this->D3DDevice->SetRenderState(D3DRENDERSTATE_ZENABLE,    TRUE);
        _("D3Device->SetRenderState(D3DRENDERSTATE_ZENABLE, TRUE) to enable depth bufer surface");
    ...

cu
 
Zuletzt bearbeitet:
Zurück