Skip to content

Commit

Permalink
less memory thrashing
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Dec 30, 2024
1 parent cbda01e commit de1918c
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions src/main/java/com/marginallyclever/ro3/mesh/Mesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class Mesh {

private final EventListenerList listeners = new EventListenerList();

private VertexProvider vertexProvider;

public Mesh() {
super();
boundingBox.setShape(this);
Expand Down Expand Up @@ -162,6 +164,42 @@ private void updateBuffers(GL3 gl) {
}

gl.glBindVertexArray(0);

createNewVertexProvider();
}

private void createNewVertexProvider() {
if (hasIndexes) {
vertexProvider = new VertexProvider() {
@Override
public Vector3d provideVertex(int index) {
return getVertex(indexArray.get(index));
}
@Override
public Vector3d provideNormal(int index) {
return getNormal(indexArray.get(index));
}
@Override
public int provideCount() {
return indexArray.size();
}
};
} else {
vertexProvider = new VertexProvider() {
@Override
public Vector3d provideVertex(int index) {
return getVertex(index);
}
@Override
public Vector3d provideNormal(int index) {
return getNormal(index);
}
@Override
public int provideCount() {
return getNumVertices();
}
};
}
}

private void checkBufferSizes() {
Expand Down Expand Up @@ -393,40 +431,10 @@ public RayHit intersect(Ray ray) {
return null; // no hit
}

VertexProvider vp;
if (hasIndexes) {
vp = new VertexProvider() {
@Override
public Vector3d provideVertex(int index) {
return getVertex(indexArray.get(index));
}
@Override
public Vector3d provideNormal(int index) {
return getNormal(indexArray.get(index));
}
@Override
public int provideCount() {
return indexArray.size();
}
};
} else {
vp = new VertexProvider() {
@Override
public Vector3d provideVertex(int index) {
return getVertex(index);
}
@Override
public Vector3d provideNormal(int index) {
return getNormal(index);
}
@Override
public int provideCount() {
return getNumVertices();
}
};
if(vertexProvider==null) {
createNewVertexProvider();
}

return intersect(ray,vp);
return intersect(ray, vertexProvider);
}


Expand All @@ -437,7 +445,7 @@ public int provideCount() {
* @return null if no intersection, otherwise a RayHit object with the intersection point and normal.
*/
private RayHit intersect(Ray ray, VertexProvider provider) {
int a=0,b=0,c=0;
int a=0;

double nearest = Double.MAX_VALUE;
for(int i=0;i<provider.provideCount();i+=3) {
Expand All @@ -448,22 +456,22 @@ private RayHit intersect(Ray ray, VertexProvider provider) {
if(nearest > t) {
nearest = t;
a=i;
b=i+1;
c=i+2;
}
}

if(nearest<ray.getMaxDistance()) {
Vector3d normal;
if(hasNormals) {
normal = provider.provideNormal(a);
normal.add(provider.provideNormal(b));
normal.add(provider.provideNormal(c));
normal.add(provider.provideNormal(a+1));
normal.add(provider.provideNormal(a+2));
// average of normals
normal.normalize();
} else {
Vector3d v0 = provider.provideVertex(a);
Vector3d v1 = provider.provideVertex(b);
Vector3d v2 = provider.provideVertex(c);
Vector3d v1 = provider.provideVertex(a+1);
Vector3d v2 = provider.provideVertex(a+2);
// normal from face
normal = IntersectionHelper.buildNormalFrom3Points(v0, v1, v2);
}
Point3d p = new Point3d(ray.getDirection());
Expand Down

0 comments on commit de1918c

Please sign in to comment.