Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

10.04.2008, 04:56

IDirect3D9::CreateDevice will nicht gelingen

Hy

Nachdem ich nun das Kapitel 2 im Buch abgeschlossen habe, will ich mal selber bissl mit dem bisher erlernten rumspielen, nur leider schon die erste Hürde:



C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCmdLine, int iShowCmd){

HWND        hwnd;
WNDCLASSEXW wndclass;

// Fenster bauen

wndclass.cbSize         = sizeof(WNDCLASSEXW);
wndclass.style          = CS_CLASSDC;
wndclass.lpfnWndProc    = WindowProc;
wndclass.cbClsExtra     = 0;
wndclass.cbWndExtra     = 0;
wndclass.hInstance      = hInstance;
wndclass.hIcon          = NULL;
wndclass.hCursor        = NULL;
wndclass.hbrBackground  = NULL;
wndclass.lpszMenuName   = NULL;
wndclass.lpszClassName  = L"Testfenster";
wndclass.hIconSm        = NULL;

RegisterClassExW(&wndclass);

hwnd = CreateWindowW(L"Testfenster", 
                     L"Testfenster", 
                     WS_VISIBLE | WS_OVERLAPPEDWINDOW,
                     GetSystemMetrics(SM_CXSCREEN) / 2 - 200,
                     GetSystemMetrics(SM_CYSCREEN) / 2 - 100,
                     400,
                     200,
                     NULL,
                     NULL,
                     NULL,
                     NULL);
    
ShowWindow(hwnd, iShowCmd);
UpdateWindow(hwnd);

//Direct3D Teil


IDirect3D9 *pD3D = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS PresentParams;
PresentParams.BackBufferWidth               = 1680;
PresentParams.BackBufferHeight              = 1050;
PresentParams.BackBufferFormat              = D3DFMT_A8R8G8B8;
PresentParams.BackBufferCount               = 1;
PresentParams.MultiSampleType               = D3DMULTISAMPLE_4_SAMPLES;
PresentParams.MultiSampleQuality            = 4;
PresentParams.SwapEffect                    = D3DSWAPEFFECT_DISCARD;
PresentParams.hDeviceWindow                 = hwnd;
PresentParams.Windowed                      = FALSE;
PresentParams.EnableAutoDepthStencil        = TRUE;
PresentParams.AutoDepthStencilFormat        = D3DFMT_D24S8;
PresentParams.Flags                         = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
PresentParams.FullScreen_RefreshRateInHz    = D3DPRESENT_RATE_DEFAULT;
PresentParams.PresentationInterval          = D3DPRESENT_INTERVAL_DEFAULT;

PDIRECT3DDEVICE9 test;


pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &PresentParams, &test);



// test->Release();

pD3D->Release();

return 0;
}


Das Problem ist das CreateDevice nicht gelingt, ich aber den Fehler nicht finde. Alle Parameter, welche ich angegeben habe, werden definitiv unterstützt. Ich such hier nun schon 2 Stunden und probier rum.
Kann es sein, das was mit "hwnd" also dem Handle zum Fenster nicht stimmt irgendwo?

2

10.04.2008, 08:07

Schätze, das liegt am Multisampling:

C-/C++-Quelltext

1
2
PresentParams.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES;
PresentParams.MultiSampleQuality = 4; 


Bevor du nicht mit CheckDeviceMultiSampleType geprüft hast, was definitiv unterstützt wird, würde ich das mal ändern zu:

C-/C++-Quelltext

1
2
PresentParams.MultiSampleType = D3DMULTISAMPLE_NONE;
PresentParams.MultiSampleQuality = 0; 


Die Flags erstmal rausnehmen:

C-/C++-Quelltext

1
PresentParams.Flags = 0;

Dann sollte es funktionieren. Bis auf die Kleinigkeit, das dein Programm sofort wieder beendet wird, weil du keinen MessageLoop definiert hast. ;)

3

10.04.2008, 11:10

Das wars leider nicht :(
MultiSample aus und die Flags auf 0...

Das mit dem MessageLoop ist mit bewusst :) Hab halt erst ma das unwichtige weggelassen, bis die Initialisierung von DirectX klappt.

4

10.04.2008, 11:26

Hab das mal getestet und hochgeladen: http://pasteall.org/391/cpp
Änderungen habe ich mit dem Kommentar "<-Hier" versehen (ohne Anspruch auf Vollständigkeit ;)).
Also bei mir (Vista) läuft das soweit...

5

10.04.2008, 11:56

hm, nun laeufts, danke...

Der Fehler war weder Multisampling, noch das Fenster ansich sondern ich hatte die WindowProc schnell hingehuscht und die sah so aus:

C-/C++-Quelltext

1
2
3
4
RESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

        return 0;
}


Das er halt nur beim kompilieren keinen Fehler wirft. Wollte es dann später verfeinern :)

Nun laeufts durch, wenn da ein return 1; steht... Das Wieso ist nun die andere Frage... Anscheinend verhaspelt er sich, weil nach meinem

C-/C++-Quelltext

1
UpdateWindow(hwnd);
ja die Nachrichtenschleife durchlaufen wird.
Da geht dann wohl was in die Hose danach, wenn man DirectX das hwnd mit übergibt....

Trotzdem vielen Dank, ich hab lauter an der falschen Stelle gesucht...

6

10.04.2008, 12:00

Solltest deiner WinProc auf jeden Fall ein

C-/C++-Quelltext

1
return DefWindowProc (hwnd, nMsg, wParam, lParam);

verpassen, damit Windows alle Nachrichten weiterverarbeitet.

7

10.04.2008, 17:17

Oookay, ich hab mir derweil mal den Rest zusammen gebaut. Es soll eigentlich nur ein Dreieck dargestellt werden, nix rotiert oder so.
Desweiteren komplett ohne Tribase, es wurde also z.B. das DirectX eigene D3DXMATRIX als Datentyp benutzt.

Nun, mein Problem: Kein Dreieck :D
Aber ich vermute es ist kein technischens Problem, sonden es stimmt was nicht mit Welt-, Translationsmatrix usw. Hier fehlt mir aber irgendwie grad bissl der Durchblick.
Da bei mir nichts Transformiert oder Verschoben werden sollte, habbich da einfach nix berechnet. Ist das mein Fehler?

Außerdem bin ich mir nicht ganz sicher, ob die Projektionsmatrix so stimmt, das Codebeispiel zur Berechnung ist aus der DirectX Doku.

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <Windows.h>
#include <StdIO.h>
#include <D3D9.h>
#include <d3dx9.h>
#include <string.h>
#include <iostream>
#include <math.h>


LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCmdLine, int iShowCmd){
    
HWND        hwnd;
MSG         msg;
WNDCLASSEXW wndclass;


// Fenster bauen

wndclass.cbSize         = sizeof(WNDCLASSEXW);
wndclass.style          = CS_CLASSDC;
wndclass.lpfnWndProc    = WindowProc;
wndclass.cbClsExtra     = 0;
wndclass.cbWndExtra     = 0;
wndclass.hInstance      = hInstance;
wndclass.hIcon          = NULL;
wndclass.hCursor        = NULL;
wndclass.hbrBackground  = (HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName   = NULL;
wndclass.lpszClassName  = L"Testfenster";
wndclass.hIconSm        = NULL;

RegisterClassExW(&wndclass);

hwnd = CreateWindowW(L"Testfenster", 
                     L"Testfenster", 
                     WS_VISIBLE | WS_OVERLAPPEDWINDOW,
                     GetSystemMetrics(SM_CXSCREEN) / 2 - 400,
                     GetSystemMetrics(SM_CYSCREEN) / 2 - 200,
                     800,
                     400,
                     NULL,
                     NULL,
                     NULL,
                     NULL);
    
ShowWindow(hwnd, iShowCmd);
UpdateWindow(hwnd);

//Direct3D Teil


IDirect3D9 *pD3D = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS PresentParams;
PresentParams.BackBufferWidth               = 1680;
PresentParams.BackBufferHeight              = 1050;
PresentParams.BackBufferFormat              = D3DFMT_A8R8G8B8;
PresentParams.BackBufferCount               = 1;
PresentParams.MultiSampleType               = D3DMULTISAMPLE_4_SAMPLES;
PresentParams.MultiSampleQuality            = 4;
PresentParams.SwapEffect                    = D3DSWAPEFFECT_DISCARD;
PresentParams.hDeviceWindow                 = hwnd;
PresentParams.Windowed                      = FALSE;
PresentParams.EnableAutoDepthStencil        = TRUE;
PresentParams.AutoDepthStencilFormat        = D3DFMT_D24X8;
PresentParams.Flags                         = 0;
PresentParams.FullScreen_RefreshRateInHz    = D3DPRESENT_RATE_DEFAULT;
PresentParams.PresentationInterval          = D3DPRESENT_INTERVAL_DEFAULT;

PDIRECT3DDEVICE9 test;


 pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &PresentParams, &test);


 test->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
 test->SetRenderState(D3DRS_LIGHTING, FALSE);
 test->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
 test->SetRenderState(D3DRS_DITHERENABLE, TRUE);


// Projektionsmatrix erzeugen

D3DXMATRIX Projektionsmatrix;

const float near_plane=0.1f; // Distance to near clipping // plane

const float far_plane=100.0f;  // Distance to far clipping // plane

const float fov_horiz=45.0f;  // Horizontal field of view // angle, in radians

const float fov_vert=45.0f;   // Vertical field of view // angle, in radians


    float    h, w, Q;

    w = static_cast<float>(1/tan(fov_horiz*0.5));  // 1/tan(x) == cot(x)

    h = static_cast<float>(1/tan(fov_vert*0.5));   // 1/tan(x) == cot(x)

    Q = far_plane/(far_plane - near_plane);

    
    ZeroMemory(&Projektionsmatrix, sizeof(Projektionsmatrix));

    Projektionsmatrix(0, 0) = w;
    Projektionsmatrix(1, 1) = h;
    Projektionsmatrix(2, 2) = Q;
    Projektionsmatrix(3, 2) = -Q*near_plane;
    Projektionsmatrix(2, 3) = 1;

    test->SetTransform(D3DTS_PROJECTION, &Projektionsmatrix);

// End of ProjectionMatrix




D3DXMATRIX Rotation;
D3DXMATRIX Translation;
D3DXMATRIX Welt;

Rotation=*(&Translation);
Welt=Rotation;
test->SetTransform(D3DTS_WORLD, &Welt);

//Das Dreieck bauen


struct SVertex {
    D3DVECTOR   Position;
    DWORD       Farbe;
};

SVertex Dreieck[3];
Dreieck[0].Position.x=0.0f;
Dreieck[0].Position.y=0.0f;
Dreieck[0].Position.z=70.0f;
Dreieck[0].Farbe=(0x80FF00FF);

Dreieck[1].Position.x=1.0f;
Dreieck[1].Position.y=1.0f;
Dreieck[1].Position.z=70.0f;
Dreieck[1].Farbe=(0x80F0000F);


Dreieck[2].Position.x=1.0f;
Dreieck[2].Position.y=0.0f;
Dreieck[2].Position.z=70.0f;
Dreieck[2].Farbe=(0x800F0F0F);


msg.message=WM_NULL;

while(msg.message != WM_QUIT)

{
        if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
        {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
        }
        else
        {
            test->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);        
            test->BeginScene();
            test->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 3, &Dreieck, sizeof(D3DVECTOR));
            test->EndScene();
            test->Present(NULL,NULL,NULL,NULL);
         }
}



UnregisterClassW(wndclass.lpszClassName, hInstance);
test->Release();
pD3D->Release();

return 0;
}



LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{
       switch (message)
        {
                case WM_DESTROY:
                PostQuitMessage (0);
                return 0;
                break;
        }
        return DefWindowProc (hwnd, message, wParam, lParam);
}

8

10.04.2008, 17:29

Wird das Dreieck ohne Transformieren gerendert?

9

10.04.2008, 17:31

Es wird anscheinend gar nicht gerendert, da der Bildschirm schwarz bleibt.
Ich weiss nur nicht, ob es daran liegt, das ich quasi nix mit der Translations- und Rotationsmatrix mache oder ob einfach die Kamera "falsch" ausgerichtet ist.

10

10.04.2008, 18:04

Ohne das Transformations-Zeuch:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SVertex Dreieck[3];
Dreieck[0].Position.x=  -1.0f;
Dreieck[0].Position.y=  -1.0f;
Dreieck[0].Position.z=  0.0f;
Dreieck[0].Farbe=(0xFF00FF00);

Dreieck[1].Position.x = 0.0f;
Dreieck[1].Position.y = 1.0f;
Dreieck[1].Position.z = 0.0f;
Dreieck[1].Farbe=(0xFFFF00FF);

Dreieck[2].Position.x =1.0f;
Dreieck[2].Position.y =-1.0f;
Dreieck[2].Position.z=0.0f;
Dreieck[2].Farbe=(0xFF0000FF);


Der Fehler liegt beim Zeichnenvorgang:

C-/C++-Quelltext

1
2
// 1 Dreieck - und sizeof(SVertex), nicht 3 und nicht sizeof(D3DVECTOR)

test->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, &Dreieck, sizeof(SVertex));

Werbeanzeige