FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Bugs
   Software Bugs
(Moderator: fry)
   3D not rendering lights
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: 3D not rendering lights  (Read 3062 times)
Artie Kuhn
Guest
Email
3D not rendering lights
« on: Apr 9th, 2004, 11:54pm »

I'm brand-spanking new to Processing, so this could be an issue with my machine, but here goes.
 
I just copied and pasted this code to make a sphere out of the reference:
noStroke();  
lights();  
translate(58, 48, 0);  
sphere(2;  
 
Looks good, right? Problem is that when I hit the play button, all I get is a grey circle on a slightly darker grey background with a grey border around the edges.  
 
It seems like the lights are doing their thing...
 
REAS

WWW
Re: 3D not rendering lights
« Reply #1 on: Apr 10th, 2004, 12:14am »

I believe this is a problem with sphere(), not light. Try a box() example to see the lights work. I'm not certain when sphere() broke, but I'm trying to figure it out.
 
Code:

lights();
translate(58, 48, 0);  
rotateY(0.5);  
box(40);  
 
adbo

adbow WWW
Re: 3D not rendering lights
« Reply #2 on: Apr 10th, 2004, 3:30pm »

I'm having this issue too.
 
// Primitives 3D  
// by REAS <http://www.groupc.net>  
 
size(200, 200);  
lights();  
noStroke();  
 
push();  
translate(47, height/2.6, 0);  
rotateY(0.75);  
box(50);  
pop();  
 
push();  
translate(200, height/2, -10);  
sphere(100);  
pop();  
 
It does seem to be the sphere that is messed up, because box renders fine with lights.
I've tried it out on Windows 2000 and Linux with the same results.
 
mKoser

WWW Email
Re: 3D not rendering lights
« Reply #3 on: Apr 10th, 2004, 4:39pm »

i've got a sneaky suspicion it has to do with toxi's sphere hack which was included from 0068 and on.
 
in 0067 sphere's are rendered with light-shadings, and in the 0068 revisions.txt file, ben announces the new sphere code.
 
"- fix to the sphere() function by toxi (yay toxi!)"
 
+ m
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
toxi_
Guest
Email
Re: 3D not rendering lights
« Reply #4 on: Apr 10th, 2004, 8:52pm »

doh! that really was me, though am still not sure, exactly why it does happens.
 
some background: in order to avoid some multiplies for each of the sphere's vertices, i'd changed the code to always draw a unit sphere, which would be scaled up with the scale command to the radius to be used. though somehow, the lighting calculations seem to be done with the untransformed coordinates (in the range of -1...+1), which probably gets into rounding errors again, though i imagined the lighting equation only to use normal vectors anyway... but now it looks like it doesn't
 
anyway, for the interim, please copy&paste the code below into your sketch and use the xsphere(r) method instead of sphere()...
 
Code:

// [toxi040410] re-replaced scale() with inline multiplies in vertex() calls
// to fix illumination bug with current version
void xsphere(float r) {
  if (g.sphere_detail == 0) {
    sphereDetail(30);
  }
  int sd=g.sphere_detail;
  float[] sx=g.sphereX;
  float[] sy=g.sphereY;
  float[] sz=g.sphereZ;
  int v1,v2;
  push();
  // scale(r); // REMOVED!!!
 
  // 1st ring from south pole
  beginShape(TRIANGLE_STRIP);
  for (int i = 0; i <= sd; i++) {
    v2 = i % sd;
    vertex(0, -r, 0);
    vertex(sx[v2]*r, sy[v2]*r, sz[v2]*r);
  }
  endShape();
 
  // middle rings
  int voff = 0;
  for(int i = 2; i < sd; i++) {
    voff += sd;
    beginShape(TRIANGLE_STRIP);
    for (int j = 0; j <= sd; j++) {
 v1 = voff - sd + j % sd;
 v2 = voff + j % sd;
 vertex(sx[v1]*r, sy[v1]*r, sz[v1]*r);
 vertex(sx[v2]*r, sy[v2]*r, sz[v2]*r);
    }
    endShape();
  }
 
  // add the northern cap
  beginShape(TRIANGLE_STRIP);
  for (int i = 0; i <= sd; i++) {
    v2 = voff + i % sd;
    vertex(0, r, 0);
    vertex(sx[v2]*r, sy[v2]*r, sz[v2]*r);
  }
  endShape();
  pop();
}

 
sorry again & happy easter bunnies!
 
mKoser

WWW Email
Re: 3D not rendering lights
« Reply #5 on: Apr 11th, 2004, 11:53am »

( toxi, no need to apologize... you da man )
 
...just gave it a go, and now it works...  
well done, and happy easter to you too!
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
Artie

WWW
Re: 3D not rendering lights
« Reply #6 on: Apr 12th, 2004, 5:22pm »

Great! I'm just glad I didn't do something wrong!
 
toxi_
Guest
Email
Re: 3D not rendering lights
« Reply #7 on: Apr 13th, 2004, 1:56pm »

here's another generic test case to confirm the issue with the lighting calculations. press mouse button to see the shading problems with small triangles...
 
Code:
void setup() {
  size(256,256);
  lights();
  stroke(255);
}
 
void loop() {
  background(255);
  fill(mouseX*2,0,mouseY*2);
  translate(mouseX,mouseY,-100);
  rotateX(mouseY*0.02);
  rotateY(mouseX*0.02);
  if (mousePressed) {
    // lighting calculation has problems with small
    // faces, the effect of gouraud shading seems zero  
    // possibly because lighting comp. is using untransformed vertices
    // (ie. does not take scaling into account) ?
    scale(100);
    beginShape(TRIANGLE_STRIP);
    vertex(-1,-1,0);
    vertex(1,-1,0);
    vertex(-1,1,0);
    vertex(1,1,0);
  } else {
    // using premultiplied vertices yields correct shading results
    beginShape(TRIANGLE_STRIP);
    vertex(-100,-100,0);
    vertex(100,-100,0);
    vertex(-100,100,0);
    vertex(100,100,0);
  }
  endShape();
}
 
TomC

WWW
Re: 3D not rendering lights
« Reply #8 on: Apr 13th, 2004, 3:08pm »

Not knowing the internals of the 3D engine, I couldn't be sure, but if Processing's lighting models itself on OpenGL then the length of the surface normal is used in lighting calculations.
 
That means that adding:
Code:

normal(0.0,0.0,0.01);

after your first beginShape() at least corrects the intensity of the colour, if not the shading.  
 
You can compensate the other way with a normal(0,0,100), if you like, but unit normals should be used for consistent lighting, I think.
 
It's also worth bearing in mind that lights are scaled in OpenGL along with geometry, so I think that the OpenGL equivalent of the above tests wouldn't give identical results either.
 
 
fry


WWW
Re: 3D not rendering lights
« Reply #9 on: Apr 13th, 2004, 7:37pm »

yeah, i think what we're seeing here is a mix of some horked-up stuff in the lighting calculations and in the sphere code (or maybe just the lighting calc). simong has done a repaired version of the lighting, but it requires signficant internal changes to patch it in, so i'm trying to figure out when/how we can get it in there.
 
toxi, should i be patching xsphere() in instead of sphere()? or is xsphere just a workaround for current lighting bugs?
 
(the problem with the lighting calcs were things like the normals on the shapes being affected by the current transform matrix, instead of properly storing an inverse matrix as the ctm is built.. d'oh)
 
Pages: 1 

« Previous topic | Next topic »