示例#1
0
  /**
   * Called back immediately after the OpenGL context is initialized. Can be used to perform
   * one-time initialization. Run only once.
   */
  @Override
  public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2(); // get the OpenGL graphics context
    glu = new GLU(); // get GL Utilities
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color
    gl.glClearDepth(1.0f); // set clear depth value to farthest
    gl.glEnable(GL_DEPTH_TEST); // enables depth testing
    gl.glDepthFunc(GL_LEQUAL); // the type of depth test to do
    gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // best perspective correction
    gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting

    // Load the texture image
    try {
      // Create a OpenGL Texture object from (URL, mipmap, file suffix)
      // Use URL so that can read from JAR and disk file.
      texture =
          TextureIO.newTexture(
              this.getClass().getResource(textureFileName), false, textureFileType);
    } catch (GLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Use linear filter for texture if image is larger than the original texture
    gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // Use linear filter for texture if image is smaller than the original texture
    gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    // Texture image flips vertically. Shall use TextureCoords class to retrieve
    // the top, bottom, left and right coordinates, instead of using 0.0f and 1.0f.
    TextureCoords textureCoords = texture.getImageTexCoords();
    textureTop = textureCoords.top();
    textureBottom = textureCoords.bottom();
    //      textureLeft = textureCoords.left();
    //      textureRight = textureCoords.right();

    // Enable the texture
    texture.enable(gl);
    // gl.glEnable(GL_TEXTURE_2D);

    // we want back facing polygons to be filled completely and that we want front
    // facing polygons to be outlined only.
    gl.glPolygonMode(GL_BACK, GL_FILL); // Back Face Is Filled In
    gl.glPolygonMode(GL_FRONT, GL_LINE); // Front Face Is Drawn With Lines

    for (int x = 0; x < numPoints; x++) { // Loop Through The Y Plane
      for (int y = 0; y < numPoints; y++) {
        // Apply The Wave To Our Mesh
        // xmax is 45. Get 9 after dividing by 5. Subtract 4.5 to centralize.
        points[x][y][0] = (float) x / 5.0f - 4.5f;
        points[x][y][1] = (float) y / 5.0f - 4.5f;
        // Sine wave pattern
        points[x][y][2] = (float) (Math.sin(Math.toRadians(x / 5.0f * 40.0f)));
      }
    }
  }
示例#2
0
  /**
   * Called back immediately after the OpenGL context is initialized. Can be used to perform
   * one-time initialization. Run only once.
   */
  @Override
  public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2(); // get the OpenGL graphics context
    glu = new GLU(); // get GL Utilities
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color
    gl.glClearDepth(1.0f); // set clear depth value to farthest
    // Do not enable depth test
    //      gl.glEnable(GL_DEPTH_TEST); // enables depth testing
    //      gl.glDepthFunc(GL_LEQUAL);  // the type of depth test to do
    gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // best perspective correction
    gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting

    // Load the texture image
    try {
      // Use URL so that can read from JAR and disk file.
      BufferedImage image = ImageIO.read(this.getClass().getResource(textureFileName));

      // Create a OpenGL Texture object
      texture = AWTTextureIO.newTexture(GLProfile.getDefault(), image, false);
      // Use linear filter if image is larger than the original texture
      gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      // Use linear filter if image is smaller than the original texture
      gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    } catch (GLException | IOException e) {
      e.printStackTrace();
    }

    // Texture image flips vertically. Shall use TextureCoords class to retrieve
    // the top, bottom, left and right coordinates, instead of using 0.0f and 1.0f.
    TextureCoords textureCoords = texture.getImageTexCoords();
    textureCoordTop = textureCoords.top();
    textureCoordBottom = textureCoords.bottom();
    textureCoordLeft = textureCoords.left();
    textureCoordRight = textureCoords.right();

    // Enable the texture
    texture.enable(gl);
    texture.bind(gl);
    // gl.glEnable(GL_TEXTURE_2D);

    // Enable blending
    gl.glEnable(GL_BLEND);
    gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE);

    // Allocate the stars
    for (int i = 0; i < stars.length; i++) {
      stars[i] = new Star();
      // Linearly distributed according to the star number
      stars[i].distance = ((float) i / numStars) * 5.0f;
    }
  }
  /**
   * initSimplexTexture(GLuinttexID) - create and load a 1D texture for a simplex traversal order
   * lookup table. This is used for simplex noise only, and only for 3D and 4D noise where there are
   * more than 2 simplices. (3D simplex noise has 6 cases to sort out, 4D simplex noise has 24
   * cases.)
   */
  private void initSimplexTexture(GL2 gl, int[] texID) {
    gl.glGenTextures(1, texID, 0); // Generate a unique texture ID
    gl.glBindTexture(GL2.GL_TEXTURE_1D, texID[0]); // Bind the texture to texture unit 1

    ByteBuffer simplexBuffer = ByteBuffer.allocate(64 * 4);
    for (int[] myBytes : simplex4) {
      for (int myByte : myBytes) {
        simplexBuffer.put((byte) myByte);
      }
    }
    simplexBuffer.rewind();

    // GLFW texture loading functions won't work here - we need
    // GL.GL_NEAREST lookup.
    gl.glTexImage1D(
        GL2.GL_TEXTURE_1D, 0, GL.GL_RGBA, 64, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, simplexBuffer);
    gl.glTexParameteri(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
    gl.glTexParameteri(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
  }
示例#4
0
  public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();

    // Storage for the 96 Shader Values ( NEW )
    FloatBuffer shaderData = GLBuffers.newDirectFloatBuffer(96);

    // Start Of User Initialization
    // Really Nice perspective calculations Light Grey Background
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
    gl.glClearColor(0.7f, 0.7f, 0.7f, 0.0f);
    // Depth Buffer Setup
    gl.glClearDepth(1.0f);

    // Enable Depth Testing
    gl.glEnable(GL2.GL_DEPTH_TEST);
    // The Type Of Depth Test To Do
    gl.glDepthFunc(GL2.GL_LESS);

    // Enables Smooth Color Shading ( NEW )
    gl.glShadeModel(GL2.GL_SMOOTH);
    // Initially Disable Line Smoothing ( NEW )
    gl.glDisable(GL2.GL_LINE_SMOOTH);

    // Enable OpenGL Face Culling ( NEW )
    gl.glEnable(GL2.GL_CULL_FACE);

    // Disable OpenGL Lighting ( NEW )
    gl.glDisable(GL2.GL_LIGHTING);

    StringBuffer readShaderData = new StringBuffer();

    try {
      InputStream inputStream =
          ResourceRetriever.getResourceAsStream("demos/data/models/Shader.txt");
      int info;
      while ((info = inputStream.read()) != -1) readShaderData.append((char) info);
      inputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }

    StringTokenizer tokenizer = new StringTokenizer(readShaderData.toString());

    // Loop Though The 32 Greyscale Values
    while (tokenizer.hasMoreTokens()) {
      float value = Float.parseFloat(tokenizer.nextToken());
      shaderData.put(value);
      shaderData.put(value);
      shaderData.put(value);
    }
    shaderData.flip();

    gl.glGenTextures(1, shaderTexture, 0); // Get A Free Texture ID ( NEW )
    gl.glBindTexture(GL2.GL_TEXTURE_1D, shaderTexture[0]); // Bind This
    // Texture. From
    // Now On It
    // Will Be 1D
    // For Crying Out Loud Don't Let OpenGL Use Bi/Trilinear Filtering!
    gl.glTexParameteri(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
    gl.glTexParameteri(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);

    gl.glTexImage1D(
        GL2.GL_TEXTURE_1D, 0, GL2.GL_RGB, 32, 0, GL2.GL_RGB, GL2.GL_FLOAT, shaderData); // Upload

    // Set The X Direction
    lightAngle.X = 0.0f;
    // Set The Y Direction
    lightAngle.Y = 0.0f;
    // Set The Z Direction
    lightAngle.Z = 1.0f;
    lightAngle.normalize();

    try {
      // Return The Value Of ReadMesh
      readMesh();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }