Hide Rubik's Cube Internal Wirings
In the context of a Java/OpenGL application I am drawing a black wired only (without filling) big cube composed of 27 smaller cubes. To do that I wrote the following code:
for (int x = 1; x <= 3; x++) {
for (int y = 1; y <= 3; y++) {
for (int z = 1; z <= 3; z++) {
wireCube(x - 2, 2 - y, 2 - z);
}
}
}
The wireCube
method is implemented using GL11.glBegin(GL11.GL_LINE_LOOP);
Using the right call to gluPerspective
to define the projection and the correct call to gluLookAt
to position the "camera" I am able to display my big cube as needed and ....I am very happy with that !!!
My new problem is now, how to mod开发者_运维百科ify this code in order to "hide" all the wirings that are inside the big cube ? To help visualize the scene, these wirings are the ones that are usually drawn has dashed lines when learning 3D geometry at school.
Thanks in advance for help
Manu
Enable depth testing (glEnable(GL_DEPTH_TEST)
) and put quads on the surfaces of the cubes.
To draw a quad, use glBegin(GL_QUADS)
followed by the four vertices and the glEnd()
call.
Draw all your cubes with black polygons (or disable color output : glColorMask(false,false,false,false); ): this will fill the depth buffer.
Then draw your lines. The ones hidden by the polygons will not appear. There will be z-fighting though, so glDepthTest(GL_LEQUAL);
If you want to draw "unvisible" lines dashed, this won't be enough. You can draw again with glDepthTest(GL_GREATER);
Another solution is to draw polygons that face the camera with a solid line, and other with a dashed line. This is a simple dot product (camDir.faceNorm).
Use glPolygonOffset() to drown or to emerge your wireframe above (or below) the polygons with the same coordinates.
精彩评论