2

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

blackRose
  • 180
  • 1
  • 9

2 Answers2

2

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.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • Do you have any samples or articles which could be useful? I've updated my post with link to project. I understood what you mean generally, but because of that I'm new to OpenGl I don't know where to start- what kind of functions should I use. – blackRose Sep 27 '18 at 07:55
  • 1
    @blackRose The main hub for anything OpenGl related is https://opengl.org There was also gamedev and if you you search github for opengl and sharpgl, you'll find a few examples. Note, if you want to deal with 3d graphics , regardless which API you use, you have to gain an understanding of linear algebra. The exact use of functions depends on which profile mode and which platform you use. Most old examples use fixed functions pipeline whe you have glRotate, glTranslate, glScale and such. In modern api those are ditched for sake of programmer's ability to have low level control. – Swift - Friday Pie Sep 27 '18 at 12:04
0

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.

bcperth
  • 2,191
  • 1
  • 10
  • 16
  • on GL ES systems glOrtho wouldn't work. Technically, all fixed functions are considered deprecated – Swift - Friday Pie Sep 27 '18 at 06:09
  • OK you did not mention was an embedded system (or maybe I did not notice!). However I agree with Swift - Friday Pie and what I recommend is go back a step and make a really simple hand coded set of coordinates (cube or triangle) work, then will be easy to factor in your more elaborate system for generating and storing coordinates. – bcperth Sep 27 '18 at 06:22
  • @ bcperth Oh, sorry, I added that for clarification. OP uses SharpGL, which is modern port for C#. Provided its nature it's something similar to ES, or at least oriented onto modern systems and mobile\embedded systems. I didn't wrote anything in C# that used OpenGL, just my two bits of thought. – Swift - Friday Pie Sep 27 '18 at 07:21