Project

General

Profile

Download (5.95 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ 8ebbc730

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.main;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLES31;
28

    
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.type.Static3D;
31

    
32
/**
33
 * Class which represents the Screen.
34
 * <p>
35
 * User is able to render to it just like to a DistortedFramebuffer.
36
 */
37
public class DistortedScreen extends DistortedFramebuffer
38
  {
39
  ///// DEBUGGING ONLY /////////////////////////
40
  private boolean mShowFPS;
41

    
42
  private static final int NUM_FRAMES  = 100;
43

    
44
  private MeshObject fpsMesh;
45
  private DistortedTexture fpsTexture;
46
  private DistortedEffects fpsEffects;
47
  private Canvas fpsCanvas;
48
  private Bitmap fpsBitmap;
49
  private Paint mPaint;
50
  private int fpsH, fpsW;
51
  private String fpsString;
52
  private long lastTime=0;
53
  private long[] durations;
54
  private int currDuration;
55
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
56
  ///// END DEBUGGING //////////////////////////
57

    
58
  private int mCurrFBO;
59
  private static final int NUM_FBO = 3;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
// PUBLIC API
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
/**
65
 * Create a new Screen. Initially 1x1 in size.
66
 * <p>
67
 * Has to be followed by a 'resize()' to set the size.
68
 */
69
  public DistortedScreen()
70
    {
71
    super(NUM_FBO,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
72
    mShowFPS = false;
73
    mCurrFBO = 0;
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
/**
78
 * Draws all the attached children to this OutputSurface.
79
 * <p>
80
 * Must be called from a thread holding OpenGL Context.
81
 *
82
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
83
 * @return Number of objects rendered.
84
 */
85
  public int render(long time)
86
    {
87
    if( mShowFPS )
88
      {
89
      if( lastTime==0 ) lastTime = time;
90

    
91
      currDuration++;
92
      if (currDuration >= NUM_FRAMES) currDuration = 0;
93
      durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
94
      durations[currDuration] = time - lastTime;
95

    
96
      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
97

    
98
      mPaint.setColor(0xffffffff);
99
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
100
      mPaint.setColor(0xff000000);
101
      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
102
      fpsTexture.setTexture(fpsBitmap);
103

    
104
      lastTime = time;
105
      }
106

    
107
    int numrender = super.render(time,mCurrFBO);
108

    
109
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
110
    setAsInput(mCurrFBO,0);
111
    GLES31.glColorMask(true,true,true,true);
112
    GLES31.glDepthMask(false);
113
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
114
    GLES31.glDisable(GLES31.GL_DEPTH_TEST);
115
    GLES31.glDisable(GLES31.GL_BLEND);
116

    
117
    DistortedEffects.blitPriv(this);
118

    
119
    if( mShowFPS && fpsTexture.setAsInput())
120
      {
121
      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time, 0);
122
      }
123

    
124
    mCurrFBO++;
125
    if( mCurrFBO>=NUM_FBO ) mCurrFBO=0;
126

    
127
    return numrender+1;
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
/**
132
 * Make the library show Frames Per Second in the upper-left corner.
133
 * <p>
134
 */
135
  public void showFPS()
136
    {
137
    mShowFPS = true;
138

    
139
    fpsW = 120;
140
    fpsH =  70;
141

    
142
    fpsString = "";
143
    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
144
    fpsMesh = new MeshFlat(1,1);
145
    fpsTexture = new DistortedTexture(fpsW,fpsH);
146
    fpsTexture.setTexture(fpsBitmap);
147
    fpsCanvas = new Canvas(fpsBitmap);
148
    fpsEffects = new DistortedEffects();
149
    fpsEffects.apply(mMoveEffect);
150

    
151
    mPaint = new Paint();
152
    mPaint.setAntiAlias(true);
153
    mPaint.setTextAlign(Paint.Align.CENTER);
154
    mPaint.setTextSize(0.7f*fpsH);
155

    
156
    durations = new long[NUM_FRAMES+1];
157
    currDuration = 0;
158

    
159
    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=16;  // Assume FPS will be
160
    durations[NUM_FRAMES] = NUM_FRAMES*16;              // close to 1000/16 ~ 60
161
    }
162
  }
(10-10/21)