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

質問編集履歴

3

文章を修正

2021/06/18 07:18

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,9 @@
1
- 提示コードのvoid Draw();関数ですがコメント部の////内部の関数で例外エラーが発生してしまいます。itr->face のface に値がうまく入っていないのだと思いコンストラクタのfor文を確認しましたがしっかりとfaceが代入されていますこれはなぜ例外エラーが出るのでしょか?原因がわかりません。
1
+ 提示コードのvoid Draw();関数ですがコメント部の////内部の関数で例外エラーが発生してしまいます。itr->face のface に値がうまく入っていないのだと思いコンストラクタのfor文を確認しましたがしっかりとfaceが代入されていますこれはなぜ例外エラーが出るのでしょか?原因がわかりません。一番怪しいのはft_Load_glyph();関数なのですが引数を確認しましたが正しいと思います。
2
2
 
3
3
 
4
4
 
5
+ FreeTyper: https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_glyph
6
+
5
7
  ```hpp
6
8
  #ifndef ___TEXT_HPP_
7
9
  #define ___TEXT_HPP_

2

文章を修正

2021/06/18 07:18

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -86,11 +86,7 @@
86
86
  glEnableVertexAttribArray(0);
87
87
  glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
88
88
 
89
- //FreeTypeを初期化
89
+
90
- FT_Face face; //フェイス
91
- FT_Face fc = nullptr; //フェイス
92
- FT_Library ft; //freetype
93
-
94
90
  //初期化
95
91
  if (FT_Init_FreeType(&ft) != 0)
96
92
  {
@@ -123,30 +119,45 @@
123
119
  {
124
120
  f = (int)mbrtowc(txt + i, &text[j], (size_t)MB_CUR_MAX, nullptr);
125
121
  }
126
- //////////////////////////////////////////////////////////////////////////////////////////////////
122
+ /////////////////////////////////////////////////////////////////////////////////////////////////
127
123
  for (int i = 0; txt[i] != L'\0'; i++)
128
124
  {
129
125
  unsigned int texture = 0;
130
126
 
131
127
  //グリフをロード
132
- FT_Load_Glyph(fc, FT_Get_Char_Index(face, txt[i]), FT_LOAD_RENDER);
133
128
 
134
129
 
135
130
  //文字データを設定
136
131
  Character ch =
137
132
  {
138
133
  texture,
139
- fc
134
+ face
140
135
  };
136
+
141
137
  glGenTextures(1, &ch.textureID);
138
+ glBindTexture(GL_TEXTURE_2D, ch.textureID);
139
+ FT_Load_Glyph(face, FT_Get_Char_Index(face, txt[i]), FT_LOAD_RENDER);
140
+ ch.face = face;
142
141
 
142
+ glTexImage2D(
143
+ GL_TEXTURE_2D,
144
+ 0,
145
+ GL_RED,
146
+ face->glyph->bitmap.width,
147
+ face->glyph->bitmap.rows,
148
+ 0,
149
+ GL_RED,
150
+ GL_UNSIGNED_BYTE,
151
+ face->glyph->bitmap.buffer
152
+ );
143
153
 
154
+
144
155
  //テクスチャを生成
145
156
  character.push_back(ch);
146
157
  }
147
- ////////////////////////////////////////////////////////////////////////////////////////////////
158
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////
148
159
  //グリフ解放
149
- FT_Done_Face(face);
160
+ // FT_Done_Face(face);
150
161
  FT_Done_FreeType(ft);
151
162
 
152
163
  glBindBuffer(GL_ARRAY_BUFFER, 0);
@@ -174,39 +185,24 @@
174
185
  shader->setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, FrameWork::getWindowContext()->getSize().x, 0.0f, FrameWork::getWindowContext()->getSize().y));
175
186
 
176
187
 
177
-
178
188
 
189
+
179
190
  for (std::vector<Character>::iterator itr = character.begin(); itr != character.end(); itr++)
180
191
  {
181
-
182
- ///////////////////////////////////////////////////////////////////////////////////
192
+
183
- //テクスチャを生成
184
- std::cout << "いいいいい" << std::endl;
185
-
186
- glTexImage2D(
187
- GL_TEXTURE_2D,
188
- 0,
189
- GL_RED,
190
- itr->face->glyph->bitmap.width,
191
- itr->face->glyph->bitmap.rows,
192
- 0,
193
- GL_RED,
194
- GL_UNSIGNED_BYTE,
195
- itr->face->glyph->bitmap.buffer
196
- );
197
- /////////////////////////////////////////////////////////////////////////////////
198
-
199
- std::cout << "ああああ" << std::endl;
200
-
201
193
  //テクスチャタイプを設定
202
194
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
203
195
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
204
196
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
205
197
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
206
198
 
199
+ //テクスチャを生成
200
+
201
+ ///////////////////////////////////////////////////////////////////////////////////
202
+
207
203
  float xpos = pos.x + itr->face->glyph->bitmap_left * 1.0f;
208
204
  float ypos = pos.y - (itr->face->glyph->bitmap.rows - itr->face->glyph->bitmap_top) * 1.0f;
209
-
205
+ //////////////////////////////////////////////////////////////////////////////////////
210
206
  float w = itr->face->glyph->bitmap.width * 1.0f;
211
207
  float h = itr->face->glyph->bitmap.rows * 1.0f;
212
208
 
@@ -221,7 +217,6 @@
221
217
  { xpos + w, ypos + h, 1.0f, 0.0f }
222
218
  };
223
219
 
224
- glBindTexture(GL_TEXTURE_2D, itr->textureID);
225
220
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
226
221
  glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
227
222
  glDrawArrays(GL_TRIANGLES, 0, 6);

1

提示コードを追加

2021/06/18 06:58

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -2,7 +2,57 @@
2
2
 
3
3
 
4
4
 
5
+ ```hpp
6
+ #ifndef ___TEXT_HPP_
7
+ #define ___TEXT_HPP_
8
+ #include "Transform_2D.hpp"
9
+ #include "Shader.hpp"
10
+ #include "glm/glm.hpp"
11
+ #include <map>
12
+ #include "ft2build.h"
13
+ #include FT_FREETYPE_H
5
14
 
15
+ /*#########################################################################
16
+ # 文字描画クラス
17
+
18
+ 説明
19
+ 文字を描画する
20
+ ###########################################################################*/
21
+ namespace FrameWork
22
+ {
23
+ class Window;
24
+
25
+ class Text : protected FrameWork::Transform_2D
26
+ {
27
+
28
+ public:
29
+ Text(int fontSize, const char* str, ...); //コンストラクタ
30
+ ~Text(); //デストラクタ
31
+
32
+ void Draw(glm::vec2 pos,glm::vec3 color); //描画
33
+ private:
34
+
35
+ struct Character
36
+ {
37
+ unsigned int textureID; // グリフのテクスチャID
38
+ FT_Face face; //フェイス
39
+ };
40
+
41
+ std::vector<Character> character; //文字データ
42
+ std::shared_ptr<Window> windowContext; //ウインドウコンテキスト
43
+ int charSize; //文字の大きさ(ピクセル)
44
+
45
+ //FreeType
46
+ //FreeTypeを初期化
47
+ FT_Face face = nullptr; //フェイス
48
+ FT_Face fc = nullptr; //フェイス
49
+ FT_Library ft; //freetype
50
+
51
+ };
52
+ }
53
+ #endif
54
+ ```
55
+
6
56
  ```cpp
7
57
  #include "Text.hpp"
8
58