将Java 3D坐标转换为2D屏幕坐标



我正在使用一个名为"Walrus"的Java 3D应用程序,该应用程序用于显示有向图。该代码已经具有突出显示节点并在给定其屏幕坐标的图形中绘制相邻标签的功能。

旋转屏幕后,该节点不再突出显示。

我得到的是三维中的节点坐标。我需要给它画一个标签。

使用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());
  1. 我如何用3D坐标绘制标签(栅格图形抛出错误Renderer:错误创建即时模式Canvas3D图形上下文)

  2. 如何将3D坐标转换为2D屏幕并使用现有代码在2D屏幕点绘制标签

谢谢,

Dakshina

我有一个用depth参数将[x,y,z]转换为[x,y]的算法/方法:

x值为:(int) (x - (z / depth * x))

y的值为:(int) (y - (z / depth * y))

本质上,深度是焦点。消失点将在[0,0,depth]

这是我用来将我的3D坐标转换为2D视角的方法,x2和y2是2维坐标,xyz是3D坐标。

使用下列公式:

x2 = cos(30)*x - cos(30)*y

y = sin(30)*x + sin(30)*y + z

我选择角度30,因为它很容易透视,也用于等距网格在2D纸上绘制3D。因为z轴是垂直的,所以x轴和y轴分别与z轴左右成60度。等距网格图。

我仍然在旋转,但没有改变轴,只是在3D坐标旋转。享受。

我找到了解决方案。这是在图像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);
}

这是一个将3D坐标转换为图像2D坐标并使用上述函数进行渲染的函数

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;
}

最新更新