Java 多边形顶点作为UV坐标

Java 多边形顶点作为UV坐标,java,swing,graphics,3d,Java,Swing,Graphics,3d,我正在使用Graphics类在Java中开发一个3D渲染器,它现在可以绘制任何带有彩色面的形状,但是我想知道是否可以对这些面进行纹理处理?我见过很多人用Javascript创建软件渲染器,所以肯定有一个等价的函数/方法,不管他们用Java做什么… 到目前为止,我已经环顾四周,但我能找到的只是图形。setClip(Shape),我认为它不合适,因为它只设置背景纹理,如果顶点移动,它不会拉伸纹理-这只是在2D中,当纹理与相机成一定角度时,它还需要拉伸/倾斜纹理(想想旋转立方体的侧面) 我真的不知道从

我正在使用Graphics类在Java中开发一个3D渲染器,它现在可以绘制任何带有彩色面的形状,但是我想知道是否可以对这些面进行纹理处理?我见过很多人用Javascript创建软件渲染器,所以肯定有一个等价的函数/方法,不管他们用Java做什么…
到目前为止,我已经环顾四周,但我能找到的只是图形。setClip(Shape),我认为它不合适,因为它只设置背景纹理,如果顶点移动,它不会拉伸纹理-这只是在2D中,当纹理与相机成一定角度时,它还需要拉伸/倾斜纹理(想想旋转立方体的侧面)

我真的不知道从哪里开始,我不能使用XOR模式,因为没有倾斜,如果我必须手动操作,我真的不知道如何进行计算。

这些Javascript软件渲染器是如何做到这一点的?

您可以利用、演示和演示。在此上下文中,您应该知道
TexturePaint
与渲染曲面的光栅对齐,而不是与形状的边界对齐

附录:虽然是一个广泛的话题,但也考虑了一种基于像素的方法,使用alpha渐变阴影,在<代码> KineticModel < /代码>中引用。请注意,这种渐变可以应用于

纹理绘制
可写光栅


对于非仿射转换,请参见引用的
javax.media.jai.Warp.Warp

我假设您仅使用Swing/AWT框架进行图形。如果这是错误的,请更新您的问题

如果您正在使用Swing和
Graphics2D
类(Swing组件使用的类),那么您正在处理一个2D框架。这仅仅意味着花哨的3D东西不是内置的——你必须自己实现转换(或者开始抓取3D类来完成你的工作)

因此,您在正确的轨道上-您必须首先设置剪辑(使其适合您的形状),然后执行旋转(使其以正确的角度显示)

也就是说,进行基本的旋转变换并不太困难。(基本)旋转有一个很好的轮廓。当然,当旋转不是只基于一个轴时,它会变得有点复杂。但正如本文后面解释的,如果将矩阵(Rx)(Ry)(Rz)相乘,则可以使用得到的矩阵来确定像素的位置

我创建了一个在Y轴上旋转的快速示例。请注意,我制作了一个哑巴算法(Magic VaningPoint Technology®),给出了一个模糊的深度错觉。我假设你已经有了这样的东西——这可能更正确

import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Rotation3D extends JPanel{
    Image img;

    BufferedImage rotatedImage;
    final int ROTATION_DEGREES = 70;

    int vanishX = 0;
    int vanishY = 0;
    int vanishZ = -1000;

    public Rotation3D(){

        try {
            //Grabbed an image from the java folder - hopefully your computer has it
            img = ImageIO.read(new File(System.getProperty("java.home") + "/lib/deploy/splash.gif"));
            setPreferredSize(new Dimension(img.getWidth(this) * 2,img.getHeight(this) * 2));

            //Create a buffered image with the appropriate size, and draw the image on it
            BufferedImage shadedImage = new BufferedImage(img.getWidth(this), img.getWidth(this), BufferedImage.TYPE_INT_ARGB);
            shadedImage.getGraphics().drawImage(img, 0, 0, this);
            Raster r = shadedImage.getData();

            //Not really necessary unless you're using Magic Vanishingpoint Technology®
            vanishX = shadedImage.getWidth() /2;
            vanishY = shadedImage.getHeight() /2;

            //Create a Wraster for the transformed image
            WritableRaster wr = r.createCompatibleWritableRaster();

            //Do the transformation
            for(int i = 0; i < shadedImage.getWidth(); i++){
                for(int j = 0; j < shadedImage.getHeight(); j++){
                    //Remapping the pixel based on a matrix rotation
                    int[] result = r.getPixel(i, j, new int[4]);
                    Double radians = Math.toRadians(ROTATION_DEGREES);
                    Double newX, newY, newZ;
                    //newX = ((i-vanishX) * Math.cos(radians)) + vanishX; // places the rotation in the middle of the image
                    // x * cos(θ) + y * 0 + z * sin(θ)
                    newX = i * Math.cos(radians); //places the rotation in the y=0 axis
                    // x * 0 + y * 1 + z * 0
                    newY = j * 1.0;
                    // x * -sin(θ) + y * 0 + z * cos(θ)
                    newZ= i * Math.sin(radians) * -1;

                    //Apply Magic Vanishingpoint Technology®
                    //(Not actually trademarked or correct - just something thrown together)
                    if(newZ < vanishZ){
                        newX = 0.0;
                        newY = 0.0;
                    }else if(newZ < 0){
                        double magicVanish =  newZ / vanishZ;
                        newX += magicVanish * newX;
                        newY += magicVanish * newY;
                    }

                    //Print the pixel if it fits on the screen to the new Raster
                    if(newX > 0 && newX < shadedImage.getWidth() && newY > 0 && newY < shadedImage.getHeight())
                        wr.setPixel(newX.intValue(), newY.intValue(), result);
                }
            }

            //Create an image based on the raster.
            rotatedImage = new BufferedImage(img.getWidth(this), img.getWidth(this), BufferedImage.TYPE_INT_ARGB);
            rotatedImage.setData(wr);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(rotatedImage, 0, 0, this);
    }

    public static void main(String[] args){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Rotation3D());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
import java.awt.*;
导入java.awt.image.*;
导入java.io.*;
导入javax.imageio.imageio;
导入javax.swing.*;
公共类Rotation3D扩展了JPanel{
图像img;
缓冲图像旋转图像;
最终整数旋转度=70;
int=x=0;
int=0;
int=z=-1000;
公共旋转3D(){
试一试{
//从java文件夹中抓取了一个图像-希望您的计算机有它
img=ImageIO.read(新文件(System.getProperty(“java.home”)+“/lib/deploy/splash.gif”);
setPreferredSize(新尺寸(img.getWidth(此)*2,img.getHeight(此)*2));
//创建具有适当大小的缓冲图像,并在其上绘制图像
BuffereImage shadeImage=新的BuffereImage(img.getWidth(this)、img.getWidth(this)、BuffereImage.TYPE_INT_ARGB);
graphics().drawImage(img,0,0,this);
光栅r=shadeImage.getData();
//除非您使用的是Magic VaningPoint Technology®,否则不需要
vanishX=shadeImage.getWidth()/2;
消失=shadeImage.getHeight()/2;
//为变换后的图像创建Wraster
WritableRaster wr=r.createCompatibleWritableRaster();
//进行转换
对于(int i=0;i0&&newX0&&newY