Add texture UV coordinate calculation to ModelDefinition
This commit is contained in:
@@ -27,6 +27,8 @@ public class ModelDefinition
|
||||
public short[] textureTriangleVertexIndices1;
|
||||
public short[] textureTriangleVertexIndices2;
|
||||
public short[] textureTriangleVertexIndices3;
|
||||
public float[][] faceTextureUCoordinates;
|
||||
public float[][] faceTextureVCoordinates;
|
||||
public short[] texturePrimaryColors;
|
||||
public short[] faceTextures;
|
||||
public byte[] textureCoordinates;
|
||||
@@ -150,4 +152,118 @@ public class ModelDefinition
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the UV coordinates for every three-vertex face that has a texture.
|
||||
*/
|
||||
public void computeTextureUVCoordinates() {
|
||||
this.faceTextureUCoordinates = new float[faceCount][];
|
||||
this.faceTextureVCoordinates = new float[faceCount][];
|
||||
|
||||
for (int i = 0; i < faceCount; i++) {
|
||||
int textureCoordinate;
|
||||
if (textureCoordinates == null) {
|
||||
textureCoordinate = -1;
|
||||
} else {
|
||||
textureCoordinate = textureCoordinates[i];
|
||||
}
|
||||
|
||||
int textureIdx;
|
||||
if (faceTextures == null) {
|
||||
textureIdx = -1;
|
||||
} else {
|
||||
textureIdx = faceTextures[i] & 0xFFFF;
|
||||
}
|
||||
|
||||
if (textureIdx != -1) {
|
||||
float[] u = new float[3];
|
||||
float[] v = new float[3];
|
||||
|
||||
if (textureCoordinate == -1) {
|
||||
u[0] = 0.0F;
|
||||
v[0] = 1.0F;
|
||||
|
||||
u[1] = 1.0F;
|
||||
v[1] = 1.0F;
|
||||
|
||||
u[2] = 0.0F;
|
||||
v[2] = 0.0F;
|
||||
} else {
|
||||
textureCoordinate &= 0xFF;
|
||||
|
||||
byte textureRenderType = 0;
|
||||
if (textureRenderTypes != null) {
|
||||
textureRenderType = textureRenderTypes[textureCoordinate];
|
||||
}
|
||||
|
||||
if (textureRenderType == 0) {
|
||||
int faceVertexIdx1 = faceVertexIndices1[i];
|
||||
int faceVertexIdx2 = faceVertexIndices2[i];
|
||||
int faceVertexIdx3 = faceVertexIndices3[i];
|
||||
|
||||
short triangleVertexIdx1 = textureTriangleVertexIndices1[textureCoordinate];
|
||||
short triangleVertexIdx2 = textureTriangleVertexIndices2[textureCoordinate];
|
||||
short triangleVertexIdx3 = textureTriangleVertexIndices3[textureCoordinate];
|
||||
|
||||
float triangleX = (float) vertexPositionsX[triangleVertexIdx1];
|
||||
float triangleY = (float) vertexPositionsY[triangleVertexIdx1];
|
||||
float triangleZ = (float) vertexPositionsZ[triangleVertexIdx1];
|
||||
|
||||
float f_882_ = (float) vertexPositionsX[triangleVertexIdx2] - triangleX;
|
||||
float f_883_ = (float) vertexPositionsY[triangleVertexIdx2] - triangleY;
|
||||
float f_884_ = (float) vertexPositionsZ[triangleVertexIdx2] - triangleZ;
|
||||
float f_885_ = (float) vertexPositionsX[triangleVertexIdx3] - triangleX;
|
||||
float f_886_ = (float) vertexPositionsY[triangleVertexIdx3] - triangleY;
|
||||
float f_887_ = (float) vertexPositionsZ[triangleVertexIdx3] - triangleZ;
|
||||
float f_888_ = (float) vertexPositionsX[faceVertexIdx1] - triangleX;
|
||||
float f_889_ = (float) vertexPositionsY[faceVertexIdx1] - triangleY;
|
||||
float f_890_ = (float) vertexPositionsZ[faceVertexIdx1] - triangleZ;
|
||||
float f_891_ = (float) vertexPositionsX[faceVertexIdx2] - triangleX;
|
||||
float f_892_ = (float) vertexPositionsY[faceVertexIdx2] - triangleY;
|
||||
float f_893_ = (float) vertexPositionsZ[faceVertexIdx2] - triangleZ;
|
||||
float f_894_ = (float) vertexPositionsX[faceVertexIdx3] - triangleX;
|
||||
float f_895_ = (float) vertexPositionsY[faceVertexIdx3] - triangleY;
|
||||
float f_896_ = (float) vertexPositionsZ[faceVertexIdx3] - triangleZ;
|
||||
|
||||
float f_897_ = f_883_ * f_887_ - f_884_ * f_886_;
|
||||
float f_898_ = f_884_ * f_885_ - f_882_ * f_887_;
|
||||
float f_899_ = f_882_ * f_886_ - f_883_ * f_885_;
|
||||
float f_900_ = f_886_ * f_899_ - f_887_ * f_898_;
|
||||
float f_901_ = f_887_ * f_897_ - f_885_ * f_899_;
|
||||
float f_902_ = f_885_ * f_898_ - f_886_ * f_897_;
|
||||
float f_903_ = 1.0F / (f_900_ * f_882_ + f_901_
|
||||
* f_883_ + f_902_ * f_884_);
|
||||
|
||||
u[0] = (f_900_ * f_888_ + f_901_ * f_889_ + f_902_
|
||||
* f_890_)
|
||||
* f_903_;
|
||||
u[1] = (f_900_ * f_891_ + f_901_ * f_892_ + f_902_
|
||||
* f_893_)
|
||||
* f_903_;
|
||||
u[2] = (f_900_ * f_894_ + f_901_ * f_895_ + f_902_
|
||||
* f_896_)
|
||||
* f_903_;
|
||||
|
||||
f_900_ = f_883_ * f_899_ - f_884_ * f_898_;
|
||||
f_901_ = f_884_ * f_897_ - f_882_ * f_899_;
|
||||
f_902_ = f_882_ * f_898_ - f_883_ * f_897_;
|
||||
f_903_ = 1.0F / (f_900_ * f_885_ + f_901_ * f_886_ + f_902_
|
||||
* f_887_);
|
||||
|
||||
v[0] = (f_900_ * f_888_ + f_901_ * f_889_ + f_902_
|
||||
* f_890_)
|
||||
* f_903_;
|
||||
v[1] = (f_900_ * f_891_ + f_901_ * f_892_ + f_902_
|
||||
* f_893_)
|
||||
* f_903_;
|
||||
v[2] = (f_900_ * f_894_ + f_901_ * f_895_ + f_902_
|
||||
* f_896_)
|
||||
* f_903_;
|
||||
}
|
||||
}
|
||||
|
||||
this.faceTextureUCoordinates[i] = u;
|
||||
this.faceTextureVCoordinates[i] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user