Reputation:
I have a tetrahedron object created in openGL ES 2.0. What I'm trying to achieve is to show the actual wireframe of the polygon over its base color. Is there a method for achieving this effect?
Also, my tetrahedron is pink. How can I change its color?
Upvotes: 3
Views: 7673
Reputation: 11
Although this has been here a little while (over a year now), this may also help: Note that you may not need to use another shader to achieve the desired result. You can disable the vertex attribute array then specify and load the constant vertex attribute data (the wireframe line color) to draw the wire frame. For example:
float coloredcube[] = { 2, 2, 2, 1, 0, 0, -2, 2, 2, 0, 1, 0, -2, -2, 2, 0,
0, 1, 2, -2, 2, 1, 1, 0, 2, 2, -2, 1, 0, 1, -2, 2, -2, 0, 1, 1, -2,
-2, -2, 1, 1, 1, 2, -2, -2, 0, 0, 0
};
short indices[] = { 0, 1, 2, 0, 2, 3, //back
0, 4, 7, //right
0, 7, 3,
7, 6, 2, //bottom
7, 2, 3,
4, 5, 6, //front
4, 6, 7,
5, 1, 2, //left
5, 2, 6,
0, 1, 5, 0, 5, 4 //top
};
short lineindices[] = { 0, 1, 1, 2, 0, 2, 2, 3,
0, 4, 4, 7, 0, 7, 7, 3,
7, 6, 6, 2, 7, 2, 4, 5,
5, 6, 4, 6, 5, 2, 1, 5, 0, 5, 0, 3 };
glVertexAttribPointer(iPosition, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),coloredcube);
glEnableVertexAttribArray(iPosition);
glVertexAttribPointer(iColor, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),coloredcube + 3);
glEnableVertexAttribArray(iColor);
// offset the filled object to avoid the stitching that can arise when the wireframe lines are drawn
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(2.0f, 2.0f);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, indices);
glDisable(GL_POLYGON_OFFSET_FILL);
//Then disable the vertex colors and draw the wire frame with one constant color
glDisableVertexAttribArray(iColor);
glLineWidth(1.0f);
GLfloat color[4] = { 1.0f, 0.0f, 0.0f, 0.5f };
glVertexAttrib4fv(iColor, color);
glDrawElements(GL_LINES, 36, GL_UNSIGNED_SHORT, lineindices);
A similar example can be found in "The OpenGL ES 2.0 programming guide" pp 109,110.
Upvotes: 1
Reputation: 45948
In addition to what Jave said. Instead of enlarging the whole object (whose optimal amount always depends on the object and the current view) to prevent z-fighting artefacts, you can also use the polygon offset (using glPolygonOffset
), whose major application are indeed wireframe overlays.
It basically works by slightly adjusting the depth values of the resulting fragments, which you cannot achieve differently in ES, since you cannot write to the depth buffer (which I guess is the reason they didn't drop it from ES, like they did in desktop GL 3+). So you basically render your solid object and your line-version of the object using the same vertices, but using a polygon offset configuration for the solid object that slightly increases the resulting fragments' depth values (pushes them away from the viewer), thus placing the triangles always behind the lines in view space (or window space actually). See here for further information.
Though the case of a tetrahedron might make some problems because of its very sharp edges.
Upvotes: 3
Reputation: 31846
1: First draw your object as usual, then draw it again (with a different shader, or it will be the same color as the object and thus invisible), using GLES20.GL_LINES, GL_LINE_LOOP or GL_LINE_STRIP.
You might want to scale the object up slightly when drawing the lines so that the depth-testing don't decides that the lines are behind the object and ignores them.
2: This is done in your shader, set gl_FragColor to a vec4 containing your wanted rgba-values for a solid color.
Upvotes: 4