简体   繁体   中英

OpenGL texture not drawing?

I'm trying to draw text using FreeType2 and OpenGL in C, but it's just rendering a square. I've been following tutorials here , here , and here . This code is running on Red Hat 5.6, which only has OpenGL 1.4.

Here's the code I have. First, I load the font with 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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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