예제 #1
0
 @Override
 protected void serialize(int i, Vector3f store) {
   int j = i * getTupleSize();
   array[j] = store.getX();
   array[j + 1] = store.getY();
   array[j + 2] = store.getZ();
 }
  @Override
  public void update(float tpf) {

    if (forward ^ backward) {
      //            float yCoord = charControl.getRigidBody().getLinearVelocity().getY();
      Vector3f charLocation = charControl.getPhysicsLocation();
      Vector3f walkDir =
          charLocation
              .subtract(app.getCamera().getLocation().clone().setY(charLocation.getY()))
              .normalizeLocal();
      if (backward) {
        walkDir.negateLocal();
      }

      charControl.setWalkDirection(walkDir);
      charControl.setMove(true);
    } else {
      charControl.setMove(false);
    }

    if (jump) {
      charControl.setJump();
      jump = false;
    }
    Vector3f camdir = app.getCamera().getDirection().clone();
    Quaternion newRot = new Quaternion();
    newRot.lookAt(camdir.setY(0), Vector3f.UNIT_Y);
    charControl.setRotationInUpdate(newRot);
  }
 /**
  * Warp into the scene on the nav mesh, finding the nearest cell. The currentCell and position3d
  * are updated and this new position is returned. This updates the state of the path finder!
  *
  * @return the new position in the nearest cell
  */
 public Vector3f warp(Vector3f newPos) {
   Vector3f newPos2d = new Vector3f(newPos.x, 0, newPos.z);
   currentCell = navMesh.findClosestCell(newPos2d);
   currentPos3d.set(navMesh.snapPointToCell(currentCell, newPos2d));
   currentPos3d.setY(newPos.getY());
   currentPos.set(currentPos3d.getX(), currentPos3d.getZ());
   return currentPos3d;
 }
예제 #4
0
파일: World.java 프로젝트: Sindusk/polarity
 public Wall(Vector3f start, float xi, float zi, int spaces) {
   float x = start.getX();
   float z = start.getZ();
   float xl = 0;
   float zl = 0;
   Vector3f loc = new Vector3f(0, 0, 0);
   int i = 0;
   ends[0] = start.clone();
   while (i < spaces) {
     loc = new Vector3f(x + (i * xi), start.getY(), z + (i * zi));
     if (world.get(loc) != null
         && (world.get(loc).contains("h") || world.get(loc).contains("w"))) {
       break;
     } else {
       world.put(loc, "w");
     }
     xl += xi;
     zl += zi;
     i++;
   }
   i--;
   if (i > 0) {
     spaces -= i;
   } else {
     spaces -= 1;
   }
   if (spaces > 0) {
     walls.add(new Wall(loc.clone().add(xi, 0, zi), xi, zi, spaces));
   }
   if (xl == 0 && zl == 0) {
     return;
   }
   loc = new Vector3f(x + ((i / 2f) * xi), start.getY(), z + ((i / 2f) * zi));
   map.add(
       geoWall(
           loc,
           xl,
           zl,
           T.getMaterialPath("synthetic"),
           new Vector2f(Math.max(FastMath.abs(xl), FastMath.abs(zl)), 2),
           true));
 }
예제 #5
0
 private void lockToSight() {
   Ship target = host.getTarget();
   if (target != null) {
     setVisible(true);
     float speed = host.getAverageCannonSpeed();
     Vector3f del = host.leadTargetLocation(target, speed);
     // get screen coordinates
     Vector3f sLoc = camera.getScreenCoordinates(del);
     // set position
     setX((int) sLoc.getX() - width / 2);
     setY((int) sLoc.getY() - height / 2);
   } else {
     setVisible(false);
   }
 }
  public Vector3f onMove(Vector3f moveVec) {
    if (moveVec.equals(Vector3f.ZERO)) {
      return currentPos3d;
    }

    Vector3f newPos2d = new Vector3f(currentPos3d);
    newPos2d.addLocal(moveVec);
    newPos2d.setY(0);

    Vector3f currentPos2d = new Vector3f(currentPos3d);
    currentPos2d.setY(0);

    // Cell nextCell = navMesh.resolveMotionOnMesh(currentPos2d, currentCell, newPos2d, newPos2d);
    // currentCell = nextCell;
    newPos2d.setY(currentPos3d.getY());
    return newPos2d;
  }
예제 #7
0
  private static void convertNormals(FloatBuffer input, ByteBuffer output) {
    if (output.capacity() < input.capacity())
      throw new RuntimeException("Output must be at least as large as input!");

    input.clear();
    output.clear();
    Vector3f temp = new Vector3f();
    int vertexCount = input.capacity() / 3;
    for (int i = 0; i < vertexCount; i++) {
      BufferUtils.populateFromBuffer(temp, input, i);

      // offset and scale vector into -128 ... 127
      temp.multLocal(127).addLocal(0.5f, 0.5f, 0.5f);

      // quantize
      byte v1 = (byte) temp.getX();
      byte v2 = (byte) temp.getY();
      byte v3 = (byte) temp.getZ();

      // store
      output.put(v1).put(v2).put(v3);
    }
  }
  @Override
  public void initParticleData(Emitter emitter, int numParticles) {
    setMode(Mode.Triangles);

    this.emitter = emitter;

    this.finVerts = BufferUtils.createFloatBuffer(templateVerts.capacity() * numParticles);
    try {
      this.finCoords = BufferUtils.createFloatBuffer(templateCoords.capacity() * numParticles);
    } catch (Exception e) {
    }
    this.finIndexes = BufferUtils.createShortBuffer(templateIndexes.size() * numParticles);
    this.finNormals = BufferUtils.createFloatBuffer(templateNormals.capacity() * numParticles);
    this.finColors = BufferUtils.createFloatBuffer(templateVerts.capacity() / 3 * 4 * numParticles);

    int index = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0;
    int indexOffset = 0;

    for (int i = 0; i < numParticles; i++) {
      templateVerts.rewind();
      for (int v = 0; v < templateVerts.capacity(); v += 3) {
        tempV3.set(templateVerts.get(v), templateVerts.get(v + 1), templateVerts.get(v + 2));
        finVerts.put(index, tempV3.getX());
        index++;
        finVerts.put(index, tempV3.getY());
        index++;
        finVerts.put(index, tempV3.getZ());
        index++;
      }
      try {
        templateCoords.rewind();
        for (int v = 0; v < templateCoords.capacity(); v++) {
          finCoords.put(index2, templateCoords.get(v));
          index2++;
        }
      } catch (Exception e) {
      }
      for (int v = 0; v < templateIndexes.size(); v++) {
        finIndexes.put(index3, (short) (templateIndexes.get(v) + indexOffset));
        index3++;
      }
      indexOffset += templateVerts.capacity() / 3;

      templateNormals.rewind();
      for (int v = 0; v < templateNormals.capacity(); v++) {
        finNormals.put(index4, templateNormals.get(v));
        index4++;
      }

      for (int v = 0; v < finColors.capacity(); v++) {
        finColors.put(v, 1.0f);
      }
    }

    // Help GC
    //	tempV3 = null;
    //	templateVerts = null;
    //	templateCoords = null;
    //	templateIndexes = null;
    //	templateNormals = null;

    // Clear & ssign buffers
    this.clearBuffer(VertexBuffer.Type.Position);
    this.setBuffer(VertexBuffer.Type.Position, 3, finVerts);
    this.clearBuffer(VertexBuffer.Type.TexCoord);
    try {
      this.setBuffer(VertexBuffer.Type.TexCoord, 2, finCoords);
    } catch (Exception e) {
    }
    this.clearBuffer(VertexBuffer.Type.Index);
    this.setBuffer(VertexBuffer.Type.Index, 3, finIndexes);
    this.clearBuffer(VertexBuffer.Type.Normal);
    this.setBuffer(VertexBuffer.Type.Normal, 3, finNormals);
    this.clearBuffer(VertexBuffer.Type.Color);
    this.setBuffer(VertexBuffer.Type.Color, 4, finColors);
    this.updateBound();
  }
  @Override
  public void updateParticleData(ParticleData[] particles, Camera cam, Matrix3f inverseRotation) {

    for (int i = 0; i < particles.length; i++) {
      ParticleData p = particles[i];
      int offset = templateVerts.capacity() * i;
      int colorOffset = templateColors.capacity() * i;
      if (p.life == 0 || !p.active) {
        for (int x = 0; x < templateVerts.capacity(); x += 3) {
          finVerts.put(offset + x, 0);
          finVerts.put(offset + x + 1, 0);
          finVerts.put(offset + x + 2, 0);
        }
        continue;
      }

      for (int x = 0; x < templateVerts.capacity(); x += 3) {
        switch (emitter.getBillboardMode()) {
          case Velocity:
            if (p.velocity.x != Vector3f.UNIT_Y.x
                && p.velocity.y != Vector3f.UNIT_Y.y
                && p.velocity.z != Vector3f.UNIT_Y.z)
              up.set(p.velocity).crossLocal(Vector3f.UNIT_Y).normalizeLocal();
            else up.set(p.velocity).crossLocal(lock).normalizeLocal();
            left.set(p.velocity).crossLocal(up).normalizeLocal();
            dir.set(p.velocity);
            break;
          case Velocity_Z_Up:
            if (p.velocity.x != Vector3f.UNIT_Y.x
                && p.velocity.y != Vector3f.UNIT_Y.y
                && p.velocity.z != Vector3f.UNIT_Y.z)
              up.set(p.velocity).crossLocal(Vector3f.UNIT_Y).normalizeLocal();
            else up.set(p.velocity).crossLocal(lock).normalizeLocal();
            left.set(p.velocity).crossLocal(up).normalizeLocal();
            dir.set(p.velocity);
            rotStore = tempQ.fromAngleAxis(-90 * FastMath.DEG_TO_RAD, left);
            left = rotStore.mult(left);
            up = rotStore.mult(up);
            break;
          case Velocity_Z_Up_Y_Left:
            up.set(p.velocity).crossLocal(Vector3f.UNIT_Y).normalizeLocal();
            left.set(p.velocity).crossLocal(up).normalizeLocal();
            dir.set(p.velocity);
            tempV3.set(left).crossLocal(up).normalizeLocal();
            rotStore = tempQ.fromAngleAxis(90 * FastMath.DEG_TO_RAD, p.velocity);
            left = rotStore.mult(left);
            up = rotStore.mult(up);
            rotStore = tempQ.fromAngleAxis(-90 * FastMath.DEG_TO_RAD, left);
            up = rotStore.mult(up);
            break;
          case Normal:
            emitter.getShape().setNext(p.triangleIndex);
            tempV3.set(emitter.getShape().getNormal());
            if (tempV3 == Vector3f.UNIT_Y) tempV3.set(p.velocity);

            up.set(tempV3).crossLocal(Vector3f.UNIT_Y).normalizeLocal();
            left.set(tempV3).crossLocal(up).normalizeLocal();
            dir.set(tempV3);
            break;
          case Normal_Y_Up:
            emitter.getShape().setNext(p.triangleIndex);
            tempV3.set(p.velocity);
            if (tempV3 == Vector3f.UNIT_Y) tempV3.set(Vector3f.UNIT_X);

            up.set(Vector3f.UNIT_Y);
            left.set(tempV3).crossLocal(up).normalizeLocal();
            dir.set(tempV3);
            break;
          case Camera:
            up.set(cam.getUp());
            left.set(cam.getLeft());
            dir.set(cam.getDirection());
            break;
          case UNIT_X:
            up.set(Vector3f.UNIT_Y);
            left.set(Vector3f.UNIT_Z);
            dir.set(Vector3f.UNIT_X);
            break;
          case UNIT_Y:
            up.set(Vector3f.UNIT_Z);
            left.set(Vector3f.UNIT_X);
            dir.set(Vector3f.UNIT_Y);
            break;
          case UNIT_Z:
            up.set(Vector3f.UNIT_X);
            left.set(Vector3f.UNIT_Y);
            dir.set(Vector3f.UNIT_Z);
            break;
        }

        tempV3.set(templateVerts.get(x), templateVerts.get(x + 1), templateVerts.get(x + 2));
        tempV3 = rotStore.mult(tempV3);
        tempV3.multLocal(p.size);

        rotStore.fromAngles(p.angles.x, p.angles.y, p.angles.z);
        tempV3 = rotStore.mult(tempV3);

        tempV3.addLocal(p.position);
        if (!emitter.getParticlesFollowEmitter()) {
          tempV3.subtractLocal(
              emitter
                  .getEmitterNode()
                  .getWorldTranslation()
                  .subtract(p.initialPosition)); // .divide(8f));
        }

        finVerts.put(offset + x, tempV3.getX());
        finVerts.put(offset + x + 1, tempV3.getY());
        finVerts.put(offset + x + 2, tempV3.getZ());
      }
      if (p.emitter.getApplyLightingTransform()) {
        for (int v = 0; v < templateNormals.capacity(); v += 3) {
          tempV3.set(
              templateNormals.get(v), templateNormals.get(v + 1), templateNormals.get(v + 2));

          rotStore.fromAngles(p.angles.x, p.angles.y, p.angles.z);
          mat3.set(rotStore.toRotationMatrix());
          float vx = tempV3.x, vy = tempV3.y, vz = tempV3.z;
          tempV3.x = mat3.get(0, 0) * vx + mat3.get(0, 1) * vy + mat3.get(0, 2) * vz;
          tempV3.y = mat3.get(1, 0) * vx + mat3.get(1, 1) * vy + mat3.get(1, 2) * vz;
          tempV3.z = mat3.get(2, 0) * vx + mat3.get(2, 1) * vy + mat3.get(2, 2) * vz;

          finNormals.put(offset + v, tempV3.getX());
          finNormals.put(offset + v + 1, tempV3.getY());
          finNormals.put(offset + v + 2, tempV3.getZ());
        }
      }
      for (int v = 0; v < templateColors.capacity(); v += 4) {
        finColors
            .put(colorOffset + v, p.color.r)
            .put(colorOffset + v + 1, p.color.g)
            .put(colorOffset + v + 2, p.color.b)
            .put(colorOffset + v + 3, p.color.a * p.alpha);
      }
    }

    this.setBuffer(VertexBuffer.Type.Position, 3, finVerts);
    if (particles[0].emitter.getApplyLightingTransform())
      this.setBuffer(VertexBuffer.Type.Normal, 3, finNormals);
    this.setBuffer(VertexBuffer.Type.Color, 4, finColors);

    updateBound();
  }
예제 #10
0
파일: World.java 프로젝트: Sindusk/polarity
    public Hallway(Vector3f start, float xi, float zi) {
      float x = start.getX() + xi;
      float z = start.getZ() + zi;
      float rng, dist;
      int len = 0;
      int spread = 0;
      int lenMax = FastMath.nextRandomInt(HALL_LENGTH_MIN, HALL_LENGTH_MAX);
      boolean b = false;
      // Make sure both xi and zi have an absolute value of 1:
      xi = A.sign(xi);
      zi = A.sign(zi);
      // Assign the first 2 corners:
      corners[0] =
          new Vector3f(
              (zi * (HALL_WIDTH - 1)) + x,
              start.getY(),
              (xi * (HALL_WIDTH - 1)) + z); // Bottom Left
      corners[1] =
          new Vector3f(
              (-zi * (HALL_WIDTH - 1)) + x,
              start.getY(),
              (-xi * (HALL_WIDTH - 1)) + z); // Bottom Right
      Vector3f left = corners[0].clone();
      Vector3f right = corners[1].clone();
      ArrayList<HallData> newHalls = new ArrayList(1);
      while (len <= lenMax) {
        if (world.get(left) != null && world.get(left).contains("h")) {
          b = true;
        }
        if (world.get(right) != null && world.get(right).contains("h")) {
          b = true;
        }
        // Check each of the spaces in this step for hallway:
        if (b) {
          right.addLocal(-xi, 0, -zi);
          left.addLocal(-xi, 0, -zi);
          break;
        }
        world.put(left.clone(), "h");
        world.put(right.add(zi, 0, xi), "h");
        world.put(right.clone(), "h");
        // Check distance & random to see if more hallways should be created:
        dist = left.distance(Vector3f.ZERO);
        rng = FastMath.nextRandomFloat();
        if (dist < HALL_MAX_RADIUS && spread > HALL_SPREAD && len < lenMax) {
          if (rng < 0.13f) {
            newHalls.add(new HallData(left.clone(), zi, xi));
            spread = 0;
          } else if (rng < 0.26f) {
            newHalls.add(new HallData(right.clone(), -zi, -xi));
            spread = 0;
          } else if (rng < 0.33f) {
            newHalls.add(new HallData(left.clone(), zi, xi));
            newHalls.add(new HallData(right.clone(), -zi, -xi));
            spread = 0;
          }
        }
        x += xi;
        z += zi;
        spread++;
        len++;
        left.addLocal(xi, 0, zi);
        right.addLocal(xi, 0, zi);
      }
      corners[2] = right.clone(); // Top Right
      corners[3] = left.clone(); // Top Left

      // Generate hallways:
      int j = 0;
      while (j < newHalls.size()) {
        hallways.add(new Hallway(newHalls.get(j).start, newHalls.get(j).xi, newHalls.get(j).zi));
        j++;
      }

      // Return if there's no hallway to generate (0 in size):
      float xs = FastMath.abs(corners[1].getX() - corners[3].getX()) + 1;
      float zs = FastMath.abs(corners[1].getZ() - corners[3].getZ()) + 1;
      if (Math.min(FastMath.abs(xs), FastMath.abs(zs)) < 1) {
        return;
      }

      // Generate the front wall:
      walls.add(new Wall(left.add(xi, 0, zi), -zi, -xi, HALL_WIDTH * 2 - 1));
      walls.add(new Wall(left.add(zi, 0, xi), -xi, -zi, len + 1));
      walls.add(new Wall(right.add(-zi, 0, -xi), -xi, -zi, len + 1));

      // Generate the actual floor:
      float xloc = (corners[3].getX() + corners[1].getX()) * 0.5f;
      float zloc = (corners[3].getZ() + corners[1].getZ()) * 0.5f;
      NPCManager.addNew("grunt", new Vector3f(xloc, start.getY(), zloc).mult(ZS).add(0, 5, 0));
      center = new Vector3f(xloc, start.getY() - 0.5f, zloc);
      floor = geoFloor(center, xs, zs, T.getMaterialPath("BC_Tex"), new Vector2f(zs, xs), true);
      map.add(floor);
    }
  private double basicCSG(Vector3f point, Skeleton currentSkeleton) {
    double sum = 0;
    boolean isCSG = SceneManager.get().csgEnabled;

    for (int i = 0; i < currentSkeleton.branches.size(); i++) {
      SkeletonBranch branch = currentSkeleton.branches.get(i);
      for (int j = 0; j < branch.points.size() - 1; j++) {
        SkeletonPoint SP1 = branch.points.get(j);
        SkeletonPoint SP2 = branch.points.get(j + 1);
        //            TriangulationPoint P1 = SP1.point;
        //            TriangulationPoint P2 = SP2.point;
        //            Vector3f p = new Vector3f(P1.getXf(), P1.getYf(), P1.getZf());
        //            Vector3f q = new Vector3f(P2.getXf(), P2.getYf(), P2.getZf());
        Vector3f p = SP1.point;
        Vector3f q = SP2.point;

        // calculate radius of influence
        float Rp = (float) SP1.radius;
        float Rq = (float) SP2.radius;

        // calculate surface points Sp and Sq
        Vector3f PQ = q.subtract(p);
        Vector3f normalVector = new Vector3f(-PQ.getY(), PQ.getX(), PQ.getZ()).normalize();
        Vector3f Sp = p.add(normalVector.mult(Rp));
        Vector3f Sq = q.add(normalVector.mult(Rq));
        Vector3f Smid = Sp.add(Sq).divide(2f);

        // System.out.println("L: " + PQ.length() + " Sp: " + Sp + " Sq: " +
        // Sq + " Smid: " + Smid);

        // calculate weights
        double w0 = T / flt(Sp, p, q);
        double w1 = T / ft(Sq, p, q);

        // division factor
        w0 /= (double) SP1.divisionFactor;
        w1 /= (double) SP2.divisionFactor;

        double k = T / (w0 * flt(Smid, p, q) + w1 * ft(Smid, p, q));

        // scale factor
        w0 *= k;
        w1 *= k;

        // System.out.println("j " + j);
        // System.out.println("w0 " + w0 + " w1 " + w1);

        // CSG blending
        if (isCSG) {
          double fx = cauchy(point, p, q, w0, w1);
          double R = cauchy(Smid, p, q, w0, w1);
          double blending = blending(-fx + T, R);
          sum += blending;
        } else {
          // common sum
          sum += cauchy(point, p, q, w0, w1);
        }
      }
    }

    // if(sum > 1)
    // blendingGTone++;
    // else if(sum < 1)
    // blendingLTone++;

    // sum /= n;

    // Vector3f A = new Vector3f(-5f, 0.0f, 0.0f);
    // Vector3f B = new Vector3f(5f, 0.0f, 0.0f);
    // Vector3f C = new Vector3f(-5f, 4.0f, 0.0f);
    // Vector3f D = new Vector3f(5f, 4.0f, 0.0f);
    //
    // // define threshold based on a pre determined surface point
    // float radius = 5.0f;
    // Vector3f AB = B.subtract(A);
    // Vector3f normalVectorAB = new Vector3f(-AB.getY(), AB.getX(),
    // AB.getZ()).normalize().mult(radius);
    // Vector3f midPointAB = (A.add(B)).divide(2f);
    // Vector3f surfacePointAB = midPointAB.add(normalVectorAB);
    //
    // T = 1.0;
    //
    // sum += blending(-cauchy(point, A, B) + T, cauchy(surfacePointAB, A,
    // B));
    // sum += blending(-cauchy(point, C, D) + T, cauchy(surfacePointAB, C,
    // D));

    // sum = cauchy(point, A, B) + cauchy(point, C, D);

    //      if(!single) //i.e. is multiple
    //      {
    //          // common sum
    ////          double result = -sum + T;
    ////          if (result > 0)
    ////             positive++;
    ////          else if (result < 0)
    ////             negative++;
    ////          return result;
    //          return sum;
    //      }
    //      else
    //      {
    if (!isCSG) return T - sum;
    else
      // CSG composition
      return 1 - sum;
    //      }
  }
예제 #12
0
 private double[] convertVectorToArray(final Vector3f f) {
   return new double[] {f.getX(), f.getY(), f.getZ()};
 }
예제 #13
0
파일: Material.java 프로젝트: pilsnils/LEGO
  /**
   * Uploads the lights in the light list as two uniform arrays.<br>
   * <br>
   * *
   *
   * <p><code>uniform vec4 g_LightColor[numLights];</code><br>
   * // g_LightColor.rgb is the diffuse/specular color of the light.<br>
   * // g_Lightcolor.a is the type of light, 0 = Directional, 1 = Point, <br>
   * // 2 = Spot. <br>
   * <br>
   * <code>uniform vec4 g_LightPosition[numLights];</code><br>
   * // g_LightPosition.xyz is the position of the light (for point lights)<br>
   * // or the direction of the light (for directional lights).<br>
   * // g_LightPosition.w is the inverse radius (1/r) of the light (for attenuation) <br>
   */
  protected void updateLightListUniforms(Shader shader, Geometry g, int numLights) {
    if (numLights == 0) { // this shader does not do lighting, ignore.
      return;
    }

    LightList lightList = g.getWorldLightList();
    Uniform lightColor = shader.getUniform("g_LightColor");
    Uniform lightPos = shader.getUniform("g_LightPosition");
    Uniform lightDir = shader.getUniform("g_LightDirection");
    lightColor.setVector4Length(numLights);
    lightPos.setVector4Length(numLights);
    lightDir.setVector4Length(numLights);

    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList));

    int lightIndex = 0;

    for (int i = 0; i < numLights; i++) {
      if (lightList.size() <= i) {
        lightColor.setVector4InArray(0f, 0f, 0f, 0f, lightIndex);
        lightPos.setVector4InArray(0f, 0f, 0f, 0f, lightIndex);
      } else {
        Light l = lightList.get(i);
        ColorRGBA color = l.getColor();
        lightColor.setVector4InArray(
            color.getRed(), color.getGreen(), color.getBlue(), l.getType().getId(), i);

        switch (l.getType()) {
          case Directional:
            DirectionalLight dl = (DirectionalLight) l;
            Vector3f dir = dl.getDirection();
            lightPos.setVector4InArray(dir.getX(), dir.getY(), dir.getZ(), -1, lightIndex);
            break;
          case Point:
            PointLight pl = (PointLight) l;
            Vector3f pos = pl.getPosition();
            float invRadius = pl.getInvRadius();
            lightPos.setVector4InArray(pos.getX(), pos.getY(), pos.getZ(), invRadius, lightIndex);
            break;
          case Spot:
            SpotLight sl = (SpotLight) l;
            Vector3f pos2 = sl.getPosition();
            Vector3f dir2 = sl.getDirection();
            float invRange = sl.getInvSpotRange();
            float spotAngleCos = sl.getPackedAngleCos();

            lightPos.setVector4InArray(pos2.getX(), pos2.getY(), pos2.getZ(), invRange, lightIndex);
            lightDir.setVector4InArray(
                dir2.getX(), dir2.getY(), dir2.getZ(), spotAngleCos, lightIndex);
            break;
          case Ambient:
            // skip this light. Does not increase lightIndex
            continue;
          default:
            throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
      }

      lightIndex++;
    }

    while (lightIndex < numLights) {
      lightColor.setVector4InArray(0f, 0f, 0f, 0f, lightIndex);
      lightPos.setVector4InArray(0f, 0f, 0f, 0f, lightIndex);

      lightIndex++;
    }
  }
예제 #14
0
 public static Vector3f compensateFloatRoundingErrors(Vector3f vector) {
   return new Vector3f(
       compensateFloatRoundingErrors(vector.getX()),
       compensateFloatRoundingErrors(vector.getY()),
       compensateFloatRoundingErrors(vector.getZ()));
 }
예제 #15
0
파일: Material.java 프로젝트: pilsnils/LEGO
  protected void renderMultipassLighting(Shader shader, Geometry g, RenderManager rm) {

    Renderer r = rm.getRenderer();
    LightList lightList = g.getWorldLightList();
    Uniform lightDir = shader.getUniform("g_LightDirection");
    Uniform lightColor = shader.getUniform("g_LightColor");
    Uniform lightPos = shader.getUniform("g_LightPosition");
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    boolean isFirstLight = true;
    boolean isSecondLight = false;

    for (int i = 0; i < lightList.size(); i++) {
      Light l = lightList.get(i);
      if (l instanceof AmbientLight) {
        continue;
      }

      if (isFirstLight) {
        // set ambient color for first light only
        ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList));
        isFirstLight = false;
        isSecondLight = true;
      } else if (isSecondLight) {
        ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
        // apply additive blending for 2nd and future lights
        r.applyRenderState(additiveLight);
        isSecondLight = false;
      }

      TempVars vars = TempVars.get();
      Quaternion tmpLightDirection = vars.quat1;
      Quaternion tmpLightPosition = vars.quat2;
      ColorRGBA tmpLightColor = vars.color;
      Vector4f tmpVec = vars.vect4f;

      ColorRGBA color = l.getColor();
      tmpLightColor.set(color);
      tmpLightColor.a = l.getType().getId();
      lightColor.setValue(VarType.Vector4, tmpLightColor);

      switch (l.getType()) {
        case Directional:
          DirectionalLight dl = (DirectionalLight) l;
          Vector3f dir = dl.getDirection();

          tmpLightPosition.set(dir.getX(), dir.getY(), dir.getZ(), -1);
          lightPos.setValue(VarType.Vector4, tmpLightPosition);
          tmpLightDirection.set(0, 0, 0, 0);
          lightDir.setValue(VarType.Vector4, tmpLightDirection);
          break;
        case Point:
          PointLight pl = (PointLight) l;
          Vector3f pos = pl.getPosition();
          float invRadius = pl.getInvRadius();

          tmpLightPosition.set(pos.getX(), pos.getY(), pos.getZ(), invRadius);
          lightPos.setValue(VarType.Vector4, tmpLightPosition);
          tmpLightDirection.set(0, 0, 0, 0);
          lightDir.setValue(VarType.Vector4, tmpLightDirection);
          break;
        case Spot:
          SpotLight sl = (SpotLight) l;
          Vector3f pos2 = sl.getPosition();
          Vector3f dir2 = sl.getDirection();
          float invRange = sl.getInvSpotRange();
          float spotAngleCos = sl.getPackedAngleCos();

          tmpLightPosition.set(pos2.getX(), pos2.getY(), pos2.getZ(), invRange);
          lightPos.setValue(VarType.Vector4, tmpLightPosition);

          // We transform the spot directoin in view space here to save 5 varying later in the
          // lighting shader
          // one vec4 less and a vec4 that becomes a vec3
          // the downside is that spotAngleCos decoding happen now in the frag shader.
          tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0);
          rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
          tmpLightDirection.set(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos);

          lightDir.setValue(VarType.Vector4, tmpLightDirection);

          break;
        default:
          throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
      }
      vars.release();
      r.setShader(shader);
      r.renderMesh(g.getMesh(), g.getLodLevel(), 1);
    }

    if (isFirstLight && lightList.size() > 0) {
      // There are only ambient lights in the scene. Render
      // a dummy "normal light" so we can see the ambient
      ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList));
      lightColor.setValue(VarType.Vector4, ColorRGBA.BlackNoAlpha);
      lightPos.setValue(VarType.Vector4, nullDirLight);
      r.setShader(shader);
      r.renderMesh(g.getMesh(), g.getLodLevel(), 1);
    }
  }
예제 #16
0
  private static Transform convertPositions(FloatBuffer input, BoundingBox bbox, Buffer output) {
    if (output.capacity() < input.capacity())
      throw new RuntimeException("Output must be at least as large as input!");

    Vector3f offset = bbox.getCenter().negate();
    Vector3f size = new Vector3f(bbox.getXExtent(), bbox.getYExtent(), bbox.getZExtent());
    size.multLocal(2);

    ShortBuffer sb = null;
    ByteBuffer bb = null;
    float dataTypeSize;
    float dataTypeOffset;
    if (output instanceof ShortBuffer) {
      sb = (ShortBuffer) output;
      dataTypeOffset = shortOff;
      dataTypeSize = shortSize;
    } else {
      bb = (ByteBuffer) output;
      dataTypeOffset = byteOff;
      dataTypeSize = byteSize;
    }
    Vector3f scale = new Vector3f();
    scale.set(dataTypeSize, dataTypeSize, dataTypeSize).divideLocal(size);

    Vector3f invScale = new Vector3f();
    invScale.set(size).divideLocal(dataTypeSize);

    offset.multLocal(scale);
    offset.addLocal(dataTypeOffset, dataTypeOffset, dataTypeOffset);

    // offset = (-modelOffset * shortSize)/modelSize + shortOff
    // scale = shortSize / modelSize

    input.clear();
    output.clear();
    Vector3f temp = new Vector3f();
    int vertexCount = input.capacity() / 3;
    for (int i = 0; i < vertexCount; i++) {
      BufferUtils.populateFromBuffer(temp, input, i);

      // offset and scale vector into -32768 ... 32767
      // or into -128 ... 127 if using bytes
      temp.multLocal(scale);
      temp.addLocal(offset);

      // quantize and store
      if (sb != null) {
        short v1 = (short) temp.getX();
        short v2 = (short) temp.getY();
        short v3 = (short) temp.getZ();
        sb.put(v1).put(v2).put(v3);
      } else {
        byte v1 = (byte) temp.getX();
        byte v2 = (byte) temp.getY();
        byte v3 = (byte) temp.getZ();
        bb.put(v1).put(v2).put(v3);
      }
    }

    Transform transform = new Transform();
    transform.setTranslation(offset.negate().multLocal(invScale));
    transform.setScale(invScale);
    return transform;
  }
예제 #17
0
 public void setPlayerPos(Vector3f pos) {
   this.playerpos = new PlayerPos(pos.getX(), pos.getY(), pos.getZ());
   this.conn.send(new PlayerPosMessage(this.playerpos, this.playerID, this));
 }