C++ 转换GLSL现代OpenGL 3.2
我正在关注一个关于运行10.9和Xcode 5的mac上wikibooks的freetype教程。我用着色器版本120运行它,但我想使用一些现代功能,所以我将SDL提示设置为OpenGL 3.2,并将着色器转换为150。问题是在版本150中,使用texture2D将阻止着色器编译 以下是着色器的版本120:C++ 转换GLSL现代OpenGL 3.2,c++,opengl,glsl,C++,Opengl,Glsl,我正在关注一个关于运行10.9和Xcode 5的mac上wikibooks的freetype教程。我用着色器版本120运行它,但我想使用一些现代功能,所以我将SDL提示设置为OpenGL 3.2,并将着色器转换为150。问题是在版本150中,使用texture2D将阻止着色器编译 以下是着色器的版本120: const GLchar* vTextSource = "#version 120\n" "attribute vec4 coord;" "varying vec2 texcoord;" "
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(…)更改为texture(…)。它现在编译了,但没有显示任何内容。我不知道如何调试这个。我已经包括纹理初始化例程:
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添加到“我的顶点”设置中。我现在得到了文本应该在的地方的纯色方块。所以看起来纹理坐标又乱了
我在每次调用后都添加了检查,发现我在glewInit之后得到了1280代码,但不是在…之前,而不是在片段着色器中使用
texture2D
,您应该使用texture
:
void main(void) {
outColor = vec4(1, 1, 1, texture(tex, texcoord).a) * color;
}
将GLSL代码更新到最新标准看起来不错,除了
texture2D()
的问题。如前所述,纹理采样函数现在已过载,需要使用texture()
而不是texture2D()
剩下的问题主要是更新代码以使用核心概要文件,这不推荐许多遗留特性。查看发布的代码,这包括:
- 设置顶点状态时必须使用VAOs(顶点数组对象)。使用
和glgenvertexarray()
等函数设置VAO,并确保在使用顶点状态设置函数(如glbinvertexarray()
和glVertexAttribPointer()
时将其绑定glEnableVertexAttribArray()
- 不再支持
纹理格式。对于使用单个8位组件的纹理格式,内部格式使用GL_ALPHA
,格式使用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);
outColor = vec4(1,1,1, texture2D(tex, texcoord).r) * color;
glGetError()
的调用,并查看是否报告了错误以及在哪里报告了错误。我创建了VAO,现在我得到了纯色块!在到处输入glGetError后,我发现glewInit是原因,但只有当我设置为core profile和3.2兼容性时。忽略这一点,我在glTexImage2D之后得到另一个1280错误。它与使用alpha或byte而不是浮点有关吗?GL_alpha
纹理格式在核心配置文件中消失了。我总结了一个答案,并总结了其余的内容。