Creating 3D graphics using Java Processing and Matrix Stacks


For my last computer graphics project I created a robot that was assembled (nothing elaborate) and then shot a bullet. I tried to get a demo to run in the browser but was having too much difficulty to get it running. Here is the source file for it:
Source code: hays_nathan_Project_2

There is examples of using moving shapes, using a matrix stack, creating a cylinder, and using a timer to animate and change camera angles.  I hope this comes in handy for someone.

import processing.opengl.*;

float time = 44;  // keep track of passing of time

void setup() {
  size(500, 500, OPENGL);  // sets screen size
  noStroke();     // do not draw the edges of polygons
}

// Draw a scene, gets called every cycle
void draw() {
  println(time);

  resetMatrix();  // set the transformation matrix to the identity (important!)

  background(255);  // clear the screen to black

  // set up for perspective projection
  perspective (PI * 0.333, 1.0, 0.01, 1000.0);

  // place the camera in the scene (just like gluLookAt()) at different times
  beginCamera();
if(time<46)camera (0.0, 0.0, 80.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
if(time>46 && time<65)camera ((time-46)*5, 0.0, 80.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
if(time>65 && time<90)camera (95,  0.0, 80.0 - ((time-65)*5), - ((time-65)*5) , 0.0, -1.0, 0.0, 1.0, 0.0);
if(time>90)camera (95, -( (time-90)*4),-45, -125.00641 , 0.0, -1.0, 0.0, 1.0, 0.0);
f(time>120) println("the end");
endCamera();

  scale (1.0, -1.0, 1.0);  // change to right-handed coordinate system

 /*************************************
********************* Scene
**************************************/

  // create two directional light sources
  lightSpecular(20, 104, 24);
  directionalLight(22, 22, 202, 0.7, 0.7, 1);
  directionalLight(152, 152, 152, 0, 0, -1);
  if(time> 44) directionalLight(2, 132, 142, .8, .5, 0);
  fill(100, 100, 100);       // "fill" sets both diffuse and ambient color (yuck!)
  ambient (50, 50, 50);      // set ambient color
  specular(150, 150, 150);   // set specular color
  shininess(10.0);            // set specular exponent

  //torso
  pushMatrix();
  rotateY(95);
  box(20, 35, 20);
  popMatrix();
  //leg right
  pushMatrix();
  rotate(PI);
  if(time>0 && time<15 )translate(-20, 60-time*3, 0);
if(time>15)translate(-20, 15, 0);
  leg();
  popMatrix();
  //right leg
  pushMatrix();
  rotate(PI);
  if(time>0 && time<15 ) translate(18, 60-time*3, 0);
 if(time>15) translate(18, 15, 0);
  leg();
  popMatrix();

  //rigbht arm
  pushMatrix();
  if(time<15)translate(-55, 10, -3);
if(time>15 && time <29) translate(-55+(time*3.1333-45), 10, -3);
if(time>29 && time<45)translate(-8,10,-3);
if(time >45 && time<46.5) translate(-8+((time-45)*5),10,-3);
 if(time >46.5 && time<48)translate(-0.5-((time-46.5)*5),10,-3);
 if(time>48)translate(-8,10,-3);
  rotate(PI/2);
  gun();
  popMatrix();

  //left arm
  pushMatrix();
  if(time<15)translate(53, 10, -3);
if(time>15 && time <29) translate(53-(time*3-45), 10, -3);
 if(time>29) translate(11,10,-3);
  rotate(-PI/2);
  gun();
  popMatrix();

  //head
  pushMatrix();
  if(time<29)translate(0, 60, 0);
 if(time>29 && time <44) translate(0,60-((time-29)*2.14) , 0);
if(time>44)translate(0, 27.5, 0);  // sets exactly on the torso
  if(time>45 && time<54.5) rotateY(radians(-(time-45)*8));
  if(time>54.5) rotateY(radians(-75));
  head();
  popMatrix();

  //bullet
  pushMatrix();
  if(time<46.5)translate(-130,10,-5); //out of the picture
 if(time>47.5)translate(-30-((time-47.5)*5),10,-5);
  sphere(3.5);
  popMatrix();

  // step forward in time
  time += 0.05;
}

// Draw a cylinder of a given radius, height and number of sides.
// The base is on the y=0 plane, and it extends vertically in the y direction.
void cylinder (float radius, float height, int sides) {
  int i,ii;
  float []c = new float[sides];
  float []s = new float[sides];

  for (i = 0; i < sides; i++) {
    float theta = TWO_PI * i / (float) sides;
    c[i] = cos(theta);
    s[i] = sin(theta);
  }

  // bottom end cap

  normal (0.0, -1.0, 0.0);
  for (i = 0; i < sides; i++) {
    ii = (i+1) % sides;
    beginShape(TRIANGLES);
    vertex (c[ii] * radius, 0.0, s[ii] * radius);
    vertex (c[i] * radius, 0.0, s[i] * radius);
    vertex (0.0, 0.0, 0.0);
    endShape();
  }

  // top end cap

  normal (0.0, 1.0, 0.0);
  for (i = 0; i < sides; i++) {
    ii = (i+1) % sides;
    beginShape(TRIANGLES);
    vertex (c[ii] * radius, height, s[ii] * radius);
    vertex (c[i] * radius, height, s[i] * radius);
    vertex (0.0, height, 0.0);
    endShape();
  }

  // main body of cylinder
  for (i = 0; i < sides; i++) {
    ii = (i+1) % sides;
    beginShape();
    normal (c[i], 0.0, s[i]);
    vertex (c[i] * radius, 0.0, s[i] * radius);
    vertex (c[i] * radius, height, s[i] * radius);
    normal (c[ii], 0.0, s[ii]);
    vertex (c[ii] * radius, height, s[ii] * radius);
    vertex (c[ii] * radius, 0.0, s[ii] * radius);
    endShape(CLOSE);
  }
}

void gun(){  //arms
 cylinder(7.0, 18, 6);   //radius, height, sides
 translate(0, 18, 0);
 cylinder(4.0, 7, 8);   //sets directly on the previous cyclinder
} 

void leg(){
 cylinder(7.0, 15, 6);   //radius, height, sides
 translate(0, 15, 0);
 cylinder(4.0, 7, 8);   //sets directly on the previos cylinder
 translate(0, 7, 0);   //sets directly on the previous cylinder
 box(10, 4, 10);
}
void head(){
 sphere(10);
 translate(-2, 0, 10);
 sphere(1);
 translate(5, 0, 0);
 sphere(1);
 translate(-3, -10, -10);
 cylinder(7.0, 5, 10);
 }

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys