All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.llbit.chunky.world.ChunkTexture Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
/* Copyright (c) 2012-2013 Jesper Öqvist 
 *
 * This file is part of Chunky.
 *
 * Chunky is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Chunky is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Chunky.  If not, see .
 */
package se.llbit.chunky.world;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Chunk texture
 *
 * @author Jesper Öqvist 
 */
public class ChunkTexture {

  float[][] data = new float[Chunk.X_MAX * Chunk.Z_MAX][3];

  /**
   * Create new texture
   */
  public ChunkTexture() {
  }

  /**
   * Set color value at (x, z).
   *
   * @param frgb RGB color components to set
   */
  public void set(int x, int z, float[] frgb) {
    int index = x + z * Chunk.X_MAX;
    data[index][0] = frgb[0];
    data[index][1] = frgb[1];
    data[index][2] = frgb[2];
  }

  /**
   * @return RGB color components at (x, z)
   */
  public float[] get(int x, int z) {
    int index = x + z * Chunk.X_MAX;
    return data[index];
  }

  /**
   * Write this chunk texture to an output stream.
   *
   * @throws IOException
   */
  public void store(DataOutputStream out) throws IOException {
    for (int i = 0; i < Chunk.X_MAX * Chunk.Z_MAX; ++i) {
      out.writeFloat(data[i][0]);
      out.writeFloat(data[i][1]);
      out.writeFloat(data[i][2]);
    }
  }

  /**
   * Load a chunk texture from an input stream.
   *
   * @return The loaded texture
   * @throws IOException
   */
  public static ChunkTexture load(DataInputStream in) throws IOException {
    ChunkTexture texture = new ChunkTexture();
    for (int i = 0; i < Chunk.X_MAX * Chunk.Z_MAX; ++i) {
      texture.data[i][0] = in.readFloat();
      texture.data[i][1] = in.readFloat();
      texture.data[i][2] = in.readFloat();
    }
    return texture;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy