質問編集履歴
3
文章を修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -459,3 +459,621 @@
|
|
459
459
|
}
|
460
460
|
|
461
461
|
```
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
```cpp
|
468
|
+
|
469
|
+
#include "Shader.hpp"
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
#include <iostream>
|
474
|
+
|
475
|
+
#include <fstream>
|
476
|
+
|
477
|
+
#include <string>
|
478
|
+
|
479
|
+
#include <vector>
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
#include <glm/gtc/type_ptr.hpp>
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
//コンストラクタ
|
488
|
+
|
489
|
+
Shader::Shader(const char* vert, const char* frag)
|
490
|
+
|
491
|
+
{
|
492
|
+
|
493
|
+
program = loadProgram(vert,frag);
|
494
|
+
|
495
|
+
if (program == 0)
|
496
|
+
|
497
|
+
{
|
498
|
+
|
499
|
+
std::cerr << "シェーダープログラム作成エラー"<<std::endl;
|
500
|
+
|
501
|
+
exit(1);
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
//std::cout<< program << std::endl;
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
//シェーダーをロード
|
522
|
+
|
523
|
+
GLuint Shader::loadProgram(const char* vert, const char* frag)
|
524
|
+
|
525
|
+
{
|
526
|
+
|
527
|
+
std::vector<GLchar> vsrc;
|
528
|
+
|
529
|
+
const bool vstat = ReadShaderSource(vert, vsrc);
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
std::vector<GLchar> fsrc;
|
534
|
+
|
535
|
+
const bool fstat = ReadShaderSource(frag, fsrc);
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
if (vstat && fstat)
|
542
|
+
|
543
|
+
{
|
544
|
+
|
545
|
+
return CreateProgram(vsrc.data(), fsrc.data());
|
546
|
+
|
547
|
+
}
|
548
|
+
|
549
|
+
else {
|
550
|
+
|
551
|
+
return 0;
|
552
|
+
|
553
|
+
}
|
554
|
+
|
555
|
+
}
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
|
562
|
+
|
563
|
+
//シェーダーファイルを読み込む
|
564
|
+
|
565
|
+
bool Shader::ReadShaderSource(const char* name, std::vector<GLchar>& buffer)
|
566
|
+
|
567
|
+
{
|
568
|
+
|
569
|
+
if (name == NULL)
|
570
|
+
|
571
|
+
{
|
572
|
+
|
573
|
+
return false;
|
574
|
+
|
575
|
+
}
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
|
580
|
+
|
581
|
+
std::ifstream file(name, std::ios::binary);
|
582
|
+
|
583
|
+
if (file.fail())
|
584
|
+
|
585
|
+
{
|
586
|
+
|
587
|
+
std::cerr << "ソースファイルが読み込めません: " << name << std::endl;
|
588
|
+
|
589
|
+
file.close();
|
590
|
+
|
591
|
+
return false;
|
592
|
+
|
593
|
+
}
|
594
|
+
|
595
|
+
|
596
|
+
|
597
|
+
file.seekg(0L, std::ios::end);
|
598
|
+
|
599
|
+
GLsizei length = static_cast<GLsizei>(file.tellg());
|
600
|
+
|
601
|
+
buffer.resize(length + 1);
|
602
|
+
|
603
|
+
|
604
|
+
|
605
|
+
file.seekg(0L, std::ios::beg);
|
606
|
+
|
607
|
+
file.read(buffer.data(), length);
|
608
|
+
|
609
|
+
buffer[length] = '\0';
|
610
|
+
|
611
|
+
|
612
|
+
|
613
|
+
if (file.fail())
|
614
|
+
|
615
|
+
{
|
616
|
+
|
617
|
+
std::cerr << "ソースファイルを読み込めません: " << name << std::endl;
|
618
|
+
|
619
|
+
file.close();
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
return false;
|
624
|
+
|
625
|
+
}
|
626
|
+
|
627
|
+
|
628
|
+
|
629
|
+
file.close();
|
630
|
+
|
631
|
+
return true;
|
632
|
+
|
633
|
+
}
|
634
|
+
|
635
|
+
|
636
|
+
|
637
|
+
|
638
|
+
|
639
|
+
//シェーダーエラーログを取得
|
640
|
+
|
641
|
+
GLboolean Shader::CompileInfoLog(GLuint shader,const char* str)
|
642
|
+
|
643
|
+
{
|
644
|
+
|
645
|
+
GLint status;
|
646
|
+
|
647
|
+
|
648
|
+
|
649
|
+
//コンパイル結果
|
650
|
+
|
651
|
+
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
652
|
+
|
653
|
+
if (status == GL_FALSE)
|
654
|
+
|
655
|
+
{
|
656
|
+
|
657
|
+
std::cerr <<"Compile Error: " << str << std::endl;
|
658
|
+
|
659
|
+
}
|
660
|
+
|
661
|
+
|
662
|
+
|
663
|
+
//エラーログの長さを得る
|
664
|
+
|
665
|
+
GLsizei bufSize;
|
666
|
+
|
667
|
+
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &bufSize);
|
668
|
+
|
669
|
+
|
670
|
+
|
671
|
+
if (bufSize > 1)
|
672
|
+
|
673
|
+
{
|
674
|
+
|
675
|
+
std::vector<GLchar> infoLog(bufSize);
|
676
|
+
|
677
|
+
GLsizei length;
|
678
|
+
|
679
|
+
glGetShaderInfoLog(shader, bufSize, &length, &infoLog[0]);
|
680
|
+
|
681
|
+
|
682
|
+
|
683
|
+
std::cerr<< &infoLog[0] << std::endl;
|
684
|
+
|
685
|
+
}
|
686
|
+
|
687
|
+
|
688
|
+
|
689
|
+
|
690
|
+
|
691
|
+
|
692
|
+
|
693
|
+
return (GLboolean)status;
|
694
|
+
|
695
|
+
}
|
696
|
+
|
697
|
+
|
698
|
+
|
699
|
+
|
700
|
+
|
701
|
+
|
702
|
+
|
703
|
+
|
704
|
+
|
705
|
+
//プログラムオブジェクト作成
|
706
|
+
|
707
|
+
GLuint Shader::CreateProgram(const char* vsrc, const char* fsrc)
|
708
|
+
|
709
|
+
{
|
710
|
+
|
711
|
+
const GLuint program = glCreateProgram(); //シェーダープログラムを作成
|
712
|
+
|
713
|
+
|
714
|
+
|
715
|
+
//std::cout << program << std::endl;
|
716
|
+
|
717
|
+
|
718
|
+
|
719
|
+
if (vsrc != NULL)
|
720
|
+
|
721
|
+
{
|
722
|
+
|
723
|
+
const GLuint vobj = glCreateShader(GL_VERTEX_SHADER);
|
724
|
+
|
725
|
+
glShaderSource(vobj, 1, &vsrc, NULL);
|
726
|
+
|
727
|
+
glCompileShader(vobj);
|
728
|
+
|
729
|
+
|
730
|
+
|
731
|
+
CompileInfoLog(vobj, vsrc);
|
732
|
+
|
733
|
+
|
734
|
+
|
735
|
+
glAttachShader(program, vobj);
|
736
|
+
|
737
|
+
glDeleteShader(vobj);
|
738
|
+
|
739
|
+
|
740
|
+
|
741
|
+
}
|
742
|
+
|
743
|
+
else {
|
744
|
+
|
745
|
+
std::cout << "頂点シェーダー読み込み失敗" << std::endl;
|
746
|
+
|
747
|
+
}
|
748
|
+
|
749
|
+
|
750
|
+
|
751
|
+
if (fsrc != NULL)
|
752
|
+
|
753
|
+
{
|
754
|
+
|
755
|
+
const GLuint fobj = glCreateShader(GL_FRAGMENT_SHADER);
|
756
|
+
|
757
|
+
glShaderSource(fobj, 1, &fsrc, NULL);
|
758
|
+
|
759
|
+
glCompileShader(fobj);
|
760
|
+
|
761
|
+
|
762
|
+
|
763
|
+
CompileInfoLog(fobj, fsrc);
|
764
|
+
|
765
|
+
|
766
|
+
|
767
|
+
glAttachShader(program, fobj);
|
768
|
+
|
769
|
+
glDeleteShader(fobj);
|
770
|
+
|
771
|
+
}
|
772
|
+
|
773
|
+
else {
|
774
|
+
|
775
|
+
std::cout << "フラグメントシェーダー読み込み失敗" << std::endl;
|
776
|
+
|
777
|
+
|
778
|
+
|
779
|
+
}
|
780
|
+
|
781
|
+
|
782
|
+
|
783
|
+
glLinkProgram(program);
|
784
|
+
|
785
|
+
|
786
|
+
|
787
|
+
ProgramInfoLog(program);
|
788
|
+
|
789
|
+
|
790
|
+
|
791
|
+
|
792
|
+
|
793
|
+
return program;
|
794
|
+
|
795
|
+
}
|
796
|
+
|
797
|
+
|
798
|
+
|
799
|
+
//プログラムのエラーを表示
|
800
|
+
|
801
|
+
GLboolean Shader::ProgramInfoLog(GLuint program)
|
802
|
+
|
803
|
+
{
|
804
|
+
|
805
|
+
GLsizei bufSize;
|
806
|
+
|
807
|
+
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufSize);
|
808
|
+
|
809
|
+
|
810
|
+
|
811
|
+
if (bufSize > 1)
|
812
|
+
|
813
|
+
{
|
814
|
+
|
815
|
+
std::vector<GLchar> infoLog(bufSize);
|
816
|
+
|
817
|
+
|
818
|
+
|
819
|
+
GLsizei length;
|
820
|
+
|
821
|
+
glGetProgramInfoLog(program, bufSize, &length, &infoLog[0]);
|
822
|
+
|
823
|
+
std::cerr<<"Program Info Log: "<< infoLog.data() <<std::endl;
|
824
|
+
|
825
|
+
|
826
|
+
|
827
|
+
return false;
|
828
|
+
|
829
|
+
}
|
830
|
+
|
831
|
+
else
|
832
|
+
|
833
|
+
{
|
834
|
+
|
835
|
+
return true;
|
836
|
+
|
837
|
+
}
|
838
|
+
|
839
|
+
}
|
840
|
+
|
841
|
+
|
842
|
+
|
843
|
+
|
844
|
+
|
845
|
+
|
846
|
+
|
847
|
+
|
848
|
+
|
849
|
+
//locationを取得
|
850
|
+
|
851
|
+
GLint Shader::getAttribLocation(const char* str)
|
852
|
+
|
853
|
+
{
|
854
|
+
|
855
|
+
return glGetAttribLocation(program,str);
|
856
|
+
|
857
|
+
}
|
858
|
+
|
859
|
+
|
860
|
+
|
861
|
+
|
862
|
+
|
863
|
+
|
864
|
+
|
865
|
+
//頂点シェーダーに属性変数を関連ずける
|
866
|
+
|
867
|
+
void Shader::setBindAttribVertex(const char* str)
|
868
|
+
|
869
|
+
{
|
870
|
+
|
871
|
+
GLint n = glGetAttribLocation(program, str);
|
872
|
+
|
873
|
+
|
874
|
+
|
875
|
+
//エラー処理
|
876
|
+
|
877
|
+
if (n == -1)
|
878
|
+
|
879
|
+
{
|
880
|
+
|
881
|
+
std::cerr <<"setBindAttribVertex(): "<< n << std::endl;
|
882
|
+
|
883
|
+
}
|
884
|
+
|
885
|
+
else
|
886
|
+
|
887
|
+
{
|
888
|
+
|
889
|
+
glBindAttribLocation(program, n, str);
|
890
|
+
|
891
|
+
}
|
892
|
+
|
893
|
+
}
|
894
|
+
|
895
|
+
|
896
|
+
|
897
|
+
|
898
|
+
|
899
|
+
//フラグメントシェーダーに属性変数を関連ずける
|
900
|
+
|
901
|
+
void Shader::setBindAttribFragment(const char* str)
|
902
|
+
|
903
|
+
{
|
904
|
+
|
905
|
+
GLint n = glGetAttribLocation(program, str);
|
906
|
+
|
907
|
+
|
908
|
+
|
909
|
+
//エラー処理
|
910
|
+
|
911
|
+
if (n == -1)
|
912
|
+
|
913
|
+
{
|
914
|
+
|
915
|
+
std::cerr << "setBindAttribFragment(): " << n << std::endl;
|
916
|
+
|
917
|
+
}
|
918
|
+
|
919
|
+
else
|
920
|
+
|
921
|
+
{
|
922
|
+
|
923
|
+
glBindFragDataLocation(program, n, str);
|
924
|
+
|
925
|
+
}
|
926
|
+
|
927
|
+
}
|
928
|
+
|
929
|
+
|
930
|
+
|
931
|
+
//有効にする
|
932
|
+
|
933
|
+
void Shader::setEnable()
|
934
|
+
|
935
|
+
{
|
936
|
+
|
937
|
+
glUseProgram(program);
|
938
|
+
|
939
|
+
|
940
|
+
|
941
|
+
}
|
942
|
+
|
943
|
+
|
944
|
+
|
945
|
+
//無効にする
|
946
|
+
|
947
|
+
void Shader::setDisable()
|
948
|
+
|
949
|
+
{
|
950
|
+
|
951
|
+
glUseProgram(0);
|
952
|
+
|
953
|
+
|
954
|
+
|
955
|
+
}
|
956
|
+
|
957
|
+
|
958
|
+
|
959
|
+
|
960
|
+
|
961
|
+
// ######################################################### Uniform 設定
|
962
|
+
|
963
|
+
|
964
|
+
|
965
|
+
//vec1
|
966
|
+
|
967
|
+
void Shader::setUniform1f(const char* name, const float vec)
|
968
|
+
|
969
|
+
{
|
970
|
+
|
971
|
+
const GLuint object = glGetUniformLocation(program, name);
|
972
|
+
|
973
|
+
glUniform1f(object,vec);
|
974
|
+
|
975
|
+
}
|
976
|
+
|
977
|
+
|
978
|
+
|
979
|
+
//vec2
|
980
|
+
|
981
|
+
void Shader::setUniform2f(const char* name, const glm::vec2 vec)
|
982
|
+
|
983
|
+
{
|
984
|
+
|
985
|
+
const GLuint object = glGetUniformLocation(program, name);
|
986
|
+
|
987
|
+
glUniform2f(object,vec.x,vec.y);
|
988
|
+
|
989
|
+
}
|
990
|
+
|
991
|
+
|
992
|
+
|
993
|
+
//vec3
|
994
|
+
|
995
|
+
void Shader::setUniform3f(const char* name, const glm::vec3 vec)
|
996
|
+
|
997
|
+
{
|
998
|
+
|
999
|
+
const GLuint object = glGetUniformLocation(program, name);
|
1000
|
+
|
1001
|
+
glUniform3f(object,vec.x, vec.y,vec.z);
|
1002
|
+
|
1003
|
+
}
|
1004
|
+
|
1005
|
+
|
1006
|
+
|
1007
|
+
//vec4
|
1008
|
+
|
1009
|
+
void Shader::setUniform4f(const char* name, const glm::vec4 vec)
|
1010
|
+
|
1011
|
+
{
|
1012
|
+
|
1013
|
+
const GLuint object = glGetUniformLocation(program,name);
|
1014
|
+
|
1015
|
+
glUniform4f(object, vec.x, vec.y, vec.z, vec.w);
|
1016
|
+
|
1017
|
+
}
|
1018
|
+
|
1019
|
+
|
1020
|
+
|
1021
|
+
|
1022
|
+
|
1023
|
+
//行列2
|
1024
|
+
|
1025
|
+
void Shader::setUniformMatrix2fv(const char* name, const glm::mat2 m)
|
1026
|
+
|
1027
|
+
{
|
1028
|
+
|
1029
|
+
const GLuint object = glGetUniformLocation(program, name);
|
1030
|
+
|
1031
|
+
glUniformMatrix2fv(object,1,false,glm::value_ptr(m));
|
1032
|
+
|
1033
|
+
}
|
1034
|
+
|
1035
|
+
|
1036
|
+
|
1037
|
+
//行列3
|
1038
|
+
|
1039
|
+
void Shader::setUniformMatrix3fv(const char* name, const glm::mat3 m)
|
1040
|
+
|
1041
|
+
{
|
1042
|
+
|
1043
|
+
const GLuint object = glGetUniformLocation(program, name);
|
1044
|
+
|
1045
|
+
glUniformMatrix3fv(object, 1, false, glm::value_ptr(m));
|
1046
|
+
|
1047
|
+
}
|
1048
|
+
|
1049
|
+
|
1050
|
+
|
1051
|
+
//行列4
|
1052
|
+
|
1053
|
+
void Shader::setUniformMatrix4fv(const char* name, const glm::mat4 m)
|
1054
|
+
|
1055
|
+
{
|
1056
|
+
|
1057
|
+
const GLuint object = glGetUniformLocation(program, name);
|
1058
|
+
|
1059
|
+
glUniformMatrix4fv(object, 1, false, glm::value_ptr(m));
|
1060
|
+
|
1061
|
+
|
1062
|
+
|
1063
|
+
}
|
1064
|
+
|
1065
|
+
|
1066
|
+
|
1067
|
+
//デストラクタ
|
1068
|
+
|
1069
|
+
Shader::~Shader()
|
1070
|
+
|
1071
|
+
{
|
1072
|
+
|
1073
|
+
|
1074
|
+
|
1075
|
+
}
|
1076
|
+
|
1077
|
+
|
1078
|
+
|
1079
|
+
```
|
2
文章を修正しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
参考サイト
|
5
|
+
[参考サイト](http://www.opengl-tutorial.org/jp/beginners-tutorials/tutorial-3-matrices/)
|
8
6
|
|
9
7
|
|
10
8
|
|
1
文章を修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
提示コードの頂点シェーダーコードなのですが参考サイトを参考にモデル行列計算やすべての行列計算をシェーダーで行いたいのですが画面に正方形の四角い線が描画されません。これはなぜなのでしょうか?そもそもシェーダーでは出来ないのでしょうか?
|
1
|
+
提示コードの頂点シェーダーコードなのですが参考サイトを参考にモデル行列計算やすべての行列計算をシェーダーで行いたいのですが画面に正方形の四角い線が描画されません。これはなぜなのでしょうか?そもそもシェーダーでは出来ないのでしょうか?行列をかける順番や処理の内容を参考サイトを参考によく考えましたがどうしても原因がわかりません。
|
2
2
|
|
3
3
|
|
4
4
|
|