// square_obj v.02 elout de kok | 28 september 2003 | http://www.xs4all.nl/~elout/ // this needs a texture-image in your data diretory // and reads an 3D .obj file // obj format; http://www.google.com/search?q=obj+format&ie=UTF-8&oe=UTF-8&hl=nl&lr= // used the free max2obj plug-in http://www.habware.at/duck4.htm // for creating the obj file. // // processing - version.0062 BImage tex1; //texture width/height int texw=128; int texh=128; //------mouse & stuff int mx,my,sx=140,sy=120, middenx,middeny; float xangle,yangle,zangle; float zpos=150; int no_vertex; float [] point_buff = new float[5000]; int pointbuffcounter=0; int no_texvertex; int [] pointtex_buff = new int[5000]; int pointtexbuffcounter=0; int no_faces; int [] face_buff = new int [5000]; int [] texface_buff = new int [5000]; int facebuffcounter=0; // my personal textbuffer -> now max 5000 lines int my_textbuffersize=5000; String [] my_textbuffer = new String[my_textbuffersize]; void setup() { size(400,400); noStroke(); cursor(ARROW); middenx=width/2; middeny=height/2; //load the texture image - from your data folder //tex1 = loadImage("wierdbox.gif"); tex1 = loadImage("star.gif"); //load a textfile - from your data directory //String lines[] = loadStrings("wierdbox.obj"); String lines[] = loadStrings("star.obj"); //println("------------------------------------------------"); //println("--------------------------start-----------------"); //check the 3D data that`s loaded for (int i= 0; i < lines.length; i++) { // println(lines[i]); if (lines[i].equals("")) { //do nothing - an empty line } else { String llist[] = splitStrings(lines[i],' '); // '\t' for (int j= 0; j < llist.length; j++) { // load the vertex information if (llist[j].equals("v")) { float floatlist1[] = splitFloats(llist[j+2]); point_buff[pointbuffcounter]=floatlist1[0]; float floatlist2[] = splitFloats(llist[j+3]); point_buff[pointbuffcounter+1]=floatlist2[0]; float floatlist3[] = splitFloats(llist[j+4]); point_buff[pointbuffcounter+2]=floatlist3[0]; //println("x "+point_buff[pointbuffcounter]+" y "+point_buff[pointbuffcounter+1]+" z "+point_buff[pointbuffcounter+2]); pointbuffcounter=pointbuffcounter+3; } //load texture point information - and convert it to the texture width/height if (llist[j].equals("vt")) { float floatlist4[] = splitFloats(llist[j+2]); pointtex_buff[pointtexbuffcounter]=(int) (floatlist4[0]*(float)texw); //u float floatlist5[] = splitFloats(llist[j+3]); pointtex_buff[pointtexbuffcounter+1]=(int) (texh-(floatlist5[0]*(float)texh)); //v - texture seems to flipped vertical thats why 'texh-' float floatlist6[] = splitFloats(llist[j+4]); pointtex_buff[pointtexbuffcounter+2]=(int) floatlist6[0]; //not used yet //println("u "+pointtex_buff[pointtexbuffcounter]+" v "+pointtex_buff[pointtexbuffcounter+1]+" w "+pointtex_buff[pointtexbuffcounter+2]); pointtexbuffcounter=pointtexbuffcounter+3; } //load face information - note .obj points starts at 1 not 0; if (llist[j].equals("f")) { int intlist1[] = splitInts(llist[j+1],'/'); face_buff[facebuffcounter]=intlist1[0]-1; texface_buff[facebuffcounter]=intlist1[1]-1; int intlist2[] = splitInts(llist[j+2],'/'); face_buff[facebuffcounter+1]=intlist2[0]-1; texface_buff[facebuffcounter+1]=intlist2[1]-1; int intlist3[] = splitInts(llist[j+3],'/'); face_buff[facebuffcounter+2]=intlist3[0]-1; texface_buff[facebuffcounter+2]=intlist3[1]-1; //println("a "+face_buff[facebuffcounter]+" b "+face_buff[facebuffcounter+1]+" c "+face_buff[facebuffcounter+2]); //println("ta "+texface_buff[facebuffcounter]+" tb "+texface_buff[facebuffcounter+1]+" tc "+texface_buff[facebuffcounter+2]); facebuffcounter=facebuffcounter+3; no_faces++; } } } } //println("total vertex "+pointbuffcounter/3); //println("total texture vertex "+pointtexbuffcounter/3); //println("total faces "+facebuffcounter/3); } void loop() { background(0,0,0); //zoom in/out using arrow keys if(keyPressed) { if (key == UP){ zpos=zpos+20.0f;} if (key == DOWN){ zpos=zpos-20.0f;} } // set the position to the middle of your screen translate(middenx, middeny, zpos); //check mouse for rotation mx=mouseX; my=mouseY; if ( mx > middenx){sx=0-((middenx-mx)/2);} else{sx=((mx-middenx)/2);} if ( my > middeny){sy=0-((middeny-my)/2);} else{sy=((my-middeny)/2);} //xangle= xangle+(sy/2000.0f); //yangle= yangle+((sx-sy)/2000.0f); zangle= zangle+(sx/400.0f); //rotateX(xangle); rotateX(1.6+(-sy/80.0)); //rotateX(1.6); rotateZ(zangle); //rotateZ(1.0+(sx/60.0)); //draw the model beginShape(TRIANGLES); texture(tex1); for (int i=0;i