1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
// This code was written for the OpenTK library and has been released
// to the Public Domain.
// It is provided "as is" without express or implied warranty of any kind.
using System;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using Examples.Shapes;
namespace Examples.Tutorial
{
/// <summary>
/// This demo shows over which triangle the cursor is, it does so by assigning all 3 vertices of a triangle the same Ids.
/// Each Id is a uint, split into 4 bytes and used as triangle color. In an extra pass, the screen is cleared to uint.MaxValue,
/// and then the mesh is drawn using color. Using GL.ReadPixels() the value under the mouse cursor is read and can be converted.
/// </summary>
[Example("Picking", ExampleCategory.OpenGL, "1.x", Documentation = "Picking")]
class Picking : GameWindow
{
int mouse_x, mouse_y;
/// <summary>Creates a 800x600 window with the specified title.</summary>
public Picking()
: base(800, 600, GraphicsMode.Default, "Picking", GameWindowFlags.Default, DisplayDevice.Default, 1, 1, GraphicsContextFlags.Default)
{
VSync = VSyncMode.On;
MouseMove += (sender, e) =>
{
mouse_x = e.X;
mouse_y = e.Y;
};
}
struct Byte4
{
public byte R, G, B, A;
public Byte4(byte[] input)
{
R = input[0];
G = input[1];
B = input[2];
A = input[3];
}
public uint ToUInt32()
{
byte[] temp = new byte[] { this.R, this.G, this.B, this.A };
return BitConverter.ToUInt32(temp, 0);
}
public override string ToString()
{
return this.R + ", " + this.G + ", " + this.B + ", " + this.A;
}
}
struct Vertex
{
public Byte4 Color; // 4 bytes
public Vector3 Position; // 12 bytes
public const byte SizeInBytes = 16;
}
const TextureTarget Target = TextureTarget.TextureRectangleArb;
float angle;
PrimitiveType VBO_PrimMode;
Vertex[] VBO_Array;
uint VBO_Handle;
uint SelectedTriangle;
// int VertexShaderObject, FragmentShaderObject, ProgramObject;
/// <summary>Load resources here.</summary>
/// <param name="e">Not used.</param>
protected override void OnLoad(EventArgs e)
{
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.CullFace);
#region prepare data for VBO from procedural object
DrawableShape temp_obj = new SierpinskiTetrahedron(3f, SierpinskiTetrahedron.eSubdivisions.Five, false);
VertexT2fN3fV3f[] temp_VBO;
uint[] temp_IBO;
temp_obj.GetArraysforVBO(out VBO_PrimMode, out temp_VBO, out temp_IBO);
temp_obj.Dispose();
if (temp_IBO != null)
throw new Exception("Expected data for GL.DrawArrays, but Element Array is not null.");
// Convert from temp mesh to final object, copy position and add triangle Ids for the color attribute.
VBO_Array = new Vertex[temp_VBO.Length];
int TriangleCounter = -1;
for (int i = 0; i < temp_VBO.Length; i++)
{
// Position
VBO_Array[i].Position = temp_VBO[i].Position;
// Index
if (i % 3 == 0)
TriangleCounter++;
VBO_Array[i].Color = new Byte4(BitConverter.GetBytes(TriangleCounter));
}
#endregion prepare data for VBO from procedural object
#region Setup VBO for drawing
GL.GenBuffers(1, out VBO_Handle);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO_Handle);
GL.BufferData<Vertex>(BufferTarget.ArrayBuffer, (IntPtr)(VBO_Array.Length * Vertex.SizeInBytes), VBO_Array, BufferUsageHint.StaticDraw);
GL.InterleavedArrays(InterleavedArrayFormat.C4ubV3f, 0, IntPtr.Zero);
ErrorCode err = GL.GetError();
if (err != ErrorCode.NoError)
Trace.WriteLine("VBO Setup failed (Error: " + err + "). Attempting to continue.");
#endregion Setup VBO for drawing
#region Shader
/*
// Load&Compile Vertex Shader
using (StreamReader sr = new StreamReader("Data/Shaders/Picking_VS.glsl"))
{
VertexShaderObject = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(VertexShaderObject, sr.ReadToEnd());
GL.CompileShader(VertexShaderObject);
}
err = GL.GetError();
if (err != ErrorCode.NoError)
Trace.WriteLine("Vertex Shader: " + err);
string LogInfo;
GL.GetShaderInfoLog(VertexShaderObject, out LogInfo);
if (LogInfo.Length > 0 && !LogInfo.Contains("hardware"))
Trace.WriteLine("Vertex Shader failed!\nLog:\n" + LogInfo);
else
Trace.WriteLine("Vertex Shader compiled without complaint.");
// Load&Compile Fragment Shader
using (StreamReader sr = new StreamReader("Data/Shaders/Picking_FS.glsl"))
{
FragmentShaderObject = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(FragmentShaderObject, sr.ReadToEnd());
GL.CompileShader(FragmentShaderObject);
}
GL.GetShaderInfoLog(FragmentShaderObject, out LogInfo);
err = GL.GetError();
if (err != ErrorCode.NoError)
Trace.WriteLine("Fragment Shader: " + err);
if (LogInfo.Length > 0 && !LogInfo.Contains("hardware"))
Trace.WriteLine("Fragment Shader failed!\nLog:\n" + LogInfo);
else
Trace.WriteLine("Fragment Shader compiled without complaint.");
// Link the Shaders to a usable Program
ProgramObject = GL.CreateProgram();
GL.AttachShader(ProgramObject, VertexShaderObject);
GL.AttachShader(ProgramObject, FragmentShaderObject);
// link it all together
GL.LinkProgram(ProgramObject);
err = GL.GetError();
if (err != ErrorCode.NoError)
Trace.WriteLine("LinkProgram: " + err);
GL.UseProgram(ProgramObject);
err = GL.GetError();
if (err != ErrorCode.NoError)
Trace.WriteLine("UseProgram: " + err);
// flag ShaderObjects for delete when not used anymore
GL.DeleteShader(VertexShaderObject);
GL.DeleteShader(FragmentShaderObject);
int temp;
GL.GetProgram(ProgramObject, ProgramParameter.LinkStatus, out temp);
Trace.WriteLine("Linking Program (" + ProgramObject + ") " + ((temp == 1) ? "succeeded." : "FAILED!"));
if (temp != 1)
{
GL.GetProgramInfoLog(ProgramObject, out LogInfo);
Trace.WriteLine("Program Log:\n" + LogInfo);
}
Trace.WriteLine("End of Shader build. GL Error: " + GL.GetError());
GL.UseProgram(0);
*/
#endregion Shader
}
protected override void OnUnload(EventArgs e)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DeleteBuffers(1, ref VBO_Handle);
base.OnUnload(e);
}
/// <summary>
/// Called when your window is resized. Set your viewport here. It is also
/// a good place to set up your projection matrix (which probably changes
/// along when the aspect ratio of your window).
/// </summary>
/// <param name="e">Contains information on the new Width and Size of the GameWindow.</param>
protected override void OnResize(EventArgs e)
{
GL.Viewport(ClientRectangle);
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, this.Width / (float)this.Height, 0.1f, 10.0f);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
}
/// <summary>
/// Called when it is time to setup the next frame. Add you game logic here.
/// </summary>
/// <param name="e">Contains timing information for framerate independent logic.</param>
protected override void OnUpdateFrame(FrameEventArgs e)
{
var keyboard = OpenTK.Input.Keyboard.GetState();
if (keyboard[Key.Escape])
Exit();
}
/// <summary>
/// Called when it is time to render the next frame. Add your rendering code here.
/// </summary>
/// <param name="e">Contains timing information.</param>
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.Color3(Color.White);
GL.EnableClientState(ArrayCap.ColorArray);
#region Pass 1: Draw Object and pick Triangle
GL.ClearColor(1f, 1f, 1f, 1f); // clears to uint.MaxValue
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Matrix4 modelview = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
GL.Translate(0f, 0f, -3f);
GL.Rotate(angle, Vector3.UnitX);
GL.Rotate(angle, Vector3.UnitY);
angle += (float)e.Time * 3.0f;
// 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
GL.Color3(Color.White);
GL.DisableClientState(ArrayCap.ColorArray);
#region Pass 2: Draw Shape
if (SelectedTriangle == uint.MaxValue)
GL.ClearColor(.2f, .1f, .3f, 1f); // purple
else
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.Front, PolygonMode.Line);
GL.Color3(Color.Red);
GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length);
GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
if (SelectedTriangle != uint.MaxValue)
{
GL.Disable(EnableCap.DepthTest);
GL.Color3(Color.Green);
GL.DrawArrays(VBO_PrimMode, (int)SelectedTriangle * 3, 3);
GL.Enable(EnableCap.DepthTest);
}
#endregion Pass 2: Draw Shape
this.SwapBuffers();
ErrorCode err = GL.GetError();
if (err != ErrorCode.NoError)
Trace.WriteLine("Error at Swapbuffers: " + err);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// The 'using' idiom guarantees proper resource cleanup.
// We request 30 UpdateFrame events per second, and unlimited
// RenderFrame events (as fast as the computer can handle).
using (Picking example = new Picking())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Title = String.Format("OpenTK | {0} {1}: {2} (use the mouse to pick)", info.Category, info.Difficulty, info.Title);
example.Run(30.0);
}
}
}
}
|