[英]Translate Java 3D coordinates to 2D screen coordinates
我正在使用一个名为“Walrus”的 Java 3D 应用程序,用于显示有向图。 该代码已经具有突出显示节点并在给定屏幕坐标的情况下在图形中相邻绘制 label 的功能。
旋转屏幕后,节点不再突出显示。
我所拥有的是 3D 中的节点坐标。 我需要给它画上 label。
使用 3D 坐标突出显示的代码
Point3d p = new Point3d();
m_graph.getNodeCoordinates(node, p);
PointArray array = new PointArray(1, PointArray.COORDINATES);
array.setCoordinate(0, p);
m_parameters.putModelTransform(gc);
gc.setAppearance(m_parameters.getPickAppearance());
如何使用 3D 坐标绘制 Label(光栅图形引发错误渲染器:创建即时模式 Canvas3D 图形上下文时出错)
如何将 3D 坐标转换为 2D 屏幕并使用现有代码在 2D 屏幕点绘制 label
谢谢,
达克希娜
这是我用来将 3D 坐标转换为透视 2D 的方法,x2 和 y2 是二维坐标,xyz 是 3D 坐标。
使用这些公式:
x2 = cos(30)*x - cos(30)*y
y2 = sin(30)*x + sin(30)*y + z
我选择了角度 30,因为它很容易用于透视目的,也用于等距网格中,用于在 2D 纸上绘制 3D。 因为 z 轴是垂直的,所以 x 和 y 是左右 60 度的轴。 等距网格图片。
我仍在研究旋转,但不改变轴,只是在 3D 中协调旋转。 享受。
我有一个使用depth
参数将[x,y,z]
转换为[x,y]
的算法/方法:
x
值为: (int) (x - (z / depth * x))
y
值为: (int) (y - (z / depth * y))
本质上,深度是焦点。 消失点将在[0,0,depth]
处。
我找到了解决方案。 这是 function 在图像 2D 坐标处显示 Text3D
public void drawLabel(GraphicsContext3D gc, double x, double y, int zOffset, String s) {
boolean frontBufferRenderingState = gc.getFrontBufferRendering();
gc.setBufferOverride(true);
gc.setFrontBufferRendering(true);
Point3d eye = getEye();
double labelZ = zOffset * LABEL_Z_OFFSET_SCALE
+ LABEL_Z_SCALE * eye.z + LABEL_Z_OFFSET;
double xOffset = LABEL_X_OFFSET * m_pixelToMeterScale;
double yOffset = LABEL_Y_OFFSET * m_pixelToMeterScale;
Point3d p = new Point3d(x + xOffset, y + yOffset, 0.0);
{
// Project given (x, y) coordinates to the plane z=labelZ.
// Convert from image-plate to eye coordinates.
p.x -= eye.x;
p.y -= eye.y;
double inversePerspectiveScale = 1.0 - labelZ / eye.z;
p.x *= inversePerspectiveScale;
p.y *= inversePerspectiveScale;
// Convert from eye to image-plate coordinates.
p.x += eye.x;
p.y += eye.y;
}
Transform3D scale = new Transform3D();
scale.set(LABEL_SCALE);
Vector3d t = new Vector3d(p.x, p.y, labelZ);
Transform3D translation = new Transform3D();
translation.set(t);
translation.mul(scale);
Transform3D transform = new Transform3D(m_imageToVworld);
transform.mul(translation);
gc.setModelTransform(transform);
//-----------------
int fontSize=(int)(10*m_magnification);
if(fontSize>20)
fontSize=20;
//---------------
// XXX: Courier may not be available on all systems.
Text2D text = new Text2D(s, new Color3f(1.0f, 1.0f, 1.0f),
"Courier", fontSize, Font.BOLD);
gc.draw(text);
gc.flush(true);
// NOTE: Resetting the model transform here is very important.
// For some reason, not doing this causes the immediate
// following frame to render incorrectly (but subsequent
// frames will render correctly). In some ways, this
// makes sense, because most rendering code assumes that
// GraphicsContext3D has been set to some reasonable
// transform.
gc.setModelTransform(m_objectTransform);
gc.setFrontBufferRendering(frontBufferRenderingState);
}
这是 function 获取 3D 坐标并将它们转换为图像 2D 坐标并使用上面的 function 进行渲染
private boolean displayOnScreenLabel(int node, String label) {
boolean success = false;
try {
Transform3D transform = m_parameters.getObjectToEyeTransform();
Point3d nodeC = new Point3d();
m_graph.getNodeCoordinates(node, nodeC);
transform.transform(nodeC);
Point3d eye = m_parameters.getEye();
double perspectiveScale = 1.0 / (1.0 - nodeC.z / eye.z);
double centerX = eye.x + nodeC.x * perspectiveScale;
double centerY = eye.y + nodeC.y * perspectiveScale;
GraphicsContext3D gc = m_canvas.getGraphicsContext3D();
m_parameters.drawLabel(gc, centerX, centerY, m_labelZOffsetCounter++, label);
success = true;
} catch (final java.lang.OutOfMemoryError error) {
JOptionPane.showMessageDialog(m_frame, "The 3D Graphics is unable to find enough memory on your system. Kill the application!", "Out Of Memory!", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
success = false;
}
return success;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.