height map mesh
in
Contributed Library Questions
•
2 years ago
hi guys
Im super new to this but im trying to make a mesh terrain model using a hieght map and the un lekker 3dmesh,
here is my code but i cant seem to get it to work can any one help me ?
- import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*; - import processing.opengl.*;
- import unlekker.util.*;
import unlekker.geom.*;
import unlekker.data.*; - // 2-dimensional array of 3D vector objects to contain our mesh
Vec3 mesh[][]; - // num will decide the mesh dimensions
int numX;
int numY; - // use the CTRL key for interface
boolean ctrlDown=false; - // translation the Z direction
float xUnit, yUnit, zTrans;
float rotX, rotY, amplitude; - PImage extrude;
int[][] values;
void setup() {
size(1200, 1000, OPENGL);- // allocate space for the mesh and fill it with empty vectors
numX=200;
numY=200;
mesh=new Vec3[numX][numY];
meshPlane(); - xUnit=width/(float)numX;
yUnit=height/(float)numY; - rotX=30;
rotY=0;
amplitude=200;
zTrans=-width/2;
extrude = loadImage("ystone08.jpg");
extrude.loadPixels();
values = new int[extrude.width][extrude.height];
for (int y = 0; y < extrude.height; y++) {
for (int x = 0; x < extrude.width; x++) {
color pixel = extrude.get(x, y);
values[x][y] = int(brightness(pixel));
}
}
}- void draw() {
background(0);
// translate to center of screen and use mouse movement
// to rotate the scene
translate(width/2, height/2, zTrans);
rotateX(rotX);
rotateY(rotY);
stroke(255, 255, 255, 50);
noFill();
drawMesh();
}
void keyPressed() {
// check for CTRL key
if (key == CODED && keyCode==CONTROL) {
ctrlDown=true;
println("ctrlDown "+ctrlDown);
}
}- void keyReleased() {
// check for CTRL key to see if it has been released
if (key == CODED && keyCode==CONTROL) {
ctrlDown=false;
}
} - void mouseDragged() {
// if CTRL key is down, then adjust amplitude
if (ctrlDown) {
amplitude=((float)mouseY/(float)height)*height;
println("amplitude "+amplitude);
}
// else rotate the viewport
else {
rotX=((float)mouseX/(float)width)*2*PI;
rotY=((float)mouseY/(float)height)*2*PI;
}
}
// drawMesh is a custom method that takes care of drawing the
// mesh using beginShape() / endShape()
void drawMesh() {- for (int i=0; i<numX-1; i++) {
beginShape(QUAD_STRIP);
for (int j=0; j<numY; j++) {
vertex(mesh[j][i].x, mesh[j][i].y * amplitude, mesh[j][i].z);
vertex(mesh[j][i+1].x, mesh[j][i+1].y * amplitude, mesh[j][i+1].z);
}
endShape();
}
} - // sets the mesh to an even grid plane
void meshPlane() { - for (int y = 0; y < extrude.width-1; y++) {
for (int x = 0; x < extrude.height-1; x++) {
mesh[y][x]=new Vec3((x-numX/2)*10, 0, (-values[x][y]));
}
}
}
1