95 lines
2.3 KiB
Java
95 lines
2.3 KiB
Java
package com.danitheskunk.skunkworks.gfx.threedee;
|
|
|
|
import com.danitheskunk.skunkworks.IWindow;
|
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
|
import com.danitheskunk.skunkworks.gfx.Image;
|
|
import org.lwjgl.assimp.*;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.IntBuffer;
|
|
|
|
public class Model {
|
|
Mesh[] meshes;
|
|
|
|
public Model(ByteBuffer data, IWindow window) {
|
|
var flags = Assimp.aiProcess_Triangulate;
|
|
var ai = Assimp.aiImportFileFromMemory(data, flags, "glb");
|
|
if(ai == null) {
|
|
throw new RuntimeException("couldn't load model because: " +
|
|
Assimp.aiGetErrorString());
|
|
}
|
|
int numMaterials = ai.mNumMaterials();
|
|
var matTex = new int[numMaterials];
|
|
var matPB = ai.mMaterials();
|
|
for(int i = 0; i < numMaterials; ++i) {
|
|
var aiMat = AIMaterial.create(matPB.get(i));
|
|
var texCount = Assimp.aiGetMaterialTextureCount(aiMat,
|
|
Assimp.aiTextureType_DIFFUSE
|
|
);
|
|
var tpath = AIString.calloc();
|
|
var tex = Assimp.aiGetMaterialTexture(aiMat,
|
|
Assimp.aiTextureType_DIFFUSE,
|
|
0,
|
|
tpath,
|
|
(IntBuffer) null,
|
|
null,
|
|
null,
|
|
null,
|
|
null,
|
|
null
|
|
);
|
|
var tpathstr = tpath.dataString();
|
|
matTex[i] = tpathstr.startsWith("*") ? Integer.parseInt(tpathstr.substring(
|
|
1)) : -1;
|
|
System.out.printf("tex count: %d, path: %s\n",
|
|
texCount,
|
|
tpath.dataString()
|
|
);
|
|
}
|
|
|
|
int numTextures = ai.mNumTextures();
|
|
var textures = new ITexture[numTextures];
|
|
var texPB = ai.mTextures();
|
|
for(int i = 0; i < numTextures; ++i) {
|
|
var aiTex = AITexture.create(texPB.get(i));
|
|
var img = new Image(aiTex.pcDataCompressed());
|
|
var tex = window.loadTexture(img);
|
|
textures[i] = tex;
|
|
System.out.printf("tex %dx%d\n", img.getWidth(), img.getHeight());
|
|
}
|
|
|
|
|
|
var numMeshes = ai.mNumMeshes();
|
|
var meshPB = ai.mMeshes();
|
|
meshes = new Mesh[numMeshes];
|
|
int faces = 0;
|
|
for(int i = 0; i < numMeshes; ++i) {
|
|
var aiMesh = AIMesh.create(meshPB.get(i));
|
|
var mesh = Mesh.fromAIMesh(aiMesh);
|
|
var tex = aiMesh.mMaterialIndex();
|
|
if(tex >= 0) {
|
|
mesh.setTexture(textures[matTex[tex]]);
|
|
}
|
|
meshes[i] = mesh;
|
|
System.out.println(aiMesh.mNumFaces());
|
|
faces += aiMesh.mNumFaces();
|
|
}
|
|
|
|
//window.setDebug(true);
|
|
|
|
System.out.printf("numMeshes: %d, faces: %d, textures: %d\n",
|
|
numMeshes,
|
|
faces,
|
|
numTextures
|
|
);
|
|
}
|
|
|
|
public int getMeshCount() {
|
|
return meshes.length;
|
|
}
|
|
|
|
public Mesh getMesh(int i) {
|
|
return meshes[i];
|
|
}
|
|
}
|