J'utilise freetype, et la seule chose qu'il me reste à faire pour rendre le texte est de convertir un ft_bitmap en quelque chose qui peut être rendu avec opengl quelqu'un peut-il expliquer comment faire cela? J'utilise glfw. Avec la façon dont je l'ai essayé de le faire, il donne juste un écran vide est le code ici que j'utilise:Rendu texte-freetype écran vide
#include <exception>
#include <iostream>
#include <string>
#include <glew.h>
#include <GL/glfw.h>
#include <iterator>
#include "../include/TextRenderer.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include <stdexcept>
#include <freetype/ftglyph.h>
using std::runtime_error;
using std::cout;
TextRenderer::TextRenderer(int x, int y, FT_Face Face, std::string s)
{
FT_Set_Char_Size(
Face, /* handle to face object */
0, /* char_width in 1/64th of points */
16*64, /* char_height in 1/64th of points */
0, /* horizontal device resolution */
0); /* vertical device resolution */
slot= Face->glyph;
text = s;
setsx(x);
setsy(y);
penX = x;
penY = y;
face = Face;
//shaders
GLuint v = glCreateShader(GL_VERTEX_SHADER) ;
const char* vs = "void main(){ gl_Position = ftransform();}";
glShaderSource(v,1,&vs,NULL);
glCompileShader(v);
GLuint f = glCreateShader(GL_FRAGMENT_SHADER) ;
const char* fs = "uniform sampler2D texture1; void main() { gl_FragColor = texture2D(texture1, gl_TexCoord[0].st); //And that is all we need}";
glShaderSource(f,1,&fs,NULL);
glCompileShader(f);
Program= glCreateProgram();
glAttachShader(Program,v);
glAttachShader(Program,f);
glLinkProgram(Program);
}
void TextRenderer::render()
{
glUseProgram(Program);
FT_UInt glyph_index;
for (int n = 0; n < text.size(); n++)
{
/* retrieve glyph index from character code */
glyph_index = FT_Get_Char_Index(face, text[n]);
/* load glyph image into the slot (erase previous one) */
error = FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER);
draw(&face->glyph->bitmap,penX + slot->bitmap_left,penY - slot->bitmap_top);
penX += *(&face->glyph->bitmap.width)+3;
penY += slot->advance.y >> 6; /* not useful for now */
}
}
void TextRenderer::draw(FT_Bitmap * bitmap,float x,float y)
{
GLuint texture [0] ;
glGenTextures(1,texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RED , bitmap->width, bitmap->rows, 0, GL_RED , GL_UNSIGNED_BYTE, bitmap);
// int loc = glGetUniformLocation(Program, "texture1");
// glUniform1i(loc, 0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glEnable(GL_TEXTURE_2D);
int height=bitmap->rows/10;
int width=bitmap->width/10;
glBegin(GL_QUADS);
glTexCoord2f (0.0, 0.0);
glVertex2f(x,y);
glTexCoord2f (1.0, 0.0);
glVertex2f(x+width,y);
glTexCoord2f (1.0, 1.0);
glVertex2f(x+width,y+height);
glTexCoord2f (0.0, 1.0);
glVertex2f(x,y+height);
glEnd();
glDisable(GL_TEXTURE_2D);
}
Qu'est-ce que j'utilise pour initialiser le texte renderer:
FT_Library library;
FT_Face arial;
FT_Error error = FT_Init_FreeType(&library);
if (error)
{
throw std::runtime_error("Freetype failed");
}
error = FT_New_Face(library,
"C:/Windows/Fonts/Arial.ttf",
0,
&arial);
if (error == FT_Err_Unknown_File_Format)
{
throw std::runtime_error("font format not available");
}
else if (error)
{
throw std::runtime_error("Freetype font failed");
}
TextRenderer t(5,10,arial,"Hello");
t.render();
Pour commencer, vous passez un pointeur brut, non initialisée à une fonction ('glGenTextures()') qui attend un tableau * non nul * pointeur de base. Allouez une quantité appropriée de mémoire pour votre tableau de texture ou passez un tableau fixe automatique (dans votre cas, probablement le dernier, puisque vous en obtenez un seul). – WhozCraig
mon mauvais mal compris que Fonction- modifié de manière appropriée, maintenant j'obtenir juste un écran vide – user2673108