teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

文章を修正

2021/06/22 07:33

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -5,17 +5,127 @@
5
5
 
6
6
  参考サイト: https://www.jpcert.or.jp/sc-rules/c-msc39-c.html
7
7
  ```cpp
8
+ // ##################################### フォーマット書式描画 #####################################
9
+ void FrameWork::Text::DrawFormatString(glm::vec2 pos, glm::vec4 color, const char* str)
10
+ {
11
+
12
+ std::vector<Character> textData(0); //文字データを格納
13
+
14
+ if (str != NULL)
15
+ {
16
+ ///////////////////////////////////////////////////////////////////////////////////////////////
8
- //マルチバイト文字をワイド文字変換
17
+ //マルチバイト文字をワイド文字変換
9
18
  wchar_t txt[1000] = { L'\0' };
10
- char text[1000];
19
+ char text[1000] = {'\0'};
11
20
 
12
21
  va_list args = NULL;
13
- va_list args2 = NULL;
14
-
15
- va_start(args2, str);
22
+ va_start(args, str);
16
- va_copy(args,args2);
17
-
18
23
  vsprintf_s(text, sizeof(text), str, args);
19
24
  va_end(args);
25
+ //////////////////////////////////////////////////////////////////////////////////////////////
26
+ int i, j, f;
27
+ for (i = 0, j = 0; text[j]; i++, j += f)
28
+ {
29
+ f = (int)mbrtowc(txt + i, &text[j], (size_t)MB_CUR_MAX, nullptr);
30
+ }
20
31
 
32
+ //文字をロード
33
+ for (int i = 0; txt[i] != L'\0'; i++)
34
+ {
35
+ //グリフをロード
36
+ FT_Load_Glyph(face, FT_Get_Char_Index(face, txt[i]), FT_LOAD_RENDER);
37
+
38
+ Character ch =
39
+ {
40
+ 0,
41
+ glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
42
+ glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
43
+ (unsigned int)face->glyph->advance.x
44
+ };
45
+
46
+ glGenTextures(1, &ch.textureID);
47
+ glBindTexture(GL_TEXTURE_2D, ch.textureID);
48
+ glActiveTexture(GL_TEXTURE0);
49
+
50
+ glTexImage2D
51
+ (
52
+ GL_TEXTURE_2D,
53
+ 0,
54
+ GL_RED,
55
+ face->glyph->bitmap.width,
56
+ face->glyph->bitmap.rows,
57
+ 0,
58
+ GL_RED,
59
+ GL_UNSIGNED_BYTE,
60
+ face->glyph->bitmap.buffer
61
+ );
62
+
63
+ //テクスチャタイプを設定
64
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
65
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
66
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
67
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
68
+
69
+
70
+ //テクスチャを生成
71
+ textData.push_back(ch);
72
+
73
+ }
74
+
75
+
76
+
77
+ shader->setEnable(); //シェーダーを有効にする
78
+ glBindVertexArray(vao);
79
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
80
+
81
+ //色をRGBにして位置を反転
82
+ pos.y = FrameWork::getWindowContext()->getSize().y - pos.y - charSize;
83
+ float c = 1.0f / 255.0f;
84
+
85
+ //Unform
86
+ shader->setUniform4f("uTextColor", color * c);
87
+ shader->setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, FrameWork::getWindowContext()->getSize().x, 0.0f, FrameWork::getWindowContext()->getSize().y));
88
+
89
+ for (std::vector<Character>::iterator itr = textData.begin(); itr != textData.end(); itr++)
90
+ {
91
+ #define SCALE 1.0f //文字の大きさ
92
+
93
+ float xpos = pos.x + itr->Bearing.x * SCALE;
94
+ float ypos = pos.y - (itr->Size.y - itr->Bearing.y) * SCALE;
95
+
96
+ float w = itr->Size.x * SCALE;
97
+ float h = itr->Size.y * SCALE;
98
+
99
+ float vertices[6][4] =
100
+ {
101
+ { xpos, ypos + h, 0.0f, 0.0f },
102
+ { xpos, ypos, 0.0f, 1.0f },
103
+ { xpos + w, ypos, 1.0f, 1.0f },
104
+
105
+ { xpos, ypos + h, 0.0f, 0.0f },
106
+ { xpos + w, ypos, 1.0f, 1.0f },
107
+ { xpos + w, ypos + h, 1.0f, 0.0f }
108
+ };
109
+
110
+ glBindTexture(GL_TEXTURE_2D, itr->textureID);
111
+ glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
112
+ glDrawArrays(GL_TRIANGLES, 0, 6);
113
+ glBindTexture(GL_TEXTURE_2D, 0);
114
+
115
+ pos.x += ((itr->Advance >> 6) * SCALE); //次のグリフに進める
116
+ #undef SCALE
117
+ }
118
+
119
+ glBindVertexArray(0);
120
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
121
+ shader->setDisable(); //シェーダーを無効にする
122
+ }
123
+
124
+
125
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
126
+ glBindVertexArray(0);
127
+ }
128
+
129
+
130
+
21
131
  ```