コード例 #1
0
  private void setTransformation(Matrix4f transformation) {
    // Compute the modelview matrix by multiplying the camera matrix and
    // the transformation matrix of the object
    Matrix4f modelview = new Matrix4f(sceneManager.getCamera().getCameraMatrix());
    modelview.mul(transformation);

    // Set modelview and projection matrices in shader
    gl.glUniformMatrix4fv(
        gl.glGetUniformLocation(activeShaderID, "modelview"),
        1,
        false,
        transformationToFloat16(modelview),
        0);
    gl.glUniformMatrix4fv(
        gl.glGetUniformLocation(activeShaderID, "projection"),
        1,
        false,
        transformationToFloat16(sceneManager.getFrustum().getProjectionMatrix()),
        0);

    int id = gl.glGetUniformLocation(activeShaderID, "camera");
    if (id != -1) {
      Vector3f cop = sceneManager.getCamera().getCenterOfProjection();
      gl.glUniform4f(id, cop.x, cop.y, cop.z, 0);
    }
    // } else
    // System.out.println("Could not get location of uniform variable camera");
  }
コード例 #2
0
  /**
   * Set up a material for rendering. Activate its shader, and pass the material properties,
   * textures, and light sources to the shader.
   *
   * @param m the material to be set up for rendering
   */
  private void setMaterial(Material m) {

    // Set up the shader for the material, if it has one
    if (m != null && m.shader != null) {

      // Identifier for shader variables
      int id;

      // Activate the shader
      useShader(m.shader);

      // Activate the diffuse texture, if the material has one
      if (m.diffuseMap != null) {
        // OpenGL calls to activate the texture
        gl.glActiveTexture(GL3.GL_TEXTURE0); // Work with texture unit 0
        gl.glEnable(GL3.GL_TEXTURE_2D);
        gl.glBindTexture(GL3.GL_TEXTURE_2D, ((GLTexture) m.diffuseMap).getId());
        gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_LINEAR);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_LINEAR);
        // We assume the texture in the shader is called "myTexture"
        id = gl.glGetUniformLocation(activeShaderID, "myTexture");
        gl.glUniform1i(
            id,
            0); // The variable in the shader needs to be set to the desired texture unit, i.e., 0
      }

      // Pass a default light source to shader
      String lightString = "lightDirection[" + 0 + "]";
      id = gl.glGetUniformLocation(activeShaderID, lightString);
      if (id != -1) gl.glUniform4f(id, 0, 0, 1, 0.f); // Set light direction
      else System.out.print("Could not get location of uniform variable " + lightString + "\n");
      int nLights = 1;

      // Iterate over all light sources in scene manager (overwriting the default light source)
      Iterator<Light> iter = sceneManager.lightIterator();

      Light l;
      if (iter != null) {
        nLights = 0;
        while (iter.hasNext() && nLights < 8) {
          l = iter.next();

          // Pass light direction to shader, we assume the shader stores it in an array
          // "lightDirection[]"
          lightString = "lightDirection[" + nLights + "]";
          id = gl.glGetUniformLocation(activeShaderID, lightString);
          if (id != -1)
            gl.glUniform4f(
                id, l.direction.x, l.direction.y, l.direction.z, 0.f); // Set light direction
          else System.out.print("Could not get location of uniform variable " + lightString + "\n");

          nLights++;
        }

        // Pass number of lights to shader, we assume this is in a variable "nLights" in the shader
        id = gl.glGetUniformLocation(activeShaderID, "nLights");
        if (id != -1) gl.glUniform1i(id, nLights); // Set number of lightrs
        // Only for debugging
        //				else
        //					System.out.print("Could not get location of uniform variable nLights\n");
      }
    }
  }