예제 #1
0
  public GLTextureManager() {
    final ResourceLoader<Texture> loader = getResourceLoader();
    loader.add(
        new ResourceDelegate<Texture>() {
          public boolean isLoadable(final String _file) {
            return true;
          }

          public Texture load(final String _file, final Settings _settings) {
            return loadTextureASync(_file);
          }

          protected Texture loadTextureASync(final String _file) {
            // We want to allocate the key for the resource so the texture
            // is not reloaded if another object wishes to use it before
            // the texture has fully loaded.
            // The Renderer should skip the texture, until it is finally
            // available to render/
            add(_file, null);

            TextureThread load = new TextureThread("LOAD_TEXTURE", _file);
            load.start();

            return null;
          }
        });
  }
예제 #2
0
  public GLProgramManager() {
    final ResourceLoader<GLProgram> loader = getResourceLoader();
    loader.add(
        new ResourceDelegate<GLProgram>() {
          public boolean isLoadable(final String _file) {
            return GlobalFileSystem.isExtension(_file, ".jgl", ".JGL");
          }

          public GLProgram load(final String _file, final Settings _settings) {
            final FileStream stream = GlobalFileSystem.getFile(_file);
            if (stream.exists() == false) {
              System.out.println("Unable to find: " + _file);
              return null;
            }

            add(_file, PLACEHOLDER);
            JSONObject.construct(
                stream,
                new JSONObject.ConstructCallback() {
                  public void callback(final JSONObject _obj) {
                    generateGLProgram(_obj);
                  }
                });

            return null;
          }

          private void generateGLProgram(final JSONObject _jGL) {
            final List<GLShader> shaders = MalletList.<GLShader>newList();
            final List<GLShaderMap> paths = MalletList.<GLShaderMap>newList();

            fill(paths, _jGL.getJSONArray("VERTEX"), GLES30.GL_VERTEX_SHADER);
            // fill( paths, _jGL.getJSONArray( "GEOMETRY" ), GLES30.GL_GEOMETRY_SHADER ) ;
            fill(paths, _jGL.getJSONArray("FRAGMENT"), GLES30.GL_FRAGMENT_SHADER);

            final List<Tuple<String, Uniform>> uniforms =
                MalletList.<Tuple<String, Uniform>>newList();
            final List<String> swivel = MalletList.<String>newList();

            fillUniforms(uniforms, _jGL.getJSONArray("UNIFORMS"));
            fillAttributes(swivel, _jGL.getJSONArray("SWIVEL"));

            readShaders(_jGL.optString("NAME", "undefined"), paths, shaders, uniforms, swivel);
          }

          private void fill(
              final List<GLShaderMap> _toFill, final JSONArray _base, final int _type) {
            if (_base == null) {
              return;
            }

            final int length = _base.length();
            for (int i = 0; i < length; i++) {
              _toFill.add(new GLShaderMap(_base.getString(i), _type));
            }
          }

          private void fillAttributes(final List<String> _toFill, final JSONArray _base) {
            if (_base == null) {
              return;
            }

            final int length = _base.length();
            for (int i = 0; i < length; i++) {
              _toFill.add(_base.getString(i));
            }
          }

          private void fillUniforms(
              final List<Tuple<String, Uniform>> _toFill, final JSONArray _base) {
            if (_base == null) {
              return;
            }

            final int length = _base.length();
            for (int i = 0; i < length; i++) {
              final JSONObject obj = _base.getJSONObject(i);
              final String name = obj.optString("NAME", null);
              final Uniform type = Uniform.valueOf(obj.optString("TYPE", null));

              if (name != null && type != null) {
                _toFill.add(new Tuple<String, Uniform>(name, type));
              }
            }
          }

          /**
           * Recusive function. Loop through _jShaders loading the sources into _glShaders, once
           * _jShaders is empty construct a GLProgram and add it to the toBind array.
           */
          private void readShaders(
              final String _name,
              final List<GLShaderMap> _jShaders,
              final List<GLShader> _glShaders,
              final List<Tuple<String, Uniform>> _uniforms,
              final List<String> _swivel) {
            if (_jShaders.isEmpty() == true) {
              if (_glShaders.isEmpty() == true) {
                System.out.println("Unable to generate GLProgram, no shaders specified.");
                return;
              }

              synchronized (toBind) {
                // We don't want to compile the Shaders now
                // as that will take control of the OpenGL context.
                toBind.add(new GLProgram(_name, _glShaders, _uniforms, _swivel));
              }

              return;
            }

            final GLShaderMap map = _jShaders.remove(0);
            final FileStream stream = GlobalFileSystem.getFile(map.path);
            if (stream.exists() == false) {
              System.out.println("Unable to find: " + map.path);
              readShaders(_name, _jShaders, _glShaders, _uniforms, _swivel);
              return;
            }

            stream.getStringInCallback(
                new StringInCallback() {
                  private final StringBuilder source = new StringBuilder();

                  public int resourceAsString(final String[] _resource, final int _length) {
                    for (int i = 0; i < _length; i++) {
                      source.append(_resource[i]);
                      source.append('\n');
                    }

                    return 1;
                  }

                  public void start() {}

                  public void end() {
                    _glShaders.add(new GLShader(map.type, map.path, source.toString()));
                    readShaders(_name, _jShaders, _glShaders, _uniforms, _swivel);
                  }
                },
                1);
          }
        });
  }