Difference between revisions of "FSOSS 2010/processing.js/texture1"
Line 3: | Line 3: | ||
FSOSS 2010 | FSOSS 2010 | ||
Andor Salga | Andor Salga | ||
+ | Example of texture | ||
*/ | */ | ||
import processing.opengl.*; | import processing.opengl.*; | ||
+ | // Preload the image using a Pjs directive | ||
/* @pjs preload="processing.png"; */ | /* @pjs preload="processing.png"; */ | ||
− | + | ||
PImage crateImage; | PImage crateImage; | ||
Line 14: | Line 16: | ||
size(400, 400, OPENGL); | size(400, 400, OPENGL); | ||
crateImage = loadImage("processing.png"); | crateImage = loadImage("processing.png"); | ||
+ | |||
+ | // We're going to specify our texture coordinates using | ||
+ | // normalized values (0..1) rather than image coordinates | ||
textureMode(NORMALIZED); | textureMode(NORMALIZED); | ||
} | } | ||
− | + | ||
− | + | /* | |
+ | Draw a box using a specific Pimage | ||
+ | */ | ||
void drawBox(PImage img){ | void drawBox(PImage img){ | ||
background(200); | background(200); | ||
Line 31: | Line 38: | ||
vertex( 1, 1, -1, 0, 1); | vertex( 1, 1, -1, 0, 1); | ||
+ | // +Z "front" face | ||
vertex(-1, -1, 1, 0, 0); | vertex(-1, -1, 1, 0, 0); | ||
vertex( 1, -1, 1, 1, 0); | vertex( 1, -1, 1, 1, 0); | ||
Line 62: | Line 70: | ||
endShape(); | endShape(); | ||
} | } | ||
− | |||
void draw() | void draw() | ||
Line 69: | Line 76: | ||
stroke(255); | stroke(255); | ||
− | // center in the canvas | + | // center in the canvas, bring towards the camera |
translate(width/2, height/2, 150); | translate(width/2, height/2, 150); | ||
+ | // convert 45 degrees into radians and | ||
+ | // rotate by that amount | ||
rotateY(radians(45)); | rotateY(radians(45)); | ||
rotateX( frameCount/250.0f ); | rotateX( frameCount/250.0f ); | ||
+ | // make the box a bit larger | ||
scale(50); | scale(50); | ||
Latest revision as of 13:56, 27 October 2010
/*
FSOSS 2010
Andor Salga
Example of texture
*/
import processing.opengl.*;
// Preload the image using a Pjs directive
/* @pjs preload="processing.png"; */
PImage crateImage;
void setup()
{
size(400, 400, OPENGL);
crateImage = loadImage("processing.png");
// We're going to specify our texture coordinates using
// normalized values (0..1) rather than image coordinates
textureMode(NORMALIZED);
}
/*
Draw a box using a specific Pimage
*/
void drawBox(PImage img){
background(200);
beginShape(QUADS);
texture(img);
// -Z "back" face
vertex( 1, -1, -1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(-1, 1, -1, 1, 1);
vertex( 1, 1, -1, 0, 1);
// +Z "front" face
vertex(-1, -1, 1, 0, 0);
vertex( 1, -1, 1, 1, 0);
vertex( 1, 1, 1, 1, 1);
vertex(-1, 1, 1, 0, 1);
// +Y "bottom" face
vertex(-1, 1, 1, 0, 0);
vertex( 1, 1, 1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex(-1, 1, -1, 0, 1);
// -Y "top" face
vertex(-1, -1, -1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, -1, 1, 1, 1);
vertex(-1, -1, 1, 0, 1);
// +X "right" face
vertex( 1, -1, 1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex( 1, 1, 1, 0, 1);
// -X "left" face
vertex(-1, -1, -1, 0, 0);
vertex(-1, -1, 1, 1, 0);
vertex(-1, 1, 1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
}
void draw()
{
background(0);
stroke(255);
// center in the canvas, bring towards the camera
translate(width/2, height/2, 150);
// convert 45 degrees into radians and
// rotate by that amount
rotateY(radians(45));
rotateX( frameCount/250.0f );
// make the box a bit larger
scale(50);
drawBox(crateImage);
}