我在运行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之后而不是之前...
请问你的顶点状态的设置看?你正在创建一个VAO(顶点数组对象)?这是现在您使用核心配置文件所必需的。如果还没有尝试过,我会将调用添加到'glGetError()'中,并查看是否报告了错误以及在哪里报告错误。 – 2014-08-31 20:29:54
我创建了VAO,现在我得到了坚实的色块!在我把glGetError放在任何地方后,我发现glewInit是原因,但只有当我设置为核心配置文件和3.2兼容性。不管如何,我在glTexImage2D之后得到了另一个1280错误。它与使用字母或字节而不是浮点有关吗? – omikun 2014-09-01 02:10:09
'GL_ALPHA'纹理格式在Core Profile中消失了。我就此回答了一个问题,并总结了其余部分。 – 2014-09-01 03:14:47