PDA

View Full Version : gl_FragDepth messes with colors



Procra
09-13-2015, 10:41 AM
Hi,

I made a small program that is supposed to write in the Depth Buffer with a fragment shader, using gl_FragDepth.
Unfortunately, not only it fails (every pixel have the same depth), but it seems to write in the red component of gl_FragColor.
I put gl_GetError everywhere and there is no problem with my code.
This is a shorter version of it, I got rid of any useless stuff (maybe):



// Draws red and green stripes.
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 =
// "#version 120"
"attribute vec3 Coord;"
"void main() {\
gl_Position = vec4(Coord, 1.0);\
}";

char const *frag1 =
// "#version 120"
"void main () {\
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\
gl_FragDepth = sin(gl_FragCoord.x * 0.1);\
}";
// */

// /*
char const *frag2 =
// "#version 120
"void main () {\
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\
gl_FragDepth = cos(gl_FragCoord.x * 0.1);\
}";
// */

// For Windows.
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);

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);
}


What I want:
2087

What I get:
2088

I tested it on OSX and it works fine, but on my Windows I can't make it work. The first drawcall draws a red and black image, and the second one does nothing.
Here are some specs from 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

Thanks.