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.
Page Index Toggle Pages: 1
Lights (bis) (Read 323 times)
Lights (bis)
Feb 27th, 2009, 6:56pm
 
Hello.
How can I light the sphere, as the moon in quarter, for example ?
Thank's.


import processing.core.*;
import processing.opengl.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import javax.vecmath.Color3f;
import toxi.geom.Vec3D;

public class Sphere_bis extends JFrame {

 PGraphicsOpenGL pgl;
 GL gl;

 boolean opengl=true;

 int largeur_ecran, hauteur_ecran, x1, y1, z1;
 float debut,x9,y9,z9;

 double alpha, delta, alpha_centre=5.0, delta_centre=0.0;

 sp_bis fenetre;

 float rayon_sphere=200.0f;
 float rotX, rotY, rotZ;

 PImage texImg, fond;

 int NUM_ROWS = 50, NUM_LINES = 50;
 Vec3D p[][]; //bidimensional array of vectors that compose each point in the sphere

 float sphereX[][], sphereY[][], sphereZ[][];

 public static void main(String args[])
  {
  new Sphere_bis().setVisible(true);
  }

 public Sphere_bis()
  {
  setSize(1400,900);

  add(fenetre = new sp_bis());
  fenetre.init();
  }

 class sp_bis extends PApplet {

 float u, theta, v, phi, x, y, z;

public void setup()
{

  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  largeur_ecran=screenSize.width;
  hauteur_ecran=screenSize.height;

 size(largeur_ecran, hauteur_ecran, opengl ? OPENGL: P3D);

 fond=loadImage("fonds/preferes/baviere.jpg");
 texImg = loadImage("textures_planetes/mars501.jpg");

 size(largeur_ecran, hauteur_ecran, OPENGL);
 noStroke();
 
 Sphere();
}

public void draw()
{
 defineLights();
 background(0);

 pgl = (PGraphicsOpenGL) g;

 gl = pgl.beginGL();
 gl.setSwapInterval(1);

 gl.glNormal3f(0.0f,0.0f,-1.0f);

 gl.glEnable(gl.GL_TEXTURE_2D);
 render();
 gl.glDisable(gl.GL_TEXTURE_2D);

pgl.endGL();
}

 public void render(){

  pushMatrix();

  calculer_x_y_3D(alpha_centre,delta_centre);

   gl.glTranslatef(x1, y1, z1);
   gl.glScalef(rayon_sphere, rayon_sphere, rayon_sphere);

   renderSphere();

   popMatrix();
 }

  void calculer_x_y_3D(double alph, double delt)
    {
    float facteur_zoom=1.0f;

    float centreX=(float) (largeur_ecran/2);
    float centreY=(float) (hauteur_ecran/2);
    float centreZ=150.0f+(float) (20.0*facteur_zoom);

    double ct1=180/Math.PI;

    float rayon_sphere_ciel=(float) (largeur_ecran/2*5.0f);

    alph=(alph-alpha_centre+12)*15/ct1;
    delt=-delt/ct1;

    x9 = centreX + rayon_sphere_ciel * (float) (Math.cos(delt)*Math.sin(alph));
    y9 = centreY + rayon_sphere_ciel * (float) (Math.cos(delta_centre)*Math.sin(delt)-Math.sin(delta_centre)*Math.cos(delt)*Mat
h.cos(alph));
    z9 = centreZ + rayon_sphere_ciel * (float) (Math.sin(delta_centre)*Math.sin(delt)+Math.cos(delta_centre)*Math.cos(delt)*Mat
h.cos(alph));

    // Coordonnées 2D pour les clics sur les objets.
    x1=(int) screenX(x9,y9,z9);
    y1=(int) screenY(x9,y9,z9);
    z1=(int) screenZ(x9,y9,z9);
    }

void defineLights() {

 spotLight(255f, 0f, 0f,  // Color
           300f, 400f, 100f,     // Position
           1f, 0.0f, 0-1f,  // Direction
           (float) (PI / 2), 2f);     // Angle, concentration
}

 public void Sphere(){

   p = new Vec3D[NUM_ROWS][NUM_LINES];

   sphereX = new float[NUM_ROWS][NUM_LINES];
   sphereY = new float[NUM_ROWS][NUM_LINES];
   sphereZ = new float[NUM_ROWS][NUM_LINES];

   for(int i=0; i<NUM_ROWS; i++){
     u = (float)i/(NUM_ROWS-1);
     theta = u*TWO_PI;
     for(int j=0; j<NUM_LINES; j++){
       v = (float)j/(NUM_LINES-1);
       phi = PI*v;

       x = cos(theta) * sin(phi);
       y = sin(theta) * sin(phi);
       z = cos(phi);

       p[i][j] = new Vec3D(x, y, z); //normalized vector of one point in the sphere
     
       sphereX[i][j] = x;
       sphereY[i][j] = y;
       sphereZ[i][j] = z;
     }
   }
 }

 public void renderSphere(){
   pgl.bindTexture(texImg);

   gl.glTexEnvf(gl.GL_TEXTURE_ENV,gl.GL_TEXTURE_ENV_MODE,gl.GL_MODULATE);

   float u, uNext;
   float v;
   for(int i=0; i<NUM_ROWS; i++){

   gl.glBegin(gl.GL_TRIANGLE_STRIP);

     int next = (i+1)%NUM_ROWS;
     u = (float)i/(NUM_ROWS-1);
     uNext = (float)next/(NUM_ROWS-1);

     for(int j=0; j<NUM_LINES; j++){
       v = (float)j/(NUM_LINES-1);

       gl.glTexCoord2f(u, v);
       // '-' en x pour afficher correctement la sphère.
       // Car sinon les formations et labels sont inversés de gauche à droite.
       gl.glColor3f(1.0f,0.0f,0.0f);
       gl.glVertex3f(-p[i][j].x, p[i][j].y, p[i][j].z);
       gl.glTexCoord2f(uNext, v);
       gl.glColor3f(1.0f,0.0f,0.0f);
       gl.glVertex3f(-p[next][j].x, p[next][j].y, p[next][j].z);
     }

     gl.glEnd();
   }
 }


}

}
Page Index Toggle Pages: 1