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

simplex3d.engine.asset.AssetManager.scala Maven / Gradle / Ivy

/*
 * Simplex3dEngine - Core Module
 * Copyright (C) 2011, Aleksey Nikiforov
 *
 * This file is part of Simplex3dEngine.
 *
 * Simplex3dEngine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Simplex3dEngine 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */

package simplex3d.engine.asset

import scala.reflect._
import scala.collection.mutable.ArrayBuffer
import simplex3d.math.types._
import simplex3d.math._
import simplex3d.math.double._
import simplex3d.data._
import simplex3d.data.double._
import simplex3d.engine.graphics._


// XXX add loaded caching and linked sources
abstract class AssetManager {
  // XXX work out default settings, add mass changes
  private val magFilter = ImageFilter.Linear
  private val minFilter = ImageFilter.Linear
  private val mipMapFilter = MipMapFilter.Linear
  private val anisotropyLevel = 4
  
  
  protected val loaders = ArrayBuffer[AssetLoader]()
  
  protected def resolve[R](loadOperation: (AssetLoader) => Option[R]) :Option[R] = {
    var loaded: Option[R] = None
    var i = 0; while (!loaded.isDefined && i < loaders.size) {
      loaded = loadOperation.apply(loaders(i))
      i += 1
    }
    loaded
  }
  
  def loadTexture2d[A <: Accessor](path: String)
    (implicit accessorTag: ClassTag[A])
  :Option[Texture2d[A]] = {
    val img = accessorTag match {
      case Vec3.Tag => resolve(_.loadRgbImg(path))
      case Vec4.Tag => resolve(_.loadRgbaImg(path))
      case _ => None
    }
    
    if (img.isDefined) {
      val (dimensions, data) = img.get
      val texture = Texture2d.fromUncheckedSrc[A](dimensions, data)
      
      texture.magFilter = magFilter
      texture.minFilter = minFilter
      texture.mipMapFilter = mipMapFilter
      texture.anisotropyLevel = anisotropyLevel
      
      Some(texture)
    }
    else None
  }
  
  // getLinkedTexture // shared, link-cached
  // getReadableTexture // shared, data-cached
  // getWritableTexture // unique, data-cached
  // uncacheTexture // clear caches
  
  
  def loadObj(path: String)
  :Option[(
    DataBuffer[SInt, Unsigned],// indices
    DataBuffer[Vec3, RFloat], // vertices
    Option[DataBuffer[Vec3, RFloat]], // normals
    Option[DataBuffer[Vec2, RFloat]] // texCoords
  )] = {
    resolve(_.loadObj(path))
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy