看起来我的电脑上无法使用gl_FragDepth。除此之外,我的程序工作正常,没有glsl错误,glGetError返回0,但我无法从片段着色器中写入深度缓冲区。
此外,在gl_FragDepth中写入会改变像素颜色的红色分量。
这是我程序的简化版本。我删除了所有无用的东西(我猜?),但它并没有变得更好:
int main(void)
{
// These are custom structures for handling shader programs.
t_glprog prog;
t_glprog prog2;
GLuint vbo;
GLFWwindow *window;
static const GLfloat vertab[] =
{
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0
};
char const *vert =
"attribute vec3 Coord;"
"void main() {\
gl_Position = vec4(Coord, 1.0);\
}";
char const *frag1 =
"void main () {\
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\
gl_FragDepth = sin(gl_FragCoord.x * 0.1);\
}";
char const *frag2 =
"void main () {\
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\
gl_FragDepth = cos(gl_FragCoord.x * 0.1);\
}";
if (!glfwInit())
{
fprintf(stderr, "GLFW failed to init.\n");
return (-1);
}
glfwWindowHint(GLFW_DEPTH_BITS, 64);
window = glfwCreateWindow(640, 480, "TEST", NULL, NULL);
if (window == NULL)
{
fprintf( stderr, "Failed to open GLFW window.\n" );
glfwTerminate();
return (-1);
}
glfwMakeContextCurrent(window);
// For Windows.
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return (-1);
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearDepth(1.0);
glViewport(0, 0, 640, 480);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertab), vertab, GL_STATIC_DRAW);
create_shaders_prog(&prog, vert, frag1);
create_shaders_prog(&prog2, vert, frag2);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glUseProgram(prog.prog);
glDrawArrays(GL_QUADS, 0, 4);
glUseProgram(prog2.prog);
glDrawArrays(GL_QUADS, 0, 4);
glFlush();
glfwSwapBuffers(window);
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0)
{
glfwPollEvents();
}
return (0);
}
这段代码本应该画出红色和绿色条纹,但实际上只能看到模糊的红线。如果我删掉第二条命令,效果也是一样的。 在Windows系统下会出现这种情况。我在OSX上测试正常。
这里是从glGetString中获取到的一些规格:
GL_VENDOR : Intel
GL_RENDERER : Mobile Intel(R) 4 Series Express Chipset Family
GL_VERSION : 2.1.0 - Build 8.15.10.1892
GL_SHADING_LANGUAGE_VERSION : 1.20 - Intel Build 8.15.10.1892