Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesRenderer.java @ 107e4b72

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.cubes;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24

    
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

    
28
import org.distorted.examples.R;
29

    
30
import org.distorted.library.effect.FragmentEffectAlpha;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshObject;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static4D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.main.Distorted;
43

    
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLSurfaceView;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class CubesRenderer implements GLSurfaceView.Renderer 
51
{
52
    private GLSurfaceView mView;
53
    private DistortedTexture mTexture;
54
    private DistortedEffects mEffects;
55
    private MeshObject mMesh;
56
    private DistortedScreen mScreen;
57
    private int mObjWidth, mObjHeight, mObjDepth;
58
    private Static3D mMove, mScale, mCenter;
59
    private Static1D mAlpha;
60

    
61
    Static4D mQuat1, mQuat2;
62
    int mScreenMin;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    CubesRenderer(GLSurfaceView v)
67
      {
68
      mView = v;
69

    
70
      mAlpha = new Static1D(1.0f);
71

    
72
      mMove = new Static3D(0,0,0);
73
      mScale= new Static3D(1,1,1);
74
      mCenter=new Static3D(0,0,0);
75

    
76
      CubesActivity act = (CubesActivity)v.getContext();
77

    
78
      mTexture = act.getTexture();
79
      mMesh = act.getMesh();
80

    
81
      mObjWidth = mTexture.getWidth();
82
      mObjHeight= mTexture.getHeight();
83
      mObjDepth = mTexture.getDepth(mMesh);
84

    
85
      mQuat1 = new Static4D(0,0,0,1);  // unity
86
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
87
      
88
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
89
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
90

    
91
      quatInt1.add(mQuat1);
92
      quatInt2.add(mQuat2);
93

    
94
      mEffects = new DistortedEffects();
95
      mEffects.apply( new MatrixEffectMove(mMove) );
96
      mEffects.apply( new MatrixEffectScale(mScale));
97
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
98
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
99
      mEffects.apply( new FragmentEffectAlpha(mAlpha));
100

    
101
      mScreen = new DistortedScreen();
102
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
    public void onDrawFrame(GL10 glUnused) 
108
      {
109
      mScreen.render( System.currentTimeMillis() );
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
115
      {
116
      mScreenMin = width<height ? width:height;
117
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.75f*height)/mObjHeight :  (0.75f*width)/mObjWidth;
118
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
119
      mScale.set(factor,factor,factor);
120
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, -(float)mObjDepth/2 );
121
      mScreen.resize(width, height);
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    void setRenderModeToOIT(boolean oit)
127
      {
128
      mScreen.setOrderIndependentTransparency(oit);
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
    void setTransparency(int level)
134
      {
135
      mAlpha.set((float)level/100.0f);
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
141
      {
142
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
143
      Bitmap bitmap;
144
        
145
      try 
146
        {
147
        bitmap = BitmapFactory.decodeStream(is);
148
        } 
149
      finally 
150
        {
151
        try 
152
          {
153
          is.close();
154
          } 
155
        catch(IOException e) { }
156
        }  
157
      
158
      mTexture.setTexture(bitmap);
159

    
160
      mScreen.detachAll();
161
      mScreen.attach(mTexture,mEffects,mMesh);
162

    
163
      FragmentEffectAlpha.enable();
164

    
165
      try
166
        {
167
        Distorted.onCreate(mView.getContext());
168
        }
169
      catch(Exception ex)
170
        {
171
        android.util.Log.e("Cubes", ex.getMessage() );
172
        }
173
      }
174
}
(2-2/3)