簡體   English   中英

將 Java 3D 坐標轉換為 2D 屏幕坐標

[英]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());
  1. 如何使用 3D 坐標繪制 Label(光柵圖形引發錯誤渲染器:創建即時模式 Canvas3D 圖形上下文時出錯)

  2. 如何將 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM