Question

I have:

float[,] nodesN = null; //indexes:
                        //number of node;
                        //value index 0->x, 1->y, 2->temperature
int[,] elements = null; //indexes:
                        //indexof element (triangle)
                        //1, 2, 3 - vertexes (from nodesN)
List<Pair> edges = null; //Pair is a class containing two int values which are
                         //indexes of nodesN

And function which is supposed do all elements and edges on SharpGL.OpenGLCtrl

    private void openGLCtrl1_Load(object sender, EventArgs e)
    {
        gl = this.glCtrl.OpenGL;
        gl.ClearColor(this.BackColor.R / 255.0f,
            this.BackColor.G / 255.0f,
            this.BackColor.B / 255.0f, 1.0f);
        gl.Clear(OpenGL.COLOR_BUFFER_BIT | OpenGL.DEPTH_BUFFER_BIT);
    }

    private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e)
    {
        gl.Clear(OpenGL.COLOR_BUFFER_BIT | OpenGL.DEPTH_BUFFER_BIT);
        gl.LoadIdentity();
        gl.Translate(0.0f, 0.0f, -6.0f);

        if (!draw) return;

        bool drawElements = false;

        if (drawElements)
        {
            gl.Begin(OpenGL.TRIANGLES);

            for (int i = 0; i < elementNo; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    float x, y, t;
                    x = nodesN[elements[i, j], 0];
                    y = nodesN[elements[i, j], 1];
                    t = nodesN[elements[i, j], 2];

                    gl.Color(t, 0.0f, 1.0f - t);
                    gl.Vertex(x, y, 0.0f);
                }
            }

            gl.End();
        }

        gl.Color(0f, 0f, 0f);            

        gl.Begin(OpenGL.LINES);

        //for(int i=edges.Count-1; i>=0; i--)
        for(int i=0; i<edges.Count; i++)
        {               

            float x1, y1, x2, y2;
            x1 = nodesN[edges[i].First, 0];
            y1 = nodesN[edges[i].First, 1];

            x2 = nodesN[edges[i].Second, 0];
            y2 = nodesN[edges[i].Second, 1];

            gl.Vertex(x1, y1, 0.0f);
            gl.Vertex(x2, y2, 0.0f);                
        }   

        gl.End();          
    }

But it doesn't draw all the edges. If i change drawElements it draws different number of edges. Changing for(int i=0; i<edges.Count; i++) to for(int i=edges.Count-1; i>=0; i--) shows that esges are generated correctly, but they are not drawn.

Images:
for(int i=0; i<edges.Count; i++)
drawElements=false
http://img225.imageshack.us/img225/9295/noup.jpg

for(int i=edges.Count-1; i>=0; i--)
drawElements=false
http://img828.imageshack.us/img828/9595/nodown.jpg

for(int i=0; i<edges.Count; i++)
drawElements=true
http://img64.imageshack.us/img64/4929/withup.jpg

for(int i=edges.Count-1; i>=0; i--)
drawElements=true
http://img833.imageshack.us/img833/9167/withdown.jpg

What is wrong with this? How can I draw all edges?

EDIT:
Never mind, I dropped SharpGL and wrote exactly the same code in OpenTK. It works excelent without me wondering what was wrong. This was a good call because SharpGL uses enormously big amount of memory.

Was it helpful?

Solution

Once I had a very similar problem.

It was due to z-buffer. If you have a plane and want to draw it's wireframe than the coordinates overlap and artifacts like those arise. It's numerically undefined behavior - drawing two objects on the same depth. You never know which one comes on top.

One solution is to offset the wireframe a bit. I noticed that in some 3d modelling packages. In game engines it's also common to offset sprites on geometry (gunshot marks on a wall). Another might be disabling the z-buffer and manually occlude hidden lines.

The case when you disable drawing of elements might be to another issue with z-buffer. It's bounded by far and near clipping planes. Most probably you draw the lines exactly at the depth of one of them (my guess is far one).

EDIT. I read Your code a bit. One I'd like to see is how You construct the projection matrix. If you didn't touch it at all than (If I recall correctly) the near and far clipping planes are at -1.0 and 1.0 respectively. But, I might be wrong since You draw at z=-6.0...

The other thing, try replacing:

gl.Vertex(x1, y1, 0.0f);
gl.Vertex(x2, y2, 0.0f);  

With something along the lines of:

gl.Vertex(x1, y1, 0.01f);
gl.Vertex(x2, y2, 0.01f);  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top