TriangleMesh and GLModel problems
in
Contributed Library Questions
•
2 years ago
Hello,
I've been trying to use the excellent toxiclibs TriangleMesh with the GLModel from GLGraphics, which I understand will be a part of Processing in 2.0. I can't get it to work properly, unfortunately. I have the TriangleMesh drawing perfectly, but I'm unsure how to send its data to the GLModel correctly. There are a number of functions, and my first choice was to simply use:
I've been trying to use the excellent toxiclibs TriangleMesh with the GLModel from GLGraphics, which I understand will be a part of Processing in 2.0. I can't get it to work properly, unfortunately. I have the TriangleMesh drawing perfectly, but I'm unsure how to send its data to the GLModel correctly. There are a number of functions, and my first choice was to simply use:
- // get mesh as vertex array with stride 4
- float[] triVerts = mesh.getMeshAsVertexArray(null, 0,4);
- // why does this not work?
model.updateVertices(triVerts);
- // TriMesh GLModel Test by Evan Raskob
- // Based on Swarming points using sprite textures
// By Andres Colubri and the MeshDoodle sketch by Karsten Schmidt - // requires GLGraphics and toxiclibs
import processing.opengl.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import codeanticode.glgraphics.*;
import toxi.geom.*;
import toxi.geom.mesh.*;
import toxi.math.*;
import java.nio.FloatBuffer;
TriangleMesh triMesh;
Vec3D prev=new Vec3D();
Vec3D p=new Vec3D();
Vec3D q=new Vec3D();
Vec2D rotation=new Vec2D();
boolean mouseWasDown = false;
float MIN_DIST = 7.0f;
float weight=0;
ArrayList<GLModel> models;
GLTexture tex;
float[] coords;
float[] colors;
void setup()
{
size(640, 480, GLConstants.GLGRAPHICS);
GL gl;
PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change
gl = pgl.beginGL(); // always use the GL object returned by beginGL
gl.setSwapInterval( 1 ); // use value 0 to disable v-sync
pgl.endGL();
models = new ArrayList<GLModel>();
triMesh =new TriangleMesh("mesh1");
// any particle texture... small is better
tex = new GLTexture(this, "whitetoady.png");
}
void draw()
{
background(0);
// rotate around center of screen (accounted for in mouseDragged() function)
translate(width/2, height/2, 0);
rotateX(rotation.x);
rotateY(rotation.y);
// draw mesh as polygon (in white)
drawMesh();
// draw mesh unique points only (in green)
drawMeshUniqueVerts();
// now models
for (GLModel model : models)
{
model.render();
}
// udpate rotation
rotation.addSelf(0.014, 0.0237);
}
GLModel makeModel(TriangleMesh mesh)
{
// get mesh as vertex array with stride 4
//float[] triVerts = mesh.getMeshAsVertexArray(null, 0,4);
// why does this not work?
// model.updateVertices(triVerts);
// get unique x,y,z vertices, use with indices
float[] triVerts = mesh.getUniqueVerticesAsArray();
println("Got " + triVerts.length + " verts");
int[] faces = mesh.getFacesAsArray();
GLModel model = new GLModel(this, triVerts.length/3, GLModel.POINT_SPRITES, GLModel.DYNAMIC);
// GLModel model = new GLModel(this, triVerts.length/3, GLModel.TRIANGLES, GLModel.DYNAMIC);
//GLModel model = new GLModel(this, triVerts.length/3, GLModel.POINTS, GLModel.DYNAMIC);
model.initColors();
// TESTING - MAKE SURE WE HAVE CORRECT VERTS
model.beginUpdateVertices();
for (int n = 0; n < triVerts.length/3; n+=3)
{
model.updateVertex(n, triVerts[n], triVerts[n+1], triVerts[n+2]);
}
model.endUpdateVertices();
model.initIndices(faces.length);
model.updateIndices(mesh.getFacesAsArray());
//
// for (int n=0; n<triVerts.length; n += 4)
// {
// println("TRIVERT["+n+"]="+ triVerts[n] +","+triVerts[n+1] +","+triVerts[n+2]);
// }
//
// Handle colors
//
model.beginUpdateColors();
FloatBuffer cbuf = model.colors;
float col[] = {
0, 0, 0, 0
};
for (int n = 0; n < model.getSize(); ++n) {
// get colors (debugging purposes)
cbuf.position(4 * n);
cbuf.get(col, 0, 4);
//println("Color["+n+"]="+ col[0] +","+col[1] +","+col[2] +","+col[3]);
// process col... make opaque white for testing
col[0] = col[1] = col[2] = col[3] = 1.0f;
cbuf.position(4 * n);
cbuf.put(col, 0, 4);
}
cbuf.rewind();
model.endUpdateColors();
float pmax = model.getMaxPointSize();
//println("Maximum sprite size supported by the video card: " + pmax + " pixels.");
model.initTextures(1);
model.setTexture(0, tex);
// Setting the maximum sprite to the 90% of the maximum point size.
model.setMaxSpriteSize(0.9 * pmax);
// Setting the distance attenuation function so that the sprite size
// is 20 when the distance to the camera is 400.
model.setSpriteSize(20, 400);
model.setPointSize(10);
model.setBlendMode(BLEND);
return model;
}
void vertex(Vec3D v) {
vertex(v.x, v.y, v.z);
}
void mouseReleased()
{
// MAKE A MODEL FROM CURRENT TRI MESH
models.add( makeModel (triMesh) );
// clear tri mesh
triMesh.clear();
}
void mousePressed()
{
}
void mouseDragged()
{
// get 3D rotated mouse position
Vec3D pos=new Vec3D(mouseX-width/2, mouseY-height/2, 0);
pos.rotateX(rotation.x);
pos.rotateY(rotation.y);
// use distance to previous point as target stroke weight
weight+=(sqrt(pos.distanceTo(prev))*2-weight)*0.1;
// define offset points for the triangle strip
//println("weight " + weight);
//if (weight < MIN_DIST && triMeshes.size() > 0)
if (true)
{
Vec3D a=pos.add(0, 0, weight);
Vec3D b=pos.add(0, 0, -weight);
// add 2 faces to the mesh
triMesh.addFace(p, b, q);
triMesh.addFace(p, a, b);
// store current points for next iteration
prev=pos;
p=a;
q=b;
}
}
void drawMesh() {
noStroke();
fill(255,80);
beginShape(TRIANGLES);
// iterate over all faces/triangles of the mesh
for (Iterator i=triMesh.faces.iterator(); i.hasNext();) {
TriangleMesh.Face f=(TriangleMesh.Face)i.next();
// create vertices for each corner point
vertex(f.a);
vertex(f.b);
vertex(f.c);
}
endShape();
}
void drawMeshUniqueVerts() {
// noStroke();
stroke(0,255,0);
strokeWeight(4);
beginShape(POINTS);
// get unique vertices, use with indices
float[] triVerts = triMesh.getUniqueVerticesAsArray();
for (int i=0; i < triVerts.length; i += 3)
{
vertex(triVerts[i], triVerts[i+1], triVerts[i+2]);
}
endShape();
}
void keyPressed()
{
switch(key)
{
case 'x':
//mesh.saveAsOBJ(sketchPath("doodle.obj"));
//mesh.saveAsSTL(sketchPath("doodle.stl"));
break;
case ' ':
// now models
for (GLModel model : models)
{
model.delete();
}
models.clear();
break;
}
}
1