1

Using SharpGL, is it possible to transform the screen cursor position back to a model view location (ie. create a ray-cast function)?

The example I have been working with is similar to the following:

private void OpenGLControl_OpenGLDraw(object sender, SharpGL.OpenGLEventArgs args)
{

    OpenGL gl = args.OpenGL;
    gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
    gl.LoadIdentity(); 

    gl.Translate(0.0f, 0.0f, -6.0f);
    gl.Rotate(rotatePyramid, 0.0f, 1.0f, 0.0f);

    gl.Begin(OpenGL.GL_TRIANGLES);

        gl.Color(1.0f, 0.0f, 0.0f);        
        gl.Vertex(0.0f, 1.0f, 0.0f);    
        gl.Color(0.0f, 1.0f, 0.0f);        
        gl.Vertex(-1.0f, -1.0f, 1.0f);    
        gl.Color(0.0f, 0.0f, 1.0f);        
        gl.Vertex(1.0f, -1.0f, 1.0f);    

        gl.Color(1.0f, 0.0f, 0.0f);        
        gl.Vertex(0.0f, 1.0f, 0.0f);    
        gl.Color(0.0f, 0.0f, 1.0f);        
        gl.Vertex(1.0f, -1.0f, 1.0f);    
        gl.Color(0.0f, 1.0f, 0.0f);        
        gl.Vertex(1.0f, -1.0f, -1.0f);    

        gl.Color(1.0f, 0.0f, 0.0f);        
        gl.Vertex(0.0f, 1.0f, 0.0f);    
        gl.Color(0.0f, 1.0f, 0.0f);        
        gl.Vertex(1.0f, -1.0f, -1.0f);    
        gl.Color(0.0f, 0.0f, 1.0f);        
        gl.Vertex(-1.0f, -1.0f, -1.0f);    

        gl.Color(1.0f, 0.0f, 0.0f);        
        gl.Vertex(0.0f, 1.0f, 0.0f);    
        gl.Color(0.0f, 0.0f, 1.0f);        
        gl.Vertex(-1.0f, -1.0f, -1.0f);    
        gl.Color(0.0f, 1.0f, 0.0f);        
        gl.Vertex(-1.0f, -1.0f, 1.0f);    

    gl.End();
    gl.Flush();
}

Using SharpGL, and the approach demonstrated in the above sample, is it possible to implement a ray-cast/mouse selection? Or would I need to delve deeper into the OpenGL workings (Manually manage matrices etc) to achieve this?

Edit: To clarify, I am trying to retrieve the 3D interpretation of the 2D cursor location. Maybe through perspective (w) scaling etc.

James
  • 97
  • 1
  • 9
  • This is called object selection / object picking and there are a number of articles written about it. https://en.wikibooks.org/wiki/OpenGL_Programming/Object_selection – Dietrich Epp Sep 26 '17 at 18:44
  • 1
    My question really was is there a way to achieve this using the SharpGL library – James Sep 26 '17 at 18:46
  • Yes, you do things the normal OpenGL but access the API calls through the SharpGL wrapper. That's just how SharpGL works… it's not really a different API, it's just a thin wrapper that lets you call OpenGL functions from C#. – Dietrich Epp Sep 26 '17 at 19:07

2 Answers2

1

You can just use gluUnproject() and pass it the 2D screen space mouse position you retrieved from mouse callback.This function will return the 3D world space position for you which you can use as you want.

0

I don't know if the previous answers were good for you, but BTW glVertex, glRotate, glBegin and so on are very old GL functions (v1 or v2). Is it your choice to use them, or don't you know about v3 or v4 way of doing 3D things with opengl (VAO, VBO, and so on)?

NynnFR
  • 11
  • 2