[.net] c# and opengl + glReadPixels, gluUnProject compile errors

Started by
3 comments, last by Nibbles 16 years, 1 month ago
i'm trying to convert a function for getting 3d world coords in opengl from the mouse position using opengl and c#. the function below in c/c++ works fine (aside from the array variables at the top which i changed to c#. however the rest of the function, primarily glReadPixels and gluUnProject give compiler errors.

CVector mouse2dto3d()
{
    unsafe
    {
        int[] viewport = new int[4];
        double[] modelview = new double[16];
        double[] projection = new double[16];
        float  wz;
        double x;
        double y;
        double z;

        Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);
        Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, modelview);
        Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, projection);
        Gl.glReadPixels(mouse_x, viewport[3] - mouse_x, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, &wz);
        Glu.gluUnProject((double)mouse_y, (double)viewport[3] - (double)mouse_y, (double)wz, modelview, projection, viewport, (double)x, (double)y, (double)z);

        CVector temp = new CVector((float)x, (float)y, (float)z);

        return temp;
    }
}
Errors: Error 1 The best overloaded method match for 'Tao.OpenGl.Gl.glReadPixels(int, int, int, int, int, int, object)' has some invalid arguments Error 2 Argument '7': cannot convert from 'float*' to 'object' Error 3 The best overloaded method match for 'Tao.OpenGl.Glu.gluUnProject(double, double, double, double[], double[], int[], out double, out double, out double)' has some invalid arguments Error 4 Argument '7' must be passed with the 'out' keyword Error 5 Argument '8' must be passed with the 'out' keyword Error 6 Argument '9' must be passed with the 'out' keyword Thanks! Scott
Advertisement
for glReadPixels, I'd imagine you'd need to make wz a float array, even if it only has one element, since pointers can't be converted to objects.

For gluUnProject, the issue is just what it says, you need to pass the x,y,z parameters as out:

Glu.gluUnProject((double)mouse_y, (double)viewport[3] - (double)mouse_y, (double)wz, modelview, projection, viewport, out x, out y, out z);
glReadPixels has two overloads: IntPtr and object. Try this:

Gl.glReadPixels(mouse_x, viewport[3] - mouse_x, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, (IntPtr)&wz);

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

I'm trying:

Gl.glReadPixels((double)last_mouseX, (double)viewport[3] - (double)last_mouseY, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, (IntPtr)&wz);


but it results in the error:

Error 1 'System.IntPtr' is a 'type' but is used like a 'variable'

thanks for the reply,
Scott
aw i'm dumb. got it.

i forgot to make wz a float array... just like you told me to do.

thanks for the help.

This topic is closed to new replies.

Advertisement