有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中三维渲染三维线条?

好的,我有一个小问题:我正在尝试编写一个能够在2d平面上渲染3d线条的引擎,但是我遇到了一些问题。 我有一个point3d类,它采用x y和z坐标,一个line3d类,它采用两个point3d端点,我有一个包含line3d的arraylist的世界对象。我还有point2d和line2d,它们与它们的3d对应物一样,只是它们缺少z坐标

以下是渲染方法:

public void render(){
    for(Line3d line : world.lines){ //for every 3d line in the world
        panel.l3d(line, world.main); //render that line
    }
    panel.repaint(); //repaint the graphic
}

它调用l3d方法:

public void l3d(Line3d line, Camera view){
    Point2d start = p3Top2(line.start, view), end = p3Top2(line.end, view); //convert 3d points to 2d points
    if(start==null || end==null)return; //if either is null, return
    lineAbs(new Line2d(start, end)); //draw the line
}

这需要p3Top2:

public Point2d p3Top2(Point3d point, Camera view){ //convert 3d point to 2d point on screen
    int relativeZed = point.z - view.z; //difference in z values between camera and point
    if(relativeZed <= 0)return null; //if it's behind the camera, return
    int sx, sy, ex, ey, tx, ty; //begin declaring rectangle formed from extending camera angle to the z coord of the point(angle of 1 = 90 degrees)
    sx = (int) (view.x - (width / 2) - relativeZed * (width * view.angle)); //starting x of rectangle
    ex = (int) (view.x + (width / 2) + relativeZed * (width * view.angle)); //ending x
    sy = (int) (view.y - (height / 2) - relativeZed * (height * view.angle)); //starting y
    ey = (int) (view.y + (height / 2) + relativeZed * (height * view.angle)); //ending y
    tx = point.x - sx; //difference between point's x and start of rectangle
    ty = point.y - sy; //same for y
    float px = (float)tx / (float)(ex - sx), py = (float)ty / (float)(ey - sy); //px and py are the ratios of the point's x/y coords compared to the x/y of the rectangle their in
    return new Point2d((int)(px * width), (int)(py * height)); //multiply it by the width and height to get positions on screen
}

在lineAbs上:

public void lineAbs(Line2d line){
    Point2d start = line.start;
    Point2d end = line.end;
    if(start.x>end.x){
        start = line.end;
        end = line.start;
    }
    int y = start.y; //starting y point
    int white = 0xffffff;
    for(int x = start.x; x<end.x; x++){ //for each x in the line
        if(x < 0 || y < 0 || x > canvas.getWidth() || y > canvas.getHeight())continue; //if the point is outside of the screen, continue
        y += line.getSlope().slope; //increment y by the slope
        canvas.setRGB(x, y, white); //draw the point to the canvas
    }
}

“canvas”是绘制到屏幕的缓冲区图像。使用任意相机和1的角度,以及一些任意线,我确实看到了每一条线,但它们似乎没有被正确渲染。例如,当我有三个point3d作为顶点,三条线,每一条线都有两个点的不同组合时,虽然每一条线都是可见的,但这些线看起来根本不会相交

我怀疑问题在我的p3Top2中,但我不确定在哪里,你能告诉我吗


共 (0) 个答案