Freetype库不带有教程代码
FreeType library is not display with tutorial code
- 使用Mac Erina Display 2304x1440
我成功地包括了freetype库,并试图根据教程绘制,但没有显示任何内容。
没有错误,所以我无法识别问题。
// For LoadShaders
#include "shader.hpp"
#include <vector>
#include <cstring>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include <ft2build.h>
#include FT_FREETYPE_H
FT_Library library;
FT_Face face;
GLuint TextShaderID;
GLuint uniform_tex;
GLuint uniform_color;
GLuint vbo00;
// I have already acquired this font.
const char * fontfilename = "OpenSans-Regular.ttf";
void iii()
{
/* Initialize the FreeType2 library */
if (FT_Init_FreeType(&library)) {
fprintf(stderr, "Could not init freetype libraryn");
}
/* Load a font */
if (FT_New_Face(library, fontfilename, 0, &face)) {
fprintf(stderr, "Could not open font %sn", fontfilename);
}
// Initialize Shader (Here is not problem. Maybe.)
TextShaderID = LoadShaders( "material/shader/FreeType.vertexshader", "material/shader/FreeType.fragmentshader" );
// Initialize uniforms' IDs
uniform_tex = glGetUniformLocation( TextShaderID, "myTextureSampler" );
uniform_color = glGetUniformLocation( TextShaderID, "texcol" );
glGenBuffers(1, &vbo00);
}
void render_text(const char *text, float x, float y, float sx, float sy) {
const char *p;
FT_GlyphSlot g = face->glyph;
glActiveTexture(GL_TEXTURE0);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(uniform_tex, 0);
/* We require 1 byte alignment when uploading texture data */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/* Clamping to edges is important to prevent artifacts when scaling */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
/* Linear filtering usually looks best for text */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* Set up the VBO for our vertex data */
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo00);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
/* Loop through all characters */
for (p = text; *p; p++) {
/* Try to load and render the character */
if (FT_Load_Char(face, *p, FT_LOAD_RENDER))
continue;
/* Upload the "bitmap", which contains an 8-bit grayscale image, as an alpha texture */
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
/* Calculate the vertex and texture coordinates */
float x2 = x + g->bitmap_left * sx;
float y2 = -y - g->bitmap_top * sy;
float w = g->bitmap.width * sx;
float h = g->bitmap.rows * sy;
GLfloat box[4][4] = {
{x2, -y2, 0, 0},
{x2 + w, -y2, 1, 0},
{x2, -y2 - h, 0, 1},
{x2 + w, -y2 - h, 1, 1},
};
/* Draw the character on the screen */
glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
/* Advance the cursor to the start of the next character */
x += (g->advance.x >> 6) * sx;
y += (g->advance.y >> 6) * sy;
}
glDisableVertexAttribArray(0);
glDeleteTextures(1, &tex);
}
void rrr()
{
glUseProgram(TextShaderID);
/* White background */
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
float sx = 2/2304/4;
float sy = 2/1440/4;
FT_Set_Pixel_Sizes(face, 0, 48);
/* Effects of alignment */
GLfloat red[4] = { 1, 0, 0, 1 };
glUniform4fv(uniform_color, 1, red);
render_text("The Quick Brown Fox Jumps Over The Lazy Dog", 0, 0, sx, sy);
glutSwapBuffers();
}
float sx = 2/2304/4; float sy = 2/1440/4;
是组成部分。如果一个积分数除以更大的积分数,则结果为0。
您要进行浮点部门:
float sx = 2.0f/2304.0f/4.0f;
float sy = 2.0f/1440.0f/4.0f;
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
生成纹理,其中数据被编码到alpha通道。在着色器代码中必须考虑这一点,当查找纹理时(由.a
(:
,例如
#version 460
// [...]
uniform sampler2D myTextureSampler;
uniform vec4 texcol;
void main()
{
vec4 col = texcol * texture(myTextureSampler, uv).a;
// [...]
}
无论如何,GL_ALPHA
被弃用和传统opengl。这意味着如果您使用核心配置文件上下文,这将无法工作。
而是使用GL_RED
:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED,
g->bitmap.width, g->bitmap.rows, 0,
GL_RED, GL_UNSIGNED_BYTE, g->bitmap.buffer);
读取片段着色器中的红色通道(.r
(的数据:
#version 460 core
// [...]
uniform sampler2D myTextureSampler;
uniform vec4 texcol;
void main()
{
vec4 col = texcol * texture(myTextureSampler, uv).r;
// [...]
}
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- Freetype库不带有教程代码
- 当我使用的教程完全没有错误时,为什么我的代码错误
- 简单的教程SFML代码在Ubuntu 16.04上崩溃
- 用于并行化代码的 TBB 教程/文档
- 使用生成器和发行版扩展c++11随机的教程或示例代码
- boost::log 教程中的代码如何工作
- Ogre3D尝试编译教程框架并不断收到错误.代码::块
- C++需要有关 OpenCV 教程中代码的帮助
- 我的代码是否等同于Boost.Asio教程中给出的代码
- LLVM JIT教程代码崩溃与简单的参数化函数.为什么
- OpenCV教程:加载和显示图像(代码块,fedora20)