We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Using light, shading 3d
Page Index Toggle Pages: 1
Using light, shading 3d (Read 11166 times)
Using light, shading 3d
Oct 13th, 2009, 9:24pm
I am always impressed by some 3d images created with processing i see arround. Although they are from different kind of people i always ask myself the same question. How do they get such good render results.

Just for example, this image by marius. How did he get these great shading effekts? Is it just the clever use of the already existing Lights in processing? or are there any tricks i dont know of.


When i start to use Lighs(), directionalLight(), pointligh() etc. i always get bad results. Most of the time it looks really flat, especially when turning of the strokes.

Re: Using light, shading 3d
Reply #1 - Oct 13th, 2009, 10:40pm
There are a lot of surface qualities and light options you can only get through pure openGL...not sure if that's what everyone you mentioned has used, but that's all I can suggest...
here's a decent manual, it's a bit out of date but hasn't steered me wrong yet:
Re: Using light, shading 3d
Reply #2 - Oct 14th, 2009, 6:49am
hmm i actually hope not, but maybe you are right. I just wish i would know.
Re: Using light, shading 3d
Reply #3 - Oct 14th, 2009, 3:58pm
Hi Cedric, I'm glad you think my image has good lighting. But it's nothing facny, just a combination of 3-6 plain old directionalLight() calls.

The direction of the lights is given by slightly randomized vectors to keep things interesting. I usually also have a way to interactively increase or decrease the strength of the light.

You can look at the following sketch for a basic example. Press "l" to reset the lights, "+" and "-" to adjust light strength.

import processing.opengl.*;

PVector obj[],l[];
int col[];
float str;

void setup() {
 size(600,600, OPENGL);

void draw() {

//  lights();
 for(int i=0; i<l.length; i++)
   directionalLight(str,str,str, l[i].x,l[i].y,l[i].z);

 for(int i=0; i<obj.length; i++) {
void initScene() {
 obj=new PVector[(int)random(50,100)];
 col=new int[obj.length];
 for(int i=0; i<obj.length; i++) {
   obj[i]=new PVector(

void initLight() {
   l=new PVector[(int)random(3,7)];
 for(int i=0; i<l.length; i++) {
   l[i]=new PVector(cos(str),0.3,sin(str));

void keyPressed() {
 if(key=='-') str-=5;
 if(key=='+') str+=5;
 if(key=='+' || key=='-') {
   str=constrain(str, 0,255);
   println("Light strength: "+str);
 if(key=='l') initLight();
 if(key==' ') initScene();
Re: Using light, shading 3d
Reply #4 - Oct 14th, 2009, 6:57pm
Thats great, big thanks. That finally shed some light on it...

although i am still wondering where these fine shadows and gradients come from. You can see them especially on the larger yellow boxes in the center http://www.flickr.com/photos/watz/3211272469/sizes/o/in/set-72157612719752007/

when i run your sketch, i get different nice light sets but still. every side of the box still has one color and is not shaded. I am wondering if it has something to do with my OpenGL drivers / VideoCard or if this is just the way it is.


Re: Using light, shading 3d
Reply #5 - Oct 15th, 2009, 1:45am

you might need to define normals for each corner of the quad rather than just once.

(some of this will apply only to raw opengl, can't remember how it applies to processing)

(as an aside, i love the look of flat shading)
Re: Using light, shading 3d
Reply #6 - Oct 15th, 2009, 4:11am
Good to know, i never thought about it.

I have no clue about raw OPENGL but i found this

there are some infos, but i am not sure how to apply it in this case.
i will test it though
Re: Using light, shading 3d
Reply #7 - Jan 3rd, 2010, 1:50pm
Hi Cedric,
normals are the way to go. The TriangleMesh class in toxi.geom.util has a methode to calc the normals for a whole mesh, so you dont have to mess this vector stuff. Test itv with surfaceLib and at works.
Re: Using light, shading 3d
Reply #8 - Jan 3rd, 2010, 2:47pm
sounds good. do you see any difference. any before after images of surfacelib that blow my mind Smiley ?
Re: Using light, shading 3d
Reply #9 - Jan 4th, 2010, 12:00pm
Here it is:
Its really an improvement but I'm a little bit unsure how to implement this into surfaceLib.
Re: Using light, shading 3d
Reply #10 - Jan 4th, 2010, 2:21pm
thats really a nice improvement without changing the meshresolution
Re: Using light, shading 3d
Reply #11 - Jan 6th, 2010, 12:26pm

I figured out the normals stuff, so until now you can have smooth surfaces too. Using the approach of the Trianglemesh class (thanks Karsten) it wasn't that hard. It will be svn today and the jar for processing will come not later than tomorrow. If you wanna try it just use mySurface.useVertexNormals(true);. Thanks Cedric to  put my on this trail.
Re: Using light, shading 3d
Reply #12 - Jun 30th, 2010, 1:31am
Hi everyone !

I'm pretty near the solution, but I cant make it work:

I've got a sketch running with toxiclib's triangleMesh.
I've got another one running with surfaceLib.

They both work fine, and i can use useVertexNormals() with surfaceLib, but I can't figure out how to connect those two.
I understand that it's possible to have a toxiclib's triangleMesh with the nice useVertexNormals from surfaceLib. But how ?!
Does anyone have an example ?

Thanks for your help
Page Index Toggle Pages: 1