简体   繁体   English

OpenGL 纹理不绘制?

[英]OpenGL texture not drawing?

I'm trying to draw text using FreeType2 and OpenGL in C, but it's just rendering a square.我试图在 C 中使用 FreeType2 和 OpenGL 绘制文本,但它只是渲染一个正方形。 I've been following tutorials here , here , and here .我一直在关注教程hereherehere This code is running on Red Hat 5.6, which only has OpenGL 1.4.这段代码在 Red Hat 5.6 上运行,它只有 OpenGL 1.4。

Here's the code I have.这是我的代码。 First, I load the font with FreeType.首先,我使用 FreeType 加载字体。 I've printed out the buffer for the character I want to the terminal, and it appears to be loading correctly.我已经打印出我想要到终端的字符的缓冲区,它似乎正在正确加载。 I'm omitting some error checks for clarity.为清楚起见,我省略了一些错误检查。 Also, I've had to manually transcribe this code, so please ignore any syntax errors if they look like an obvious mistype.此外,我不得不手动转录此代码,因此,如果它们看起来像是明显的错误类型,请忽略任何语法错误。

FT_Library ft;
FT_Init_Freetype(&ft);
FT_Face face;
FT_New_Face(ft, fontpath, 0, &face);
Ft_Set_Pixel_Sizes(face, 0, 16);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
FT_Load_Char(face, c, FT_LOAD_RENDER);    

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 
             0,
             GL_RGBA,
             face->glyph->bitmap.width,
             face->glyph->bitmap.rows,
             0,
             GL_INTENSITY,
             GL_UNSIGNED_BYTE,
             face->glyph->bitmap.buffer);
FT_Done_Face(face);
FT_Done_Freetype(ft);

As previously stated, if I loop over the rows and width of the buffer and print the values they look good.如前所述,如果我遍历缓冲区的行和宽度并打印它们看起来不错的值。 I think the call to glTexImage2D is what I want, considering each pixel is a binary intensity value.我认为对 glTexImage2D 的调用是我想要的,考虑到每个像素都是一个二进制强度值。

The code to draw the symbol is below, but it doesn't seem to work.绘制符号的代码如下,但似乎不起作用。 Instead it just draws a rectangle:相反,它只是绘制一个矩形:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, texture);
glColor3f(1,1,1);

GLfloat x = 0;
GLfloat y = 0;
GLfloat w = 5;
GLfloat h = 5;

glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(x,y);
glTexCoord2f(1,0);
glVertex2f(x+w,y);
glTexCoord2f(1,1);
glVertex2f(x+w,y+h);
glTexCoord2f(0,1);
glVertex2f(x,y+h);
glEnd();

glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);

At this point I'm at a loss.在这一点上,我不知所措。 I've read the OpenGL documentation and I don't see anything obviously wrong.我已经阅读了 OpenGL 文档,我没有看到任何明显的错误。 Can someone tell me what I'm missing?有人可以告诉我我错过了什么吗?

I found the solution after a lot of trial and error.经过大量试验和错误,我找到了解决方案。 OpenGL 1.4 requires textures to be a power of two. OpenGL 1.4 要求纹理是 2 的幂。 I allocated a temporary array and used that.我分配了一个临时数组并使用了它。 Also the GL_INTENSITY setting didn't seem to work so I manually made a RGBA array.另外 GL_INTENSITY 设置似乎不起作用,所以我手动制作了一个 RGBA 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM