質問編集履歴

1

ソースコード

2017/11/14 07:35

投稿

windowsaa
windowsaa

スコア16

test CHANGED
File without changes
test CHANGED
@@ -3,885 +3,3 @@
3
3
 
4
4
 
5
5
  どうすれば描画できるようになりますか?
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
- [sorce code]
14
-
15
- ```C++
16
-
17
- #include<iostream>
18
-
19
- #include<gl/glut.h>
20
-
21
- #include"stlreader.h"
22
-
23
-
24
-
25
- #define STL_FILE "C:\Users\owner\Desktop\nsx_concept_LOW.stl"
26
-
27
-
28
-
29
- void display(void);
30
-
31
- void reshape(int w, int h);
32
-
33
- void draw();
34
-
35
- void init();
36
-
37
- void DRAW_XYZ();
38
-
39
-
40
-
41
- Triangle *triangles;
42
-
43
- int num;
44
-
45
- STLreader stlreader;
46
-
47
- //unsigned char key = NULL;
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
- int main(int argc, char *argv[])
56
-
57
- {
58
-
59
- glutInitWindowPosition(128, 128);
60
-
61
- glutInitWindowSize(800, 800);
62
-
63
- glutInit(&argc, argv);
64
-
65
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
66
-
67
- glutCreateWindow("argv[0]");
68
-
69
- init();
70
-
71
- glutDisplayFunc(display);
72
-
73
- glutReshapeFunc(reshape);
74
-
75
- // glutKeyboardFunc(keyboard);
76
-
77
-
78
-
79
- glutMainLoop();
80
-
81
-
82
-
83
- if (triangles)
84
-
85
- delete[] triangles;
86
-
87
-
88
-
89
- return 0;
90
-
91
- }
92
-
93
-
94
-
95
-
96
-
97
- void display(void)
98
-
99
- {
100
-
101
-
102
-
103
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104
-
105
-
106
-
107
- glLoadIdentity();
108
-
109
- gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //視点の設定
110
-
111
-
112
-
113
- draw(); //描画
114
-
115
-
116
-
117
- glutSwapBuffers(); //ウィンドウに出力
118
-
119
-
120
-
121
- }
122
-
123
-
124
-
125
-
126
-
127
- void reshape(int w, int h)
128
-
129
- {
130
-
131
- glViewport(0, 0, w, h); //ビューポートの設定
132
-
133
-
134
-
135
- glMatrixMode(GL_PROJECTION);
136
-
137
- glLoadIdentity();
138
-
139
- gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0); //視野の設定
140
-
141
- glMatrixMode(GL_MODELVIEW);
142
-
143
- }
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
- void draw()
152
-
153
- {
154
-
155
- if (triangles)
156
-
157
- delete[] triangles;
158
-
159
-
160
-
161
- if (!(num = STLreader::countTriangles(string(STL_FILE))))
162
-
163
-
164
-
165
- return;
166
-
167
-
168
-
169
- triangles = new Triangle[num];
170
-
171
-
172
-
173
- stlreader.readASCIISTLFile(STL_FILE, triangles);
174
-
175
-
176
-
177
- stlreader.readBinarySTLFile(STL_FILE, triangles);
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
- glClear(GL_COLOR_BUFFER_BIT);
186
-
187
-
188
-
189
- DRAW_XYZ();
190
-
191
-
192
-
193
- glBegin(GL_TRIANGLES);
194
-
195
-
196
-
197
- glColor3f(0.0, 0.0, 0.0);
198
-
199
-
200
-
201
- for (int i = 0; i < num; i++)
202
-
203
- {
204
-
205
-
206
-
207
- glNormal3dv(triangles[i].nornVec);
208
-
209
- glVertex3dv(triangles[i].pnt1);
210
-
211
- glVertex3dv(triangles[i].pnt2);
212
-
213
- glVertex3dv(triangles[i].pnt3);
214
-
215
-
216
-
217
- }
218
-
219
-
220
-
221
-
222
-
223
- glEnd();
224
-
225
- glFlush();
226
-
227
-
228
-
229
- }
230
-
231
-
232
-
233
-
234
-
235
- void init()
236
-
237
- {
238
-
239
-
240
-
241
- glClearColor(1.0, 1.0, 1.0, 1.0);
242
-
243
- }
244
-
245
-
246
-
247
- void DRAW_XYZ()
248
-
249
- {
250
-
251
- glBegin(GL_LINES);
252
-
253
-
254
-
255
- glColor3f(0, 1, 0);//x
256
-
257
- glVertex2f(-100, 0);
258
-
259
- glVertex2f(100, 0);
260
-
261
-
262
-
263
- glColor3f(1, 0, 0);//y
264
-
265
- glVertex2f(0, 0);
266
-
267
- glVertex2f(0, 100);
268
-
269
-
270
-
271
- glColor3f(0, 0, 1);//z
272
-
273
- glVertex3f(0, 0, -100);
274
-
275
- glVertex3f(0, 0, 100);
276
-
277
- glEnd();
278
-
279
-
280
-
281
- }
282
-
283
-
284
-
285
-
286
-
287
- ```C++
288
-
289
- ヘッダーファイルです
290
-
291
- ```
292
-
293
- #include <iostream>
294
-
295
- #include <cmath>
296
-
297
- #include <string>
298
-
299
- #include <fstream>
300
-
301
-
302
-
303
- using namespace std;
304
-
305
-
306
-
307
- struct vert
308
-
309
- {
310
-
311
- double pnt1[3];
312
-
313
- double pnt2[3];
314
-
315
- double pnt3[3];
316
-
317
- };
318
-
319
-
320
-
321
- struct vert4
322
-
323
- {
324
-
325
- double pnt1[4];//x,y,z,w
326
-
327
- double pnt2[4];
328
-
329
- double pnt3[4];
330
-
331
- };
332
-
333
-
334
-
335
- struct Triangle {
336
-
337
- double nornVec[3];
338
-
339
- double pnt1[3];
340
-
341
- double pnt2[3];
342
-
343
- double pnt3[3];
344
-
345
- unsigned int index;
346
-
347
- };
348
-
349
-
350
-
351
-
352
-
353
- class STLreader
354
-
355
- {
356
-
357
- ////一時的に利用する点の構造体
358
-
359
- //typedef struct tmp_point{
360
-
361
- // double coord[3]; //座標
362
-
363
- // unsigned int index; //点に付随するユニークなインデックス
364
-
365
- //}tmp_point;
366
-
367
-
368
-
369
- ////一時的に利用する辺の構造体
370
-
371
- //typedef struct tmp_edge{
372
-
373
- // unsigned int start;
374
-
375
- // unsigned int end;
376
-
377
- // tmp_edge *next;
378
-
379
- //}tmp_edge;
380
-
381
-
382
-
383
- ////点とその個数、一時的な利用
384
-
385
- //tmp_point *point_array[INT_MAX / 100000];
386
-
387
-
388
-
389
- //unsigned int num_points;//点の数
390
-
391
- //unsigned int num_edges;//辺の数
392
-
393
-
394
-
395
- unsigned int num_tmp_points;//一時的な変数
396
-
397
- unsigned int num_triangles;//三角形の数
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
- public:
406
-
407
- static int countTriangles(string filename)
408
-
409
- {
410
-
411
- //STLファイルのオープン
412
-
413
- ifstream ifs;
414
-
415
- char line[80];
416
-
417
- ifs.open(filename);
418
-
419
- if (ifs.fail())
420
-
421
- return false;
422
-
423
- string str;
424
-
425
- int count = 0;
426
-
427
- int a;
428
-
429
- getline(ifs, str);
430
-
431
- //int h = str.find("solid");
432
-
433
- if (str.find("solid")==-1){
434
-
435
- ifs.close();
436
-
437
- ifs.open(filename, ios::binary);
438
-
439
- if (ifs.fail())
440
-
441
- return false;
442
-
443
- ifs.read(line, 80);
444
-
445
- ifs.read(line, 4);
446
-
447
- return *(int *)line;
448
-
449
- }
450
-
451
- while (getline(ifs, str)){
452
-
453
- a = str.find("normal");
454
-
455
- if (a > 0)
456
-
457
- count++;
458
-
459
- }
460
-
461
- ifs.close();
462
-
463
- return count;
464
-
465
- };
466
-
467
-
468
-
469
- bool readASCIISTLFile(string filename, Triangle *values)
470
-
471
- {
472
-
473
-
474
-
475
- //STLファイルのオープン
476
-
477
- ifstream ifs(filename);
478
-
479
- if (ifs.fail())
480
-
481
- return false;
482
-
483
-
484
-
485
- //vector<mesh> Stldata;
486
-
487
- string str;
488
-
489
- int index = 0, j;
490
-
491
- double x = 0, y = 0, z = 0;
492
-
493
- //tmp_point *tmp_pnt = new tmp_point;
494
-
495
-
496
-
497
- ////STLファイルのオープン
498
-
499
- //in = fopen(filename.c_str(), "r");
500
-
501
- //if (in == NULL)
502
-
503
- // return false;
504
-
505
- getline(ifs, str);
506
-
507
- if (0 < str.find("solid"))
508
-
509
- return false;
510
-
511
- //ファイルから座標値の読み込み
512
-
513
- printf("Tring text STL file ...");
514
-
515
- num_tmp_points = 0;
516
-
517
- num_triangles = 0;
518
-
519
-
520
-
521
-
522
-
523
- while (getline(ifs, str)){
524
-
525
-
526
-
527
- ////normalが見つかるまで読み飛ばす
528
-
529
- //i = str.find("normal");
530
-
531
- //if (i > 0){
532
-
533
- // for (int c = 2; c >= 0; c--){
534
-
535
- // values[num_triangles].nornVec[c] = atof(str.substr(str.rfind(" ")).c_str());
536
-
537
- // str.erase(str.rfind(" "));
538
-
539
- // num_tmp_points++;
540
-
541
- // }
542
-
543
- //}
544
-
545
-
546
-
547
- //vertexが見つかるまで読み飛ばす
548
-
549
- j = str.find("vertex");
550
-
551
- if (j<0)
552
-
553
- continue;
554
-
555
- //連続する3頂点の読み込みポリゴンを登録
556
-
557
- //tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
558
-
559
- for (int c = 2; c >= 0; c--)
560
-
561
- {
562
-
563
- values[num_triangles].pnt1[c] = atof(str.substr(str.rfind(" ")).c_str());
564
-
565
- str.erase(str.rfind(" "));
566
-
567
- num_tmp_points++;
568
-
569
- }
570
-
571
- //point_array[num_tmp_points-1] = tmp_pnt;
572
-
573
-
574
-
575
- //tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
576
-
577
- getline(ifs, str);
578
-
579
-
580
-
581
- for (int c = 2; c >= 0; c--)
582
-
583
- {
584
-
585
- values[num_triangles].pnt2[c] = atof(str.substr(str.rfind(" ")).c_str());
586
-
587
- str.erase(str.rfind(" "));
588
-
589
- //tmp_pnt->index = num_tmp_points;
590
-
591
- num_tmp_points++;
592
-
593
- }
594
-
595
- //point_array[num_tmp_points-1] = tmp_pnt;
596
-
597
-
598
-
599
- //tmp_pnt = (tmp_point *)malloc(sizeof(tmp_point));
600
-
601
- getline(ifs, str);
602
-
603
-
604
-
605
- for (int c = 2; c >= 0; c--)
606
-
607
- {
608
-
609
- values[num_triangles].pnt3[c] = atof(str.substr(str.rfind(" ")).c_str());
610
-
611
- str.erase(str.rfind(" "));
612
-
613
- //tmp_pnt->index = num_tmp_points;
614
-
615
- num_tmp_points++;
616
-
617
- }
618
-
619
-
620
-
621
- values[num_triangles].nornVec[0] = getNormalVert(values[num_triangles])[0];
622
-
623
- values[num_triangles].nornVec[1] = getNormalVert(values[num_triangles])[1];
624
-
625
- values[num_triangles].nornVec[2] = getNormalVert(values[num_triangles])[2];
626
-
627
-
628
-
629
- values[num_triangles].index = num_triangles;
630
-
631
- num_triangles++;
632
-
633
-
634
-
635
- }
636
-
637
- ifs.close();
638
-
639
- //fclose(in);
640
-
641
- if (num_triangles > 0)
642
-
643
- printf("Done.\n");
644
-
645
- else
646
-
647
- printf("Failed\n");
648
-
649
- return(num_triangles > 0);
650
-
651
-
652
-
653
- }
654
-
655
-
656
-
657
- bool readBinarySTLFile(string STL_file, Triangle *values)
658
-
659
- {
660
-
661
- char line[81];
662
-
663
- float *coord;
664
-
665
- int num;
666
-
667
- FILE *in;
668
-
669
- errno_t error;
670
-
671
-
672
-
673
- ifstream ifs(STL_file);
674
-
675
- if (ifs.fail())
676
-
677
- return false;
678
-
679
- //ifstream ifs(STL_file, ios_base::binary);
680
-
681
- //ofstream ofs("result.txt");//結果を書き込む用
682
-
683
-
684
-
685
- //if (!ifs)
686
-
687
- //{
688
-
689
- // // エラー処理
690
-
691
- // ofs << "Can't open file " << endl;
692
-
693
- // ifs.close();
694
-
695
- // return false;
696
-
697
- //}
698
-
699
-
700
-
701
- if ((error = fopen_s(&in,STL_file.c_str(), "rb")) != 0)
702
-
703
- {
704
-
705
- // エラー処理
706
-
707
- //ofs << "Can't open file " << endl;
708
-
709
- fclose(in);
710
-
711
- return false;
712
-
713
- }
714
-
715
-
716
-
717
- //最初の84ビット分を読み飛ばす
718
-
719
- if (fread(line, 1, 80, in) != 80)
720
-
721
- return false;
722
-
723
-
724
-
725
- printf("Tring binary STL file ...");
726
-
727
- fread(line, 1, 4, in);
728
-
729
- num = *(int*)line;
730
-
731
-
732
-
733
- //ofs << "number of triangles: " << num<< endl;
734
-
735
- int j = 0;
736
-
737
- //ofs << "solid model" << endl;
738
-
739
- while (fread(line, 1, 50, in) == 50)
740
-
741
- {
742
-
743
- coord = (float *)line;
744
-
745
- //values[j].nornVec[0] = coord[0];
746
-
747
- //values[j].nornVec[1] = coord[1];
748
-
749
- //values[j].nornVec[2] = coord[2];
750
-
751
-
752
-
753
- values[j].pnt1[0] = coord[3];
754
-
755
- values[j].pnt1[1] = coord[4];
756
-
757
- values[j].pnt1[2] = coord[5];
758
-
759
-
760
-
761
- values[j].pnt2[0] = coord[6];
762
-
763
- values[j].pnt2[1] = coord[7];
764
-
765
- values[j].pnt2[2] = coord[8];
766
-
767
-
768
-
769
- values[j].pnt3[0] = coord[9];
770
-
771
- values[j].pnt3[1] = coord[10];
772
-
773
- values[j].pnt3[2] = coord[11];
774
-
775
-
776
-
777
- values[j].nornVec[0] = getNormalVert(values[j])[0];
778
-
779
- values[j].nornVec[1] = getNormalVert(values[j])[1];
780
-
781
- values[j].nornVec[2] = getNormalVert(values[j])[2];
782
-
783
- values[j].index = j;
784
-
785
- j++;
786
-
787
-
788
-
789
- }
790
-
791
-
792
-
793
- //ofs << "endsolid model" << endl;
794
-
795
- fclose(in);
796
-
797
- //ofs.close();
798
-
799
- if (num > 0)
800
-
801
- printf("Done.\n");
802
-
803
- else
804
-
805
- printf("Failed\n");
806
-
807
- return(num > 0);
808
-
809
-
810
-
811
- }
812
-
813
-
814
-
815
- //vec0とvec1の内積の計算
816
-
817
- double dot(double vec0[], double vec1[])
818
-
819
- {
820
-
821
- return (vec0[0] * vec1[0] + vec0[1] * vec1[1] + vec0[2] * vec1[2]);
822
-
823
- }
824
-
825
-
826
-
827
- void cross(double vec0[], double vec1[], double vec2[])
828
-
829
- {
830
-
831
- vec2[0] = vec0[1] * vec1[2] - vec0[2] * vec1[1];
832
-
833
- vec2[1] = vec0[2] * vec1[0] - vec0[0] * vec1[2];
834
-
835
- vec2[2] = vec0[0] * vec1[1] - vec0[1] * vec1[0];
836
-
837
- }
838
-
839
-
840
-
841
- void normVec(double vec[])
842
-
843
- {
844
-
845
- double norm;
846
-
847
- norm = sqrt((vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]));
848
-
849
- vec[0] /= norm; vec[1] /= norm; vec[2] /= norm;
850
-
851
- }
852
-
853
-
854
-
855
- double* getNormalVert(Triangle vertex)
856
-
857
- {
858
-
859
- double normalVec[3] = {};
860
-
861
- double vec1[3] = { vertex.pnt2[0] - vertex.pnt1[0],
862
-
863
- vertex.pnt2[1] - vertex.pnt1[1],
864
-
865
- vertex.pnt2[2] - vertex.pnt1[2] };
866
-
867
- double vec2[3] = { vertex.pnt3[0] - vertex.pnt2[0],
868
-
869
- vertex.pnt3[1] - vertex.pnt2[1],
870
-
871
- vertex.pnt3[2] - vertex.pnt2[2] };
872
-
873
- cross(vec1, vec2, normalVec);
874
-
875
- normVec(normalVec);//正規化
876
-
877
- return normalVec;
878
-
879
- }
880
-
881
-
882
-
883
-
884
-
885
- };
886
-
887
- ```