cloud22's picture

Selection - viewport problems

public int SelectAt(int x, int y)
    {
        int[] viewport = new int[4];
        GL.GetInteger(GetPName.Viewport, viewport);
        int hits;
        int[] selectBuffer = new int[64];
        GL.SelectBuffer(64, selectBuffer);
        GL.RenderMode(RenderingMode.Select);
        GL.MatrixMode(MatrixMode.Projection);
 
        Console.WriteLine("select-  " + viewport[0] + "," + viewport[1] + "," + viewport[2] + "," + viewport[3]);
        GL.InitNames();
        GL.PushName(0);
 
        GL.PushMatrix();
        GL.LoadIdentity();
        /*  create 5x5 pixel picking region near cursor location	*/
        GluPickMatrix(x, y, 5, 5, viewport);
 
        RenderObjects();
 
        GL.MatrixMode(MatrixMode.Projection);
        GL.PopMatrix();
        GL.Flush();
 
        hits = GL.RenderMode(RenderingMode.Render);
 
        var closest = uint.MaxValue;
        int selectedId = -1;
        for (int i = 0; i < hits; i++)
        {
            uint distance = (uint)selectBuffer[i * 4 + 1];
 
            if (closest >= distance)
            {
                closest = distance;
                selectedId = (int)selectBuffer[i * 4 + 3];
            }
        }
        Console.WriteLine("Hits:"+hits+" Selected id :" + selectedId);
        return selectedId;
    }
 
public void RenderObjects()
    {
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadIdentity();
        GL.Rotate(_mouseMoveX, 0, 1, 0);
        GL.Rotate(_mouseMoveY, 1, 0, 0);
 
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadIdentity();
        DrawBackGround();
        DrawCreatures();
    }

I've tried using other peoples code and it works fine but whenever I try to apply it to mine project, it breaks.
In this code piece, "GL.GetInteger(GetPName.Viewport, viewport);" always returns zeros. When I print it out in the render function, it works as expected. Any ideas?
Also, I know the selectAt function isn't finished and is missing a few key things. The viewport is creating a problem though.
Thanks!