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

4

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

但是,在旋转屏幕后,节点不再突出显示。

我拥有节点在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. 如何使用三维坐标绘制标签(光栅图形会抛出错误Renderer: Error creating immediate mode Canvas3D graphics context)

  2. 如何将三维坐标转换为二维屏幕坐标,并使用现有代码在二维屏幕点上绘制标签

谢谢,

Dakshina


可能是重复的问题: https://dev59.com/wHRB5IYBdhLWcg3wFz8g,因为一旦你解决了那个部分,绘制标签就非常容易了。 - jefflunt
3个回答

1

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

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

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

实际上,深度是焦点。消失点将位于 [0,0,depth]


0

这是我用来将我的三维坐标转换为透视二维坐标的方法,其中x2和y2是二维坐标,xyz是三维坐标。

使用以下公式:

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

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

我选择了30度的角度,因为它对于透视效果很容易理解,也在等距网格中用于在二维纸上绘制三维图形。由于z轴是垂直的,x和y是与其右侧和左侧60度的轴。等距网格图片。

我仍在研究旋转,但不会改变轴,只是在三维坐标系中进行坐标旋转。 享受吧。


-1

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接