I summon you, oh Opengl Gurus

I thought I nailed down the few bugs that were prohibiting me from getting per-pixel lighting to work properly, but I can’t help but notice that I failed miserably as per times, lighting changes direction all of a sudden (well when the light source goes outside the quad limits) and that for sure bugs the hell outta me. >:(
So can you please help me out?
Here’s the source
http://www.realityflux.com/abba/PPL.zip

PS: I’ll only post how I find out the space tangent per vertex seeing that make_room.java alone exceeds 300 lines of code :frowning:


  public static class plane{

    jcd_math.matrix4f tangent_matrix[];

    float vertices[][]  = new float[4][5],
          normal[]      = new float[3],
          texture_w,
          texture_h;

    int   texture_dot3,
          texture_base,
          counter_clock_map[] = {0,1,2,
                                 1,2,0,
                                 2,0,1,
                                 3,2,1};

    plane(jcd_math.tuple3f[] points,jcd_math.tuple3f vector,
          float w, float h, int texture_base,int texture_dot3){

      this.texture_dot3 = texture_dot3;
      this.texture_base = texture_base;
      normal[0]         = vector.x;
      normal[1]         = vector.y;
      normal[2]         = vector.z;
      texture_w         = w;
      texture_h         = h;

      float texture_s_t[] = {        0,        0,
                             texture_w,        0,
                                     0,texture_h,
                             texture_w,texture_h};

      for(int i =0, a = 0; i<4; i++, a+=2){

        vertices[i][0] = points[i].x;
        vertices[i][1] = points[i].y;
        vertices[i][2] = points[i].z;
        vertices[i][3] = texture_s_t[  a];
        vertices[i][4] = texture_s_t[a+1];
      }
      create_tagent_matrices();
    }

  /**********************************************************************************************/
  /*                                                                                            */
  /*                 http://members.rogers.com/deseric/tangentspace.htm..    .                  */
  /*                                                                                            */
  /**********************************************************************************************/

    void create_tagent_matrices(){

      tangent_matrix = new jcd_math.matrix4f[4];

      jcd_math.tuple3f p_0   = new jcd_math.tuple3f(),
                       p_1   = new jcd_math.tuple3f(),
                       p_2   = new jcd_math.tuple3f(),
                       v_1   = new jcd_math.tuple3f(),
                       v_2   = new jcd_math.tuple3f(),
                       nor[] = new jcd_math.tuple3f[3];

      for(int i=0, index =0; i<tangent_matrix.length; i++, index+=3){

        tangent_matrix[i] = new jcd_math.matrix4f();
        tangent_matrix[i].load_identity();

        for(int n= 0; n<3; n++){

          p_0.set(vertices[counter_clock_map[index+0]][n],vertices[counter_clock_map[index+0]][3],vertices[counter_clock_map[index+0]][4]);
          p_1.set(vertices[counter_clock_map[index+1]][n],vertices[counter_clock_map[index+1]][3],vertices[counter_clock_map[index+1]][4]);
          p_2.set(vertices[counter_clock_map[index+2]][n],vertices[counter_clock_map[index+2]][3],vertices[counter_clock_map[index+2]][4]);
          v_1.sub(p_1,p_0);
          v_2.sub(p_2,p_0);
          nor[n] = new jcd_math.tuple3f();
          nor[n].cross(v_1,v_2);
          nor[n].normalize();
        }

        tangent_matrix[i].elements[0] =  -nor[0].y/nor[0].x;
        tangent_matrix[i].elements[4] =  -nor[1].y/nor[1].x;
        tangent_matrix[i].elements[8] =  -nor[2].y/nor[2].x;

        tangent_matrix[i].elements[1] =  -nor[0].z/nor[0].x;
        tangent_matrix[i].elements[5] =  -nor[1].z/nor[1].x;
        tangent_matrix[i].elements[9] =  -nor[2].z/nor[2].x;

        tangent_matrix[i].elements[ 2] =  normal[0];
        tangent_matrix[i].elements[ 6] =  normal[1];
        tangent_matrix[i].elements[10] =  normal[2];
      }
    }
  }

Yesssssssssssssss fixed it ;D
Woah that was probably one of the toughest coding I had to do given the number of bugs I ran into, but I guess it’s fine now that it’s running correctly

Here’s the part that made me contemplate suicide several times

jcd_math.matrix4f tangent_matrix[];

float vertices[][]  = new float[4][5],
      normal[]      = new float[3],
      texture_w,
      texture_h;

int   texture_dot3,
      texture_base,
      counter_clock[] = {0,1,2,
                         3,2,1};

plane(jcd_math.tuple3f[] points,jcd_math.tuple3f vector,
      float w, float h, int texture_base,int texture_dot3){

  this.texture_dot3 = texture_dot3;
  this.texture_base = texture_base;
  normal[0]         = vector.x;
  normal[1]         = vector.y;
  normal[2]         = vector.z;
  texture_w         = w;
  texture_h         = h;

  float texture_s_t[] = {        0,        0,
                         texture_w,        0,
                                 0,texture_h,
                         texture_w,texture_h};

  for(int i =0, a = 0; i<4; i++, a+=2){

    vertices[i][0] = points[i].x;
    vertices[i][1] = points[i].y;
    vertices[i][2] = points[i].z;
    vertices[i][3] = texture_s_t[  a];
    vertices[i][4] = texture_s_t[a+1];
  }
  create_tagent_matrices();
}

// http://tfpsly.planet-d.net/english/3d/pplight_bump.html

void create_tagent_matrices(){

  tangent_matrix = new jcd_math.matrix4f[4];
  for(int i = 0; i<tangent_matrix.length; i++)
    tangent_matrix[i] = new jcd_math.matrix4f();

  for(int i = 0, index =0; i<2; i++, index+=3){

     jcd_math.tuple3f v_1 = new jcd_math.tuple3f(),
                      v_2 = new jcd_math.tuple3f(),
                      crs = new jcd_math.tuple3f();

     for(int xyz =0, row =0; xyz<3; xyz++, row+=4){

       v_1.set(vertices[counter_clock[index+1]][xyz]-vertices[counter_clock[index+0]][xyz],
               vertices[counter_clock[index+1]][  3]-vertices[counter_clock[index+0]][  3],
               vertices[counter_clock[index+1]][  4]-vertices[counter_clock[index+0]][  4]);

       v_2.set(vertices[counter_clock[index+2]][xyz]-vertices[counter_clock[index+0]][xyz],
               vertices[counter_clock[index+2]][  3]-vertices[counter_clock[index+0]][  3],
               vertices[counter_clock[index+2]][  4]-vertices[counter_clock[index+0]][  4]);
       crs.cross(v_1,v_2);

       if(crs.x!=0){
         crs.normalize();
         tangent_matrix[counter_clock[index+0]].elements[0+row]+= -crs.y/crs.x;
         tangent_matrix[counter_clock[index+0]].elements[1+row]+= -crs.z/crs.x;
         tangent_matrix[counter_clock[index+1]].elements[0+row]+= -crs.y/crs.x;
         tangent_matrix[counter_clock[index+1]].elements[1+row]+= -crs.z/crs.x;
         tangent_matrix[counter_clock[index+2]].elements[0+row]+= -crs.y/crs.x;
         tangent_matrix[counter_clock[index+2]].elements[1+row]+= -crs.z/crs.x;
       }
     }
   }

   for(int i = 0; i<4; i++){
     jcd_math.tuple3f tangent    = new jcd_math.tuple3f( tangent_matrix[i].elements[0],
                                                         tangent_matrix[i].elements[4],
                                                         tangent_matrix[i].elements[8]),
                      binormal   = new jcd_math.tuple3f(-tangent_matrix[i].elements[1],
                                                        -tangent_matrix[i].elements[5], 
                                                        -tangent_matrix[i].elements[9]),
                      new_normal = new jcd_math.tuple3f();
     tangent.normalize();
     binormal.normalize();
     new_normal.cross(tangent,binormal);
     if(new_normal.Dot(new jcd_math.tuple3f(normal[0],normal[1],normal[2]))< 0)
       new_normal.negate();

     tangent_matrix[i] = new jcd_math.matrix4f(tangent,binormal,new_normal,false);
   }
 }

}

That’s it I’m scared for life of anything that sounds close to “binormal” ::slight_smile:

http://www.realityflux.com/abba/PPL.zip

Wow… surprised you didnt… just looking g at it nearly gave me a seizure.