C# 控制领料问题
我再一次在OpenGL/OpenTK中遇到了一个小问题。这一次,与我不久前开始的线程不同,它实际上正在工作。唯一的问题是没有正确选择“瓷砖”。我的意思是,如果单击网格上的某个位置,它会直接选择它旁边的网格平铺。但是,如果我轻轻移动鼠标,它将正确选择栅格平铺。这很难解释,所以我将上传两张图片、代码和可执行文件本身 以下是我的观点: 它是一个基本的网格选择系统。它是有效的。然而,选择程序似乎有点不对劲 下面是底层视图的图片,用户实际上看不到它。这是当用户单击时绘制到backbuffer的场景,这只是为了帮助揭示拾取系统的实际外观 红色圆圈和x表示我刚刚单击但未正确注册的位置;尚未将该位置的体素添加到集合中。但是,如果我将鼠标稍微向右移动,或移到瓷砖中心附近,则会为该位置正确添加体素 下面是代码的主要部分;我将添加任何其他必要的内容。哦,很抱歉这里太乱了,我还没来得及清理C# 控制领料问题,c#,opentk,picking,mouse-picking,C#,Opentk,Picking,Mouse Picking,我再一次在OpenGL/OpenTK中遇到了一个小问题。这一次,与我不久前开始的线程不同,它实际上正在工作。唯一的问题是没有正确选择“瓷砖”。我的意思是,如果单击网格上的某个位置,它会直接选择它旁边的网格平铺。但是,如果我轻轻移动鼠标,它将正确选择栅格平铺。这很难解释,所以我将上传两张图片、代码和可执行文件本身 以下是我的观点: 它是一个基本的网格选择系统。它是有效的。然而,选择程序似乎有点不对劲 下面是底层视图的图片,用户实际上看不到它。这是当用户单击时绘制到backbuffer的场景,这只
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using AnimusEngine.Engine.Datatypes;
using AnimusEngine.Engine.Editor.AnimusEditor.Widgets.GLRenderer.Widgets;
using AnimusEngine.Engine.Utilities;
using AnimusEngineRuntime.Runtime.CameraBindings;
using AnimusEngineRuntime.Runtime.Entities;
using AnimusEngineRuntime.Runtime.Utilities;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
namespace AnimusEngine.Engine.Editor.AnimusEditor.Widgets.GLRenderer {
public class AnimusRenderingModule : Panel {
public enum CameraMode {
FirstPerson ,
Arcball ,
Pan
}
public GLControl OpenGlLayer;
public Camera Camera;
public ToolStrip OpenGlLayerToolstrip;
public RenderingModuleStatusStrip OpenGlLayerStatusStrip;
private Point _mouseLocation;
private Point _lastMousePosition;
private Stopwatch _renderTimer;
private FrameCounter _frameCounter;
private bool _isLooking;
private int _gridModelDisplayListHandle;
private int _planeModelDisplayListHandle;
private int _planeTileSelectionDisplayListHandle;
private Random _clientRandomInstance;
private bool _isRotating;
public CameraMode ActiveCameraMode;
private List<SelectableTile> _selectionTileList;
public List<Vector3> VoxelLocations;
public AnimusRenderingModule() {
Initialize();
}
private void DrawGrid() {
GL.PushMatrix();
{
GL.Translate( -1.0f , 0.0f , -1.0f );
GL.Begin( PrimitiveType.Lines );
for( float i = 0 ; i <= 130 ; i += 2 ) {
if( i == 0 ) {
GL.Color3( .6 , .3 , .3 );
} else {
GL.Color3( .25 , .25 , .25 );
}
GL.Vertex3( i , -1.0f , 0.0f );
GL.Vertex3( i , -1.0f , 130.0f );
if( i == 0 ) {
GL.Color3( .3 , .3 , .6 );
} else {
GL.Color3( .25 , .25 , .25 );
}
GL.Vertex3( 0.0f , -1.0f , i );
GL.Vertex3( 130.0f , -1.0f , i );
}
GL.End();
}
GL.PopMatrix();
}
private void DrawPlane() {
GL.Begin( PrimitiveType.Triangles );
// Triangle 1
GL.Vertex3( -1.0 , -1.0 , -1.0 );
GL.Vertex3( -1.0 , -1.0 , 1.0 );
GL.Vertex3( 1.0 , -1.0 , -1.0 );
// Triangle 2
GL.Vertex3( -1.0 , -1.0 , 1.0 );
GL.Vertex3( 1.0 , -1.0 , 1.0 );
GL.Vertex3( 1.0 , -1.0 , -1.0 );
GL.End();
}
private void PickGeometry( int x , int y ) {
Console.WriteLine( "Picking..." );
/**
* Draw The Geometry As solid
* colors
*/
GL.ClearColor( 1.0f , 1.0f , 1.0f , 1.0f );
GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit );
Camera.LookThrough();
GL.MatrixMode( MatrixMode.Modelview );
GL.LoadMatrix( ref Camera.CameraMatrix );
GL.PushMatrix();
GL.CallList( _planeTileSelectionDisplayListHandle );
GL.PopMatrix();
byte[] pixel = new byte[ 3 ];
int[] viewport = new int[ 4 ];
GL.GetInteger( GetPName.Viewport , viewport );
GL.ReadPixels( x , viewport[ 3 ] - y , 1 , 1 , PixelFormat.Rgba , PixelType.UnsignedByte , pixel );
Color pickedColor = Color.FromArgb( pixel[ 0 ] , pixel[ 1 ] , pixel[ 2 ] );
foreach( SelectableTile colorTest in _selectionTileList ) {
if( colorTest.TileColor.R == pickedColor.R && colorTest.TileColor.G == pickedColor.G && colorTest.TileColor.B == pickedColor.B ) {
Console.WriteLine( "Tile Location: " + colorTest.Location );
VoxelLocations.Add( colorTest.Location );
}
}
Console.WriteLine( pickedColor.ToString() );
Console.WriteLine( "Picking Done" );
GL.ClearColor( 0.3f , 0.3f , 0.3f , 0.0f );
}
private void DrawCube() {
GL.Begin( PrimitiveType.Quads );
GL.Color3( Color.Silver );
GL.Vertex3( -1.0f , -1.0f , -1.0f );
GL.Vertex3( -1.0f , 1.0f , -1.0f );
GL.Vertex3( 1.0f , 1.0f , -1.0f );
GL.Vertex3( 1.0f , -1.0f , -1.0f );
GL.Color3( Color.Honeydew );
GL.Vertex3( -1.0f , -1.0f , -1.0f );
GL.Vertex3( 1.0f , -1.0f , -1.0f );
GL.Vertex3( 1.0f , -1.0f , 1.0f );
GL.Vertex3( -1.0f , -1.0f , 1.0f );
GL.Color3( Color.Moccasin );
GL.Vertex3( -1.0f , -1.0f , -1.0f );
GL.Vertex3( -1.0f , -1.0f , 1.0f );
GL.Vertex3( -1.0f , 1.0f , 1.0f );
GL.Vertex3( -1.0f , 1.0f , -1.0f );
GL.Color3( Color.IndianRed );
GL.Vertex3( -1.0f , -1.0f , 1.0f );
GL.Vertex3( 1.0f , -1.0f , 1.0f );
GL.Vertex3( 1.0f , 1.0f , 1.0f );
GL.Vertex3( -1.0f , 1.0f , 1.0f );
GL.Color3( Color.PaleVioletRed );
GL.Vertex3( -1.0f , 1.0f , -1.0f );
GL.Vertex3( -1.0f , 1.0f , 1.0f );
GL.Vertex3( 1.0f , 1.0f , 1.0f );
GL.Vertex3( 1.0f , 1.0f , -1.0f );
GL.Color3( Color.ForestGreen );
GL.Vertex3( 1.0f , -1.0f , -1.0f );
GL.Vertex3( 1.0f , 1.0f , -1.0f );
GL.Vertex3( 1.0f , 1.0f , 1.0f );
GL.Vertex3( 1.0f , -1.0f , 1.0f );
GL.End();
}
private void Render() {
if( _isLooking ) {
Point windowCenter = WinFormsExtensions.FindCenterOfControl( this );
Vector2 mouseDelta = new Vector2( _mouseLocation.X - this.PointToClient( windowCenter ).X , _mouseLocation.Y - this.PointToClient( windowCenter ).Y );
Cursor.Position = windowCenter;
Camera.MouseSpeed.X = ( ( mouseDelta.X / 40.0f * Camera.LookSpeed ) * ( float )_renderTimer.Elapsed.TotalMilliseconds );
Camera.MouseSpeed.Y = ( ( mouseDelta.Y / 40.0f * Camera.LookSpeed ) * ( float )_renderTimer.Elapsed.TotalMilliseconds );
Camera.Yaw += Camera.MouseSpeed.X;
Camera.Pitch -= Camera.MouseSpeed.Y;
if( Camera.Pitch <= -( ( MathHelper.Pi ) * 0.5f ) + 0.01f ) {
Camera.Pitch = -( ( MathHelper.Pi ) * 0.5f ) + 0.01f;
}
if( Camera.Pitch >= ( ( MathHelper.Pi ) * 0.5f ) - 0.01f ) {
Camera.Pitch = ( ( MathHelper.Pi ) * 0.5f ) - 0.01f;
}
}
Camera.LookThrough();
GL.MatrixMode( MatrixMode.Modelview );
GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit );
GL.LoadMatrix( ref Camera.CameraMatrix );
//
//GL.CallList( _planeTileSelectionDisplayListHandle );
GL.CallList( _gridModelDisplayListHandle );
foreach( Vector3 location in VoxelLocations ) {
GL.PushMatrix();
GL.Translate( location );
DrawCube();
GL.PopMatrix();
}
//
}
private void Initialize() {
_renderTimer = new Stopwatch();
this.OpenGlLayer = new GLControl();
this._frameCounter = new FrameCounter();
this.OpenGlLayer.Dock = DockStyle.Fill;
this.OpenGlLayer.MouseDown += OpenGLLayer_MouseDown;
this.OpenGlLayer.MouseMove += OpenGLLayer_MouseMove;
this.OpenGlLayer.MouseUp += OpenGLLayer_MouseUp;
this.OpenGlLayer.Resize += OpenGLLayer_Resize;
this.OpenGlLayer.Paint += OpenGLLayer_Paint;
this.OpenGlLayer.Load += OpenGLLayer_Load;
Application.Idle += Application_Idle;
this.OpenGlLayerToolstrip = new ToolStrip();
this.OpenGlLayerToolstrip.Items.Add( new ToolStripButton() );
this.OpenGlLayerToolstrip.Dock = DockStyle.Top;
this.OpenGlLayerStatusStrip = new RenderingModuleStatusStrip();
this.Controls.Add( this.OpenGlLayerToolstrip );
this.Controls.Add( this.OpenGlLayer );
this.Controls.Add( this.OpenGlLayerStatusStrip );
this.OpenGlLayerStatusStrip.UpdateFramerateDisplay( 60 );
this.OpenGlLayerStatusStrip.UpdateTotalVoxelDisplay( 2000 );
this.ActiveCameraMode = CameraMode.FirstPerson;
this._clientRandomInstance = new Random();
this._selectionTileList = new List<SelectableTile>();
for( int tileX = 0 ; tileX <= 128 ; tileX += 2 ) {
for( int tileZ = 0 ; tileZ <= 128 ; tileZ += 2 ) {
byte red = ( byte )_clientRandomInstance.Next( 256 );
byte green = ( byte )_clientRandomInstance.Next( 256 );
byte blue = ( byte )_clientRandomInstance.Next( 256 );
this._selectionTileList.Add( new SelectableTile( Color.FromArgb( red , green , blue ) , new Vector3( tileX , 0.0f , tileZ ) ) );
}
}
VoxelLocations = new List<Vector3>();
}
private void GenerateEngineResources() {
_gridModelDisplayListHandle = MeshGenerator.Generate( DrawGrid );
_planeModelDisplayListHandle = MeshGenerator.Generate( DrawPlane );
_planeTileSelectionDisplayListHandle = MeshGenerator.Generate( delegate {
foreach( SelectableTile tile in _selectionTileList ) {
GL.PushMatrix();
GL.Translate( tile.Location );
GL.Color3( tile.TileColor );
DrawPlane();
GL.PopMatrix();
}
} );
}
private void OpenGLLayer_Load( object sender , EventArgs e ) {
_renderTimer.Start();
GL.Enable( EnableCap.DepthTest );
GL.ClearColor( 0.3f , 0.3f , 0.3f , 0.0f );
GL.CullFace( CullFaceMode.Back );
GL.Enable( EnableCap.CullFace );
GL.BlendFunc( BlendingFactorSrc.SrcAlpha , BlendingFactorDest.OneMinusSrcAlpha );
GL.Enable( EnableCap.Blend );
Camera = new Camera();
Camera.Location = new Vector3( -10.0f , 0.0f , 0.0f );
Camera.Pitch = 0.0f;
Camera.Yaw = 0.0f;
Camera.MoveSpeed = 0.02f;
Camera.LookSpeed = 0.4f;
GenerateEngineResources();
}
private void Application_Idle( object sender , EventArgs e ) {
KeyboardState keyboardState = Keyboard.GetState();
ProcessMovement( keyboardState );
this.OpenGlLayer.Invalidate();
_renderTimer.Restart();
}
private void ProcessMovement( KeyboardState keyboardState ) {
if( !this.OpenGlLayer.Focused ) {
return;
}
if( keyboardState[ Key.W ] ) {
Camera.MoveForward( ( float )_renderTimer.Elapsed.TotalMilliseconds );
}
if( keyboardState[ Key.S ] ) {
Camera.MoveBackward( ( float )_renderTimer.Elapsed.TotalMilliseconds );
}
if( keyboardState[ Key.A ] ) {
Camera.StrafeLeft( ( float )_renderTimer.Elapsed.TotalMilliseconds );
}
if( keyboardState[ Key.D ] ) {
Camera.StrafeRight( ( float )_renderTimer.Elapsed.TotalMilliseconds );
}
if( keyboardState[ Key.Space ] ) {
Camera.FlyUp( ( float )_renderTimer.Elapsed.TotalMilliseconds );
}
if( keyboardState[ Key.LShift ] || keyboardState[ Key.RShift ] ) {
Camera.FlyDown( ( float )_renderTimer.Elapsed.TotalMilliseconds );
}
if( keyboardState[ Key.AltLeft ] ) {
this._isRotating = true;
this.ActiveCameraMode = CameraMode.Arcball;
this.OpenGlLayerStatusStrip.UpdateCameraModeDisplay( this.ActiveCameraMode );
} else {
this._isRotating = false;
this.ActiveCameraMode = CameraMode.FirstPerson;
this.OpenGlLayerStatusStrip.UpdateCameraModeDisplay( this.ActiveCameraMode );
}
}
private void OpenGLLayer_Resize( object sender , EventArgs e ) {
if( !this.OpenGlLayer.IsHandleCreated ) {
return;
}
GL.Viewport( ClientRectangle.X , ClientRectangle.Y , ClientRectangle.Width , ClientRectangle.Height );
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView( ( float )Math.PI / 4 , Width / ( float )Height , 0.1f , 1000.0f );
GL.MatrixMode( MatrixMode.Projection );
GL.LoadMatrix( ref projection );
}
private void OpenGLLayer_Paint( object sender , PaintEventArgs e ) {
if( !this.OpenGlLayer.IsHandleCreated ) {
return;
}
Render();
this.OpenGlLayer.SwapBuffers();
}
private void OpenGLLayer_MouseUp( object sender , MouseEventArgs e ) {
if( e.Button == MouseButtons.Right ) {
_isLooking = false;
Cursor.Position = _lastMousePosition;
Cursor.Show();
}
}
private void OpenGLLayer_MouseMove( object sender , MouseEventArgs e ) {
_mouseLocation = new Point( e.X , e.Y );
}
private void OpenGLLayer_MouseDown( object sender , MouseEventArgs e ) {
if( e.Button == MouseButtons.Right && e.Button != MouseButtons.Left ) {
Point nativeMouse = Cursor.Position;
_lastMousePosition = nativeMouse;
Point windowCenter = WinFormsExtensions.FindCenterOfControl( this );
Cursor.Position = windowCenter;
_isLooking = true;
Cursor.Hide();
}
if( e.Button == MouseButtons.Left && e.Button != MouseButtons.Right ) {
PickGeometry( e.X , e.Y );
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用系统图;
使用System.Windows.Forms;
使用animusegine.Engine.Datatypes;
使用animusegine.Engine.Editor.AnimusEditor.Widgets.GLRenderer.Widgets;
使用animusegine.Engine.Utilities;
使用AnimusEngineRuntime.Runtime.CameraBindings;
使用AnimusEngineRuntime.Runtime.Entities;
使用AnimusEngineRuntime.Runtime.Utilities;
使用OpenTK;
使用OpenTK.Graphics.OpenGL;
使用OpenTK.Input;
使用MouseEventArgs=System.Windows.Forms.MouseEventArgs;
命名空间AnimusEngine.Engine.Editor.AnimusEditor.Widgets.GLRenderer{
公共类AnimusRenderingModule:面板{
公共枚举CameraMode{
第一人称,
电弧球,
平底锅
}
公共控制OpenGlLayer;
公共摄像机;
公共工具条OpenGlLayerToolstrip;
公共渲染模块StatusStrip OpenGLlayerStatusTrip;
私人点鼠标定位;
私用点_lastMousePosition;
私人秒表渲染器;
专用帧计数器\u帧计数器;
私人厕所;
private int\u gridModelDisplayListHandle;
私有int_planeModelDisplayListHandle;
私有int_planeTileSelectionDisplayListHandle;
私有随机性;
私人厕所是旋转的;
公共CameraMode-ActiveCameraMode;
私人列表_selectionTileList;
公共列表体素位置;
公共动画渲染模块(){
初始化();
}
私有void DrawGrid(){
GL.PushMatrix();
{
GL.平移(-1.0f,0.0f,-1.0f);
总账开始(基本类型行);
对于(float i=0;i我找到了答案!这不是控件的问题,而是我如何重新创建opengl视口本身的问题。问题只不过是一个小疏忽。我必须更改:
GL.Viewport( ClientRectangle.X , ClientRectangle.Y , ClientRectangle.Width , ClientRectangle.Height );
致:
拾取现在就像一个魔咒一样工作!吸取的教训是,确保在使用GLControl时没有使用错误的/或映射到错误的控件!是否确定正确计算了x和y坐标?如果只想选择网格位置,为什么还要渲染立方体?我可能也会使用顺序颜色为了防止可能的重复,她会选择随机的。@vesan我很确定一切都计算得很好。在我渲染彩色瓷砖的图片中,它们的坐标与正常渲染时的坐标完全相同。您尝试过演示吗?如果您正好在瓷砖前面(它占据了大部分屏幕)挑选一点问题都没有。我坐在床上逐行阅读我自己的代码……我想我可能看到了问题。但这在同一时间没有意义。我认为在backbuffer中,瓷砖是以0.0f的温度绘制的,这将使它们略高于网格。我需要在分配它们时显式地声明-1.0f。或者,我可以我错了。随着代码库本身的增长,使用代码变得相当困难。我明天会试试这个,然后再和你们联系。
GL.Viewport( this.OpenGlLayer.ClientRectangle.X , this.OpenGlLayer.ClientRectangle.Y , this.OpenGlLayer.ClientRectangle.Width , this.OpenGlLayer.ClientRectangle.Height );