在我发现实际的文本数据在 alpha 通道中之前,我花了太多时间在黑屏上。
GLuint shader_program_text;
void drawText()
{
//Render the message to an SDL_Surface, as that's what TTF_RenderText_X returns
text_font = TTF_OpenFont("bpl_binary/waltographUI.ttf", 50);
SDL_Color color = {255, 0, 0, 0};
SDL_Surface* sdl_surface = TTF_RenderText_Blended(text_font, "hello world", color);
GLuint texture_id;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sdl_surface->w, sdl_surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, sdl_surface->pixels);
glBindTexture(GL_TEXTURE_2D, 0);
SDL_FreeSurface(sdl_surface);
TTF_CloseFont(text_font);
//glDrawPixels(sdl_surface->w, sdl_surface->h, GL_RGBA, GL_UNSIGNED_BYTE, sdl_surface->pixels);
//FILE* file = fopen("output.txt", "w+");
//for(int h=0; h<sdl_surface->h; h++)
//{
//
// for(int w=0; w<sdl_surface->w; w++)
// {
// unsigned int xxx = ((unsigned int*)sdl_surface->pixels)[h*sdl_surface->w + w];
// /*if(xxx != 0)
// fprintf(file, "x", xxx);
// else
// fprintf(file, " ", xxx);*/
// fprintf(file, "%08x ", xxx);
// }
// fprintf(file, "\n");
//}
//fclose(file);
//MULTIPLY WITH ALPHA TO ACTUALLY SEE SOMETHING
glUseProgram(shader_program_text);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture_id);
glBegin(GL_TRIANGLE_STRIP);
//texcoord; position;
glVertexAttrib2f(1, 0, 1); glVertexAttrib2f(0, -1, -1); //top left
glVertexAttrib2f(1, 1, 1); glVertexAttrib2f(0, +1, -1); //top right
glVertexAttrib2f(1, 0, 0); glVertexAttrib2f(0, -1, +1); //bottom left
glVertexAttrib2f(1, 1, 0); glVertexAttrib2f(0, +1, +1); //bottom right
glEnd();
glDisable(GL_TEXTURE_2D);
glUseProgram(0);
}