質問編集履歴

1

コードの修正

2020/02/25 09:17

投稿

shukrin
shukrin

スコア14

test CHANGED
File without changes
test CHANGED
@@ -339,3 +339,225 @@
339
339
  先頭に#include <gl/glew.h>を付け足したことと画像ファイルの名前を変更したこと以外は上記サイトに掲載のコードと全く同じものです。何が原因でこのようなことになっているのか全く見当もつきません。原因に心当たりのある方がいらっしゃったらお助けいただけないでしょうか。
340
340
 
341
341
  開発環境はVIsual Studio 2019です。
342
+
343
+
344
+
345
+ ###変更したコード
346
+
347
+ いただいたアドバイスをもとに上記コードにglewの初期化処理を加えました。
348
+
349
+ ```C++
350
+
351
+ #include <gl/glew.h>
352
+
353
+ #include <GLFW/glfw3.h>
354
+
355
+ #include <iostream>
356
+
357
+ #include <fstream>
358
+
359
+ #include <vector>
360
+
361
+
362
+
363
+ using namespace std;
364
+
365
+
366
+
367
+ const int g_width = 640;
368
+
369
+ const int g_height = 480;
370
+
371
+
372
+
373
+ GLuint crateShader()
374
+
375
+ {
376
+
377
+ 省略
378
+
379
+ }
380
+
381
+
382
+
383
+ GLuint loadTexture(string filename)
384
+
385
+ {
386
+
387
+ 省略
388
+
389
+ }
390
+
391
+
392
+
393
+ int main()
394
+
395
+ {
396
+
397
+ if (!glfwInit()) {
398
+
399
+ return -1;
400
+
401
+ }
402
+
403
+
404
+
405
+ GLFWwindow* window = glfwCreateWindow(g_width, g_height, "Simple", NULL, NULL);
406
+
407
+ if (!window)
408
+
409
+ {
410
+
411
+ glfwTerminate();
412
+
413
+ return -1;
414
+
415
+ }
416
+
417
+
418
+
419
+ // モニタとの同期
420
+
421
+ glfwMakeContextCurrent(window);
422
+
423
+ glfwSwapInterval(1);
424
+
425
+
426
+
427
+
428
+
429
+ //変更箇所------------------------------------
430
+
431
+ // GLEW初期化
432
+
433
+ if (glewInit() != GLEW_OK)
434
+
435
+ {
436
+
437
+ return -1;
438
+
439
+ }
440
+
441
+ //-------------------------------------------
442
+
443
+
444
+
445
+ GLuint programId = crateShader();
446
+
447
+
448
+
449
+ GLuint texID = loadTexture("Lenna.raw");
450
+
451
+
452
+
453
+ // ゲームループ
454
+
455
+ while (!glfwWindowShouldClose(window)) {
456
+
457
+
458
+
459
+ // 画面の初期化
460
+
461
+ glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
462
+
463
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
464
+
465
+ glClearDepth(1.0);
466
+
467
+
468
+
469
+ // 頂点データ
470
+
471
+ float vertex_position[] = {
472
+
473
+ 0.5f, 0.5f,
474
+
475
+ -0.5f, 0.5f,
476
+
477
+ -0.5f, -0.5f,
478
+
479
+ 0.5f, -0.5f
480
+
481
+ };
482
+
483
+
484
+
485
+ const GLfloat vertex_uv[] = {
486
+
487
+ 1, 0,
488
+
489
+ 0, 0,
490
+
491
+ 0, 1,
492
+
493
+ 1, 1,
494
+
495
+ };
496
+
497
+
498
+
499
+ // 何番目のattribute変数か
500
+
501
+ int positionLocation = glGetAttribLocation(programId, "position");
502
+
503
+ int uvLocation = glGetAttribLocation(programId, "uv");
504
+
505
+ int textureLocation = glGetUniformLocation(programId, "texture");
506
+
507
+
508
+
509
+ // attribute属性を有効にする
510
+
511
+ glEnableVertexAttribArray(positionLocation);
512
+
513
+ glEnableVertexAttribArray(uvLocation);
514
+
515
+
516
+
517
+ // uniform属性を設定する
518
+
519
+ glUniform1i(textureLocation, 0);
520
+
521
+
522
+
523
+ // attribute属性を登録
524
+
525
+ glVertexAttribPointer(positionLocation, 2, GL_FLOAT, false, 0, vertex_position);
526
+
527
+ glVertexAttribPointer(uvLocation, 2, GL_FLOAT, false, 0, vertex_uv);
528
+
529
+
530
+
531
+
532
+
533
+ // モデルの描画
534
+
535
+ glBindTexture(GL_TEXTURE_2D, texID);
536
+
537
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
538
+
539
+
540
+
541
+ // バッファの入れ替え
542
+
543
+ glfwSwapBuffers(window);
544
+
545
+
546
+
547
+ // イベント待ち
548
+
549
+ glfwPollEvents();
550
+
551
+ }
552
+
553
+
554
+
555
+ glfwTerminate();
556
+
557
+
558
+
559
+ return 0;
560
+
561
+ }
562
+
563
+ ```