2014-08-31 91 views
2

我在运行Xcode 5的Mac上运行10.9的mac上的wikibooks上的freetype教程。我使用着色器版本120运行,但我想使用一些现代功能,因此我将SDL提示设置为OpenGL 3.2并将我的着色器转换为150.问题出现在版本150中,使用texture2D将阻止着色器编译。转换GLSL现代OpenGL 3.2

这里是着色器的版本120:

const GLchar* vTextSource = 
"#version 120\n" 
"attribute vec4 coord;" 
"varying vec2 texcoord;" 
"void main(void) {" 
" gl_Position = vec4(coord.xy, 0, 1);" 
" texcoord = coord.zw;" 
"}"; 

const GLchar* fTextSource = 
"#version 120\n" 
"varying vec2 texcoord;" 
"uniform sampler2D tex;" 
"uniform vec4 color;" 
"void main(void) {" 
" gl_FragColor = vec4(1,1,0, texture2D(tex, texcoord).a) * color;" 
"}"; 

而这就是我对150的版本顶点着色器构建但片段着色器失败,除非我删除的Texture2D的任何用途。所有的CPU代码在它们之间是相同的。

const GLchar* vTextSource = 
"#version 150 \n" 
"in vec4 coord;" 
"out vec2 texcoord;" 
"void main(void) {" 
" gl_Position = vec4(coord.xy, 0, 1);" 
" texcoord = coord.zw;" 
"}"; 

const GLchar* fTextSource = 
"#version 150\n" 
"uniform sampler2D tex;" 
"in vec2 texcoord;" 
"uniform vec4 color;" 
"out vec4 outColor;" 
"void main(void) {" 
" outColor = vec4(1,1,1, texture2D(tex, texcoord).a) * color;" 
"}"; 

有什么我失踪了吗?在核心配置文件中设置纹理采样器与在兼容模式下不同?

编辑:我已经从texture2D(...)更改为片段着色器中的纹理(...)。它现在编译但没有显示任何内容。我不知道如何去调试这个。我已经包括纹理初始化程序:

void SdlApplication::render_text(const char *text, float x, float y, float sx, float sy) { 
    const char *p; 
    FT_GlyphSlot g = face->glyph; 

    /* Create a texture that will be used to hold one "glyph" */ 
    GLuint tex; 

    glActiveTexture(GL_TEXTURE0); 
    glGenTextures(1, &tex); 
    glBindTexture(GL_TEXTURE_2D, tex); 
    glUniform1i(ogl.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(ogl.attribute_coord); 
    glBindBuffer(GL_ARRAY_BUFFER, vboText); 
    glVertexAttribPointer(ogl.attribute_coord, 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; 

     point box[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(ogl.attribute_coord); 
    glDeleteTextures(1, &tex); 
} 

编辑2:新增VAO到我的顶点设置。我现在得到文本应该是纯色的正方形。所以看起来纹理坐标再次混乱了。

我加了检查每次通话后,发现我得到了1280码glewInit之后而不是之前...

+1

请问你的顶点状态的设置看?你正在创建一个VAO(顶点数组对象)?这是现在您使用核心配置文件所必需的。如果还没有尝试过,我会将调用添加到'glGetError()'中,并查看是否报告了错误以及在哪里报告错误。 – 2014-08-31 20:29:54

+0

我创建了VAO,现在我得到了坚实的色块!在我把glGetError放在任何地方后,我发现glewInit是原因,但只有当我设置为核心配置文件和3.2兼容性。不管如何,我在glTexImage2D之后得到了另一个1280错误。它与使用字母或字节而不是浮点有关吗? – omikun 2014-09-01 02:10:09

+0

'GL_ALPHA'纹理格式在Core Profile中消失了。我就此回答了一个问题,并总结了其余部分。 – 2014-09-01 03:14:47

回答

5

GLSL代码更新到最新标准看起来很好,除了texture2D()的问题。正如已经指出的那样,纹理采样函数现在过载了,需要使用texture()而不是texture2D()

其余问题主要是更新代码以使用核心配置文件,该配置文件弃用了许多旧功能。查看发布的代码,这包括:

  • 使用VAO(顶点数组对象)是设置顶点状态的必需条件。使用glGenVertexArrays()glBindVertexArray()等功能来设置VAO,并确保在使用顶点状态设置功能(如glVertexAttribPointer()glEnableVertexAttribArray())时绑定它。

  • GL_ALPHA纹理格式不再受支持。对于使用纹理格式与单个8位组件,使用GL_R8用于内部格式,并且GL_RED的格式:

    glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, g->bitmap.width, g->bitmap.rows, 0, 
          GL_RED, GL_UNSIGNED_BYTE, g->bitmap.buffer); 
    

    这也需要在着色器代码中的微小的变化,由于用于采样值1组分质地现在在红色成分:

    outColor = vec4(1,1,1, texture2D(tex, texcoord).r) * color; 
    
3

而不是使用您的片段着色器texture2D的,你应该用texture

void main(void) { 
    outColor = vec4(1, 1, 1, texture(tex, texcoord).a) * color; 
} 
+0

谢谢!这固定了着色器编译错误。不幸的是,着色器不能输出任何我能看到的东西。 – omikun 2014-08-31 20:07:25