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

質問編集履歴

2

再度文章を修正

2021/05/10 10:18

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,8 +1,9 @@
1
- Openglでfreetypeを使って日本語の文字を描画したいです。 提示サイトでは英語の文字表示をやり方が乗っているのでその通描画したのですが英語しか描画されせん、日本語を描画させるにどういっ行為を行うのでしょうか? 色々調べましたわかりません。
1
+ Openglでfreetypeを使って日本語の文字を描画したいです。 提示サイトでは英語の文字表示をやり方が乗っている方では事前に用意したアルファベット文字を描画していすが。日本語ははずです。提示コード//////コメント内部のfor文参考サイトを参考に
2
+ const char16_t型にキャストして日本語文字の描画を試みましたが字の枠しか描画されませんこれはどうしたらいいのでしょうか?
2
3
 
3
4
 
4
5
 
5
-
6
+ ![イメージ説明](3a3c2811ee4c418acc232cc701b82478.png)
6
7
  参考サイト: http://blendgimper.hatenablog.jp/entry/2016/01/01/074615
7
8
  参考サイト: https://learnopengl.com/In-Practice/Text-Rendering
8
9
  ```cpp
@@ -64,58 +65,12 @@
64
65
 
65
66
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // disable byte-alignment restriction
66
67
 
67
- /*
68
- for (unsigned char c = 0; c < 128; c++)
69
- {
70
- // load character glyph
71
- if (FT_Load_Char(face, c, FT_LOAD_RENDER))
72
- {
73
- std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
74
- continue;
75
- }
76
- // generate texture
77
- unsigned int texture;
78
- glGenTextures(1, &texture);
79
- glBindTexture(GL_TEXTURE_2D, texture);
80
- glTexImage2D(
81
- GL_TEXTURE_2D,
82
- 0,
83
- GL_RED,
84
- face->glyph->bitmap.width,
85
- face->glyph->bitmap.rows,
86
- 0,
87
- GL_RED,
88
- GL_UNSIGNED_BYTE,
89
- face->glyph->bitmap.buffer
90
- );
91
- // set texture options
92
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
93
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
94
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
95
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
96
- // now store character for later use
97
- Character character = {
98
- texture,
99
- glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
100
- glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
101
- (unsigned int)face->glyph->advance.x
102
-
103
- };
104
-
105
- Characters.insert(std::pair<char, Character>(c, character));
106
-
107
- }
108
- */
109
-
110
-
111
-
112
-
113
68
  //アルファブレンドを有効
114
69
  glEnable(GL_BLEND);
115
70
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
116
71
  }
117
72
 
118
- void FrameWork::Text::Draw(glm::vec2 pos,std::string text,float scale, glm::vec3 color)
73
+ void FrameWork::Text::Draw(glm::vec2 pos,const char16_t* text,float scale, glm::vec3 color)
119
74
  {
120
75
  setEnable(); //シェーダーを有効にする
121
76
 
@@ -128,20 +83,14 @@
128
83
  //Unform
129
84
  setUniform3f("textColor",color);
130
85
  setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, windowContext->getSize().x, 0.0f, windowContext->getSize().y));
131
-
132
-
133
-
134
-
135
-
136
-
137
- std::string::const_iterator c;
86
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
138
- for (c = text.begin(); c != text.end(); c++)
87
+ for (int i = 0; text[i] != '\0'; i++)
139
88
  {
140
89
 
141
90
  unsigned int texture = 0;
142
91
 
143
92
  // load character glyph
144
- FT_Load_Glyph(face, FT_Get_Char_Index(face, *c), FT_LOAD_RENDER);
93
+ FT_Load_Glyph(face, FT_Get_Char_Index(face, (char16_t)text[i]), FT_LOAD_RENDER);
145
94
 
146
95
 
147
96
  // now store character for later use
@@ -155,8 +104,9 @@
155
104
 
156
105
 
157
106
  // generate texture
158
- glGenTextures(1, &texture);
107
+ glGenTextures(1, &ch.textureID);
159
- glBindTexture(GL_TEXTURE_2D, texture);
108
+ glBindTexture(GL_TEXTURE_2D, ch.textureID);
109
+
160
110
  glTexImage2D(
161
111
  GL_TEXTURE_2D,
162
112
  0,
@@ -193,7 +143,6 @@
193
143
  { xpos + w, ypos + h, 1.0f, 0.0f }
194
144
  };
195
145
  // render glyph texture over quad
196
- glBindTexture(GL_TEXTURE_2D, ch.textureID);
197
146
  // update content of VBO memory
198
147
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
199
148
  glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
@@ -207,45 +156,7 @@
207
156
  // now advance cursors for next glyph (note that advance is number of 1/64 pixels)
208
157
  pos.x += ((ch.Advance >> 6) * scale); // bitshift by 6 to get value in pixels (2^6 = 64)
209
158
  }
210
-
211
- /*
212
- // iterate through all characters
213
- std::string::const_iterator c;
214
- for (c = text.begin(); c != text.end(); c++)
215
- {
216
- Character ch = Characters[*c];
217
-
218
-
219
- float xpos = pos.x + ch.Bearing.x * scale;
220
- float ypos = pos.y - (ch.Size.y - ch.Bearing.y) * scale;
221
-
222
- float w = ch.Size.x * scale;
223
- float h = ch.Size.y * scale;
224
- // update VBO for each character
225
- float vertices[6][4] = {
226
- { xpos, ypos + h, 0.0f, 0.0f },
227
- { xpos, ypos, 0.0f, 1.0f },
228
- { xpos + w, ypos, 1.0f, 1.0f },
229
-
230
- { xpos, ypos + h, 0.0f, 0.0f },
231
- { xpos + w, ypos, 1.0f, 1.0f },
232
- { xpos + w, ypos + h, 1.0f, 0.0f }
233
- };
234
- // render glyph texture over quad
235
- glBindTexture(GL_TEXTURE_2D, ch.textureID);
236
- // update content of VBO memory
237
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
238
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
239
- glBindBuffer(GL_ARRAY_BUFFER, 0);
240
- // render quad
241
- glDrawArrays(GL_TRIANGLES, 0, 6);
242
-
243
- // now advance cursors for next glyph (note that advance is number of 1/64 pixels)
244
- pos.x += ((ch.Advance >> 6) * scale); // bitshift by 6 to get value in pixels (2^6 = 64)
159
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
245
-
246
- }
247
- */
248
-
249
160
  glBindVertexArray(0);
250
161
  glBindTexture(GL_TEXTURE_2D, 0);
251
162
  setDisable(); //シェーダーを無効にする

1

提示コードを修正

2021/05/10 10:18

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -43,7 +43,7 @@
43
43
  //FreeTypeを初期化
44
44
  //
45
45
  //初期化
46
- FT_Library ft;
46
+
47
47
  if (FT_Init_FreeType(&ft) != 0)
48
48
  {
49
49
  std::cout << "ERROR::FREETYPE: Could not init FreeType Library"<<std::endl;
@@ -51,9 +51,8 @@
51
51
  }
52
52
 
53
53
  //フェイス作成
54
- FT_Face face;
55
54
 
56
- if (FT_New_Face(ft, "Font/ariali.ttf", 0, &face) != 0)
55
+ if (FT_New_Face(ft, "C:\Windows\Fonts\meiryo.ttc", 0, &face) != 0)
57
56
  {
58
57
  std::cout<<"ERROR::FREETYPE: Failed to load font" << std::endl;
59
58
  assert(0);
@@ -65,6 +64,7 @@
65
64
 
66
65
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // disable byte-alignment restriction
67
66
 
67
+ /*
68
68
  for (unsigned char c = 0; c < 128; c++)
69
69
  {
70
70
  // load character glyph
@@ -101,16 +101,15 @@
101
101
  (unsigned int)face->glyph->advance.x
102
102
 
103
103
  };
104
+
104
105
  Characters.insert(std::pair<char, Character>(c, character));
106
+
105
107
  }
108
+ */
106
109
 
107
110
 
108
111
 
109
112
 
110
- //グリフ解放
111
- FT_Done_Face(face);
112
- FT_Done_FreeType(ft);
113
-
114
113
  //アルファブレンドを有効
115
114
  glEnable(GL_BLEND);
116
115
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -128,10 +127,88 @@
128
127
 
129
128
  //Unform
130
129
  setUniform3f("textColor",color);
130
+ setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, windowContext->getSize().x, 0.0f, windowContext->getSize().y));
131
+
131
132
 
133
+
134
+
135
+
136
+
137
+ std::string::const_iterator c;
138
+ for (c = text.begin(); c != text.end(); c++)
139
+ {
140
+
141
+ unsigned int texture = 0;
142
+
143
+ // load character glyph
144
+ FT_Load_Glyph(face, FT_Get_Char_Index(face, *c), FT_LOAD_RENDER);
145
+
146
+
147
+ // now store character for later use
148
+ Character ch = {
149
+ texture,
150
+ glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
151
+ glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
152
+ (unsigned int)face->glyph->advance.x
153
+
154
+ };
155
+
156
+
157
+ // generate texture
158
+ glGenTextures(1, &texture);
159
+ glBindTexture(GL_TEXTURE_2D, texture);
160
+ glTexImage2D(
161
+ GL_TEXTURE_2D,
162
+ 0,
163
+ GL_RED,
164
+ face->glyph->bitmap.width,
165
+ face->glyph->bitmap.rows,
166
+ 0,
167
+ GL_RED,
168
+ GL_UNSIGNED_BYTE,
169
+ face->glyph->bitmap.buffer
170
+ );
171
+ // set texture options
172
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
173
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
174
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
175
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
176
+ // now store character for later use
177
+
178
+
179
+
180
+ float xpos = pos.x + ch.Bearing.x * scale;
181
+ float ypos = pos.y - (ch.Size.y - ch.Bearing.y) * scale;
182
+
183
+ float w = ch.Size.x * scale;
184
+ float h = ch.Size.y * scale;
185
+ // update VBO for each character
186
+ float vertices[6][4] = {
187
+ { xpos, ypos + h, 0.0f, 0.0f },
188
+ { xpos, ypos, 0.0f, 1.0f },
189
+ { xpos + w, ypos, 1.0f, 1.0f },
190
+
191
+ { xpos, ypos + h, 0.0f, 0.0f },
192
+ { xpos + w, ypos, 1.0f, 1.0f },
193
+ { xpos + w, ypos + h, 1.0f, 0.0f }
194
+ };
195
+ // render glyph texture over quad
196
+ glBindTexture(GL_TEXTURE_2D, ch.textureID);
132
- // glm::ortho(0.0f, 800.0f, 0.0f, 600.0f);
197
+ // update content of VBO memory
198
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
199
+ glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
200
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
201
+
202
+ // render quad
203
+ glDrawArrays(GL_TRIANGLES, 0, 6);
204
+
205
+
206
+
133
- setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, windowContext->getSize().x, 0.0f, windowContext->getSize().y));
207
+ // now advance cursors for next glyph (note that advance is number of 1/64 pixels)
134
-
208
+ pos.x += ((ch.Advance >> 6) * scale); // bitshift by 6 to get value in pixels (2^6 = 64)
209
+ }
210
+
211
+ /*
135
212
  // iterate through all characters
136
213
  std::string::const_iterator c;
137
214
  for (c = text.begin(); c != text.end(); c++)
@@ -167,8 +244,8 @@
167
244
  pos.x += ((ch.Advance >> 6) * scale); // bitshift by 6 to get value in pixels (2^6 = 64)
168
245
 
169
246
  }
247
+ */
170
248
 
171
-
172
249
  glBindVertexArray(0);
173
250
  glBindTexture(GL_TEXTURE_2D, 0);
174
251
  setDisable(); //シェーダーを無効にする
@@ -178,6 +255,9 @@
178
255
 
179
256
  FrameWork::Text::~Text()
180
257
  {
258
+ //グリフ解放
259
+ FT_Done_Face(face);
260
+ FT_Done_FreeType(ft);
181
261
 
182
262
  }
183
263