Project

General

Profile

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

examples / src / main / java / org / distorted / examples / stencil / StencilRenderer.java @ 32d08f39

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.examples.stencil;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.Distorted;
28
import org.distorted.library.DistortedEffects;
29
import org.distorted.library.DistortedScreen;
30
import org.distorted.library.DistortedTexture;
31
import org.distorted.library.EffectNames;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.MeshCubes;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.MeshObject;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39

    
40
import java.io.IOException;
41
import java.io.InputStream;
42

    
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class StencilRenderer implements GLSurfaceView.Renderer
49
{
50
    private GLSurfaceView mView;
51
    private DistortedScreen mScreen;
52
    private DistortedTexture mCubeTex, mFloorTex;
53
    private DistortedEffects mCube1Effects, mCube2Effects, mFloorEffects;
54
    private MeshObject mCubeMesh, mFloorMesh;
55
    private int bmpHeight, bmpWidth;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
    StencilRenderer(GLSurfaceView v)
60
      {
61
      mView = v;
62

    
63
      mCubeMesh  = new MeshCubes(1,1,false);
64
      mFloorMesh = new MeshFlat(1,1);
65

    
66
      mCubeTex   = new DistortedTexture(1,1);
67
      mFloorTex  = new DistortedTexture(1,1);
68

    
69
      mCube1Effects = new DistortedEffects();
70
      mCube2Effects = new DistortedEffects();
71
      mFloorEffects = new DistortedEffects();
72

    
73
      mCube2Effects.brightness(new Static1D(0.5f));
74

    
75
      mScreen = new DistortedScreen();
76
      mScreen.attach(mCubeTex ,mCube1Effects,mCubeMesh );
77
      mScreen.attach(mFloorTex,mFloorEffects,mFloorMesh);
78
      mScreen.attach(mCubeTex ,mCube2Effects,mCubeMesh );
79

    
80
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
   
85
    public void onDrawFrame(GL10 glUnused) 
86
      {
87
      mScreen.render(System.currentTimeMillis());
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
93
      {
94
      float cw = mCubeTex.getWidth();
95
      float ch = mCubeTex.getHeight();
96
      float cd = mCubeTex.getDepth(mCubeMesh);
97

    
98
      float fw = mFloorTex.getWidth();
99
      float fh = mFloorTex.getHeight();
100
      float fd = mFloorTex.getDepth(mFloorMesh);
101

    
102
      float cubeScale = 0.4f*(width>height ? height/ch:width/cw);
103
      float floorScale= 0.8f*(width>height ? height/fh:width/fw);
104

    
105
      Static3D cubeCenter = new Static3D(cw/2,ch  ,-cd/2);
106
      Static3D floorCenter= new Static3D(fw/2,fh/2,-fd/2);
107

    
108
      Static3D axisX = new Static3D(1,0,0);
109
      Static3D axisY = new Static3D(0,1,0);
110
      Static3D axisZ = new Static3D(0,0,1);
111

    
112
      Dynamic1D rotDyn = new Dynamic1D(5000,0.0f);
113
      rotDyn.setMode(Dynamic1D.MODE_JUMP);
114
      rotDyn.add(new Static1D(  0));
115
      rotDyn.add(new Static1D(360));
116

    
117
      float angle = 30.0f;
118

    
119
      Static1D rotSt1 = new Static1D(angle);
120
      Static1D rotSt2 = new Static1D(angle-90.0f);
121

    
122
      mCube1Effects.abortEffects(EffectTypes.MATRIX);
123
      mCube1Effects.move( new Static3D( (width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2) );
124
      mCube1Effects.scale(cubeScale);
125
      mCube1Effects.rotate(rotSt1, axisX, cubeCenter);
126
      mCube1Effects.rotate(rotDyn, axisY, cubeCenter);
127

    
128
      mCube2Effects.abortEffects(EffectTypes.MATRIX);
129
      mCube2Effects.move( new Static3D( (width-cubeScale*cw)/2 , height/2-cubeScale*ch , cubeScale *cd/2) );
130
      mCube2Effects.scale(cubeScale);
131
      mCube2Effects.rotate(rotSt1, axisX, cubeCenter);
132
      mCube2Effects.rotate(rotDyn, axisY, cubeCenter);
133
      mCube2Effects.scale(new Static3D(1,-1,1) );
134
      mCube2Effects.move( new Static3D( 0, -2*ch , 0) );
135

    
136
      mFloorEffects.abortEffects(EffectTypes.MATRIX);
137
      mFloorEffects.move( new Static3D( (width-floorScale*fw)/2 ,height/2-floorScale*fh/2, floorScale*fd/2) );
138
      mFloorEffects.scale(floorScale);
139
      mFloorEffects.rotate(rotSt2, axisX, floorCenter);
140
      mFloorEffects.rotate(rotDyn, axisZ, floorCenter);
141

    
142
      mScreen.resize(width, height);
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
    
147
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
148
      {
149
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
150
      Bitmap bitmap;
151

    
152
      try
153
        {
154
        bitmap = BitmapFactory.decodeStream(is);
155
        }
156
      finally
157
        {
158
        try
159
          {
160
          is.close();
161
          }
162
        catch(IOException e) { }
163
        }
164

    
165
      bmpHeight = bitmap.getHeight();
166
      bmpWidth  = bitmap.getWidth();
167

    
168
      mCubeTex.setTexture(bitmap);
169
      mFloorTex.setColor(0xff000000);  // ARGB
170

    
171
      DistortedEffects.enableEffect(EffectNames.BRIGHTNESS);
172

    
173
      try
174
        {
175
        Distorted.onCreate(mView.getContext());
176
        }
177
      catch(Exception ex)
178
        {
179
        android.util.Log.e("Stencil", ex.getMessage() );
180
        }
181
      }
182
}
(2-2/3)