[英]gl_FragDepth breaks color
It seems that I can't use gl_FragDepth on my computer. 好像我不能在我的电脑上使用gl_FragDepth。 My program works well otherwise, no glsl error, glGetError returns 0, but I can't write in the depth buffer from my fragment shader.
我的程序运行良好,否则没有glsl错误,glGetError返回0,但我无法从片段着色器中写入深度缓冲区。
Besides, writing in gl_FragDepth changes the red component of the pixel color. 此外,写入gl_FragDepth会更改像素颜色的红色分量。
There is simplified version of my program. 有我的程序的简化版本。 I pruned all the useless stuff (I gess?), and it does not work much better:
我修剪了所有无用的东西(我gess?),并且它的效果不是很好:
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);
}
It's supposed to draw red and green stripes, but instead I get blurred red lines. 它应该绘制红色和绿色条纹,但我得到模糊的红线。 And if I remove the second drawcall, it's the same.
如果我删除第二个drawcall,它是相同的。 On Windows, that is.
在Windows上,即。 I tested it on OSX and works as expected.
我在OSX上测试它并按预期工作。
Here is some specs from glGetString : 以下是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
Is it possible that your integrated graphics card driver is choking on this line? 您的集成显卡驱动程序是否可能在这条线路上窒息?
glfwWindowHint(GLFW_DEPTH_BITS, 64);
64 bits is an awful lot for a depth buffer. 对于深度缓冲区,64位是非常多的。 24 bits is a more typical value.
24位是更典型的值。 Context creation should fail if a 64-bit depth buffer isn't supported, but I've seen strange behavior from some OpenGL drivers if the depth buffer isn't set up properly.
如果不支持64位深度缓冲区,则上下文创建应该会失败,但是如果没有正确设置深度缓冲区,我会看到一些OpenGL驱动程序的奇怪行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.