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

25.02.2005, 07:12

Index und VertexBuffer Probleme: Nix Passiert

Hoi,

also folgendes Problem: Ich habe gerade versucht mit dem Vertex und IndexBuffer nen Wuerfel auf den bildschirm zu bringen, leider hab ich das nicht so ganz hingekriegt, ich bin aber ratlos, also hier mal der quelltext:

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Coder: Patrick Metzger

//

// Vertex- und Index Buffer Tests!



#include <TriBase.h>
#include "..\\Buch\\3D Spieleprogrammierung\\Beispiele\\Allgemeines\\InitWindow.h"
#include "..\\Buch\\3D Spieleprogrammierung\\Beispiele\\Allgemeines\\Direct3DEnum.h"
#include "..\\Buch\\3D Spieleprogrammierung\\Beispiele\\Allgemeines\\InitDirect3D.h"

struct SVertex
{
    tbVector3           vPosition;
    DWORD               dwColor;
    static const DWORD  dwFVF;  
};

const DWORD SVertex::dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;


tbResult InitApplication();
tbResult ExitApplication();
tbResult InitScene();
tbResult ExitScene();
tbResult Render(float fNumSecsPassed);
tbResult Move(float fNumSecsPassed);


float g_fTime = 0.0f;

SVertex                 Objekt[8];
PDIRECT3DVERTEXBUFFER9  g_ppVertexBuffer;
PDIRECT3DINDEXBUFFER9   g_ppIndexBuffer;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
    tbResult Result;

    Result = InitApplication();
    if (Result == TB_ERROR)
    {
        MessageBox(NULL,"Fehler beim Initialisieren der Anwendung!","Fehler",MB_OK | MB_ICONERROR);
        return 1;
    }
    else if (Result == TB_CANCELED)
    {
        return 0;
    }

    tbDoMessageLoop(Move,Render);

    ExitApplication();

    return 0;
}


tbResult InitApplication()
{
    if(tbInit())
    {
        MessageBox(NULL,"Fehler beim Initialisieren der Engine!","Fehler",MB_OK | MB_ICONERROR);
    }

    SDirect3DParameters Direct3DParameters;
    tbResult Result;
    
    Result = GetDirect3DParameters(&Direct3DParameters);
    if (Result == TB_ERROR)
    {
        MessageBox(NULL,"Fehler beim abfragen der Direct3D Parameter!","Fehler",MB_OK | MB_ICONERROR);
        return TB_ERROR;
    }
    else if (Result == TB_CANCELED)
    {
        tbExit();
        return TB_CANCELED;
    }

    if (InitWindow(Direct3DParameters.VideoMode.Width,Direct3DParameters.VideoMode.Height,
                    "Testumgebung",NULL))
    {
        MessageBox(NULL,"Fehler beim erstellen des Fensters!","Fehler",MB_OK | MB_ICONERROR);
        tbExit();
        return TB_ERROR;
    }

    if (InitDirect3D(&Direct3DParameters,g_hWindow))
    {
        MessageBox(g_hWindow,"Fehler beim Initialisieren von Direct3D!","Fehler",MB_OK | MB_ICONERROR);
        ExitWindow();
        tbExit();
        return TB_ERROR;
    }

    if (InitScene())
    {
        MessageBox(g_hWindow,"Fehler beim Initialisieren der Szene!","Fehler",MB_OK | MB_ICONERROR);
        ExitDirect3D();
        ExitWindow();
        tbExit();
        return TB_ERROR;
    }


    return TB_OK;
}


tbResult ExitApplication()
{
    ExitScene();
    ExitWindow();
    ExitDirect3D();
    tbExit();
    return TB_OK;
}

tbResult Move(float fNumSecsPassed)
{
    g_fTime += fNumSecsPassed;
    return TB_OK;
}

tbResult Render(float fNumSecsPassed)
{

    tbMatrix mRotation    = tbMatrixRotation(TB_DEG_TO_RAD(30.0f),
                                             TB_DEG_TO_RAD(30.0f),
                                             TB_DEG_TO_RAD(30.0f));

    tbMatrix mTranslation = tbMatrixTranslation(tbVector3(0.0f,0.0f,10.0f));

    tbMatrix mWorld = mRotation * mTranslation;
    g_pD3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld));

    g_pD3DDevice->Clear(0,
                        NULL,
                        D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                        tbColor(0.0f,0.0f,0.0f),
                        1.0f,
                        0);
    
    g_pD3DDevice->BeginScene();
    
    g_pD3DDevice->SetStreamSource(0,g_ppVertexBuffer,0,sizeof(SVertex));
    g_pD3DDevice->SetIndices(g_ppIndexBuffer);

    HRESULT hResult;

    hResult = g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
                                                 0,
                                                 0,
                                                 8,
                                                 0,
                                                 12);

    if (FAILED(hResult))
    {
        TB_ERROR_DIRECTX("g_pD3DDevice->DrawIndexedPrimitive",hResult,TB_ERROR);
    };

    g_pD3DDevice->EndScene();

    g_pD3DDevice->Present(NULL,NULL,NULL,NULL);

    return TB_OK;
}

tbResult InitScene()
{

    HRESULT hResult;
    if(FAILED(hResult = g_pD3DDevice->SetFVF(SVertex::dwFVF)))
    {
        //Fehler beim setzen des Vertexformats!

        TB_ERROR_DIRECTX("g_pD3DDevice->SetFVF",hResult,TB_ERROR);
    }       
    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    g_pD3DDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);

    
    if (FAILED(hResult = g_pD3DDevice->CreateVertexBuffer(8*sizeof(SVertex),
                                                          0,
                                                          SVertex::dwFVF,
                                                          D3DPOOL_MANAGED,
                                                          &g_ppVertexBuffer,
                                                          NULL)))
    {
        // Fehler

        TB_ERROR_DIRECTX("g_pD3DDevice->CreateVertexBuffer",hResult,TB_ERROR);
    }

    if (FAILED(hResult = g_pD3DDevice->CreateIndexBuffer(36,
                                                         0,
                                                         D3DFMT_INDEX16,
                                                         D3DPOOL_MANAGED,
                                                         &g_ppIndexBuffer,
                                                         NULL)))
    {
        // Fehler

        TB_ERROR_DIRECTX("g_pD3DDevice->CreateVertexBuffer",hResult,TB_ERROR);
    }

    Objekt[0].vPosition = tbVector3(-1.0f, 1.0f, 0.0f);
    Objekt[1].vPosition = tbVector3(-1.0f, 1.0f, 1.0f);
    Objekt[2].vPosition = tbVector3( 1.0f, 1.0f, 1.0f);
    Objekt[3].vPosition = tbVector3( 1.0f, 1.0f, 0.0f);

    Objekt[4].vPosition = tbVector3(-1.0f, -1.0f, 0.0f);
    Objekt[5].vPosition = tbVector3(-1.0f, -1.0f, 1.0f);
    Objekt[6].vPosition = tbVector3( 1.0f, -1.0f, 1.0f);
    Objekt[7].vPosition = tbVector3( 1.0f, -1.0f, 0.0f);

    for (int i = 0; i < 8; i++)
    {
        Objekt[i].dwColor = tbColor(0.5f,0.5f,0.5f);
    };

    SVertex*    pvVertices = NULL;
    WORD*       pwIndices = NULL;

    g_ppVertexBuffer->Lock(0,0,(void**)(&pvVertices),D3DLOCK_NOSYSLOCK);
    g_ppIndexBuffer->Lock(0,0,(void**)(&pwIndices),D3DLOCK_NOSYSLOCK);

    for (i = 0; i < 8; i++)
    {
        pvVertices[i] = Objekt[i];
    };

    int aiIndex[36] =  {0,3,7, 0,7,4,
                        2,1,5, 2,5,6,
                        1,0,4, 1,4,5,
                        3,2,6, 3,6,7,
                        0,1,2, 0,2,3,
                        6,5,4, 6,4,7};
    
    for (i = 0; i < 36; i++)
    {
        pwIndices[i] = aiIndex[i];
    };

    g_ppVertexBuffer->Unlock();
    g_ppIndexBuffer->Unlock();

    return TB_OK;

}

tbResult ExitScene()
{
    return TB_OK;
}


der bildschirm bleibt einfach schwarz.

hoffe mir kann jemand helfen, danke schonmal im vorraus ;)

Anonymous

unregistriert

2

25.02.2005, 17:37

moin,
ich habs nur kurz ueberflogen, aber schau dir noch mal die groesse deines index-buffers an.
( tip: sizeof ( unsigned short ) != 1 ( bei mir zumindest ... ;) ) ).

mfg,
google.com

3

26.02.2005, 00:43

stimmt, habs jetzt auf 36*2 geändert, funktioniert leider immernoch nicht ???

ich komm mir dabei irgendwie ziemlich blöd vor :rolleyes:

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

4

26.02.2005, 11:04

beim erstellen der buffer würd ich D3DUSAGE_WRITEONLY angeben (wenns möglich is) und in D3DPOOL_DEFAULT ablegen.

zeig mal deine render funktion...

5

26.02.2005, 15:01

die ist oben mit drin, direkt über InitScene.

naja im buch sind das auch diese parameter im beispiel, was ist denn der unterschied zwischen Managed und Default?


/€
das is peinlich, hab die projektionsmatrix vergessen.

wäre aber trotzdem interessant den unterschied zwischen Managed und Default zu wissen ;)


danke nochmal für die hilfe :huhu:



hier nochmal der fertige quelltext falls es jemanden interessiert:

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Coder: Patrick Metzger

//

// Vertex- und Index Buffer Tests!



#include <TriBase.h>
#include "..\\Buch\\3D Spieleprogrammierung\\Beispiele\\Allgemeines\\InitWindow.h"
#include "..\\Buch\\3D Spieleprogrammierung\\Beispiele\\Allgemeines\\Direct3DEnum.h"
#include "..\\Buch\\3D Spieleprogrammierung\\Beispiele\\Allgemeines\\InitDirect3D.h"

struct SVertex
{
    tbVector3           vPosition;
    DWORD               dwColor;
    static const DWORD  dwFVF;  
};

const DWORD SVertex::dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;


tbResult InitApplication();
tbResult ExitApplication();
tbResult InitScene();
tbResult ExitScene();
tbResult Render(float fNumSecsPassed);
tbResult Move(float fNumSecsPassed);


float g_fTime = 0.0f;

SVertex                 Objekt[8];
SDirect3DParameters     Direct3DParameters;
PDIRECT3DVERTEXBUFFER9  g_ppVertexBuffer;
PDIRECT3DINDEXBUFFER9   g_ppIndexBuffer;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
    tbResult Result;

    Result = InitApplication();
    if (Result == TB_ERROR)
    {
        MessageBox(NULL,"Fehler beim Initialisieren der Anwendung!","Fehler",MB_OK | MB_ICONERROR);
        return 1;
    }
    else if (Result == TB_CANCELED)
    {
        return 0;
    }

    tbDoMessageLoop(Move,Render);

    ExitApplication();

    return 0;
}


tbResult InitApplication()
{
    if(tbInit())
    {
        MessageBox(NULL,"Fehler beim Initialisieren der Engine!","Fehler",MB_OK | MB_ICONERROR);
    }

    tbResult Result;
    
    Result = GetDirect3DParameters(&Direct3DParameters);
    if (Result == TB_ERROR)
    {
        MessageBox(NULL,"Fehler beim abfragen der Direct3D Parameter!","Fehler",MB_OK | MB_ICONERROR);
        return TB_ERROR;
    }
    else if (Result == TB_CANCELED)
    {
        tbExit();
        return TB_CANCELED;
    }

    if (InitWindow(Direct3DParameters.VideoMode.Width,Direct3DParameters.VideoMode.Height,
                    "Testumgebung",NULL))
    {
        MessageBox(NULL,"Fehler beim erstellen des Fensters!","Fehler",MB_OK | MB_ICONERROR);
        tbExit();
        return TB_ERROR;
    }

    if (InitDirect3D(&Direct3DParameters,g_hWindow))
    {
        MessageBox(g_hWindow,"Fehler beim Initialisieren von Direct3D!","Fehler",MB_OK | MB_ICONERROR);
        ExitWindow();
        tbExit();
        return TB_ERROR;
    }

    if (InitScene())
    {
        MessageBox(g_hWindow,"Fehler beim Initialisieren der Szene!","Fehler",MB_OK | MB_ICONERROR);
        ExitDirect3D();
        ExitWindow();
        tbExit();
        return TB_ERROR;
    }


    return TB_OK;
}


tbResult ExitApplication()
{
    ExitScene();
    ExitWindow();
    ExitDirect3D();
    tbExit();
    return TB_OK;
}

tbResult Move(float fNumSecsPassed)
{
    g_fTime += fNumSecsPassed;
    return TB_OK;
}

tbResult Render(float fNumSecsPassed)
{

    tbMatrix mRotation    = tbMatrixRotation(TB_DEG_TO_RAD(30.0f*g_fTime),
                                             TB_DEG_TO_RAD(30.0f*g_fTime),
                                             TB_DEG_TO_RAD(30.0f*g_fTime));

    tbMatrix mTranslation = tbMatrixTranslation(tbVector3(0.0f,-1.0f,5.0f));

    tbMatrix mWorld = mRotation * mTranslation;
    g_pD3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld));

    float fAspect = (float) (Direct3DParameters.VideoMode.Width) /
                    (float) (Direct3DParameters.VideoMode.Height);

    tbMatrix mProjection = tbMatrixProjection(TB_DEG_TO_RAD(90),
                                              fAspect,
                                              0.1f,
                                              100.0f);

    g_pD3DDevice->SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)(&mProjection));

    

    g_pD3DDevice->Clear(0,
                        NULL,
                        D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                        tbColor(0.0f,0.0f,.5.0f),
                        1.0f,
                        0);
    
    g_pD3DDevice->BeginScene();
    
    g_pD3DDevice->SetStreamSource(0,g_ppVertexBuffer,0,sizeof(SVertex));
    g_pD3DDevice->SetIndices(g_ppIndexBuffer);

    HRESULT hResult;

    hResult = g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
                                                 0,
                                                 0,
                                                 8,
                                                 0,
                                                 12);

    if (FAILED(hResult))
    {
        TB_ERROR_DIRECTX("g_pD3DDevice->DrawIndexedPrimitive",hResult,TB_ERROR);
    };

    g_pD3DDevice->EndScene();

    g_pD3DDevice->Present(NULL,NULL,NULL,NULL);

    return TB_OK;
}

tbResult InitScene()
{

    HRESULT hResult;
    if(FAILED(hResult = g_pD3DDevice->SetFVF(SVertex::dwFVF)))
    {
        //Fehler beim setzen des Vertexformats!

        TB_ERROR_DIRECTX("g_pD3DDevice->SetFVF",hResult,TB_ERROR);
    }       
    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    g_pD3DDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
    g_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

    
    if (FAILED(hResult = g_pD3DDevice->CreateVertexBuffer(8*sizeof(SVertex),
                                                          0,
                                                          SVertex::dwFVF,
                                                          D3DPOOL_MANAGED,
                                                          &g_ppVertexBuffer,
                                                          NULL)))
    {
        // Fehler

        TB_ERROR_DIRECTX("g_pD3DDevice->CreateVertexBuffer",hResult,TB_ERROR);
    }

    if (FAILED(hResult = g_pD3DDevice->CreateIndexBuffer(36*sizeof(UINT),
                                                         0,
                                                         D3DFMT_INDEX16,
                                                         D3DPOOL_MANAGED,
                                                         &g_ppIndexBuffer,
                                                         NULL)))
    {
        // Fehler

        TB_ERROR_DIRECTX("g_pD3DDevice->CreateVertexBuffer",hResult,TB_ERROR);
    }

    Objekt[0].vPosition = tbVector3(-1.0f, 1.0f, 0.0f);
    Objekt[1].vPosition = tbVector3(-1.0f, 1.0f, 1.0f);
    Objekt[2].vPosition = tbVector3( 1.0f, 1.0f, 1.0f);
    Objekt[3].vPosition = tbVector3( 1.0f, 1.0f, 0.0f);

    Objekt[4].vPosition = tbVector3(-1.0f, -1.0f, 0.0f);
    Objekt[5].vPosition = tbVector3(-1.0f, -1.0f, 1.0f);
    Objekt[6].vPosition = tbVector3( 1.0f, -1.0f, 1.0f);
    Objekt[7].vPosition = tbVector3( 1.0f, -1.0f, 0.0f);

    for (int i = 0; i < 8; i++)
    {
        Objekt[i].dwColor = tbColor(0.5f,0.5f,0.5f);
    };

    SVertex*    pvVertices = NULL;
    WORD*       pwIndices = NULL;

    g_ppVertexBuffer->Lock(0,0,(void**)(&pvVertices),D3DLOCK_NOSYSLOCK);
    g_ppIndexBuffer->Lock(0,0,(void**)(&pwIndices),D3DLOCK_NOSYSLOCK);

    for (i = 0; i < 8; i++)
    {
        pvVertices[i] = Objekt[i];
    };

    int aiIndex[36] =  {0,3,7, 0,7,4,
                        2,1,5, 2,5,6,
                        1,0,4, 1,4,5,
                        3,2,6, 3,6,7,
                        0,1,2, 0,2,3,
                        6,5,4, 6,4,7};
    
    for (i = 0; i < 36; i++)
    {
        pwIndices[i] = aiIndex[i];
    };

    g_ppVertexBuffer->Unlock();
    g_ppIndexBuffer->Unlock();

    return TB_OK;

}

tbResult ExitScene()
{
    return TB_OK;

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

6

26.02.2005, 16:43

Zitat

wäre aber trotzdem interessant den unterschied zwischen Managed und Default zu wissen


dx sdk doku!?

Zitat von »"dx sdk doku"«

D3DPOOL_MANAGED:
Resources are copied automatically to device-accessible memory as needed. Managed resources are backed by system memory and do not need to be re-created when a device is lost...


Zitat von »"dx sdk doku"«

D3DPOOL_DEFAULT:
Resources are placed in the memory pool most appropriate for the set of usages requested for the given resource...

Werbeanzeige