OpenTK. Drawing points by mouse click.

Hello,

I want draw points by mouse click. For testing I draw a point in coordinate (0f, 0.5f) It works fine:

VertexShader.glsl


#version 330

in vec4 a_Position;

void main()
{
    gl_Position = a_Position;
    gl_PointSize = 10.0;
}

FragmentShader.glsl


#version 330

out vec4 fragColor;

void main()
{
    fragColor = vec4(0.0, 1.0, 1.0, 1.0);
}

MainWindow.cs


using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using Utils;
using OpenTK.Input;

namespace DrawPointsByClick
{
    class MainWindow : GameWindow
    {
        private int program;
        private bool canDraw = false;
        private int a_Position;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Load shaders from files
            string vShaderSource = null;
            string fShaderSource = null;
            ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource);
            ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource);
            if (vShaderSource == null || fShaderSource == null)
            {
                Logger.Append("Failed to load shaders from files");
                return;
            }

            // Initialize the shaders
            if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program))
            {
                Logger.Append("Failed to initialize the shaders");
                return;
            }

            // Get the storage location of a_Position
            a_Position = GL.GetAttribLocation(program, "a_Position");
            if (a_Position < 0)
            {
                Logger.Append("Failed to get the storage location of a_Position");
                return;
            }

            // Pass coordinates of point to a_Position
            GL.VertexAttrib2(a_Position, 0f, 0.5f);

            // Enable Point Size
            GL.Enable(EnableCap.ProgramPointSize);

            // Specify the color for clearing the canvas
            GL.ClearColor(Color.OliveDrab);

            canDraw = true;
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Viewport(0, 0, Width, Height);
            base.OnRenderFrame(e);

            // Clear the canvas with current color
            GL.Clear(ClearBufferMask.ColorBufferBit);

            if (canDraw)
            {
                // Draw the points
                GL.DrawArrays(PrimitiveType.Points, 0, 1);
            }

            GL.Flush();
            SwapBuffers();
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);

            Console.WriteLine("OnMouseDown");

            // Pass coordinates of point to a_Position
            GL.VertexAttrib2(a_Position, 0f, 0f);
        }
    }
}

But I want to set the coordinates in (0, 0) by mouse click here:


        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);

            Console.WriteLine("The mouse down event was occurred");

            // Pass coordinates of point to a_Position
            GL.VertexAttrib2(a_Position, 0f, 0f);
        }

I see the message my message “The mouse down event was occurred” but the point doesn’t draw in (0,0). Why?

You can run and test my problem in VS2012 - VS2015: DrawPointsByClick_Problem.zip

I try to port this example from WebGL: ch02/ClickedPoints in OpenTK. This example draws the points in position of mouse click.

I draw one point in my example. In beginning the point is drawn in (0, 0.5). Drawing loop is 30 frames per second. When I click the point it must to redraw in (0, 0) position.

The solution:

  1. Open to: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE
  2. Right click on devenv.exe.
  3. Hover to the “Run with graphics processor…”
  4. Select “High-end NVidia processor”.

This solution I found here: Bing

This code draws the point in mouse click position:


        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);

            if (e.Button == MouseButton.Left)
            {
                float x = (e.X - Width / 2f) / (Width / 2f);
                float y = -(e.Y - Height / 2f) / (Height / 2f);
                // Pass coordinates of point to a_Position
                GL.VertexAttrib2(a_Position, x, y);

                Console.WriteLine(x + " " + y);
            }
        }

Maybe my port of program “ClickedPoints” will be useful for someone.

These programs are equivalent:

[ul]
[li]ClickedPoints (WebGL)
[/li][li]DrawPointsByClick (OpenTK)
[/li][/ul]

Screenshot:
[ATTACH=CONFIG]1444[/ATTACH]