0
\$\begingroup\$

Main.cpp

//Variables const unsigned int width = 896, height = 504; //Initiating GLFW Window glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Creating a GLFW window window = glfwCreateWindow(width, height, "Jaguar", NULL, NULL); //Checking if Window was initiated if (window == NULL) { std::cout << "GLFW FAILED TO INITIATE WINDOW!\n"; glfwTerminate(); } glfwMakeContextCurrent(window); //Centering Window int windowWidth, windowHeight; glfwGetWindowSize(window, &windowWidth, &windowHeight); const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); glfwSetWindowPos(window, mode->width / 2 - windowWidth / 2, mode->height / 2 - windowHeight / 2); //Setting-Up window's icon GLFWimage icon[1]; icon[0].pixels = stbi_load("resources/images/gui/icon/icon.png", &icon[0].width, &icon[0].height, 0, 4); glfwSetWindowIcon(window, 1, icon); stbi_image_free(icon[0].pixels); //Checking if Glad was initiated if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "GLAD FAILED TO BE INITIATED\n"; } glEnable(GL_CULL_FACE); glEnable(GL_BLEND); glEnable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); stbi_set_flip_vertically_on_load(true); //Setting-Up Viewport glViewport(0, 0, width, height); //Intitiating MainMenu states.push(new MainMenuState(*window, &states)); 

font.cpp

FT_Library ft; if (FT_Init_FreeType(&ft)) { std::cout << "FREETYPE::Failed to initialize library\n"; } FT_Face face; if (FT_New_Face(ft, filePath, 0, &face)) { std::cout << "FREETYPE::Failed to load to font: " << filePath << "\n"; } // set size to load glyphs as FT_Set_Pixel_Sizes(face, 0, px); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); for (unsigned char c = 0; c < 128; c++) { if (FT_Load_Char(face, c, FT_LOAD_RENDER)) { std::cout << "FREETYPE::Failed to load glpyh\n"; } unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width, face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); Character character = { texture, glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top) ,face->glyph->advance.x }; Characters.insert(std::pair<char, Character>(c, character)); } FT_Done_Face(face); FT_Done_FreeType(ft); 

text.cpp

// activate corresponding render state shader.use(); glUniform3f(glGetUniformLocation(shader.id, "textColor"), color.x, color.y, color.z); glActiveTexture(GL_TEXTURE0); glBindVertexArray(VAO); // iterate through all characters std::string::const_iterator c; for (c = string.begin(); c != string.end(); c++) { Character ch = font.Characters[*c]; float xpos = position.x + ch.bearing.x * scale; float ypos = position.y - (ch.size.y - ch.bearing.y) * scale; float w = ch.size.x * scale; float h = ch.size.y * scale; // update VBO for each character float vertices[6][4] = { { xpos, ypos + h, 0.0f, 0.0f }, { xpos, ypos, 0.0f, 1.0f }, { xpos + w, ypos, 1.0f, 1.0f }, { xpos, ypos + h, 0.0f, 0.0f }, { xpos + w, ypos, 1.0f, 1.0f }, { xpos + w, ypos + h, 1.0f, 0.0f } }; // render glyph texture over quad glBindTexture(GL_TEXTURE_2D, ch.textureId); // update content of VBO memory glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); glBindBuffer(GL_ARRAY_BUFFER, 0); // render quad glDrawArrays(GL_TRIANGLES, 0, 6); // now advance cursors for next glyph (note that advance is number of 1/64 pixels) position.x += (ch.advance >> 6) * scale; // bitshift by 6 to get value in pixels (2^6 = 64) } glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); 

enter image description here

\$\endgroup\$
9
  • \$\begingroup\$ There is a box because you're rendering the text over the scene. \$\endgroup\$ Commented Dec 18, 2020 at 17:56
  • \$\begingroup\$ @a_donda Is there a way to just render the text and have the box transparent? \$\endgroup\$ Commented Dec 18, 2020 at 18:04
  • 1
    \$\begingroup\$ Are you correct use alpha blending? Are you draw text last? And are you have alpha in texture? \$\endgroup\$ Commented Dec 18, 2020 at 18:09
  • \$\begingroup\$ Yes. But the freetype pages and numerous examples explain that exhaustively. Also, you don't want to have blending enabled all the time. Here's how text rendering can go: extract glyphs, assemble font texture/s, assemble static buffer/s for fixed text. Enter loop: assemble dynamic buffer/s for per frane text, render scene, enable alpha blending, render text, diable alpha blending, next frame. \$\endgroup\$ Commented Dec 18, 2020 at 18:19
  • \$\begingroup\$ @a_donda I understand the article im just not sure if im over seeing something like if i forgot to enable something at the current moment performance is not a concern right now \$\endgroup\$ Commented Dec 18, 2020 at 18:46

1 Answer 1

0
\$\begingroup\$

To remove box around text(ASSUMING YOU HAVE GL_DEPTH_TEST ENABLED): Disable GL_DEPTH_TEST before drawing your text Enable GL_DEPTH_TEST after rendering text

\$\endgroup\$
7
  • \$\begingroup\$ That's a guess. It may suppress the symptom in your case, but if done right depth remains untouched. After the scene has been rendered the text is rendered with alpha blending enabled. \$\endgroup\$ Commented Dec 18, 2020 at 20:27
  • \$\begingroup\$ @a_donda I have enable alpha blending im still getting a box around my text \$\endgroup\$ Commented Dec 18, 2020 at 22:41
  • \$\begingroup\$ Its not a depth test problem. Depth test can complete hide glyph, but cant create rect. \$\endgroup\$ Commented Dec 18, 2020 at 22:45
  • \$\begingroup\$ @НикитаСамоуков As you can see from the image i get this box around my text when i don't disable and reenable depth test..... I want to use the text as a UI like life left ect..... That doesn't interact with the world cords but just the window cords \$\endgroup\$ Commented Dec 18, 2020 at 23:12
  • \$\begingroup\$ Its not a depth test problem. Warch your draw order, you should draw text last. \$\endgroup\$ Commented Dec 20, 2020 at 0:46

You must log in to answer this question.