Lets try this approach, whats the best way to determine where the mouse is in my scene?
What i do is:
I render my objects on screen with a unique color RGBA. Since there are not so many its all red from 1 to 120 so far. So the colordata looks like (i,0,0,0) where i is basically my object ID.
After this “selection render” I draw the scene with normal colors, textures, blending and stuff.
Normal Scene
Selection Scene
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit );
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
if(selectionFrameSkip)
{
selectionFrameSkip = false;
GL.Disable(EnableCap.Blend);
GL.Disable(EnableCap.Texture2D);
VBOHelper.RenderSelection();
GL.ReadPixels(Mouse_X,Mouse_Y, 1, 1, PixelFormat.Rgba, PixelType.UnsignedByte, ref Pixel);
VBOHelper.Select(Pixel,clickstate);
clickstate = false;
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit );
}else selectionFrameSkip = true;
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
VBOHelper.RenderScene();
Context.SwapBuffers();
public static void RenderScene()
{
foreach(VBO vbo in Vbos)
{
if(vbo.Visible)
{
GL.PushMatrix();
GL.Translate(vbo.Translation);
GL.Rotate(vbo.Rotation,0,0,1);
VBOHelper.bind_Vertex(vbo);
VBOHelper.bind_Texture(vbo);
VBOHelper.bind_Color(vbo);
VBOHelper.bind_Indicies(vbo);
GL.PopMatrix();
}
}
VBOHelper.bind_Null();
}
public static void RenderSelection()
{
foreach(VBO vbo in Vbos)
{
if(vbo.Visible)
{
GL.PushMatrix();
GL.Translate(vbo.Translation);
GL.Rotate(vbo.Rotation,0,0,1);
VBOHelper.bind_Vertex(vbo);
VBOHelper.bind_Selection(vbo); // BInd unique color
VBOHelper.bind_Indicies(vbo);
GL.PopMatrix();
}
}
VBOHelper.bind_Null();
}
private static void bind_Vertex(VBO _vbo)
{
if (_vbo.VertexBufferID != 0)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.VertexBufferID);
GL.VertexPointer(2, VertexPointerType.Float, Vector2.SizeInBytes, IntPtr.Zero);
GL.EnableClientState(ArrayCap.VertexArray);
}
}
private static void bind_Indicies(VBO _vbo)
{
if (_vbo.IndiciesBufferID != 0)
{
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _vbo.IndiciesBufferID);
GL.DrawElements(BeginMode.Triangles, _vbo.IndiciesData.Length,
DrawElementsType.UnsignedInt, IntPtr.Zero);
}
}
private static void bind_Color(VBO _vbo)
{
if (_vbo.ColorBufferID != 0)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.ColorBufferID);
GL.ColorPointer(4, ColorPointerType.Float, Vector4.SizeInBytes, IntPtr.Zero);
GL.EnableClientState(ArrayCap.ColorArray);
}
}
private static void bind_Texture(VBO _vbo)
{
if(_vbo.TextureBufferID!=0)
{
GL.BindTexture(TextureTarget.Texture2D , _vbo.TexID);
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.TextureBufferID);
GL.TexCoordPointer(2,TexCoordPointerType.Float,Vector2.SizeInBytes, IntPtr.Zero);
GL.EnableClientState(ArrayCap.TextureCoordArray);
}
}
private static void bind_Selection(VBO _vbo)
{
if(_vbo.SelectionColorBufferID!=0)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo.SelectionColorBufferID);
GL.ColorPointer(4, ColorPointerType.Float, Vector4.SizeInBytes, IntPtr.Zero);
GL.EnableClientState(ArrayCap.ColorArray);
}
}
private static void bind_Null()
{
GL.BindBuffer (BufferTarget.ElementArrayBuffer, 0);
}