Question

I am currently writing an OpenGL application using the SharpGL library and I am trying to simply create a 3x3x3 set of cubes arranged in a symmetric grid. I am currently seeing some strange behaviour exhibited in the following picture:

Missing Cubes

This has me completely stumped as I can see no reason why the code is missing out the last 3 blocks. The method in charge of creating the cube looks like this:

private void CreateCube2(OpenGL gl, int cubeSize)
{
    gl.PushMatrix();
    const float spacing = 2.5f;

    for (int z = 0; z < cubeSize; z++)
    {
        for (int y = 0; y < cubeSize; y++)
        {
            for (int x = 0; x < cubeSize; x++)
            {
                var cube = new Cube();
                ColourCube(cube, cubeSize, x, y, z);
                cube.Render(gl, RenderMode.Render);

                gl.Translate(spacing, 0, 0);
            }
            gl.Translate(-spacing * cubeSize, spacing, 0);
        }
        gl.Translate(0, -spacing * cubeSize, spacing);
    }

    gl.PopMatrix();
}

where the definition of ColourCube is as follows:

private bool m_blackCubeMiddle = true;
private void ColourCube(Cube cube, int size, int x, int y, int z)
{
    cube.Faces[0].Material = (!m_blackCubeMiddle || y == 0) ? WhiteMaterial : BlackMaterial;     // Bottom
    cube.Faces[1].Material = (!m_blackCubeMiddle || y == size - 1) ? YellowMaterial : BlackMaterial;    // Top
    cube.Faces[2].Material = (!m_blackCubeMiddle || x == size - 1) ? GreenMaterial : BlackMaterial;     // Right
    cube.Faces[3].Material = (!m_blackCubeMiddle || x == 0) ? BlueMaterial : BlackMaterial;      // Left
    cube.Faces[4].Material = (!m_blackCubeMiddle || z == 0) ? OrangeMaterial : BlackMaterial;    // Front
    cube.Faces[5].Material = (!m_blackCubeMiddle || z == size - 1) ? RedMaterial : BlackMaterial;       // Back
}

The entire project can be downloaded from here.

Was it helpful?

Solution

The strange behaviour is caused by a bug in SharpGL 'Polygon.Render' method. It is not caused by your own code. When you call Triangulate on the cube, it already shows 27 cubes on screen in the correct positions. But even then, SharpGL incorrectly renders one of the cubes.

Having looked at the source code for ShapGL's Cube & Polygon classes I'ld suggest to write your own cube class. The implementation of the Cube class, that you are using now, is absolutely horible from several standpoints (GL_POLYGON (deprecated), immediate mode vertex submission (deprecated))

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top