Ejemplo n.º 1
0
  public void bindUniforms(List<Light> lights) {
    if (lights.size() > 10) {
      System.err.println("Too many lights.");
      return;
    }
    float[] positions = new float[10 * 2];
    float[] colors = new float[10 * 3];
    float[] intensities = new float[10];

    for (int i = 0; i < lights.size() * 2; i += 2) {
      positions[i] = lights.get(i >> 1).getX() - lights.get(i >> 1).getXOffset();
      positions[i + 1] =
          GameSettings.height - lights.get(i >> 1).getY() + lights.get(i >> 1).getYOffset();
    }

    for (int i = 0; i < lights.size(); i++) {
      intensities[i] = lights.get(i).intensity;
    }

    for (int i = 0; i < lights.size() * 3; i += 3) {
      colors[i] = lights.get(i / 3).vc.x;
      colors[i + 1] = lights.get(i / 3).vc.y;
      colors[i + 2] = lights.get(i / 3).vc.z;
    }

    shader.bind();
    int uniform = glGetUniformLocation(shader.getID(), "lightPosition");
    glUniform2(uniform, Buffer.createFloatBuffer(positions));

    uniform = glGetUniformLocation(shader.getID(), "lightColor");
    glUniform3(uniform, Buffer.createFloatBuffer(colors));

    uniform = glGetUniformLocation(shader.getID(), "lightIntensity");
    glUniform1(uniform, Buffer.createFloatBuffer(intensities));
    shader.release();
  }
Ejemplo n.º 2
0
  @Override
  protected void display() {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    MatrixStack modelMatrix = new MatrixStack();
    modelMatrix.setMatrix(viewPole.calcMatrix());

    Vec4 lightDirCameraSpace = Mat4.mul(modelMatrix.top(), lightDirection);

    glUseProgram(whiteDiffuseColor.theProgram);
    glUniform3(whiteDiffuseColor.dirToLightUnif, lightDirCameraSpace.fillAndFlipBuffer(vec4Buffer));
    glUseProgram(vertexDiffuseColor.theProgram);
    glUniform3(
        vertexDiffuseColor.dirToLightUnif, lightDirCameraSpace.fillAndFlipBuffer(vec4Buffer));
    glUseProgram(0);

    {
      modelMatrix.push();

      // Render the ground plane.
      {
        modelMatrix.push();

        glUseProgram(whiteDiffuseColor.theProgram);
        glUniformMatrix4(
            whiteDiffuseColor.modelToCameraMatrixUnif,
            false,
            modelMatrix.top().fillAndFlipBuffer(mat4Buffer));
        Mat3 normMatrix = new Mat3(modelMatrix.top());
        glUniformMatrix3(
            whiteDiffuseColor.normalModelToCameraMatrixUnif,
            false,
            normMatrix.fillAndFlipBuffer(mat3Buffer));
        glUniform4f(whiteDiffuseColor.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f);
        planeMesh.render();
        glUseProgram(0);

        modelMatrix.pop();
      }

      // Render the Cylinder
      {
        modelMatrix.push();

        modelMatrix.applyMatrix(objtPole.calcMatrix());

        if (drawColoredCyl) {
          glUseProgram(vertexDiffuseColor.theProgram);
          glUniformMatrix4(
              vertexDiffuseColor.modelToCameraMatrixUnif,
              false,
              modelMatrix.top().fillAndFlipBuffer(mat4Buffer));
          Mat3 normMatrix = new Mat3(modelMatrix.top());
          glUniformMatrix3(
              vertexDiffuseColor.normalModelToCameraMatrixUnif,
              false,
              normMatrix.fillAndFlipBuffer(mat3Buffer));
          glUniform4f(vertexDiffuseColor.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f);
          cylinderMesh.render("lit-color");
        } else {
          glUseProgram(whiteDiffuseColor.theProgram);
          glUniformMatrix4(
              whiteDiffuseColor.modelToCameraMatrixUnif,
              false,
              modelMatrix.top().fillAndFlipBuffer(mat4Buffer));
          Mat3 normMatrix = new Mat3(modelMatrix.top());
          glUniformMatrix3(
              whiteDiffuseColor.normalModelToCameraMatrixUnif,
              false,
              normMatrix.fillAndFlipBuffer(mat3Buffer));
          glUniform4f(whiteDiffuseColor.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f);
          cylinderMesh.render("lit");
        }

        glUseProgram(0);

        modelMatrix.pop();
      }

      modelMatrix.pop();
    }
  }