使用中心点和比例因子变换顶点?

发布于 2024-09-16 11:22:41 字数 1823 浏览 3 评论 0原文

我的应用程序是矢量绘图应用程序。它与 OpenGL 一起工作。我将修改它以改为使用 Cairo 2D 图形库。问题在于缩放。使用 openGL 相机和比例因子,工作方式如下:

 float scalediv = Current_Scene().camera.ScaleFactor / 2.0f;
 float cameraX = GetCameraX();
 float cameraY = GetCameraY();
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

 float left = cameraX - ((float)controls.MainGlFrame.Dimensions.x) * scalediv;
 float right = cameraX + ((float)controls.MainGlFrame.Dimensions.x) * scalediv;
 float bottom = cameraY - ((float)controls.MainGlFrame.Dimensions.y) * scalediv;
 float top = cameraY + ((float)controls.MainGlFrame.Dimensions.y) * scalediv;

 glOrtho(left,
  right,
  bottom, 
  top,
  -0.01f,0.01f);

 // Set the model matrix as the current matrix
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

 hdc = BeginPaint(controls.MainGlContext.mhWnd,&ps);

鼠标位置的获取方式如下:

 POINT _mouse = controls.MainGlFrame.GetMousePos();
vector2f mouse = functions.ScreenToWorld(_mouse.x,_mouse.y,GetCameraX(),GetCameraY(),
             Current_Scene().camera.ScaleFactor,
            controls.MainGlFrame.Dimensions.x,
            controls.MainGlFrame.Dimensions.y );

vector2f CGlEngineFunctions::ScreenToWorld(int x, int y, float camx, float camy, float scale, int width, int height)
{
 // Move the given point to the origin, multiply by the zoom factor and
 // add the model coordinates of the center point (camera position)
 vector2f p;


 p.x =  (float)(x - width / 2.0f) * scale +
  camx;

 p.y = -(float)(y - height / 2.0f) * scale +
  camy;

 return p;
}

从那里我绘制三角形的 VBO。这允许我平移和放大。鉴于 Cairo 只能基于坐标进行绘制,我怎样才能使顶点在不使用变换的情况下正确缩放和平移。基本上,GlOrtho 通常会设置视口,但我认为我不能在 Cairo 上做到这一点。

那么 GlOrtho 能够更改视口矩阵而不是修改顶点,但是我怎样才能修改顶点以获得相同的结果呢?

谢谢

*给定从 ScreenToWorld 获得的顶点 P,我如何修改它,以便根据相机和比例因子对其进行缩放和平移?因为通常 OpenGL 本质上会这样做

My application is a vector drawing application. It works with OpenGL. I will be modifying it to instead use the Cairo 2D graphics library. The issue is with zooming. With openGL camera and scale factor sort of work like this:

 float scalediv = Current_Scene().camera.ScaleFactor / 2.0f;
 float cameraX = GetCameraX();
 float cameraY = GetCameraY();
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

 float left = cameraX - ((float)controls.MainGlFrame.Dimensions.x) * scalediv;
 float right = cameraX + ((float)controls.MainGlFrame.Dimensions.x) * scalediv;
 float bottom = cameraY - ((float)controls.MainGlFrame.Dimensions.y) * scalediv;
 float top = cameraY + ((float)controls.MainGlFrame.Dimensions.y) * scalediv;

 glOrtho(left,
  right,
  bottom, 
  top,
  -0.01f,0.01f);

 // Set the model matrix as the current matrix
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

 hdc = BeginPaint(controls.MainGlContext.mhWnd,&ps);

Mouse position is obtained like this:

 POINT _mouse = controls.MainGlFrame.GetMousePos();
vector2f mouse = functions.ScreenToWorld(_mouse.x,_mouse.y,GetCameraX(),GetCameraY(),
             Current_Scene().camera.ScaleFactor,
            controls.MainGlFrame.Dimensions.x,
            controls.MainGlFrame.Dimensions.y );

vector2f CGlEngineFunctions::ScreenToWorld(int x, int y, float camx, float camy, float scale, int width, int height)
{
 // Move the given point to the origin, multiply by the zoom factor and
 // add the model coordinates of the center point (camera position)
 vector2f p;


 p.x =  (float)(x - width / 2.0f) * scale +
  camx;

 p.y = -(float)(y - height / 2.0f) * scale +
  camy;

 return p;
}

From there I draw the VBO's of triangles. This allows me to pan and zoom in. Given that Cairo only can draw based on coordinates, how can I make it so that a vertex is properly scaled and panned without using transformations. Basically GlOrtho sets the viewport usually but I dont think I could do this with Cairo.

Well GlOrtho is able to change the viewport matrix instead of modifying the verticies but how could I instead modify the verticies to get the same result?

Thanks

*Given vertex P, which was obtained from ScreenToWorld, how could I modify it so that it is scaled and panned accordng to the camera and scale factor? Because usually OpenGL would essentially do this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧幽蓝 2024-09-23 11:22:41

我认为开罗可以做你想做的事......请参阅 http://cairgraphics.org/matrix_transform/ 。这能解决您的问题吗?如果没有,为什么?

I think Cairo can do what you want ... see http://cairographics.org/matrix_transform/ . Does that solve your problem, and if not, why ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文