这篇主要说纹理贴图问题
纹理中的像素为图素 texeture element 纹理是根据镜头与多边形的距离缩放,图素也会缩放。
将每个像素贴图一个图素,就可以贴图。就是说读取一个图,获取这个图的每个点的像素,然后贴到视图窗口的每个像素。
计算方法:
纹理贴图的计算方程
[img]http://luanshiqunying-sina-com.iteye.com/upload/attachment/116961/b36858ff-7864-37b3-ade6-38806d618aea.jpg[/img]
x=U·(P-0) Y=V·(P-0)
视图窗口中的一个点在多边形上的具体点P
[img]http://luanshiqunying-sina-com.iteye.com/upload/attachment/116963/db3c7fcc-469e-3dab-aee4-33fd1b79d78c.jpg[/img]
z/x=pz/px 顶点用W表示的话,W=(d/pz)/p
要找到点P,就要找到PZ,平面的发现N和平面上的直线是正交
N·(P-0)=0
N·0=N·P
推导过程我就不写了
最后
x=(-v*w·0)/(u*v·w)
用类描述纹理,纹理是三维空间中的矩形
Rectangle3D
先到这里,明天修改。
纹理中的像素为图素 texeture element 纹理是根据镜头与多边形的距离缩放,图素也会缩放。
将每个像素贴图一个图素,就可以贴图。就是说读取一个图,获取这个图的每个点的像素,然后贴到视图窗口的每个像素。
计算方法:
纹理贴图的计算方程
[img]http://luanshiqunying-sina-com.iteye.com/upload/attachment/116961/b36858ff-7864-37b3-ade6-38806d618aea.jpg[/img]
x=U·(P-0) Y=V·(P-0)
视图窗口中的一个点在多边形上的具体点P
[img]http://luanshiqunying-sina-com.iteye.com/upload/attachment/116963/db3c7fcc-469e-3dab-aee4-33fd1b79d78c.jpg[/img]
z/x=pz/px 顶点用W表示的话,W=(d/pz)/p
要找到点P,就要找到PZ,平面的发现N和平面上的直线是正交
N·(P-0)=0
N·0=N·P
推导过程我就不写了
最后
x=(-v*w·0)/(u*v·w)
用类描述纹理,纹理是三维空间中的矩形
Rectangle3D
package com.jsheng.game.util.java3d.texture;
import com.jsheng.game.util.java3d.Transform3D;
import com.jsheng.game.util.java3d.Vector3D;
public class Rectangle3D {
private Vector3D origin; //原点
private Vector3D directionU; //方程中的U
private Vector3D directionV; //方程中的V
private Vector3D normal; //法线
private float width;
private float height;
public Rectangle3D() {
origin = new Vector3D();
directionU = new Vector3D(1, 0, 0);
directionV = new Vector3D(0, 1, 0);
width = 0;
height = 0;
}
public Rectangle3D(Vector3D origin, Vector3D directionU,
Vector3D directionV, float width, float height) {
this.origin = new Vector3D(origin);
this.directionU = new Vector3D(directionU);
this.directionU.normalize();
this.directionV = new Vector3D(directionV);
this.directionV.normalize();
this.width = width;
this.height = height;
}
public void setTo(Rectangle3D rect) {
origin.setTo(rect.origin);
directionU.setTo(rect.directionU);
directionV.setTo(rect.directionV);
width = rect.width;
height = rect.height;
}
protected Vector3D calcNormal() {
if (normal == null) {
normal = new Vector3D();
}
normal.setToCrossProduct(directionU, directionV);
normal.normalize();
return normal;
}
public Vector3D getOrigin() {
return origin;
}
public void setOrigin(Vector3D origin) {
this.origin = origin;
}
public Vector3D getDirectionU() {
return directionU;
}
public void setDirectionU(Vector3D directionU) {
this.directionU = directionU;
}
public Vector3D getDirectionV() {
return directionV;
}
public void setDirectionV(Vector3D directionV) {
this.directionV = directionV;
}
public Vector3D getNormal() {
if (normal == null) {
calcNormal();
}
return normal;
}
public void setNormal(Vector3D normal) {
if (normal == null) {
normal = new Vector3D(normal);
} else {
normal.setTo(normal);
}
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public void add(Transform3D xform) {
addRotation(xform);
add(xform.getLocation());
}
public void subtract(Transform3D xform) {
subtract(xform.getLocation());
subtractRotation(xform);
}
public void add(Vector3D u) {
origin.add(u);
}
public void subtract(Vector3D u) {
origin.subtract(u);
}
public void addRotation(Transform3D myForm) {
origin.addRotation(myForm);
directionU.addRotation(myForm);
directionV.addRotation(myForm);
}
public void subtractRotation(Transform3D xform) {
origin.subtractRotation(xform);
directionU.subtractRotation(xform);
directionV.subtractRotation(xform);
}
}
先到这里,明天修改。