OpenTKstart's picture

Relation from 2D to 3D (Screen to Model)


i use a GameWindow to draw Quads. Now i want to select a single point of the drawn graphic by the mouse. But i have no idea how to get a relation from 2d-screen to the 3d-model. Maybe this picture will explain it more...



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
OpenTKstart's picture
mOfl wrote:

How many quads do you draw, roughly?

Im sry, the problem occure when i draw triangles. When i draw quads i dont see anything. I dont know how many triangles they draw, i just used the code from the example. However the "SelectedTriangle have always the same value:

mOfl wrote:

What do you mean with "everything works fine"? The picking is working?

Yes, it works. But the color of the triangles (on my laptop) is different as on my PC. It seems that the color of my PC is the correct one. Maybe Graph-Driver or OpenGL-Version is different.

OpenTKstart's picture

Ok, my big Problem was that i filled the VBO_Array in his own Thread. I dont know whats the problem of this. Maybe the Thread terminates and the Binding of the Buffer will be reverted? However, my screen look now like this:

It´s poor and i dont see the lines. And i still didn´t understood the OnRenderFrame completely.

  protected override void OnRenderFrame(FrameEventArgs e)
          //  base.OnRenderFrame(e);
            GL.ClearColor(1f, 1f, 1f, 1f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LoadMatrix(ref modelview);
            GL.Scale(scaleX, scaleY, scaleZ); // scale to zoom
            GL.Translate(transformation); // translation to move
            GL.Rotate(angleY, Vector3d.UnitY);  //rotation on Y-achis
            GL.Rotate(angleX, Vector3d.UnitX);  //rotation on X-achis
            GL.Rotate(angleZ, Vector3d.UnitZ);  //rotation on Z-achis
            //  angleX += 0.5f;  // TEST: auto-rotate
            if (ready)
                #region Pass 1: Draw Object and pick Triangle
                // You may re-enable the shader, but it works perfectly without and will run on intel HW too
                // GL.UseProgram(ProgramObject);
                GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length);
                // GL.UseProgram(0);
                // Read Pixel under mouse cursor
                Byte4 Pixel = new Byte4();
                GL.ReadPixels(Mouse.X, this.Height - Mouse.Y, 1, 1, PixelFormat.Rgba, PixelType.UnsignedByte, ref Pixel);
                SelectedTriangle = Pixel.ToUInt32();
                #endregion Pass 1: Draw Object and pick Triangle
                #region Pass 2: Draw Shape
                if (SelectedTriangle == uint.MaxValue)
                    GL.ClearColor(.2f, .1f, .3f, 1f); // purple
                    GL.ClearColor(0f, .2f, .3f, 1f); // cyan
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.Color3(1f, 1f, 1f);
                GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length);
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
                GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length);
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                if (SelectedTriangle != uint.MaxValue)
                    GL.DrawArrays(VBO_PrimMode, (int)SelectedTriangle * 4, 4);
                #endregion Pass 2: Draw Shape
         //   drawFramework();  // draws the Graphic-Framework such as X,Y,Z-Achis´s
         //   drawAllObjects(); // draw all object in the graph-list
            //Show FPS=======
            this.Title = "OpenTK-GameWindow - FPS: " + RenderFrequency.ToString();
            ErrorCode err = GL.GetError();
            if (err != ErrorCode.NoError)
	                Trace.WriteLine("Error at Swapbuffers: " + err);

In this Code the Lines should be drawn, shoudn´t they? I dont want to use Cullface and disabled it.