Home > Net >  OpenGL mouse zoom center location problem
OpenGL mouse zoom center location problem

Time:11-16

Recently in learning c # version of the OpenGL (SharpGL), met a problem on the application, various gods, please help to give directions
Functional requirements: by the mouse wheel can realize the function of graphics zoom, requirements: zoom in have the mouse position at the center ,

Simple said my first implementation approach (with OpenGL not familiar with, so a lot of operation is written, everyone a great god when we have more convenient method, help to give directions) :
1, determine the screen pixel coordinates and view matrix corresponding relation between coordinates:
1.1, known to control the width and height of OpenGLControl, vision with Ortho set size, can get the screen pixels and vision aspect corresponding ratio,
Such as: gl. Ortho (10, 10, 10, 10, 0.01, 100);
Xx/openGLControl1=20.0. Width;
Yy=20.0/openGLControl1. Height;
1.2, the screen pixel coordinates is the top left corner is (0, 0), the bottom right hand corner is (width, height), coordinates (0, 0), central vision can get the mouse location corresponding to vision coordinates:
Mouse_Pos_x=xx * (e.X - openGLControl1. Width/2);
Mouse_Pos_y=yy * (- + openGLControl1 e.Y. Height/2);
2, the mouse to scroll event (below)
3, zooming in:
In the china-canada OpenGLlDraw function
Gl. Scale (Scale, Scale, Scale);
At this point, do not add any translation instruction, all zoom is based on vision center (0, 0) to zoom in,
Before zooming, add translation instruction gl. Translate (Mouse_Pos_x * (1 - Scale), Mouse_Pos_y * (1 - Scale), 0), the mouse can be implemented in the designated point to zoom effect,

Issue came , because every time OpenGLDraw drawing is according to the geometric transformation of identity matrix, when the mouse to zoom, where the mouse position and the last time of zooming, Mouse_Pos_x and Mouse_Pos_y is different, lead to OpenGLDraw draw will return to the current mouse position when zooming translation operations, can't zoom translation before the operation based on the superposition effect of the translation,
To achieve a: the scaling of gl translation. Translate operation is in the last scale translation gl. Translate operation, on the basis of

Attach major programs:
 private void openGLControl1_Resized (object sender, EventArgs e) 
{
SharpGL. OpenGL gl=this. OpenGLControl1. OpenGL.
Gl. MatrixMode (OpenGL. GL_PROJECTION);
Gl. LoadIdentity ();
Gl. Ortho (10, 10, 10/thewire, 10/thewire, 0.01, 100);
Xx/openGLControl1=20.0. Width;
Yy=20.0/openGLControl1. Height;
Gl. LookAt (0, 0, 10, 0, 0, 0, 0, 1, 0).
Gl. MatrixMode (OpenGL. GL_MODELVIEW);
}

 private void Mouse_Wheel (object sender, MouseEventArgs e) 
{
//when e.D elta & gt; 0 when the mouse wheel is scroll up, e.D elta & lt; 0 when the mouse wheel scroll down
If (Mouse_In_Flag)
{
If (e.D elta & lt; 0)
Scale +=0.1 f;
The else
Scale - f=0.1;
If (Scale & gt;
=5)Scale=5;
If (Scale & lt;=0.05)
Scale=0.05;
Mouse_Pos_x=(double) xx * (e.X - openGLControl1. Width/2);
Mouse_Pos_y=(double) yy * (- + openGLControl1 e.Y. Height/2);
}
}

 private void openGLControl1_OpenGLDraw (object sender, RenderEventArgs args) 
{
OpenGL gl=this. OpenGLControl1. OpenGL;
Gl. The Clear (OpenGL. GL_COLOR_BUFFER_BIT | OpenGL. The GL_DEPTH_BUFFER_BIT);

Gl. LoadIdentity ();
DrawGrid (gl);

Gl. PushMatrix ();
Gl. Translate (Mouse_Pos_x * (1 - Scale), Mouse_Pos_y * (1 - Scale), 0)
Gl. Scale (Scale, Scale, Scale);
Gl. Color (1 f, 1 f, 0 f);
Gl. Our LineWidth (2 f);
Gl. The Begin (OpenGL. GL_LINE_LOOP);
Gl. Vertex (0, 0, 0).
Gl. Vertex (5, 0, 0).
Gl. Vertex (5, 5, 0).
Gl. Vertex (0, 5, 0).
Gl. End ();
Gl. PopMatrix ();
Gl. Flush ();
}

CodePudding user response:

Has in the past several days, his top once
In a recent research project and unproject, see if I can find a good method
  •  Tags:  
  • C#
  • Related