以下のコードですがシェーダーを使って画面にテキストを表示させたいのですがどうすればいいのでしょうか?
RenderText()関数でレンダリングする予定なのですがシェーダーをどう渡したら良いかわかりません。
また3Dなのでview * world * posとう画面表示に必須な関数も使っているためどうしたらシェーダーで描画したらいいのか知りたです。
参考サイト: https://learnopengl.com/In-Practice/Text-Rendering
GLSL
1#version 400 2//頂点シェーダー 3 4layout(location = 0) in vec3 position; //頂点座標 5 6layout(location = 1) in vec4 text_pos; //テキストレンダリング 7 8 9uniform mat4 worldMatrix; //ワールド行列 10uniform mat4 viewMatrix; //ビュー行列 11 12out vec2 out_text; //テキスト 13 14void main() 15{ 16 //頂点 17 vec4 pos = vec4(position.xyz,1.0); 18 gl_Position = viewMatrix * worldMatrix * pos; 19 20 out_text = text_pos.zx; 21 22}
GLSL
1 2#version 400 3//フラグメントシェーダー 4 5in vec2 out_text; 6out vec4 color; 7 8uniform sampler2D uTexture; 9uniform vec3 textColor; 10 11void main() 12{ 13 vec4 sampled = vec4(1.0, 1.0, 1.0, texture(uTexture, textColor).r); 14 color = vec4(textColor, 1.0) * sampled; 15} 16 17
cpp
1 2#include "Font.hpp" 3#include "Game.hpp" 4 5Font::Font() 6{ 7 //vao 8 glGenVertexArrays(1, &vao); 9 glBindVertexArray(vao); 10 11 12 //文字描画 13 if (FT_Init_FreeType(&library) != 0) 14 { 15 printf("FT_Init Error\n"); 16 } 17 18 if (FT_New_Face(library, "C:/Windows/Fonts/meiryo.ttc", 0, &face) != 0) 19 { 20 printf("FT_New_Face(); Error \n"); 21 } 22 23 if (FT_Set_Pixel_Sizes(face, 0, 48) != 0) 24 { 25 printf("FT_Set_Pixel sizes \n"); 26 } 27 28 29 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 30 31 for (unsigned char c = 0; c < 128; c++) 32 { 33 if (FT_Load_Char(face, c, FT_LOAD_RENDER)) 34 { 35 printf("Glyph\n"); 36 continue; 37 } 38 39 unsigned int texture; 40 glGenTextures(1, &texture); 41 glBindTexture(GL_TEXTURE_2D, texture); 42 glTexImage2D( 43 GL_TEXTURE_2D, 44 0, 45 GL_RED, 46 face->glyph->bitmap.width, 47 face->glyph->bitmap.rows, 48 0, 49 GL_RED, 50 GL_UNSIGNED_BYTE, 51 face->glyph->bitmap.buffer 52 ); 53 54 55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 59 60 // now store character for later use 61 Character character = { 62 texture, 63 glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), 64 glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top), 65 face->glyph->advance.x 66 }; 67 Characters.insert(std::pair<char, Character>(c, character)); 68 69 } 70 71 FT_Done_Face(face); 72 FT_Done_FreeType(library); 73 74 75 glGenVertexArrays(1, &vao); 76 glGenBuffers(1, &vbo); 77 78 glBindVertexArray(vao); 79 glBindBuffer(GL_ARRAY_BUFFER, vbo); 80 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW); 81 glEnableVertexAttribArray(0); 82 83 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); 84 glBindBuffer(GL_ARRAY_BUFFER, 0); 85 glBindVertexArray(0); 86} 87 88 89void Font::Draw() 90{ 91 92 //透視射形行列 93 float a = f * atan((((float)WIDTH) / (float)(HEIGHT))); 94 glm::mat4 pers = glm::perspective(a, (float)WIDTH / (float)HEIGHT, 0.1f, 100.0f); 95 //glm::mat4 pers = glm::perspective(glm::radians(90.f), (((float)WIDTH) / (float)(HEIGHT)), 0.1f, 100.0f); 96 // printf("%f\n",f); 97 98 99 //カメラ 100 glm::mat4 view = glm::lookAt(glm::vec3(0, 0, -1), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0)) * pers; 101 102 memcpy(viewMatrix, &view, sizeof(float) * 16); 103 104 105 106 //Uniformリソースをプログラムに設定 シェーダプログラム 107// glUniformMatrix4fv(glGetUniformLocation(ShaderProgram, "worldMatrix"), 1, GL_FALSE, wordlMatrix); 108 109 glUniformMatrix4fv(glGetUniformLocation(ShaderProgram, "viewMatrix"), 1, GL_FALSE, viewMatrix); 110 111 112} 113 114 115 116 117void Font::RenderText(GLuint &Shader, std::string text, float x, float y, float scale, glm::vec3 color) 118{ 119 // activate corresponding render state 120 glUseProgram(Shader); 121 glUniform3f(glGetUniformLocation(Shader, "text_pos"), color.x, color.y, color.z); 122 glActiveTexture(GL_TEXTURE0); 123 glBindVertexArray(vao); 124 125 // iterate through all characters 126 std::string::const_iterator c; 127 for (c = text.begin(); c != text.end(); c++) 128 { 129 Character ch = Characters[*c]; 130 131 float xpos = x + ch.Bearing.x * scale; 132 float ypos = y - (ch.Size.y - ch.Bearing.y) * scale; 133 134 float w = ch.Size.x * scale; 135 float h = ch.Size.y * scale; 136 // update VBO for each character 137 float vertices[6][4] = { 138 { xpos, ypos + h, 0.0f, 0.0f }, 139 { xpos, ypos, 0.0f, 1.0f }, 140 { xpos + w, ypos, 1.0f, 1.0f }, 141 142 { xpos, ypos + h, 0.0f, 0.0f }, 143 { xpos + w, ypos, 1.0f, 1.0f }, 144 { xpos + w, ypos + h, 1.0f, 0.0f } 145 }; 146 // render glyph texture over quad 147 glBindTexture(GL_TEXTURE_2D, ch.TextureID); 148 149 // update content of VBO memory 150 glBindBuffer(GL_ARRAY_BUFFER, vbo); 151 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); 152 glBindBuffer(GL_ARRAY_BUFFER, 0); 153 154 // render quad 155 glDrawArrays(GL_TRIANGLES, 0, 6); 156 157 // now advance cursors for next glyph (note that advance is number of 1/64 pixels) 158 x += (ch.Advance >> 6) * scale; // bitshift by 6 to get value in pixels (2^6 = 64) 159 } 160 glBindVertexArray(0); 161 glBindTexture(GL_TEXTURE_2D, 0); 162} 163
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/10/06 03:12
2020/10/06 04:58