public void drawMesh(
      BasicTexture tex, int x, int y, int xyBuffer, int uvBuffer, int indexBuffer, int indexCount) {
    float alpha = mAlpha;
    if (!bindTexture(tex)) return;

    mGLState.setBlendEnabled(mBlendEnabled && (!tex.isOpaque() || alpha < OPAQUE_ALPHA));
    mGLState.setTextureAlpha(alpha);

    // Reset the texture matrix. We will set our own texture coordinates
    // below.
    setTextureCoords(0, 0, 1, 1);

    saveTransform();
    translate(x, y);

    mGL.glLoadMatrixf(mMatrixValues, 0);

    mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, xyBuffer);
    mGL.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);

    mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, uvBuffer);
    mGL.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);

    mGL.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    mGL.glDrawElements(GL11.GL_TRIANGLE_STRIP, indexCount, GL11.GL_UNSIGNED_BYTE, 0);

    mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBoxCoords);
    mGL.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);
    mGL.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);

    restoreTransform();
    mCountDrawMesh++;
  }
 public void drawTexture(
     BasicTexture texture, float[] mTextureTransform, int x, int y, int w, int h) {
   mGLState.setBlendEnabled(mBlendEnabled && (!texture.isOpaque() || mAlpha < OPAQUE_ALPHA));
   if (!bindTexture(texture)) return;
   setTextureCoords(mTextureTransform);
   mGLState.setTextureAlpha(mAlpha);
   textureRect(x, y, w, h);
 }
Beispiel #3
0
  /**
   * Bind VBO for a simple quad. Handy for simple custom RenderLayers Vertices: float[]{ -1, -1, -1,
   * 1, 1, -1, 1, 1 }
   *
   * <p>GL.drawArrays(GL20.TRIANGLE_STRIP, 0, 4);
   */
  public static void bindQuadVertexVBO(int location) {

    if (location >= 0) {
      GLState.bindVertexBuffer(mQuadVerticesID);
      GLState.enableVertexArrays(location, -1);
      gl.vertexAttribPointer(location, 2, GL.FLOAT, false, 0, 0);
    }
  }
  private void drawTexture(BasicTexture texture, int x, int y, int width, int height, float alpha) {
    if (width <= 0 || height <= 0) return;

    mGLState.setBlendEnabled(mBlendEnabled && (!texture.isOpaque() || alpha < OPAQUE_ALPHA));
    if (!bindTexture(texture)) return;
    mGLState.setTextureAlpha(alpha);
    drawBoundTexture(texture, x, y, width, height);
  }
Beispiel #5
0
 private static GLState stfor(Tex tex, int z, boolean clip) {
   TexGL gt;
   if (tex instanceof TexGL) gt = (TexGL) tex;
   else if ((tex instanceof TexSI) && (((TexSI) tex).parent instanceof TexGL))
     gt = (TexGL) ((TexSI) tex).parent;
   else throw (new RuntimeException("Cannot use texture for ground-tile rendering: " + tex));
   GLState ret;
   if (clip)
     ret = GLState.compose(Light.deflight, gcol, gt.draw(), gt.clip(), new MapMesh.MLOrder(z));
   else ret = GLState.compose(Light.deflight, gcol, gt.draw(), new MapMesh.MLOrder(z));
   return (ret);
 }
Beispiel #6
0
  private void createGLState() {
    if (_projectionFeature == null) {
      _projectionFeature = new ProjectionGLFeature(_projectionMatrix.asMatrix44D());
      _glState.addGLFeature(_projectionFeature, false);
    } else {
      _projectionFeature.setMatrix(_projectionMatrix.asMatrix44D());
    }

    if (_modelFeature == null) {
      _modelFeature = new ModelGLFeature(_modelviewMatrix.asMatrix44D());
      _glState.addGLFeature(_modelFeature, false);
    } else {
      _modelFeature.setMatrix(_modelviewMatrix.asMatrix44D());
    }
  }
Beispiel #7
0
 public void add(Rendered r, GLState t) {
   if (curp == null) throw (new RuntimeException("Tried to set up relative slot with no parent"));
   Object seq = null;
   Buffer cos = null;
   if ((t == null) && (curp.statroot == null)) {
     seq = r.staticp();
     curp.cs.copye(cos = new Buffer(cfg), GLState.Slot.Type.SYS);
     Cached c;
     if ((seq != null) && ((c = prevcache.get(new Cached(r, seq, cos, false))) != null)) {
       prevcache.remove(c);
       newcache.put(c, c);
       add(c, curp.cs);
       return;
     }
   }
   Slot s = getslot();
   curp.cs.copy(s.os);
   if (t != null) t.prep(s.os);
   s.os.copy(s.cs);
   if (curp.statroot != null) {
     s.statroot = curp.statroot;
   } else if (seq != null) {
     s.statroot = new Cached(r, seq, cos);
   }
   setup(s, r);
 }
  public void drawTexture(BasicTexture texture, RectF source, RectF target) {
    if (target.width() <= 0 || target.height() <= 0) return;

    // Copy the input to avoid changing it.
    mDrawTextureSourceRect.set(source);
    mDrawTextureTargetRect.set(target);
    source = mDrawTextureSourceRect;
    target = mDrawTextureTargetRect;

    mGLState.setBlendEnabled(mBlendEnabled && (!texture.isOpaque() || mAlpha < OPAQUE_ALPHA));
    if (!bindTexture(texture)) return;
    convertCoordinate(source, target, texture);
    setTextureCoords(source);
    mGLState.setTextureAlpha(mAlpha);
    textureRect(target.left, target.top, target.width(), target.height());
  }
  public void drawRect(float x, float y, float width, float height, GLPaint paint) {
    GL11 gl = mGL;

    mGLState.setColorMode(paint.getColor(), mAlpha);
    mGLState.setLineWidth(paint.getLineWidth());

    saveTransform();
    translate(x, y);
    scale(width, height, 1);

    gl.glLoadMatrixf(mMatrixValues, 0);
    gl.glDrawArrays(GL11.GL_LINE_LOOP, OFFSET_DRAW_RECT, 4);

    restoreTransform();
    mCountDrawLine++;
  }
 private boolean bindTexture(BasicTexture texture) {
   if (!texture.onBind(this)) return false;
   int target = texture.getTarget();
   mGLState.setTextureTarget(target);
   mGL.glBindTexture(target, texture.getId());
   return true;
 }
  public void drawLine(float x1, float y1, float x2, float y2, GLPaint paint) {
    GL11 gl = mGL;

    mGLState.setColorMode(paint.getColor(), mAlpha);
    mGLState.setLineWidth(paint.getLineWidth());

    saveTransform();
    translate(x1, y1);
    scale(x2 - x1, y2 - y1, 1);

    gl.glLoadMatrixf(mMatrixValues, 0);
    gl.glDrawArrays(GL11.GL_LINE_STRIP, OFFSET_DRAW_LINE, 2);

    restoreTransform();
    mCountDrawLine++;
  }
Beispiel #12
0
 private Equ(ED ed) {
   this.desc = ed.clone();
   this.id = desc.id;
   GLState bt = null;
   if (bt == null) {
     Skeleton.BoneOffset bo = ed.res.res.get().layer(Skeleton.BoneOffset.class, ed.at);
     if (bo != null) bt = bo.forpose(pose);
   }
   if ((bt == null) && (skel instanceof Skeleton.ResourceSkeleton)) {
     Skeleton.BoneOffset bo =
         ((Skeleton.ResourceSkeleton) skel).res.layer(Skeleton.BoneOffset.class, ed.at);
     if (bo != null) bt = bo.forpose(pose);
   }
   if (bt == null) {
     Skeleton.Bone bone = skel.bones.get(ed.at);
     if (bone != null) bt = pose.bonetrans(bone.idx);
   }
   if (bt == null)
     throw (new RuntimeException(
         "Transformation "
             + ed.at
             + " for equipment "
             + ed.res
             + " on skeleton "
             + skel
             + " could not be resolved"));
   if ((ed.off.x != 0.0f) || (ed.off.y != 0.0f) || (ed.off.z != 0.0f))
     this.et = GLState.compose(bt, Location.xlate(ed.off));
   else this.et = bt;
 }
  public void fillRect(float x, float y, float width, float height, int color) {
    mGLState.setColorMode(color, mAlpha);
    GL11 gl = mGL;

    saveTransform();
    translate(x, y);
    scale(width, height, 1);

    gl.glLoadMatrixf(mMatrixValues, 0);
    gl.glDrawArrays(GL11.GL_TRIANGLE_STRIP, OFFSET_FILL_RECT, 4);

    restoreTransform();
    mCountFillRect++;
  }
Beispiel #14
0
  private void draw() {

    GLState.setClearColor(mClearColor);

    gl.depthMask(true);
    gl.stencilMask(0xFF);

    gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT | GL.STENCIL_BUFFER_BIT);

    gl.depthMask(false);
    gl.stencilMask(0);

    GLState.test(false, false);
    GLState.blend(false);
    GLState.bindTex2D(-1);
    GLState.useProgram(-1);
    GLState.bindElementBuffer(-1);
    GLState.bindVertexBuffer(-1);

    mViewport.setFrom(mMap);

    if (GLAdapter.debugView) {
      /* modify this to scale only the view, to see
       * which tiles are rendered */
      mViewport.mvp.setScale(0.5f, 0.5f, 1);
      mViewport.viewproj.multiplyLhs(mViewport.mvp);
      mViewport.proj.multiplyLhs(mViewport.mvp);
    }

    /* update layers */
    LayerRenderer[] layers = mMap.layers().getLayerRenderer();

    for (int i = 0, n = layers.length; i < n; i++) {
      LayerRenderer renderer = layers[i];

      if (!renderer.isInitialized) {
        renderer.setup();
        renderer.isInitialized = true;
      }

      renderer.update(mViewport);

      if (renderer.isReady) renderer.render(mViewport);

      if (GLAdapter.debug) GLUtils.checkGlError(renderer.getClass().getName());
    }

    if (GLUtils.checkGlOutOfMemory("finish")) {
      BufferObject.checkBufferUsage(true);
      // FIXME also throw out some textures etc
    }
  }
Beispiel #15
0
  public void onSurfaceCreated() {
    // log.debug(GL.getString(GL20.EXTENSIONS));
    String vendor = gl.getString(GL.VENDOR);
    String renderer = gl.getString(GL.RENDERER);
    String version = gl.getString(GL.VERSION);
    log.debug("{}/{}/{}", vendor, renderer, version);

    // Prevent issue with Adreno 3xx series
    if (renderer != null && renderer.startsWith("Adreno (TM) 3")) {
      log.debug("==> not using glBufferSubData");
      GLAdapter.NO_BUFFER_SUB_DATA = true;
    }

    GLState.init();

    // Set up some vertex buffer objects
    BufferObject.init(200);

    // classes that require GL context for initialization
    RenderBuckets.initRenderer();

    mNewSurface = true;
  }
Beispiel #16
0
 private void instancify() {
   if (!cfg.pref.instancing.val) return;
   List<Buffer> instbuf = new ArrayList<Buffer>();
   int i = 0;
   while ((i < cur) && list[i].d) {
     Slot s = list[i];
     int o = i + 1;
     tryinst:
     {
       /* XXX: How to handle eyeorder and similar things is... tricky. This is an ugly hack. Please replace. */
       if (!(s.r instanceof Rendered.Instanced)
           || (s.os.get(Rendered.order) instanceof Rendered.EyeOrder)) break tryinst;
       boolean copy = (s.statroot != null);
       instbuf.clear();
       instbuf.add(copy ? s.os.copy() : s.os);
       for (; (o < cur) && list[o].d; o++) {
         Slot t = list[o];
         if ((t.r != s.r)
             || (t.statroot != s.statroot)
             || (t.ihash != s.ihash)
             || !s.os.iequals(t.os)) break;
         instbuf.add(copy ? t.os.copy() : t.os);
       }
       if (o - i < INSTANCE_THRESHOLD) break tryinst;
       Rendered ir = ((Rendered.Instanced) s.r).instanced(cfg, instbuf);
       if (ir == null) break tryinst;
       Buffer ist = GLState.inststate(cfg, instbuf);
       if (ist == null) break tryinst;
       s.r = ir;
       s.os = ist;
       s.instnum = instbuf.size();
       for (int u = i + 1; u < o; u++) list[u].skip = true;
       if (ir instanceof Disposable) s.disp = (Disposable) ir;
     }
     i = o;
   }
 }
Beispiel #17
0
  public void onSurfaceChanged(int width, int height) {
    // log.debug("onSurfaceChanged: new={}, {}x{}", mNewSurface, width, height);

    if (width <= 0 || height <= 0) return;

    gl.viewport(0, 0, width, height);

    // GL.scissor(0, 0, width, height);
    // GL.enable(GL20.SCISSOR_TEST);

    gl.clearStencil(0x00);

    gl.disable(GL.CULL_FACE);
    gl.blendFunc(GL.ONE, GL.ONE_MINUS_SRC_ALPHA);

    gl.frontFace(GL.CW);
    gl.cullFace(GL.BACK);

    if (!mNewSurface) {
      mMap.updateMap(false);
      return;
    }

    mNewSurface = false;

    /** initialize quad indices used by Texture- and LineTexRenderer */
    int[] vboIds = GLUtils.glGenBuffers(2);

    mQuadIndicesID = vboIds[0];

    short[] indices = new short[MAX_INDICES];
    for (int i = 0, j = 0; i < MAX_INDICES; i += 6, j += 4) {
      indices[i + 0] = (short) (j + 0);
      indices[i + 1] = (short) (j + 1);
      indices[i + 2] = (short) (j + 2);

      indices[i + 3] = (short) (j + 2);
      indices[i + 4] = (short) (j + 1);
      indices[i + 5] = (short) (j + 3);
    }
    ShortBuffer buf = MapRenderer.getShortBuffer(indices.length);
    buf.put(indices);
    buf.flip();

    GLState.bindElementBuffer(mQuadIndicesID);
    gl.bufferData(GL.ELEMENT_ARRAY_BUFFER, indices.length * 2, buf, GL.STATIC_DRAW);
    GLState.bindElementBuffer(0);

    /** initialize default quad */
    FloatBuffer floatBuffer = MapRenderer.getFloatBuffer(8);
    float[] quad = new float[] {-1, -1, -1, 1, 1, -1, 1, 1};
    floatBuffer.put(quad);
    floatBuffer.flip();
    mQuadVerticesID = vboIds[1];

    GLState.bindVertexBuffer(mQuadVerticesID);
    gl.bufferData(GL.ARRAY_BUFFER, quad.length * 4, floatBuffer, GL.STATIC_DRAW);
    GLState.bindVertexBuffer(0);

    GLState.init();

    mMap.updateMap(true);
  }
Beispiel #18
0
 public void prep(Buffer buf) {
   for (GLState st : states) st.prep(buf);
 }
Beispiel #19
0
 /**
  * Bind indices for rendering up to MAX_QUADS (512), ie. MAX_INDICES (512*6) in one draw call.
  * Vertex order is 0-1-2 2-1-3
  */
 public static void bindQuadIndicesVBO() {
   GLState.bindElementBuffer(mQuadIndicesID);
 }
  private void drawMixed(
      BasicTexture from,
      int toColor,
      float ratio,
      int x,
      int y,
      int width,
      int height,
      float alpha) {
    // change from 0 to 0.01f to prevent getting divided by zero below
    if (ratio <= 0.01f) {
      drawTexture(from, x, y, width, height, alpha);
      return;
    } else if (ratio >= 1) {
      fillRect(x, y, width, height, toColor);
      return;
    }

    mGLState.setBlendEnabled(
        mBlendEnabled && (!from.isOpaque() || !Utils.isOpaque(toColor) || alpha < OPAQUE_ALPHA));

    final GL11 gl = mGL;
    if (!bindTexture(from)) return;

    //
    // The formula we want:
    //     alpha * ((1 - ratio) * from + ratio * to)
    //
    // The formula that GL supports is in the form of:
    //     combo * from + (1 - combo) * to * scale
    //
    // So, we have combo = alpha * (1 - ratio)
    //     and     scale = alpha * ratio / (1 - combo)
    //
    float combo = alpha * (1 - ratio);
    float scale = alpha * ratio / (1 - combo);

    // Interpolate the RGB and alpha values between both textures.
    mGLState.setTexEnvMode(GL11.GL_COMBINE);

    // Specify the interpolation factor via the alpha component of
    // GL_TEXTURE_ENV_COLORs.
    // RGB component are get from toColor and will used as SRC1
    float colorScale = scale * (toColor >>> 24) / (0xff * 0xff);
    setTextureColor(
        ((toColor >>> 16) & 0xff) * colorScale,
        ((toColor >>> 8) & 0xff) * colorScale,
        (toColor & 0xff) * colorScale,
        combo);
    gl.glTexEnvfv(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_COLOR, mTextureColor, 0);

    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_INTERPOLATE);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_ALPHA, GL11.GL_INTERPOLATE);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_CONSTANT);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_RGB, GL11.GL_SRC_COLOR);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_ALPHA, GL11.GL_CONSTANT);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_ALPHA, GL11.GL_SRC_ALPHA);

    // Wire up the interpolation factor for RGB.
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_RGB, GL11.GL_CONSTANT);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_RGB, GL11.GL_SRC_ALPHA);

    // Wire up the interpolation factor for alpha.
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_ALPHA, GL11.GL_CONSTANT);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_ALPHA, GL11.GL_SRC_ALPHA);

    drawBoundTexture(from, x, y, width, height);
    mGLState.setTexEnvMode(GL11.GL_REPLACE);
  }
Beispiel #21
0
  public void dispose() {
    if (_mesh != null) _mesh.dispose();
    if (_backgroundColor != null) _backgroundColor.dispose();

    _glState._release();
  }
Beispiel #22
0
 public void prepo(GLState t) {
   t.prep(curp.os);
 }
Beispiel #23
0
 public void prepc(GLState t) {
   t.prep(curp.cs);
 }
Beispiel #24
0
  void rootdraw(GLState.Applier state, UI ui, BGL gl) {
    GLState.Buffer ibuf = new GLState.Buffer(state.cfg);
    gstate.prep(ibuf);
    ostate.prep(ibuf);
    GOut g = new GOut(gl, state.cgl, state.cfg, state, ibuf, new Coord(w, h));
    state.set(ibuf);

    g.state(ostate);
    g.apply();
    gl.glClearColor(0, 0, 0, 1);
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    synchronized (ui) {
      ui.draw(g);
    }

    if (Config.dbtext) {
      int y = h - 150;
      FastText.aprintf(
          g,
          new Coord(10, y -= 15),
          0,
          1,
          "FPS: %d (%d%%, %d%% idle)",
          fps,
          (int) (uidle * 100.0),
          (int) (ridle * 100.0));
      Runtime rt = Runtime.getRuntime();
      long free = rt.freeMemory(), total = rt.totalMemory();
      FastText.aprintf(
          g,
          new Coord(10, y -= 15),
          0,
          1,
          "Mem: %,011d/%,011d/%,011d/%,011d",
          free,
          total - free,
          total,
          rt.maxMemory());
      FastText.aprintf(g, new Coord(10, y -= 15), 0, 1, "Tex-current: %d", TexGL.num());
      FastText.aprintf(g, new Coord(10, y -= 15), 0, 1, "GL progs: %d", g.st.numprogs());
      GameUI gi = ui.root.findchild(GameUI.class);
      if ((gi != null) && (gi.map != null)) {
        try {
          FastText.aprintf(
              g, new Coord(10, y -= 15), 0, 1, "MV pos: %s (%s)", gi.map.getcc(), gi.map.camera);
        } catch (Loading e) {
        }
        if (gi.map.rls != null)
          FastText.aprintf(
              g,
              new Coord(10, y -= 15),
              0,
              1,
              "Rendered: %,d+%,d(%,d)",
              gi.map.rls.drawn,
              gi.map.rls.instanced,
              gi.map.rls.instancified);
      }
      if (Resource.remote().qdepth() > 0)
        FastText.aprintf(
            g,
            new Coord(10, y -= 15),
            0,
            1,
            "RQ depth: %d (%d)",
            Resource.remote().qdepth(),
            Resource.remote().numloaded());
    }
    Object tooltip;
    try {
      synchronized (ui) {
        tooltip = ui.root.tooltip(mousepos, ui.root);
      }
    } catch (Loading e) {
      tooltip = "...";
    }
    Tex tt = null;
    if (tooltip != null) {
      if (tooltip instanceof Text) {
        tt = ((Text) tooltip).tex();
      } else if (tooltip instanceof Tex) {
        tt = (Tex) tooltip;
      } else if (tooltip instanceof Indir<?>) {
        Indir<?> t = (Indir<?>) tooltip;
        Object o = t.get();
        if (o instanceof Tex) tt = (Tex) o;
      } else if (tooltip instanceof String) {
        if (((String) tooltip).length() > 0) tt = (Text.render((String) tooltip)).tex();
      }
    }
    if (tt != null) {
      Coord sz = tt.sz();
      Coord pos = mousepos.add(sz.inv());
      if (pos.x < 0) pos.x = 0;
      if (pos.y < 0) pos.y = 0;
      g.chcolor(244, 247, 21, 192);
      g.rect(pos.add(-3, -3), sz.add(6, 6));
      g.chcolor(35, 35, 35, 192);
      g.frect(pos.add(-2, -2), sz.add(4, 4));
      g.chcolor();
      g.image(tt, pos);
    }
    ui.lasttip = tooltip;
    Resource curs = ui.root.getcurs(mousepos);
    if (curs != null) {
      if (cursmode == "awt") {
        if (curs != lastcursor) {
          try {
            setCursor(makeawtcurs(curs.layer(Resource.imgc).img, curs.layer(Resource.negc).cc));
            lastcursor = curs;
          } catch (Exception e) {
            cursmode = "tex";
          }
        }
      } else if (cursmode == "tex") {
        Coord dc = mousepos.add(curs.layer(Resource.negc).cc.inv());
        g.image(curs.layer(Resource.imgc), dc);
      }
    }
    state.clean();
    GLObject.disposeall(state.cgl, gl);
  }
Beispiel #25
0
 public void usestate(GLState st) {
   st.prep(cur2d);
 }