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?