SharpGL: no se pueden dibujar todas las líneas de la lista

Yo tengo:

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++) a for(int i=edges.Count-1; i>=0; i--) shows that esges are generated correctly, but they are not drawn.

Imágenes:
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.

preguntado el 08 de enero de 11 a las 19:01

1 Respuestas

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);  

Respondido el 09 de enero de 11 a las 00:01

Nice shot, but I'd be surprised... Why would the error appear also without the rectangle at the background? And with the rectangle, on depth test set to LEQUAL, why would consistent linear interpolation of only 0's yield anything other than 0 in the z-coordinate? - Kos

Sometimes numerical errors arise due to numerical instability of algorithms. Since OpenGL T&L pipeline contains many transformations before anything hits the color buffer than there's a lot to go wrong along. And I'm sure OpenGL isn't using continued fractions or any numerically super-stable contraptions since it's performance oriented. You know, something along the lines: 0.2 + 0.6 = 0.8000007. - Rekin

Could You also include initiazitaion code? The one containing projection matrix. - Rekin

This can't be value issue. It draws (or not) the lines depending of direction of iteration. So this has to be some memory issue. If this was value issue it would be drawn the same way regardles of order of drawing. I noticed that this library has some memory bug so I dropped it. I use OpenTDK now and everything works perfectly :) - Miko Kronn

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.