Compare commits

..

2 Commits

Author SHA1 Message Date
DaniTheSkunk 97a6aee5cf starting to load and render .vrm 2022-11-23 03:20:20 +00:00
DaniTheSkunk b9ee6962db added Vec3i 2022-11-23 03:12:14 +00:00
11 changed files with 170 additions and 15 deletions

View File

@ -1,6 +1,9 @@
<component name="InspectionProjectProfileManager"> <component name="InspectionProjectProfileManager">
<profile version="1.0"> <profile version="1.0">
<option name="myName" value="Project Default" /> <option name="myName" value="Project Default" />
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,org.lwjgl.assimp.Assimp,aiImportFile" />
</inspection_tool>
<inspection_tool class="ClassCanBeRecord" enabled="false" level="WEAK WARNING" enabled_by_default="false" /> <inspection_tool class="ClassCanBeRecord" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
</profile> </profile>
</component> </component>

View File

@ -6,7 +6,7 @@ import com.danitheskunk.skunkworks.audio.nodes.Mixer;
import com.danitheskunk.skunkworks.audio.nodes.Node; import com.danitheskunk.skunkworks.audio.nodes.Node;
import com.danitheskunk.skunkworks.audio.nodes.SamplePlayer; import com.danitheskunk.skunkworks.audio.nodes.SamplePlayer;
import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.nodes.NodeRoot; import com.danitheskunk.skunkworks.nodes.NodeRoot;

View File

@ -2,6 +2,7 @@ package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.gfx.*; import com.danitheskunk.skunkworks.gfx.*;
import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
import java.util.List; import java.util.List;

View File

@ -1,10 +1,44 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.gfx.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
import com.danitheskunk.skunkworks.gfx.threedee.Mesh;
import org.lwjgl.assimp.AIMesh;
import org.lwjgl.assimp.Assimp;
public class Test3D extends BaseGame { public class Test3D extends BaseGame {
private Mesh[] meshes;
public Test3D() { public Test3D() {
super(new Vec2i(1920, 1080), "Skunkworks 3d test"); super(new Vec2i(1920, 1080), "Skunkworks 3d test");
System.out.printf("assimp version: %d.%d.%d\n",
Assimp.aiGetVersionMajor(),
Assimp.aiGetVersionMinor(),
Assimp.aiGetVersionPatch()
);
var path = "C:\\stream\\models\\Dani.glb";
var bytes = engine.loadBytes(path);
var flags = Assimp.aiProcess_Triangulate;
flags |= Assimp.aiProcess_OptimizeGraph;
flags |= Assimp.aiProcess_PreTransformVertices;
//flags |= Assimp.aiProcess_OptimizeMeshes;
//flags |= Assimp.aiProcess_Debone;
var ai = Assimp.aiImportFile(path, flags);
if(ai == null) {
throw new RuntimeException("couldn't load model because: " +
Assimp.aiGetErrorString());
}
var numMeshes = ai.mNumMeshes();
var meshespb = ai.mMeshes();
meshes = new Mesh[numMeshes];
int faces = 0;
for(int i = 0; i < numMeshes; ++i) {
var aiMesh = AIMesh.create(meshespb.get(i));
var mesh = Mesh.fromAIMesh(aiMesh);
meshes[i] = mesh;
System.out.println(aiMesh.mNumFaces());
faces += aiMesh.mNumFaces();
}
System.out.printf("numMeshes: %d, faces: %d\n", numMeshes, faces);
} }
static public void main(String[] args) { static public void main(String[] args) {
@ -13,11 +47,15 @@ public class Test3D extends BaseGame {
@Override @Override
protected void render3D(IRenderContext3D rc) { protected void render3D(IRenderContext3D rc) {
rc.renderTriangle(new Vec3f(-10, -10, 0), for(int i = 0; i < meshes.length; ++i) {
new Vec3f(10, -10, 0), rc.renderMesh(meshes[i]);
new Vec3f(10, 10, -1) }
/*
rc.renderTriangle(new Vec3f(-10, -10, 1),
new Vec3f(10, -10, 1),
new Vec3f(10, 10, 1)
); );
*/
} }
} }

View File

@ -0,0 +1,25 @@
package com.danitheskunk.skunkworks;
public class Vec3i {
public static Vec3i ONE = new Vec3i(1, 1, 1);
public static Vec3i ZERO = new Vec3i(0, 0, 0);
private final int x, y, z;
public Vec3i(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
}

View File

@ -1,7 +1,8 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.Vec3f;
import com.danitheskunk.skunkworks.gfx.BaseRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.BaseRenderContext3D;
import com.danitheskunk.skunkworks.gfx.threedee.Mesh;
import static org.lwjgl.opengl.GL46.*; import static org.lwjgl.opengl.GL46.*;
@ -22,4 +23,19 @@ public class RenderContext3D extends BaseRenderContext3D {
glEnd(); glEnd();
} }
@Override
public void renderMesh(Mesh mesh) {
glBegin(GL_TRIANGLES);
for(int i = 0; i < mesh.getTriangleCount(); ++i) {
var tri = mesh.getTriangle(i);
var p1 = mesh.getVertex(tri.getX());
var p2 = mesh.getVertex(tri.getY());
var p3 = mesh.getVertex(tri.getZ());
glVertex3d(p1.getX(), p1.getY(), p1.getZ());
glVertex3d(p2.getX(), p2.getY(), p2.getZ());
glVertex3d(p3.getX(), p3.getY(), p3.getZ());
}
glEnd();
}
} }

View File

@ -2,7 +2,7 @@ package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.*; import com.danitheskunk.skunkworks.*;
import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image; import com.danitheskunk.skunkworks.gfx.Image;
import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWErrorCallback;

View File

@ -1,5 +0,0 @@
package com.danitheskunk.skunkworks.gfx;
abstract public class BaseRenderContext3D implements IRenderContext3D {
}

View File

@ -0,0 +1,7 @@
package com.danitheskunk.skunkworks.gfx.threedee;
import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
abstract public class BaseRenderContext3D implements IRenderContext3D {
}

View File

@ -1,7 +1,8 @@
package com.danitheskunk.skunkworks.gfx; package com.danitheskunk.skunkworks.gfx.threedee;
import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.Vec3f;
public interface IRenderContext3D { public interface IRenderContext3D {
void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3); void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3);
void renderMesh(Mesh mesh);
} }

View File

@ -0,0 +1,69 @@
package com.danitheskunk.skunkworks.gfx.threedee;
import com.danitheskunk.skunkworks.Vec3f;
import com.danitheskunk.skunkworks.Vec3i;
import org.lwjgl.assimp.AIMesh;
import java.util.ArrayList;
import java.util.List;
public class Mesh {
private List<Vec3i> triangles;
private List<Vec3f> vertices;
public Mesh() {
vertices = new ArrayList<>();
triangles = new ArrayList<>();
}
public static Mesh fromAIMesh(AIMesh aiMesh) {
var mesh = new Mesh();
var numVertices = aiMesh.mNumVertices();
var numFaces = aiMesh.mNumFaces();
var vertexBuffer = aiMesh.mVertices();
var faceBuffer = aiMesh.mFaces();
while(vertexBuffer.remaining() > 0) {
var vert = vertexBuffer.get();
mesh.addVertex(new Vec3f(vert.x(), vert.y(), vert.z()));
}
while(faceBuffer.remaining() > 0) {
var face = faceBuffer.get();
if(face.mNumIndices() != 3) {
throw new RuntimeException("skunkworks only supports " +
"triangles currently");
}
var indices = face.mIndices();
mesh.addTriangle(indices.get(0), indices.get(1), indices.get(2));
}
return mesh;
}
public void addTriangle(int v1, int v2, int v3) {
triangles.add(new Vec3i(v1, v2, v3));
}
public void addVertex(Vec3f vertex) {
vertices.add(vertex);
}
public int getTriangleCount() {
return triangles.size();
}
public int getVertexCount() {
return vertices.size();
}
public Vec3f getVertex(int idx) {
return vertices.get(idx);
}
public Vec3i getTriangle(int idx) {
return triangles.get(idx);
}
}