moved model stuff to own class

This commit is contained in:
DaniTheSkunk 2022-12-07 23:18:21 +00:00
parent 9c33d7b6f7
commit 107e0a5c91
4 changed files with 123 additions and 82 deletions

View File

@ -6,14 +6,15 @@ import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image; import com.danitheskunk.skunkworks.gfx.Image;
import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
import com.danitheskunk.skunkworks.gfx.threedee.Mesh; import com.danitheskunk.skunkworks.gfx.threedee.Mesh;
import com.danitheskunk.skunkworks.gfx.threedee.Model;
import org.lwjgl.assimp.*; import org.lwjgl.assimp.*;
import java.nio.IntBuffer; import java.nio.IntBuffer;
@SuppressWarnings("ALL") @SuppressWarnings("ALL")
public class Test3D extends BaseGame { public class Test3D extends BaseGame {
private final Mesh[] meshes;
int trans; int trans;
Model model;
public Test3D() { public Test3D() {
super(new Vec2i(1920, 1080), "Skunkworks 3d test"); super(new Vec2i(1920, 1080), "Skunkworks 3d test");
@ -23,82 +24,16 @@ public class Test3D extends BaseGame {
Assimp.aiGetVersionPatch() Assimp.aiGetVersionPatch()
); );
trans = 255; trans = 255;
var path = "C:\\stream\\models\\Dani.glb"; var path = "C:\\stream\\models\\Dani.vrm";
//path = "C:\\stream\\models\\Amber_Arakada_V5_Blendshape.glb"; //path = "C:\\stream\\models\\Amber_Arakada_V5_Blendshape.glb";
//path = "C:\\stream\\models\\Temp Chan 3.glb"; //path = "C:\\stream\\models\\Temp Chan 3.glb";
var bytes = engine.loadBytes(path); var bytes = engine.loadBytes(path);
var flags = Assimp.aiProcess_Triangulate; model = new Model(bytes, window);
//flags |= Assimp.aiProcess_PreTransformVertices; //flags |= Assimp.aiProcess_PreTransformVertices;
var ai = Assimp.aiImportFile(path, flags);
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
);
} }
static public void main(String[] args) { static public void main(String[] args) {
@ -112,15 +47,7 @@ public class Test3D extends BaseGame {
@Override @Override
protected void render3D(IRenderContext3D rc) { protected void render3D(IRenderContext3D rc) {
for(int i = 0; i < meshes.length; ++i) { rc.renderModel(model);
//rc.renderMesh(meshes[i], new Color(255, 255, 255));
}
rc.clearDepth();
rc.renderMesh(meshes[trans * (meshes.length-1) / 255]);
for(int i = 0; i < meshes.length; ++i) {
//rc.renderMesh(meshes[i], new Color(255, 255, 255, 255 - trans));
}
//rc.renderMesh(meshes[14]);
} }
@Override @Override

View File

@ -5,6 +5,7 @@ import com.danitheskunk.skunkworks.Vec3f;
import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.Color;
import com.danitheskunk.skunkworks.gfx.threedee.BaseRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.BaseRenderContext3D;
import com.danitheskunk.skunkworks.gfx.threedee.Mesh; import com.danitheskunk.skunkworks.gfx.threedee.Mesh;
import com.danitheskunk.skunkworks.gfx.threedee.Model;
import static org.lwjgl.opengl.GL46.*; import static org.lwjgl.opengl.GL46.*;
@ -77,6 +78,18 @@ public class RenderContext3D extends BaseRenderContext3D {
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
} }
@Override
public void renderModel(Model model) {
renderModel(model, Color.WHITE);
}
@Override
public void renderModel(Model model, Color tint) {
for(var i = 0; i < model.getMeshCount(); ++i) {
renderMesh(model.getMesh(i), tint);
}
}
@Override @Override
public void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3) { public void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3) {
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);

View File

@ -4,8 +4,15 @@ import com.danitheskunk.skunkworks.Vec3f;
import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.Color;
public interface IRenderContext3D { public interface IRenderContext3D {
void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3);
void renderMesh(Mesh mesh);
void renderMesh(Mesh mesh, Color tint);
void clearDepth(); void clearDepth();
void renderMesh(Mesh mesh, Color tint);
void renderMesh(Mesh mesh);
void renderModel(Model model, Color tint);
void renderModel(Model model);
void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3);
} }

View File

@ -0,0 +1,94 @@
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];
}
}