SharpGL - draw sphere with mesh and attach color to each one

810 Views Asked by At

I'm creating a 3d visualization in SharpGL (extension for Visual Studio 2013 where we can use OpenGL library). I want to visualize tetrahedron. It needs to be created from many points - user at the start of application defines the sum of vectors. I have a dictionary with coordinates for each vertex, which I calculate in those 2 classes:

public class Matrix4
{
    private List<Tuple<int, int, int, int>> set = new List<Tuple<int, int, int, int>>();

    public Matrix4()
    {

    }

    public Matrix4(int vectorSum)
    {
        set = GiveMatrix4(vectorSum);
    }  

    public List<Tuple<int, int, int, int>> GiveMatrix4(int vectorSum)
    {
        List<Tuple<int, int, int, int>> set1 = new List<Tuple<int, int, int, int>>();

        for (int i = 0; i <= vectorSum; i++)
        {
            for (int j = 0; j <= vectorSum; j++)
            {
                for (int k = 0; k <= vectorSum; k++)
                {
                    for (int l = 0; l <= vectorSum; l++)
                    {
                        Tuple<int, int, int, int> vector = new Tuple<int, int, int, int>(i, j, k, l);

                        if (AddValuesFromVector4(vector) == vectorSum)
                        {
                            set1.Add(vector);
                            continue;
                        }
                    }
                }
            }
        }

        return set1;
    }

    public int AddValuesFromVector4(Tuple<int,int,int,int> vector)
    {
        int sum = 0;

        sum = vector.Item1 + vector.Item2 + vector.Item3 + vector.Item4;

        return sum;
    }


    public List<Tuple<int, int, int, int>> Matrix4Set
    {
        get { return set; }
    }
}

Class Matrix3 cuts one dimension and I have vectors with 3 coordinates:

public class Matrix3
    {
        private Matrix4 matrix4;
        private Dictionary<Tuple<int, int, int, int>, Tuple<int, int, int>> pairs = new Dictionary<Tuple<int, int, int, int>, Tuple<int, int, int>>();
        private int vectorSum;

        public Matrix3()
        {

        }

        public Matrix3(int vectorSum)
        {
            this.vectorSum = vectorSum;
            matrix4 = new Matrix4(vectorSum);
            pairs = CreateMatrix3(matrix4.Matrix4Set);
        }

        public int VectorSum
        {
            get { return vectorSum; }
        }

        public Dictionary<Tuple<int, int, int, int>, Tuple<int, int, int>> CreateMatrix3(List<Tuple<int, int, int, int>> set)
        {
            Tuple<int, int, int> vector;
            Dictionary<Tuple<int, int, int, int>, Tuple<int, int, int>> pair = new Dictionary<Tuple<int, int, int, int>, Tuple<int, int, int>>();

            foreach (var item in set)
            {
                vector = new Tuple<int, int, int>(item.Item1 - item.Item2 - item.Item3 + item.Item4, item.Item1 + item.Item2 - item.Item3 - item.Item4, item.Item1 - item.Item2 + item.Item3 - item.Item4);
                pair.Add(item, vector);
            }

            return pair;
        }

        public int Sum(Tuple<int, int, int> vector)
        {
            int suma = 0;

            suma = (int)(vector.Item1 + vector.Item2 + vector.Item3);

            return suma;
        }

        public Dictionary<Tuple<int, int, int, int>, Tuple<int, int, int>> Pairs
        {
            get { return pairs; }
        }

        public Matrix4 Matrix4
        {
            get { return matrix4; }
        }
    }

After that I want to create points/spheres from my dictionary, which I keep in Matrix3 class:

gl.Begin(OpenGL.GL_POINTS);

        gl.PointSize(10.0f);
        foreach (var item in matrix3.Pairs.Values)
        {               
            gl.Vertex((float)item.Item1, (float)item.Item2, (float)item.Item3);
        }

Unfortunately, it doesn't work, I don't see anything on the screen. Any suggestions would be very appreciated. After creating every point/sphere i need to rotate them and attach to each one different color based on measures which I will calculate in other classes (but that's the next step, first of all I want to display those points/spheres and rotate them).

I'm completely new to OpenGl, that's why I'm looking for any samples.

Link to whole project and screen what I want to obtain: https://drive.google.com/file/d/1IDwl46rdRa9IYxq7w8ZkF4LCeAsUH0mU/view?usp=sharing

2

There are 2 best solutions below

3
On

With any openGL program you need to specify the projection method using glOrtho() or glFrustrum() for example. Then you have to make sure that your coordinates for display are within the bounds of the defined 3D "boxes".

glOrtho() has this signature glOrtho(left, right, bottom, top, near, far) where left,right are the maximum extents of the x coordinates, bottom,top are the maximum extents of the y coordinates and near,far are the maximum extent of the z coordinates. +y is up and we look from +z to -z.

If you do not do this, then openGL will assume you meant glOrtho(-1,1,-1,1,-1,1) meaning all coordinates must fit within a 1x1x1 cube centred at the origin.

The most likely cause of no display is that your coordinates are outside this range.

I suggest the best way to start is first make a trivial example. For example use glOrtho(-1,1,-1,1,-1,1) and draw some 3D lines with manually entered coordinates. Once that is working you can experiment with the other projections and the rest should be easy.

If you do Google search there are several good tutorials on openGL coordinate systems and projections that are a must to understand before you begin. Let me know if you need further help.

2
On

My advice is to create a way (better if it would be somehow generalized) to dump some kind of signature data about your objects, scene, meshes. Dump the bounding boxes of 3d objects, the parameters of projection, viewports, etc.

No image on screen means that some of those things don't match, including order of axis in coordinate system might not match order of vertices in each triangle\quad\polygon of produced mesh, or depth test function is wrong. If face culling is on, then faces that supposed be facing away from projection place will be invisible. If depth test is on but projection directed wrong way, nothing would be visible. Obviously, if object is projected outside of current viewport bounds, it wouldn't be visible.