 |
Author |
Topic: 3d Landscape Generation from 2 Images (Read 715 times) |
|
madmerv Guest

|
3d Landscape Generation from 2 Images
« on: Nov 12th, 2003, 7:13pm » |
|
// Vector-based Landscape Renderer (Example 2) // by merv <http://www.madmerv.com> // Draw a 3d landscape based on 2 images. // Dimensions must be divisible by 2 (for best results). // Both images must share the same dimensions. // Image1: Color Map // Image2: Height Map // Created 10 November 2003 float xmag, ymag = 0; float newXmag, newYmag = 0; float xyzscale = 1; float delta = 0.01; int xRes = 640; int yRes = 480; int xcount; int ycount; BImage img; BImage bump; float vsize = .075; // distance between vertices void svertex( float x, float y, float z, color c ) { fill( c ); vertex( x*xyzscale, y*xyzscale, z*xyzscale ); } void setup() { size(xRes, yRes); noStroke(); colorMode(RGB, 1); } void loop() { int count; background(0, 0, .2); img = loadImage("marsfacetex.jpg"); bump = loadImage("marsfacebmp.gif"); // img = loadImage("texturesm.gif"); // bump = loadImage("bumpsm.gif"); push(); translate(width/2, height/2, -30); newXmag = mouseX/float(width) * TWO_PI; newYmag = mouseY/float(height) * TWO_PI; float diff = xmag-newXmag; if (abs(diff) > 0.01) { xmag -= diff/4.0; } diff = ymag-newYmag; if (abs(diff) > 0.01) { ymag -= diff/4.0; } rotateX(-ymag); rotateY(-xmag); scale(50); if ((xyzscale >= 3) || (xyzscale <= 0.4)) { delta = -delta; } xyzscale = xyzscale + delta; // beginShape(QUADS); // prism( 0, 0, 0, 1, 1, 1 ); // endShape(); int dimx = img.width; int dimx2 = img.width/2; int dimy = img.height; int dimy2 = img.height/2; color xy1, xy2, xy3, xy4; for ( xcount = -(dimx2); xcount < dimx2-1; xcount=xcount+1 ) { for ( ycount = -(dimy2); ycount < dimy2-1; ycount=ycount+1 ) { beginShape(QUADS); float vheight; // if i use this next line instead of the following, also does not work: // xy2 = red(bump.pixels[(xcount+bump.width/2)+(bump.width*(ycount+bump.height/2))]); xy1 = bump.pixels[(xcount+dimx2)+(dimx*(ycount+dimy2))]; xy2 = bump.pixels[(xcount+1+dimx2)+(dimx*(ycount+dimy2))]; xy3 = bump.pixels[(xcount+dimx2)+(dimx*(ycount+1+dimy2))]; xy4 = bump.pixels[(xcount+1+dimx2)+(dimx*(ycount+1+dimy2))]; vheight = ((xy1 % 256)/32); vheight = min(vsize,vheight); svertex( vsize*xcount, vsize*ycount, vsize*vheight, img.pixels[(xcount+dimx2)+(dimx*(ycount+dimy2))] ); vheight = ((xy3 % 256)/32); vheight = min(vsize,vheight); svertex( vsize*xcount, vsize*ycount + vsize, vsize*vheight, img.pixels[(xcount+dimx2)+(dimx*(ycount+1+dimy2))] ); vheight = ((xy4 % 256)/32); vheight = min(vsize,vheight); svertex( vsize*xcount + vsize, vsize*ycount + vsize, vsize*vheight, img.pixels[(xcount+1+dimx2)+(dimx*(ycount+1+dimy2))] ); vheight = ((xy2 % 256)/32); vheight = min(vsize,vheight); svertex( vsize*xcount + vsize, vsize*ycount, vsize*vheight, img.pixels[(xcount+1+dimx2)+(dimx*(ycount+dimy2))] ); endShape(); } } pop(); }
|
|
|
|
|