PDA

View Full Version : OpenTK. Drawing points by mouse click.



8Observer8
03-04-2017, 11:15 AM
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?

8Observer8
03-04-2017, 08:34 PM
You can run and test my problem in VS2012 - VS2015: DrawPointsByClick_Problem.zip (https://dl.dropboxusercontent.com/s/pa9j0i4e56kfrim/DrawPointsByClick_Problem.zip)

8Observer8
03-04-2017, 09:46 PM
I try to port this example from WebGL: ch02/ClickedPoints (https://jsfiddle.net/8Observer8/xf4fnc0o/) 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.

8Observer8
03-06-2017, 10:45 PM
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: https://connect.microsoft.com/VisualStudio/feedback/details/1216990/visual-studio-2013-lacks-nvidia-optimus-profile-but-it-can-run-nvidia-graphics-processor-directly

8Observer8
03-07-2017, 12:01 AM
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);
}
}

8Observer8
03-07-2017, 04:11 AM
Maybe my port of program "ClickedPoints" will be useful for someone.

These programs are equivalent:

ClickedPoints (WebGL) (https://jsfiddle.net/8Observer8/xf4fnc0o/)
DrawPointsByClick (OpenTK) (https://dl.dropboxusercontent.com/s/vzbq5wjqdus7dif/DrawPointsByClick.zip)


Screenshot:
2342