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

lfp

Frischling

  • »lfp« ist der Autor dieses Themas
  • Private Nachricht senden

1

13.03.2013, 20:01

Laden komplexer Objekte mit vielen Vertices aus *.obj-Dateien (DirectX 9)

Hallo zusammen!
Ich bin gerade dabei, die Grafikprogrammierung mit DirectX zu lernen, und habe eine Funktion geschrieben, mit der ich 3D-Modelle aus .obj Dateien in einen Vertex-Buffer laden kann.
Die Funktion sieht zwar umfangreich aus, das Problem ist aber wahrscheinlich viel leichter zu lösen, zumal ich die Fehlerquelle kenne :)

Der Fehler befindet sich in folgender Deklaration:

VERTEX Vertices [10000];

Die Vertexdaten aus der Datei werden zuvor in diesen Array geladen, der insgesamt 10000 Vertices speichern kann.
Die Funktion tut, was sie soll, aber das nur bei einfachen Testobjekten, denn "richtige" Objekte, wie zum Beispiel ein Baum mit über 47000 Vertices, führt unweigerlich dazu, dass die Funktion an eine nicht vorhandene Position im Array schreibt, und das lässt sich Windows natürlich nicht gefallen ^^

Wenn ich den Array als VERTEX Vertices [100000]; also mit Hunderttausend speicherbaren Vertices deklariere, gibt es einen Stackoverflow. Es sind einfach zu viele Daten.

Ich hab es bereits mit Vectoren versucht, was aber leider zu seltsamen Fehlern geführt hat. Sollte jemand an dem Lösungsansatz interessiert sein, kann ich die Funktion gern nochmal umschreiben, neu ausführen und die Fehlermeldung posten. Ich würde mich aber eher für Alternativlösungen interessieren, falls es welche gibt, die einfacher, bzw. zuverlässiger sind als Vectoren.

Da es hier nur grundsätzlich um das Belegen von Speicher geht, hoffe ich, dass die Lösung nicht allzu schwer für euch zu finden ist, und dass ich das richtige Forum gewählt habe.

Für die unter euch, die die Funktion gerne stehlen möchten, oder für die, die meinen, für einen völlig Fremden viel Freizeit aufopfern zu wollen, ( :D ) hier der komplette Code der Funktion (einige Deklarationen sind allerdings nicht vorhanden, da die global im header stehen) :

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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
void LoadObject(LPDIRECT3DDEVICE9 D3DDevice){
    //Deklarationen
    fstream  datei;
    
    int indexzwischenbuffer [10];
    
    int numvertices = 0;
    HRESULT _hResult;

    VERTEX Vertices [10000];
    VERTEX* Vertices_sortiert;

    bool LadenErfolgreich = true;
    bool negativ;


    datei.open("objekt.obj", ios::in);
    while( (textbuf[0] == 'f' && textbuf[1] == ' ') == false && datei.eof() == false){
     datei.getline(textbuf, 1000);
    }

    if(datei.eof()){
     LadenErfolgreich = false;
     Fehler(hWnd, "Fehler beim Lesen des Dateiformats!", MB_OK);
    }else{
     i4 = 0;
     while(textbuf[0] == 'f' && textbuf[1] == ' ' && datei.eof() == false){
         indexzwischenbuffer[0] = 0;
         indexzwischenbuffer[1] = 0;
         indexzwischenbuffer[2] = 0;
         indexzwischenbuffer[3] = 0;

         i3 = 2;
         i2 = 0;
         while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
             i3 ++;
         }
         if(textbuf[i3] != ' '){
             while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                 i3 ++;
             }
         }
         i3 ++;
         while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
             i3 ++;
         }
         if(textbuf[i3] != ' '){
             while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                 i3 ++;
             }
         }
         i3 ++;
         while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
             i3 ++;
         }
         if(textbuf[i3] != ' '){
             while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                 i3 ++;
             }
         }
         i3 ++;

         i2 = 0;
         while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
             textbuf2[i2] = textbuf[i3];

             i2 ++;
             i3 ++;
         }

         if(textbuf[i3] != '\0'){
             indexzwischenbuffer[3] = ( myatoi(textbuf2) /*- 1*/);
         }
         datei.getline(textbuf, 1000);

         if(indexzwischenbuffer[3] == 0){
             i4 ++;
             i4 ++;
             i4 ++;
             primitivecount ++;
         }else{
             i4 ++;
             i4 ++;
             i4 ++;
             i4 ++;
             i4 ++;
             i4 ++;
             primitivecount += 2;
         }
     }
    }
    datei.close();
    if(FAILED(_hResult = D3DDevice->CreateVertexBuffer(i4*sizeof(VERTEX), 0, LoadObjectFVF, D3DPOOL_MANAGED, &VertexBuffer, NULL))){
     Fehler(hWnd, "Fehler beim Erstellen des Vertexbuffers!", MB_OK);
    }
    VertexBuffer->Lock(0,0,(void**)(&Vertices_sortiert), D3DLOCK_NOSYSLOCK);

  
    datei.open("objekt.obj", ios::in);
    if(datei.good() == false){
     LadenErfolgreich = false;
     Fehler(hWnd, "Fehler beim Öffnen des Objekts", MB_OK);
    }else{
     primitivecount = 0;
     while( (textbuf[0] == 'v' && textbuf[1] == ' ') == false && datei.eof() == false){
         datei.getline(textbuf, 1000);
     }
     if(datei.eof()){
         LadenErfolgreich = false;
         Fehler(hWnd, "Fehler beim Lesen des Dateiformats!", MB_OK);
     }else{
         i3 = 0;

         while(textbuf[0] == 'v' && textbuf[1] == ' '){
             i = 2;
             i2 = 0;
             negativ = false;
             if(textbuf[i] == '-'){
                 i++;
                 negativ = true;
             }
             while(textbuf[i] != ' ' && textbuf[i] != '.'){
                 textbuf2[i2] = textbuf[i];

                 i2 ++;
                 i  ++;
             }
             textbuf2[i2] = '\0';
             f = myatoi(textbuf2);

             if(textbuf[i] == '.'){
                 i++;
                 i2 = 0;

                 i4 = 10;
                 valid = true;
                 while(textbuf[i] != ' '){
                     if(textbuf[i] == '0' && valid){
                         i4 *= 10;
                     }else{
                         valid = false;
                         textbuf2[i2] = textbuf[i];
                         i2 ++;
                     }

                     i  ++;
                 }
                 textbuf2[i2] = '\0';

                 i5 = arraylaenge(textbuf2);
                 i5 --;
                 while(i5 > 0){
                     i4 *= 10;
                     i5 --;
                 }
                 
                 f += ( ((float) myatoi(textbuf2)) / (float) i4 );
             }
             if(negativ){
                 Vertices[i3].x = ( f * (-1) );
             }else{
                 Vertices[i3].x = f;
             }

             i ++;
             i2 = 0;
             negativ = false;
             if(textbuf[i] == '-'){
                 i++;
                 negativ = true;
             }
             while(textbuf[i] != ' ' && textbuf[i] != '.'){
                 textbuf2[i2] = textbuf[i];

                 i2 ++;
                 i  ++;
             }
             textbuf2[i2] = '\0';
             f = myatoi(textbuf2);

             if(textbuf[i] == '.'){
                 i++;
                 i2 = 0;
                 i4 = 10;
                 valid = true;
                 while(textbuf[i] != ' '){
                     if(textbuf[i] == '0' && valid){
                         i4 *= 10;
                     }else{
                         valid = false;
                         textbuf2[i2] = textbuf[i];
                         i2 ++;
                     }

                     i  ++;
                 }
                 textbuf2[i2] = '\0';

                 
                 i5 = arraylaenge(textbuf2);
                 i5 --;
                 while(i5 > 0){
                     i4 *= 10;
                     i5 --;
                 }
                 f += ( ((float) myatoi(textbuf2)) / (float) i4 );
             }
             if(negativ){
                 Vertices[i3].y = ( f * (-1) );
             }else{
                 Vertices[i3].y = f;
             }

             i ++;
             i2 = 0;
             negativ = false;
             if(textbuf[i] == '-'){
                 i++;
                 negativ = true;
             }
             while(textbuf[i] != '\0' && textbuf[i] != '.'){
                 textbuf2[i2] = textbuf[i];

                 i2 ++;
                 i  ++;
             }
             textbuf2[i2] = '\0';
             f = myatoi(textbuf2);

             if(textbuf[i] == '.'){
                 i++;
                 i2 = 0;
                 i4 = 10;
                 valid = true;
                 while(textbuf[i] != '\0'){
                     if(textbuf[i] == '0' && valid){
                         i4 *= 10;
                     }else{
                         valid = false;
                         textbuf2[i2] = textbuf[i];
                         i2 ++;
                     }

                     i  ++;
                 }
                 textbuf2[i2] = '\0';

                 
                 i5 = arraylaenge(textbuf2);
                 i5 --;
                 while(i5 > 0){
                     i4 *= 10;
                     i5 --;
                 }
                 f += ( ((float) myatoi(textbuf2)) / (float) i4 );
             }
             if(negativ){
                 Vertices[i3].z = ( f * (-1) );
             }else{
                 Vertices[i3].z = f;
             }
             if(oldrand1 == myrandomnumber(0, 255)){
             
             }
             Vertices[i3].Color = D3DCOLOR_XRGB(0, 255, 50);

             i3 ++;
             datei.getline(textbuf, 1000);
         }

         while( (textbuf[0] == 'f' && textbuf[1] == ' ') == false && datei.eof() == false){
             datei.getline(textbuf, 1000);
         }

         if(datei.eof()){
             LadenErfolgreich = false;
             Fehler(hWnd, "Fehler beim Lesen des Dateiformats!", MB_OK);
         }else{
             i4 = 0;
             while(textbuf[0] == 'f' && textbuf[1] == ' ' && datei.eof() == false){
                 indexzwischenbuffer[0] = 0;
                 indexzwischenbuffer[1] = 0;
                 indexzwischenbuffer[2] = 0;
                 indexzwischenbuffer[3] = 0;

                 i3 = 2;
                 i2 = 0;
                 while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                     textbuf2[i2] = textbuf[i3];

                     i2 ++;
                     i3 ++;
                 }
                 textbuf2[i2] = '\0';

                 if(textbuf[i3] != '\0'){
                     indexzwischenbuffer[0] = ( myatoi(textbuf2) );
                 }
                 if(textbuf[i3] != ' '){
                     while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                         i3 ++;
                     }
                 }
                 i3 ++;

                 i2 = 0;
                 while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                     textbuf2[i2] = textbuf[i3];

                     i2 ++;
                     i3 ++;
                 }
                 textbuf2[i2] = '\0';

                 if(textbuf[i3] != '\0'){
                     indexzwischenbuffer[1] = ( myatoi(textbuf2) );
                 }

                 if(textbuf[i3] != ' '){
                     while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                         i3 ++;
                     }
                 }
                 i3 ++;

                 i2 = 0;
                 while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                     textbuf2[i2] = textbuf[i3];

                     i2 ++;
                     i3 ++;
                 }
                 textbuf2[i2] = '\0';

                 if(textbuf[i3] != '\0'){
                     indexzwischenbuffer[2] = (myatoi(textbuf2));
                 }

                 if(textbuf[i3] != ' '){
                     while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                         i3 ++;
                     }
                 }
                 i3 ++;

                 i2 = 0;
                 while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                     textbuf2[i2] = textbuf[i3];

                     i2 ++;
                     i3 ++;
                 }
                 textbuf2[i2] = '\0';

                 if(textbuf[i3] != '\0'){
                     indexzwischenbuffer[3] = ( myatoi(textbuf2) );
                 }
                 datei.getline(textbuf, 1000);

                 if(indexzwischenbuffer[3] == 0){
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[0]- 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[0]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[0]- 1].z;
                     Vertices_sortiert[i4].Color = Vertices[indexzwischenbuffer[0]- 1].Color;
                     i4 ++;
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[1]- 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[1]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[1]- 1].z;
                     Vertices_sortiert[i4].Color = Vertices[indexzwischenbuffer[1]- 1].Color;
                     i4 ++;
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[2]- 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[2]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[2]- 1].z;
                     Vertices_sortiert[i4].Color = Vertices[indexzwischenbuffer[2]- 1].Color;
                     i4 ++;
                     primitivecount ++;
                 }else{
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[0]- 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[0]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[0]- 1].z;
                     Vertices_sortiert[i4].Color = Vertices[indexzwischenbuffer[0]- 1].Color;
                     i4 ++;
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[1]- 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[1]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[1]- 1].z;
                     i4 ++;
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[2]- 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[2]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[2]- 1].z;
                     Vertices_sortiert[i4].Color = Vertices[indexzwischenbuffer[2]- 1].Color;
                     i4 ++;
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[0] - 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[0]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[0]- 1].z;
                     Vertices_sortiert[i4].Color = Vertices[indexzwischenbuffer[2]- 1].Color;
                     i4 ++;
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[3]- 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[3]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[3]- 1].z;
                     i4 ++;
                     Vertices_sortiert[i4].x = Vertices[indexzwischenbuffer[2]- 1].x;
                     Vertices_sortiert[i4].y = Vertices[indexzwischenbuffer[2]- 1].y;
                     Vertices_sortiert[i4].z = Vertices[indexzwischenbuffer[2]- 1].z;
                     Vertices_sortiert[i4].Color = Vertices[indexzwischenbuffer[0]- 1].Color;
                     i4 ++;

                     primitivecount += 2;
                 }
             }
             
             VertexBuffer->Unlock();

             primitivecount+=2;
             
         }
     }
    }
}

Legend

Alter Hase

Beiträge: 731

Beruf: Softwareentwickler

  • Private Nachricht senden

2

13.03.2013, 20:25

Der Ansatz mit der Vectorclass ging aber schon in die richtige Richtung, deshalb solltest du den weiter verfolgen.
"Wir müssen uns auf unsere Kernkompetenzen konzentrieren!" - "Juhu, wir machen eine Farm auf!"

Netzwerkbibliothek von mir, C#, LGPL: https://sourceforge.net/projects/statetransmitt/

lfp

Frischling

  • »lfp« ist der Autor dieses Themas
  • Private Nachricht senden

3

13.03.2013, 20:29

hmmm... ok... dann schreib ich mal ne Version mit vector-class...

Fireball

Alter Hase

Beiträge: 415

Wohnort: Werne

Beruf: Dipl. Inf.

  • Private Nachricht senden

4

13.03.2013, 23:00

warum so kompliziert

Hallo,

also was du hier machst ist ja erstmal nur das einlesen eines *.obj

Ich verstehe nicht warum du so viele Schleifen da drin hast.

Bei mir sieht das ungefähr so aus ...

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
if(input == 'v'){
            fin.get(input);
            
            if(input == ' '){
                fin >> vertices[vertexIndex].x >> 
                       vertices[vertexIndex].y >>
                       vertices[vertexIndex].z;
                vertices[vertexIndex].z = -1.0f * vertices[vertexIndex].z;

                vertexIndex++;
            }
...


Um die Array Größen zu bestimmen habe ich vorher noch einen Schleifendurchlauf wo ich die Datei einmal komplett durchnudel und gucke wieviel im *.obj drin ist.

Schöne Grüße

fb

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

5

13.03.2013, 23:17

std::vector benutzen, dann brauchst du die Datei nicht zweimal durchlaufen... ;)

6

13.03.2013, 23:38

Ja, std::vector geht nicht nur in die richtige Richtung, es ist DER Weg.
Damit hast du schon ein 'Array' dessen Größe du beliebig anpassen kannst, und in das du per push_back so viele neue Vertices reinpacken kannst wie du willst, ohne vorher zu wissen, wie viele du haben wirst.

Wichtig ist auch zu wissen, dass dein lokales Vertices[10000] Array auf dem Stack liegt, der verhältnismäßig wenig platz hat, die Daten eines std::vectors aber auf dem Heap liegen, der für normale Fälle quasi unendlich groß ist.

Als Faustregel kannst du dir merken: Sobald du von irgendetwas einmal mehr als '1' hast, sei es jetzt Vertexe oder irgendwelche Objekte, ist std::vector eine sehr gute erste Wahl. Und im Gegensatz dazu sind normale C-Arrays immer eine schlechte Wahl.
Lieber dumm fragen, als dumm bleiben!

lfp

Frischling

  • »lfp« ist der Autor dieses Themas
  • Private Nachricht senden

7

14.03.2013, 14:09

Oh, ihr scheint ja tatsächlich bereits die Lösung zu kennen.
Ich schreib jetzt die Version um, so dass die vector-class verwendet wird und schick euch dann die Fehlermeldungen, die ich dabei ja schonmal bekommen hab.
Ich geh dann auch nochmal auf die Fargen ein, da meine Funktion sicher unnötig kompliziert aussieht.

Bis gleich...

H5::

Treue Seele

Beiträge: 368

Wohnort: Kiel

  • Private Nachricht senden

8

14.03.2013, 15:07

Nur um das kurz an zu merken, es gibt ja trotzdem ab und an den Fall wo man die Dynamic und Features eines Vectors nicht benötigt. In dem Fall sollte man dann am besten auf das C++ std::array zurück greifen und die C Arrays sofern möglich ganz meiden.
:love: := Go;

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

9

14.03.2013, 15:10

Wohl eher std::unique_ptr, std::array ist ein Array statischer Größe... ;)

lfp

Frischling

  • »lfp« ist der Autor dieses Themas
  • Private Nachricht senden

10

14.03.2013, 15:21

Dem Fehler auf der Spur...

So, hier die neue Version mit Benutzen der vector-class:

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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
void LoadObject(LPDIRECT3DDEVICE9 D3DDevice){
    //Deklarationen
    fstream  datei;
    
    long int indexzwischenbuffer [10];
    
    int numvertices = 0;
    HRESULT _hResult;

    VERTEX Vertices;
    vector<VERTEX> Vertices_VECTOR;
    VERTEX Vertices_sortiert;

    bool LadenErfolgreich = true;
    bool negativ;

    datei.open("objekt.obj", ios::in);
    while( (textbuf[0] == 'f' && textbuf[1] == ' ') == false && datei.eof() == false){
     datei.getline(textbuf, 1000);
    }

    if(datei.eof()){
     LadenErfolgreich = false;
     Fehler(hWnd, "Fehler beim Lesen des Dateiformats!", MB_OK);
    }else{
     i4 = 0;
     while(textbuf[0] == 'f' && textbuf[1] == ' ' && datei.eof() == false){
         indexzwischenbuffer[0] = 0;
         indexzwischenbuffer[1] = 0;
         indexzwischenbuffer[2] = 0;
         indexzwischenbuffer[3] = 0;

         i3 = 2;
         i2 = 0;
         while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
             i3 ++;
         }
         if(textbuf[i3] != ' '){
             while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                 i3 ++;
             }
         }
         i3 ++;
         while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
             i3 ++;
         }
         if(textbuf[i3] != ' '){
             while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                 i3 ++;
             }
         }
         i3 ++;
         while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
             i3 ++;
         }
         if(textbuf[i3] != ' '){
             while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                 i3 ++;
             }
         }
         i3 ++;

         i2 = 0;
         while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
             textbuf2[i2] = textbuf[i3];

             i2 ++;
             i3 ++;
         }

         if(textbuf[i3] != '\0'){
             indexzwischenbuffer[3] = ( myatoi(textbuf2) /*- 1*/);
         }
         datei.getline(textbuf, 1000);

         if(indexzwischenbuffer[3] == 0){
             i4 ++;
             i4 ++;
             i4 ++;
             primitivecount ++;
         }else{
             i4 ++;
             i4 ++;
             i4 ++;
             i4 ++;
             i4 ++;
             i4 ++;
             primitivecount += 2;
         }
     }
    }
    datei.close();
    if(FAILED(_hResult = D3DDevice->CreateVertexBuffer(i4*sizeof(VERTEX), 0, LoadObjectFVF, D3DPOOL_MANAGED, &VertexBuffer, NULL))){
     Fehler(hWnd, "Fehler beim Erstellen des Vertexbuffers!", MB_OK);
    }
    VertexBuffer->Lock(0,0,(void**)(&Vertices_sortiert_VECTOR), D3DLOCK_NOSYSLOCK);

    datei.open("objekt.obj", ios::in);
    if(datei.good() == false){
     LadenErfolgreich = false;
     Fehler(hWnd, "Fehler beim Öffnen des Objekts", MB_OK);
    }else{
     primitivecount = 0;
     while( (textbuf[0] == 'v' && textbuf[1] == ' ') == false && datei.eof() == false){
         datei.getline(textbuf, 1000);
     }
     if(datei.eof()){
         LadenErfolgreich = false;
         Fehler(hWnd, "Fehler beim Lesen des Dateiformats!", MB_OK);
     }else{
         i3 = 0;

         while(textbuf[0] == 'v' && textbuf[1] == ' '){
             i = 2;
             i2 = 0;
             negativ = false;
             if(textbuf[i] == '-'){
                 i++;
                 negativ = true;
             }
             while(textbuf[i] != ' ' && textbuf[i] != '.'){
                 textbuf2[i2] = textbuf[i];

                 i2 ++;
                 i  ++;
             }
             textbuf2[i2] = '\0';
             f = myatoi(textbuf2);

             if(textbuf[i] == '.'){
                 i++;
                 i2 = 0;

                 i4 = 10;
                 valid = true;
                 while(textbuf[i] != ' '){
                     if(textbuf[i] == '0' && valid){
                         i4 *= 10;
                     }else{
                         valid = false;
                         textbuf2[i2] = textbuf[i];
                         i2 ++;
                     }

                     i  ++;
                 }
                 textbuf2[i2] = '\0';

                 i5 = arraylaenge(textbuf2);
                 i5 --;
                 while(i5 > 0){
                     i4 *= 10;
                     i5 --;
                 }
                 
                 f += ( ((float) myatoi(textbuf2)) / (float) i4 );
             }
             if(negativ){
                 Vertices.x = ( f * (-1) );
             }else{
                 Vertices.x = f;
             }

             i ++;
             i2 = 0;
             negativ = false;
             if(textbuf[i] == '-'){
                 i++;
                 negativ = true;
             }
             while(textbuf[i] != ' ' && textbuf[i] != '.'){
                 textbuf2[i2] = textbuf[i];

                 i2 ++;
                 i  ++;
             }
             textbuf2[i2] = '\0';
             f = myatoi(textbuf2);

             if(textbuf[i] == '.'){
                 i++;
                 i2 = 0;
                 i4 = 10;
                 valid = true;
                 while(textbuf[i] != ' '){
                     if(textbuf[i] == '0' && valid){
                         i4 *= 10;
                     }else{
                         valid = false;
                         textbuf2[i2] = textbuf[i];
                         i2 ++;
                     }

                     i  ++;
                 }
                 textbuf2[i2] = '\0';

                 
                 i5 = arraylaenge(textbuf2);
                 i5 --;
                 while(i5 > 0){
                     i4 *= 10;
                     i5 --;
                 }
                 f += ( ((float) myatoi(textbuf2)) / (float) i4 );
             }
             if(negativ){
                 Vertices.y = ( f * (-1) );
             }else{
                 Vertices.y = f;
             }

             i ++;
             i2 = 0;
             negativ = false;
             if(textbuf[i] == '-'){
                 i++;
                 negativ = true;
             }
             while(textbuf[i] != '\0' && textbuf[i] != '.'){
                 textbuf2[i2] = textbuf[i];

                 i2 ++;
                 i  ++;
             }
             textbuf2[i2] = '\0';
             f = myatoi(textbuf2);

             if(textbuf[i] == '.'){
                 i++;
                 i2 = 0;
                 i4 = 10;
                 valid = true;
                 while(textbuf[i] != '\0'){
                     if(textbuf[i] == '0' && valid){
                         i4 *= 10;
                     }else{
                         valid = false;
                         textbuf2[i2] = textbuf[i];
                         i2 ++;
                     }

                     i  ++;
                 }
                 textbuf2[i2] = '\0';

                 
                 i5 = arraylaenge(textbuf2);
                 i5 --;
                 while(i5 > 0){
                     i4 *= 10;
                     i5 --;
                 }
                 f += ( ((float) myatoi(textbuf2)) / (float) i4 );
             }
             if(negativ){
                 Vertices.z = ( f * (-1) );
             }else{
                 Vertices.z = f;
             }
             if(oldrand1 == myrandomnumber(0, 255)){
             
             }
             Vertices.Color = D3DCOLOR_XRGB(0, 255, 50);
             Vertices_VECTOR.push_back(Vertices);

             i3 ++;
             datei.getline(textbuf, 1000);
         }

         while( (textbuf[0] == 'f' && textbuf[1] == ' ') == false && datei.eof() == false){
             datei.getline(textbuf, 1000);
         }

         if(datei.eof()){
             LadenErfolgreich = false;
             Fehler(hWnd, "Fehler beim Lesen des Dateiformats!", MB_OK);
         }else{
             i4 = 0;
             while(textbuf[0] == 'f' && textbuf[1] == ' ' && datei.eof() == false){
                 indexzwischenbuffer[0] = 0;
                 indexzwischenbuffer[1] = 0;
                 indexzwischenbuffer[2] = 0;
                 indexzwischenbuffer[3] = 0;

                 i3 = 2;
                 i2 = 0;
                 while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                     textbuf2[i2] = textbuf[i3];

                     i2 ++;
                     i3 ++;
                 }
                 textbuf2[i2] = '\0';

                 if(textbuf[i3] != '\0'){
                     indexzwischenbuffer[0] = ( myatoi(textbuf2) );
                 }
                 if(textbuf[i3] != ' '){
                     while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                         i3 ++;
                     }
                 }
                 i3 ++;

                 i2 = 0;
                 while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                     textbuf2[i2] = textbuf[i3];

                     i2 ++;
                     i3 ++;
                 }
                 textbuf2[i2] = '\0';

                 if(textbuf[i3] != '\0'){
                     indexzwischenbuffer[1] = ( myatoi(textbuf2) );
                 }

                 if(textbuf[i3] != ' '){
                     while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                         i3 ++;
                     }
                 }
                 i3 ++;

                 i2 = 0;
                 while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                     textbuf2[i2] = textbuf[i3];

                     i2 ++;
                     i3 ++;
                 }
                 textbuf2[i2] = '\0';

                 if(textbuf[i3] != '\0'){
                     indexzwischenbuffer[2] = ( myatoi(textbuf2) );
                 }

                 if(textbuf[i3] != ' '){
                     while(textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                         i3 ++;
                     }
                 }
                 i3 ++;

                 i2 = 0;
                 while(textbuf[i3] != '/' && textbuf[i3] != ' ' && textbuf[i3] != '\0'){
                     textbuf2[i2] = textbuf[i3];

                     i2 ++;
                     i3 ++;
                 }
                 textbuf2[i2] = '\0';

                 if(textbuf[i3] != '\0'){
                     indexzwischenbuffer[3] = ( myatoi(textbuf2) );
                 }
                 datei.getline(textbuf, 1000);

                 if(indexzwischenbuffer[3] == 0){
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[0]- 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[0]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[0]- 1].z;
                     Vertices_sortiert.Color = Vertices_VECTOR[indexzwischenbuffer[0]- 1].Color;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[1]- 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[1]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[1]- 1].z;
                     Vertices_sortiert.Color = Vertices_VECTOR[indexzwischenbuffer[1]- 1].Color;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[2]- 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[2]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[2]- 1].z;
                     Vertices_sortiert.Color = Vertices_VECTOR[indexzwischenbuffer[2]- 1].Color;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;
                     primitivecount ++;
                 }else{
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[0]- 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[0]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[0]- 1].z;
                     Vertices_sortiert.Color = Vertices_VECTOR[indexzwischenbuffer[0]- 1].Color;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[1]- 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[1]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[1]- 1].z;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[2]- 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[2]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[2]- 1].z;
                     Vertices_sortiert.Color = Vertices_VECTOR[indexzwischenbuffer[2]- 1].Color;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[0] - 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[0]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[0]- 1].z;
                     Vertices_sortiert.Color = Vertices_VECTOR[indexzwischenbuffer[2]- 1].Color;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[3]- 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[3]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[3]- 1].z;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;
                     Vertices_sortiert.x = Vertices_VECTOR[indexzwischenbuffer[2]- 1].x;
                     Vertices_sortiert.y = Vertices_VECTOR[indexzwischenbuffer[2]- 1].y;
                     Vertices_sortiert.z = Vertices_VECTOR[indexzwischenbuffer[2]- 1].z;
                     Vertices_sortiert.Color = Vertices_VECTOR[indexzwischenbuffer[0]- 1].Color;
                     Vertices_sortiert_VECTOR.push_back(Vertices_sortiert);
                     i4 ++;

                     primitivecount += 2;
                 }
             }
             
             VertexBuffer->Unlock();

             primitivecount+=2;      
         }
     }
    }
}


In den header hab ich natürlich noch vector<VERTEX> Vertices_sortiert_VECTOR; hinzugefügt.

Beim Ausführen gibt mir mein Compiler folgenden Fehler aus:


Hilft euch das weiter?

MSDN sagt mir :
"Some components of the standard library, such as vector and string also throw exceptions of this type to signal errors resizing."

...heißt also irgendwie misshandel ich die vector-class... aber was mach ich falsch?

Ich verstehe nicht warum du so viele Schleifen da drin hast.
Das ist einfach:
Ich möchte möglichst jede Art von .obj-Datei einlesen können, und hab schon verschiedene Ausführungen gesehen, die einmal mit, einmal ohne vertex-koordinaten ausgerüstet sind oder mal keine Normals bei den faces angegeben haben. Ich müsste zwar rein theoretisch auch *.mtl-Dateien und Texturen einlesen und auch das unterteilen eines Objekts in mehrere unterobjekte berücksichtigen um behaupten zu können, eine universal-*.obj-import-funktion zu haben, aber damit fang ich lieber erst an, wenn die Basics funktionieren.

Um die Array Größen zu bestimmen habe ich vorher noch einen Schleifendurchlauf wo ich die Datei einmal komplett durchnudel und gucke wieviel im *.obj drin ist.

oh, Überraschung! Das hab ich auch!
Zeile 18: while(textbuf[0] == 'f' && textbuf[1] == ' ' && datei.eof() == false){ :this:
Dafür öffne ich die Datei auch zweimal, falls dir das noch nicht aufgefallen ist.
Apropos:
std::vector benutzen, dann brauchst du die Datei nicht zweimal durchlaufen... ;)

Tja, ich lass die Datei auch nur zweimal durchlaufen, um den VertexBuffer zu erstellen.

C-/C++-Quelltext

1
if(FAILED(_hResult = D3DDevice->CreateVertexBuffer(i4*sizeof(VERTEX), 0, LoadObjectFVF, D3DPOOL_MANAGED, &VertexBuffer, NULL))){

Eigentlich könnte ich das auch anders machen... aber das kann ich ja immernoch machen, wenns so funktioniert. Inwiefern hilft mir dabei jetzt die vector-class?

die C Arrays sofern möglich ganz meiden.

:miffy: Hey! Ich mag die normalen C-Arrays :D
ok... vielleicht such ich mir mal ne Alternative... bevor ihr mich mit den Nachteilen von C-Arrays bombadiert...

So... ich denke ich hab jetzt alle Fragen beantwortet und hoffe ihr könnt was damit anfangen...

Werbeanzeige