From 107e0a5c9156afceabf542ff720f1220572dce42 Mon Sep 17 00:00:00 2001 From: DaniTheSkunk <> Date: Wed, 7 Dec 2022 23:18:21 +0000 Subject: [PATCH] moved model stuff to own class --- com/danitheskunk/skunkworks/Test3D.java | 85 ++--------------- .../backends/gl/RenderContext3D.java | 13 +++ .../gfx/threedee/IRenderContext3D.java | 13 ++- .../skunkworks/gfx/threedee/Model.java | 94 +++++++++++++++++++ 4 files changed, 123 insertions(+), 82 deletions(-) create mode 100644 com/danitheskunk/skunkworks/gfx/threedee/Model.java diff --git a/com/danitheskunk/skunkworks/Test3D.java b/com/danitheskunk/skunkworks/Test3D.java index 19c3a51..f11015d 100644 --- a/com/danitheskunk/skunkworks/Test3D.java +++ b/com/danitheskunk/skunkworks/Test3D.java @@ -6,14 +6,15 @@ import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.Image; import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.Mesh; +import com.danitheskunk.skunkworks.gfx.threedee.Model; import org.lwjgl.assimp.*; import java.nio.IntBuffer; @SuppressWarnings("ALL") public class Test3D extends BaseGame { - private final Mesh[] meshes; int trans; + Model model; public Test3D() { super(new Vec2i(1920, 1080), "Skunkworks 3d test"); @@ -23,82 +24,16 @@ public class Test3D extends BaseGame { Assimp.aiGetVersionPatch() ); 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\\Temp Chan 3.glb"; var bytes = engine.loadBytes(path); - var flags = Assimp.aiProcess_Triangulate; + model = new Model(bytes, window); + //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) { @@ -112,15 +47,7 @@ public class Test3D extends BaseGame { @Override protected void render3D(IRenderContext3D rc) { - for(int i = 0; i < meshes.length; ++i) { - //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]); + rc.renderModel(model); } @Override diff --git a/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java b/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java index 810e34c..ae26748 100644 --- a/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java +++ b/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java @@ -5,6 +5,7 @@ import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.threedee.BaseRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.Mesh; +import com.danitheskunk.skunkworks.gfx.threedee.Model; import static org.lwjgl.opengl.GL46.*; @@ -77,6 +78,18 @@ public class RenderContext3D extends BaseRenderContext3D { 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 public void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3) { glBegin(GL_TRIANGLES); diff --git a/com/danitheskunk/skunkworks/gfx/threedee/IRenderContext3D.java b/com/danitheskunk/skunkworks/gfx/threedee/IRenderContext3D.java index c5b0375..722f76f 100644 --- a/com/danitheskunk/skunkworks/gfx/threedee/IRenderContext3D.java +++ b/com/danitheskunk/skunkworks/gfx/threedee/IRenderContext3D.java @@ -4,8 +4,15 @@ import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.gfx.Color; public interface IRenderContext3D { - void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3); - void renderMesh(Mesh mesh); - void renderMesh(Mesh mesh, Color tint); 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); } diff --git a/com/danitheskunk/skunkworks/gfx/threedee/Model.java b/com/danitheskunk/skunkworks/gfx/threedee/Model.java new file mode 100644 index 0000000..39aa029 --- /dev/null +++ b/com/danitheskunk/skunkworks/gfx/threedee/Model.java @@ -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]; + } +}