2015-04-27 52 views
2

我正在使用OpenGL 3.3上下文和GLFW3进行窗口化。我也使用C. 我正在使用STB_truetype来将字符栅格化为动态分配的无符号字符。从数据到OpenGL 2D纹理

我从STB_truetype数据这样:

unsigned char ttf_buffer[1<<25]; 
stbtt_fontinfo font; 

fread(ttf_buffer, 1, 1<<25, fopen("Script Thing.ttf", "rb")); 

stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0)); 

int w,h, xoff, yoff; 
int c = 'C', s = 60; 

unsigned char *bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, &xoff,&yoff); 

为了确保我有正确的数据,我把它打印到控制台:

for(int i = 0; i < h; i++) 
{ 
    for(int x = 0; x < w; x++) 
    { 
     printf("%c", bitmap[x+i*w]>>5 ? '1' : ' '); 
    } 
    printf("\n"); 
} 

然后,我创建了2D的OpenGL纹理:

glGenTextures(1,&Texture); 
glBindTexture(GL_TEXTURE_2D, Texture); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w,h, 
      0, GL_RED, GL_UNSIGNED_BYTE, NewTemp); 

我与 'A' 字测试它,我也得到:

    11111  
       1111111  
       111111111  
       1111111111  
       11111111111 
      111111 11111 
      111111 11111 
      111111 1111 
      111111 1111 
      11111  1111 
      111111  1111 
    111111111111 11111 
    1111111111111111111111 
    111111111111111111111  
    111111111111111111111  
    111111111 1111111111 
     11111  111111 
     1111  11111111 
     11111  11111111 
     1111   11111111 
    1111   111 111 
    1111   111 1111 
    1111   1111 111 
    111   1111 111 
    111   111 111 
    1111   111 1111 
1111    11111111 
1111    1111111 
1     111  

我渲染纹理OpenGL上下文:

enter image description here

这是正确的,你可以看到。

然而,当我尝试C字符:

   11  
      1111111111 
     1111111111111 
     111111111111111 
     11111111111111111 
    1111111  111111 
    111111  11111 
    111111   1111 
    11111   1111 
    11111   11 
    1111     
11111     
1111     
1111     
1111     
1111     
1111     
1111     
1111     
111      
111      
1111     
1111     
111     
1111     
    1111    11 
    111111  11111 
    11111111111111111 
    111111111111  
      11 

enter image description here

同为其他字符,如 'X':

 1111   1111 
     11111  11111 
     11111  111111 
     111111  111111 
     11111 1111111 
     11111 1111111 
     11111 1111111 
      1111 1111111  
      11111111111  
      1111111111  
      11111111   
      1111111   
      111111   
     111111   
     1111111   
     11111111   
     111111111   
    11111 1111   
    11111 1111   
    11111 1111   
    1111  1111   
    1111  1111   
1111   111   
111   1111   
111   111   
111   1111  
11    11111  
1111    11111  
11     1  

enter image description here

任何想法,我可能会做错吗? 谢谢。

编辑: 修正它与添加glPixelStorei(GL_UNPACK_ALIGNMENT,1);在glTexImage2D之前的 。 再次感谢doron

+0

这里有很多代码,但你没有清楚地说明问题。 –

+0

由于您的'a'文本呈现看起来是正确的(受字体限制),这是否更少关于字体和更多关于位图的问题? –

+0

当我复制图像并将相同的东西粘贴到其右侧时,它看起来不像蛇,更像是一个扭曲的'a' –

回答

1

正常情况下,每行的宽度和每个像素的字节数将是多个。但是一些2D光栅化软件需要额外的死空间(为了对齐目的)。从行到行的跳转称为步幅,可以大于宽度。从你显示的图像看,你的位图的步幅大于宽度。

因此,您需要做的是找出跨步是什么,并将位图复制到跨度等于宽度的位图中,或者使用glPixelStoreiGL_UNPACK_ROW_LENGTH

+0

您先生是我的救星,我只需要放入glPixelStorei(GL_UNPACK_ALIGNMENT,1);它固定!再次感谢! – Begah