add clamping and tiny offsetting to uv coordinates... rendering tiny bit more correctly

This commit is contained in:
DaniTheSkunk 2022-11-23 22:36:46 +00:00
parent 324a081fc9
commit a62577234e
3 changed files with 42 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image;
import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
@ -10,6 +11,7 @@ import java.nio.IntBuffer;
public class Test3D extends BaseGame {
private Mesh[] meshes;
private ITexture dtex;
public Test3D() {
super(new Vec2i(1920, 1080), "Skunkworks 3d test");
@ -20,9 +22,11 @@ public class Test3D extends BaseGame {
);
var path = "C:\\stream\\models\\Dani.glb";
//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;
flags |= Assimp.aiProcess_OptimizeGraph;
//flags |= Assimp.aiProcess_FixInfacingNormals;
//flags |= Assimp.aiProcess_OptimizeGraph;
flags |= Assimp.aiProcess_PreTransformVertices;
//flags |= Assimp.aiProcess_OptimizeMeshes;
//flags |= Assimp.aiProcess_Debone;
@ -90,6 +94,7 @@ public class Test3D extends BaseGame {
faces += aiMesh.mNumFaces();
}
dtex = meshes[14].getTexture();
//window.setDebug(true);
@ -105,11 +110,17 @@ public class Test3D extends BaseGame {
new Test3D().run();
}
@Override
protected void render(IRenderContext rc) {
//rc.drawTexture(Vec2i.ZERO, dtex);
}
@Override
protected void render3D(IRenderContext3D rc) {
for(int i = 0; i < meshes.length; ++i) {
rc.renderMesh(meshes[i]);
}
//rc.renderMesh(meshes[14]);
/*
rc.renderTriangle(new Vec3f(-10, -10, 1),
new Vec3f(10, -10, 1),

View File

@ -8,8 +8,8 @@ import com.danitheskunk.skunkworks.gfx.threedee.Mesh;
import static org.lwjgl.opengl.GL46.*;
public class RenderContext3D extends BaseRenderContext3D {
private TextureAtlas textureAtlas;
private final int texCoordIndex;
private TextureAtlas textureAtlas;
public RenderContext3D(int texCoordIndex) {
this.texCoordIndex = texCoordIndex;
@ -34,6 +34,26 @@ public class RenderContext3D extends BaseRenderContext3D {
var t1 = mesh.getUV(tri.getX());
var t2 = mesh.getUV(tri.getY());
var t3 = mesh.getUV(tri.getZ());
assert (t1.getX() >= 0.0 && t1.getX() <= 1.0);
assert (t1.getY() >= 0.0 && t1.getY() <= 1.0);
assert (t2.getX() >= 0.0 && t2.getX() <= 1.0);
assert (t2.getY() >= 0.0 && t2.getY() <= 1.0);
assert (t3.getX() >= 0.0 && t3.getX() <= 1.0);
assert (t3.getY() >= 0.0 && t3.getY() <= 1.0);
/*
t1 = new Vec2f(t1.getX() * 0.8 + 0.1,
t1.getY() * 0.8 + 0.1
);
t2 = new Vec2f(t2.getX() * 0.8 + 0.1,
t2.getY() * 0.8 + 0.1
);
t3 = new Vec2f(t3.getX() * 0.8 + 0.1,
t3.getY() * 0.8 + 0.1
);
*/
//t1 = new Vec2f(t1.getX(), 1.0 - t1.getY());
//t2 = new Vec2f(t2.getX(), 1.0 - t2.getY());
//t3 = new Vec2f(t3.getX(), 1.0 - t3.getY());
@ -41,12 +61,15 @@ public class RenderContext3D extends BaseRenderContext3D {
t2 = Vec2f.add(texOff, Vec2f.mul(texMult, t2));
t3 = Vec2f.add(texOff, Vec2f.mul(texMult, t3));
glVertexAttrib2d(texCoordIndex, t1.getX(), t1.getY());
glVertex3d(p1.getX(), p1.getY()-0.5, p1.getZ()+0.5);
glVertex3d(p1.getX(), p1.getY() - 0.5, p1.getZ() + 0.5);
glVertexAttrib2d(texCoordIndex, t2.getX(), t2.getY());
glVertex3d(p2.getX(), p2.getY()-0.5, p2.getZ()+0.5);
glVertex3d(p2.getX(), p2.getY() - 0.5, p2.getZ() + 0.5);
glVertexAttrib2d(texCoordIndex, t3.getX(), t3.getY());
glVertex3d(p3.getX(), p3.getY()-0.5, p3.getZ()+0.5);
glVertex3d(p3.getX(), p3.getY() - 0.5, p3.getZ() + 0.5);
}
glEnd();
}

View File

@ -48,7 +48,9 @@ public class Mesh {
while(texCoordsBuffer.remaining() > 0) {
var uv = texCoordsBuffer.get();
mesh.addUV(new Vec2f(uv.x(), 1.0 - uv.y()));
var x = Math.max(0.0001, Math.min(uv.x(), 0.9999));
var y = Math.max(0.0001, Math.min(uv.y(), 0.9999));
mesh.addUV(new Vec2f(x, 1.0 - y));
}
return mesh;