Subject
I am creating a terrain map with triangle strips and I would like to make the bodies of the triangles black, but have their outlines be colored.
Problem
The solution appears to be to draw the triangles twice, once in solid black, using GL_FILL
and once in color using GL_LINE
.
The problem with this approach is that, since I am using depth testing and the vertices are at the same position each time, my color lines end up blending with the black lines
Attempted Solution
According to the OpenGL docs, and other sources, it looks like I should be able to utilize the glPolygonOffset() function to give polygons that are drawn with GL_LINE
"precedence" in the depth buffer by offsetting their comparison value by the argument to the function. (so I would think 1.0 would be sufficient)
This, however, does not appear to be working for me.
My Code
I have placed glEnable(GL_POLYGON_OFFSET_LINE);
in my init function.
Here is the code in my draw loop where I do the actual rendering. col
is the array that hods the real colors, col2
is filled with black.
Please excuse the mess.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonOffset(1.0,0.0);
glDeleteBuffers(1,&vertex_buffer);
vertex_buffer = createBuffer(GL_ARRAY_BUFFER, vert, sizeof(GLfloat)*(buffer_size), GL_STATIC_DRAW);
attributeBind(vertex_buffer, 0, 3);
glDeleteBuffers(1,&color_buffer);
color_buffer = createBuffer(GL_ARRAY_BUFFER, col2, sizeof(GLfloat)*(buffer_size), GL_STATIC_DRAW);
attributeBind(color_buffer, 1, 3);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glDrawArrays(GL_TRIANGLE_STRIP,0,s);
glDeleteBuffers(1,&color_buffer);
color_buffer = createBuffer(GL_ARRAY_BUFFER, col, sizeof(GLfloat)*(buffer_size), GL_STATIC_DRAW);
attributeBind(color_buffer, 1, 3);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glDrawArrays(GL_TRIANGLE_STRIP,0,s);
I'm not sure if I need to call glPolygonOffset()
each frame or not.
These are the attributeBind()
and createbuffer()
functions:
void attributeBind(GLuint buffer, int index, int points)
{
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glVertexAttribPointer(
index, // position or color
points, // how many dimensions?
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
}
GLuint createBuffer(GLenum target, const void *buffer_data, GLsizei buffer_size, GLenum usageHint)
{
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(target, buffer);
glBufferData(target, buffer_size, buffer_data, usageHint);
return buffer;
}
glPolygonOffset(1.0f, 1.0f)
\$\endgroup\$