所以...我写了这个代码,在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)
答案 0 :(得分:1)
将鼠标位置存储为u,v,其中u和v为0.0 - 1.0。翻译你的图像,使其鼠标为0,0。操作,在这种情况下通过缩放。然后翻译回来,0,0,转到你。诉
答案 1 :(得分:1)
好的..我把它排除了..我使用了几个错误的值来放大和缩小。 这是用于围绕鼠标中心进行缩放并保持鼠标中心的整个代码。 任何人都试图弄清楚这一点......这里的代码完美无缺:)
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
答案 2 :(得分:1)
在导出正确的方程式时,我经常迷路(做愚蠢的错误)(因为我使用了许多视图转换公式而不仅仅是一个)而且我大多数时候都懒得派生所以我这样做就是这样:
将鼠标位置转换为不缩放偏移量的坐标系
如果是屏幕或世界坐标系,则取决于您的转换顺序。将结果存储为mx0,my0
应用zoom
更改
将鼠标位置转换为不缩放偏移量的坐标系
与#1 相同,但更新为zoom
。将结果存储为mx1,my1
更新偏移量
只需添加:
offset_x += mx0-mx1;
offset_y += my0-my1;
offset_x,offset_y
保持您的视图偏移(位置)。