質問編集履歴

1

文章を修正

2021/12/29 03:10

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 提示コードですが下記の.objファイルのv要素の読み込みでパターン1、パターン2、ともにアクセスエラーが発生してしまいます。またコメント部のコードは普通に実行できます。なぜ二つはアクセスエラーになるのでしょうか?
1
+ 提示コードですが下記の.objファイルのv要素の読み込みで`///`コメント部内部のパターン1、パターン2、ともにアクセスエラーが発生してしまいます。またコメント部のコードは普通に実行できます。なぜ二つはアクセスエラーになるのでしょうか?
2
2
 
3
3
 
4
4
 
@@ -6,52 +6,414 @@
6
6
 
7
7
  ```
8
8
 
9
+
10
+
11
+
12
+
13
+ // ##################################### .objファイル読み込み #####################################
14
+
9
- else if (strcmp(line, "v") == 0)
15
+ std::shared_ptr<std::vector<FrameWork::Vertex>> FrameWork::GetVertex(const char* path)
10
16
 
11
17
  {
12
18
 
13
-
19
+ #define LINE_BUFFER ((int)(500))
20
+
21
+
22
+
14
-
23
+ //FrameWork::Object_File object;
24
+
25
+ std::vector<FrameWork::Vertex> polygon;
26
+
27
+
28
+
29
+ std::vector<int> vertexIndex;
30
+
31
+ std::vector<int> uvIndex;
32
+
33
+ std::vector<int> normalIndex;
34
+
35
+
36
+
37
+ std::vector<glm::vec3> vertex;
38
+
39
+ std::vector<glm::vec2> uv;
40
+
41
+ std::vector<glm::vec3> normal;
42
+
43
+
44
+
45
+ FILE* file;
46
+
47
+ fopen_s(&file, path, "r");
48
+
49
+
50
+
51
+ bool b = false;
52
+
15
- //パターン1
53
+ if (file == NULL)
54
+
16
-
55
+ {
56
+
57
+ std::cerr << ".objファイルが開けません: " << path << std::endl;
58
+
59
+ assert(0);
60
+
61
+ }
62
+
63
+ else
64
+
65
+ {
66
+
67
+ FrameWork::vertex temp;
68
+
69
+ bool b = false;
70
+
71
+ while (true)
72
+
73
+ {
74
+
75
+
76
+
77
+ char line[500] = {'\0'};
78
+
79
+
80
+
81
+ int res = fscanf_s(file, "%s", line,LINE_BUFFER);
82
+
83
+ if (res == EOF)
84
+
85
+ {
86
+
87
+ break;
88
+
89
+ }
90
+
91
+
92
+
93
+ if (strcmp(line, "o") == 0)
94
+
95
+ {
96
+
17
- char vert[3][LINE_BUFFER] = { { '\0' } ,{ '\0' } ,{ '\0' } };
97
+ char str[LINE_BUFFER] = {'\0'};
18
-
98
+
19
- fscanf_s(file, "%s %s %s ", vert[0],vert[1],vert[2]);
99
+ fscanf_s(file, "%s", str,LINE_BUFFER);
100
+
101
+ printf("%s\n",str);
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+ if (b == true)
112
+
113
+ {
114
+
115
+ polygon.push_back(temp);
116
+
117
+ temp.attribute.resize(0);
118
+
119
+ }
120
+
121
+
122
+
123
+ if (b == false)
124
+
125
+ {
126
+
127
+ b = true;
128
+
129
+ }
130
+
131
+ }
132
+
133
+ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
134
+
135
+ else if (strcmp(line, "v") == 0)
136
+
137
+ {
20
138
 
21
139
 
22
140
 
23
-
24
-
25
- //パターン2
141
+ //パターン
26
-
142
+
27
- glm::vec3 vert = glm::vec3(0,0,0);
143
+ char vert[3][LINE_BUFFER] = { { '\0' } ,{ '\0' } ,{ '\0' } };
28
-
144
+
29
- fscanf_s(file, "%f %f %f", vert.x,vert.y,vert.z);
145
+ fscanf_s(file, "%s %s %s ", vert[0], vert[1], vert[2]);
30
146
 
31
147
 
32
148
 
33
- /*
34
-
35
- char vert[LINE_BUFFER] ={'\0'};
36
-
37
- float v = 0;
38
-
39
- fscanf_s(file, "%s", &v, LINE_BUFFER);
40
-
41
- printf("v %s\n", v);
42
-
43
- */
44
-
45
-
46
-
47
-
48
-
49
- //vertex.push_back(vert);
50
-
51
-
149
+
150
+
151
+ //パターン2
152
+
153
+ glm::vec3 v = glm::vec3(0, 0, 0);
154
+
155
+ fscanf_s(file, "%f %f %f", v.x, v.y, v..z);
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+ /*
168
+
169
+ glm::vec3 v;
170
+
171
+ char vert[LINE_BUFFER] ={'\0'};
172
+
173
+
174
+
175
+ fscanf_s(file, "%s", &vert, LINE_BUFFER);
176
+
177
+ v.x = (float)atof(vert);
178
+
179
+
180
+
181
+ fscanf_s(file, "%s", &vert, LINE_BUFFER);
182
+
183
+ v.y = (float)atof(vert);
184
+
185
+
186
+
187
+ fscanf_s(file, "%s", &vert, LINE_BUFFER);
188
+
189
+ v.z = (float)atof(vert);
190
+
191
+
192
+
193
+ vertex.push_back(v);
194
+
195
+
196
+
197
+ printf("v %f %f %f \n", v.x, v.y, v.z);
198
+
199
+ */
200
+
201
+
202
+
203
+ }
204
+
205
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
206
+
207
+ else if (strcmp(line, "vt") == 0)
208
+
209
+ {
210
+
211
+
212
+
213
+ glm::vec2 u;
214
+
215
+ char vert[LINE_BUFFER] = { '\0' };
216
+
217
+
218
+
219
+ fscanf_s(file, "%s", &vert, LINE_BUFFER);
220
+
221
+ u.x = (float)atof(vert);
222
+
223
+
224
+
225
+ fscanf_s(file, "%s", &vert, LINE_BUFFER);
226
+
227
+ u.y = (float)atof(vert);
228
+
229
+
230
+
231
+
232
+
233
+ printf("vt %f %f \n", u.x, u.y);
234
+
235
+
236
+
237
+ uv.push_back(u);
238
+
239
+ }
240
+
241
+ else if (strcmp(line, "vn") == 0)
242
+
243
+ {
244
+
245
+ glm::vec3 norm;
246
+
247
+ fscanf_s(file, "%f %f %fn", &norm.x, &norm.y, &norm.z);
248
+
249
+ normal.push_back(norm);
250
+
251
+ }
252
+
253
+ else if (strcmp(line, "f") == 0)
254
+
255
+ {
256
+
257
+ unsigned int v[3], u[3], n[3];
258
+
259
+ int matches = fscanf_s(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &v[0], &u[0], &n[0], &v[1], &u[1], &n[1], &v[2], &u[2], &n[2]);
260
+
261
+
262
+
263
+ vertexIndex.push_back(v[0]);
264
+
265
+ vertexIndex.push_back(v[1]);
266
+
267
+ vertexIndex.push_back(v[2]);
268
+
269
+
270
+
271
+ uvIndex.push_back(u[0]);
272
+
273
+ uvIndex.push_back(u[1]);
274
+
275
+ uvIndex.push_back(u[2]);
276
+
277
+
278
+
279
+ normalIndex.push_back(n[0]);
280
+
281
+ normalIndex.push_back(n[1]);
282
+
283
+ normalIndex.push_back(n[2]);
284
+
285
+ }
286
+
287
+ else if (strcmp(line, "usemtl") == 0)
288
+
289
+ {
290
+
291
+ char str[LINE_BUFFER];
292
+
293
+ char number[LINE_BUFFER];
294
+
295
+ fscanf_s(file, "%s", str, LINE_BUFFER);
296
+
297
+
298
+
299
+
300
+
301
+ strncpy_s(number, str + strlen("Material."), LINE_BUFFER);
302
+
303
+
304
+
305
+ int num = atoi(number);
306
+
307
+
308
+
309
+ printf("newmtl Material.%d\n",num);
310
+
311
+
312
+
313
+ temp.materialNumber = num;
314
+
315
+ }
316
+
317
+ else if (strcmp(line, "s") == 0)
318
+
319
+ {
320
+
321
+ char str[LINE_BUFFER];
322
+
323
+ char number[LINE_BUFFER];
324
+
325
+ fscanf_s(file, "%s", str, LINE_BUFFER);
326
+
327
+
328
+
329
+
330
+
331
+ strncpy_s(number, str, LINE_BUFFER);
332
+
333
+
334
+
335
+ int num = atoi(number);
336
+
337
+
338
+
339
+ printf("s %d\n", num);
340
+
341
+ temp.shading = num;
342
+
343
+
344
+
345
+ //temp.materialNumber = num;
346
+
347
+ }
348
+
349
+ }
350
+
351
+
352
+
353
+ for (unsigned int i = 0; i < vertexIndex.size(); i++)
354
+
355
+ {
356
+
357
+ unsigned int vi = vertexIndex[i];
358
+
359
+ unsigned int ui = uvIndex[i];
360
+
361
+ unsigned int ni = normalIndex[i];
362
+
363
+
364
+
365
+ glm::vec3 v = vertex[vi - 1];
366
+
367
+ glm::vec2 u = uv[ui - 1];
368
+
369
+ glm::vec3 n = normal[ni - 1];
370
+
371
+
372
+
373
+ FrameWork::VertexAttribute attrib;
374
+
375
+
376
+
377
+ attrib.position[0] = v.x;
378
+
379
+ attrib.position[1] = v.y;
380
+
381
+ attrib.position[2] = v.z;
382
+
383
+
384
+
385
+ attrib.uv[0] = u.x;
386
+
387
+ attrib.uv[1] = u.y;
388
+
389
+
390
+
391
+ attrib.normal[0] = n.x;
392
+
393
+ attrib.normal[1] = n.y;
394
+
395
+ attrib.normal[2] = n.z;
396
+
397
+
398
+
399
+ temp.attribute.push_back(attrib);
400
+
401
+ }
402
+
403
+ }
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+ return std::make_shared<std::vector<FrameWork::Vertex>>(polygon);
52
412
 
53
413
  }
54
414
 
415
+
416
+
55
417
  ```
56
418
 
57
419