放大和缩小并将当前像素保持在鼠标坐标处



所以...我有我写的这段代码,它在 Opengl 中以正交模式绘制一个四边形。 我知道它的位置和大小。有时是方形的。有时是矩形的。 我试图做的是放大和缩小并停留在四边形上的同一位置。 我以为它会起作用,但事实并非如此。 知道从鼠标位置到角落的距离,offset.x 和等于 rect_size.x- old_size_w基本数学的差异,我会这样: (offset.x/rect_size.x) * 差异。 这应该为我提供根据鼠标所在的位置需要移动多少位置的比例。 我希望有人能解决这个问题.. 谢谢!。。。。

一些数字...

location = 100,100
old_size_w = 1024
rect_size.x = 1088 (new size old_ * 1.0625)
mouse_delta.x = 425
offset = 100 - 425 (-325)
difference = 1088-1024 (64)
delta_x = 325/1088 (.2987132....)
x_offset = Cint(delta_x * difference) (19) (19.11764...)

所以。。。。我们只移动了19个像素。如果我们从另一个方向进行数学计算。2 必须 = 与旧缩放和新缩放的区别

delta_x = (1088-325) /1088 (.701286...)
x_offset2 = Cint(delta_x * difference) (45) (44.88235...)
19 + 45 = 64   <--- this proves out the math

还。。。我正在经历一个令人讨厌的转变,越靠近我移动的图像的右侧,这种转变就越严重。 也许有人可以找到问题所在。r_x在下面的代码中保留 X,用于证明数学。

Public Sub img_scale_up()
If Not ready_to_render Then Return
If Zoom_Factor >= 4.0 Then
Zoom_Factor = 4.0
Return 'to big and the t_bmp creation will hammer memory.
End If
Dim amt As Single = 0.0625
Zoom_Factor += amt

Dim offset As New Point
'old_w and old_h are the orginal size of the image.
Dim old_size_w, old_size_h As Double
old_size_w = (old_w * (Zoom_Factor - amt))
old_size_h = (old_h * (Zoom_Factor - amt))
offset = rect_location - (mouse_delta)
rect_size.X = Zoom_Factor * old_w
rect_size.Y = Zoom_Factor * old_h
Dim r_x As Double = ((rect_size.X - -offset.X) / rect_size.X) * (rect_size.X - old_size_w)
Dim delta_x As Double = CSng(offset.X / rect_size.X)
Dim delta_y As Double = CSng(offset.Y / rect_size.Y)
Dim x_offset = delta_x * (rect_size.X - old_size_w)
Dim y_offset = delta_y * (rect_size.Y - old_size_h)
rect_location.X += CInt(x_offset)
rect_location.Y += CInt(y_offset)
draw_(current_image)

将鼠标位置存储为 u、v,其中 u 和 v 位于 0.0 - 1.0 上。平移图像,使其鼠标为 0, 0。操作,在本例中通过缩放。然后翻译回来,使 0, 0, 去给你。

我通常会迷失(犯愚蠢的错误)推导正确的方程(因为我使用许多视图变换公式而不仅仅是一个),而且大多数情况下我懒得推导,所以我这样做:

  1. 将鼠标位置转换为偏移未缩放的坐标系

    是屏幕坐标系还是世界坐标系取决于您的变换顺序。将结果存储为mx0,my0

  2. 应用zoom更改

  3. 将鼠标位置转换为偏移未缩放的坐标系

    它与#1相同,但zoom更新。将结果存储为mx1,my1

  4. 更新偏移量

    只需添加:

    offset_x += mx0-mx1;
    offset_y += my0-my1;
    

    offset_x,offset_y保存视图偏移(位置)的位置。

另请参阅基于当前鼠标位置缩放图形

好的。我解决了这个问题..我使用了几个错误的值来放大和缩小。 这是围绕鼠标中心缩放并保持鼠标中心的完整代码。 任何试图弄清楚这一点的人..好吧,这是完美运行的代码:)

Public Sub img_scale_up()
If Not ready_to_render Then Return
If Zoom_Factor >= 4.0 Then
Zoom_Factor = 4.0
Return 'to big and the t_bmp creation will hammer memory.
End If
Dim amt As Single = 0.0625
Zoom_Factor += amt
'this bit of math zooms the texture around the mouses center during the resize.
'old_w and old_h is the original size of the image in width and height
'mouse_pos is current mouse position in the window.
Dim offset As New Point
Dim old_size_w, old_size_h As Double
old_size_w = (old_w * (Zoom_Factor - amt))
old_size_h = (old_h * (Zoom_Factor - amt))
offset = rect_location - (mouse_pos)
rect_size.X = Zoom_Factor * old_w
rect_size.Y = Zoom_Factor * old_h
Dim delta_x As Double = CDbl(offset.X / old_size_w)
Dim delta_y As Double = CDbl(offset.Y / old_size_h)
Dim x_offset = delta_x * (rect_size.X - old_size_w)
Dim y_offset = delta_y * (rect_size.Y - old_size_h)
rect_location.X += CInt(x_offset)
rect_location.Y += CInt(y_offset)
draw_(current_image)
End Sub
Public Sub img_scale_down()
If Not ready_to_render Then Return
If Zoom_Factor <= 0.25 Then
Zoom_Factor = 0.25
Return
End If
Dim amt As Single = 0.0625
Zoom_Factor -= amt
'this bit of math zooms the texture around the mouses center during the resize.
'old_w and old_h is the original size of the image in width and height
'mouse_pos is current mouse position in the window.
Dim offset As New Point
Dim old_size_w, old_size_h As Double
old_size_w = (old_w * (Zoom_Factor - amt))
old_size_h = (old_h * (Zoom_Factor - amt))
offset = rect_location - (mouse_pos)
rect_size.X = Zoom_Factor * old_w
rect_size.Y = Zoom_Factor * old_h
Dim delta_x As Double = CDbl(offset.X / (rect_size.X + (rect_size.X - old_size_w)))
Dim delta_y As Double = CDbl(offset.Y / (rect_size.Y + (rect_size.Y - old_size_h)))
Dim x_offset = delta_x * (rect_size.X - old_size_w)
Dim y_offset = delta_y * (rect_size.Y - old_size_h)
rect_location.X += -CInt(x_offset)
rect_location.Y += -CInt(y_offset)
draw_(current_image)
End Sub

最新更新