转换GLSL现代OpenGL 3.2
Converting GLSL modern OpenGL 3.2
我正在使用Xcode 5运行10.9的mac上学习关于wikibooks的免费教程。我用着色器版本120运行它,但我想使用一些现代功能,所以我将SDL提示设置为OpenGL 3.2,并将着色器转换为150。问题是在版本150中,使用texture2D将阻止着色器编译。
以下是着色器的版本120:
const GLchar* vTextSource =
"#version 120n"
"attribute vec4 coord;"
"varying vec2 texcoord;"
"void main(void) {"
" gl_Position = vec4(coord.xy, 0, 1);"
" texcoord = coord.zw;"
"}";
const GLchar* fTextSource =
"#version 120n"
"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 150n"
"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代码,但在。。。
将GLSL代码更新为最新标准看起来不错,只是texture2D()
有问题。正如已经指出的,纹理采样函数现在过载,需要使用texture()
而不是texture2D()
。
剩下的问题主要是更新代码以使用Core Profile,这会弃用许多遗留功能。查看张贴的代码,其中包括:
-
必须使用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;
与其在片段着色器中使用texture2D
,不如使用texture
:
void main(void) {
outColor = vec4(1, 1, 1, texture(tex, texcoord).a) * color;
}
- 将旧管道转换为现代 openGL 时出现问题
- 在现代OpenGL中,绘制GL_TRIANGLE奇怪的z轴行为的填充圆
- 现代OpenGL和GLEW Libraray的编译错误
- 现代OpenGL纹理金字塔
- 现代OpenGL:如何获得立方体的矢量位置
- 现代OpenGL(4.6)-将着色器编译到库中
- 无法使用现代 OpenGL 渲染基本三角形
- 现代 OpenGL 多维数据集旋转未按预期工作
- 现代OpenGL,在对象上获得白色纹理
- 了解现代OpenGL中的代码绘图.OBJ并设置不同的位置
- 现代OpenGL 3.3核心黑色纹理
- 在同一应用程序中使用传统OpenGL和现代OpenGL
- 现代Opengl:同时正投影和透视投影
- 使用WGL创建现代OpenGL上下文
- 使用SFML,现代OpenGL转换从2D点到3D
- 带有SDL/加载库的现代OpenGL
- 如何在现代openGL中绘制带有三维点的多边形
- 如何在现代 OpenGl 中选择网格的顶点或三角形(上面的 3.3)
- 什么是现代OpenGL等同于glBegin/glEnd
- 现代 OpenGL 上下文故障