Updated a bunch of opengl examples

This commit is contained in:
codeanticode
2012-01-10 00:11:57 +00:00
parent 77734fb54b
commit 3a98d95654
6 changed files with 23 additions and 11 deletions
@@ -1,5 +1,4 @@
// Status: Renders with issues
// Problem: Lights don't work
// Status: Fixed
/**
* Move Eye.
@@ -1,4 +1,4 @@
// Status: Wrong rendering
// Status: Fixed
/**
* Ortho vs Perspective.
@@ -29,11 +29,11 @@ void draw()
if(mousePressed) {
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(PI * fov / 360.0);
float cameraZ = (height/2.0) / tan(fov/2.0);
perspective(fov, float(width)/float(height),
cameraZ/2.0, cameraZ*2.0);
} else {
ortho(-width/2, width/2, -height/2, height/2, -10, 10);
ortho(0, width, 0, height, -200, +200);
}
translate(width/2, height/2, 0);
@@ -41,4 +41,3 @@ void draw()
rotateY(PI/3);
box(160);
}
@@ -24,7 +24,9 @@ void draw(){
fill(182, 62, 29);
noStroke();
// Add basic light setup
lights();
ambientLight(100, 100, 100);
directionalLight(255, 255, 255, 0, 1, 0);
translate(width/2, height*1.2, -380);
// Tip tower to see inside
rotateX(radians(-45));
@@ -56,4 +58,3 @@ void draw(){
}
}
}
@@ -1,6 +1,7 @@
class Cube {
PVector[] vertices = new PVector[24];
PVector[] normals = new PVector[6];
float w, h, d;
Cube(){ }
@@ -12,36 +13,42 @@ class Cube {
// Cube composed of 6 quads
// Front
normals[0] = new PVector(0, 0, 1);
vertices[0] = new PVector(-w/2, -h/2, d/2);
vertices[1] = new PVector(w/2, -h/2, d/2);
vertices[2] = new PVector(w/2, h/2, d/2);
vertices[3] = new PVector(-w/2, h/2, d/2);
// Left
normals[1] = new PVector(-1, 0, 0);
vertices[4] = new PVector(-w/2, -h/2, d/2);
vertices[5] = new PVector(-w/2, -h/2, -d/2);
vertices[6] = new PVector(-w/2, h/2, -d/2);
vertices[7] = new PVector(-w/2, h/2, d/2);
// Right
normals[2] = new PVector(1, 0, 0);
vertices[8] = new PVector(w/2, -h/2, d/2);
vertices[9] = new PVector(w/2, -h/2, -d/2);
vertices[10] = new PVector(w/2, h/2, -d/2);
vertices[11] = new PVector(w/2, h/2, d/2);
// Back
normals[3] = new PVector(0, 0, -1);
vertices[12] = new PVector(-w/2, -h/2, -d/2);
vertices[13] = new PVector(w/2, -h/2, -d/2);
vertices[14] = new PVector(w/2, h/2, -d/2);
vertices[15] = new PVector(-w/2, h/2, -d/2);
// Top
normals[4] = new PVector(0, 1, 0);
vertices[16] = new PVector(-w/2, -h/2, d/2);
vertices[17] = new PVector(-w/2, -h/2, -d/2);
vertices[18] = new PVector(w/2, -h/2, -d/2);
vertices[19] = new PVector(w/2, -h/2, d/2);
// Bottom
normals[5] = new PVector(0, 1, 0);
vertices[20] = new PVector(-w/2, h/2, d/2);
vertices[21] = new PVector(-w/2, h/2, -d/2);
vertices[22] = new PVector(w/2, h/2, -d/2);
@@ -51,10 +58,11 @@ class Cube {
void create(){
for (int i=0; i<6; i++){
beginShape(QUADS);
normal(normals[i].x, normals[i].y, normals[i].z);
for (int j = 0; j < 4; j++){
vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
}
endShape();
}
}
}
}
@@ -19,7 +19,7 @@ void draw() {
background(0);
float dirY = (mouseY / float(height) - 0.5) * 2;
float dirX = (mouseX / float(width) - 0.5) * 2;
directionalLight(204, 204, 204, -dirX, -dirY, -1);
directionalLight(204, 204, 204, dirX, dirY, -1);
translate(width/2 - 100, height/2, 0);
sphere(80);
translate(200, 0, 0);
@@ -104,36 +104,42 @@ class Cube {
// Front face
beginShape(QUADS);
normal(1, 0, 0);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
// Back face
normal(-1, 0, 0);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
// Left face
normal(0, -1, 0);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
// Right face
normal(0, 1, 0);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
// Top face
normal(0, 0, 1);
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
// Bottom face
normal(0, 0, -1);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
@@ -141,4 +147,3 @@ class Cube {
endShape();
}
}