Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesRenderer.java @ 57d7fdba

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.cubes;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 9c3e749e Leszek Koltunski
import org.distorted.library.effect.FragmentEffectAlpha;
31 37b324c4 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
38 7589635e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
39 9c3e749e Leszek Koltunski
import org.distorted.library.type.Static1D;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Static4D;
41
import org.distorted.library.type.Static3D;
42 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
43 427ab7bf Leszek Koltunski
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 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
54 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
55 57d7fdba Leszek Koltunski
    private MeshBase mMesh;
56 d218d64e leszek
    private DistortedScreen mScreen;
57 8a99c681 Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
58 37b324c4 Leszek Koltunski
    private Static3D mMove, mScale, mCenter;
59 9c3e749e Leszek Koltunski
    private Static1D mAlpha;
60 261fe5bd Leszek Koltunski
61 7589635e Leszek Koltunski
    Static4D mQuat1, mQuat2;
62 427ab7bf Leszek Koltunski
    int mScreenMin;
63 9c3e749e Leszek Koltunski
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 2b261a18 Leszek Koltunski
    CubesRenderer(GLSurfaceView v)
67 427ab7bf Leszek Koltunski
      {
68
      mView = v;
69 261fe5bd Leszek Koltunski
70 9c3e749e Leszek Koltunski
      mAlpha = new Static1D(1.0f);
71
72 37b324c4 Leszek Koltunski
      mMove = new Static3D(0,0,0);
73
      mScale= new Static3D(1,1,1);
74
      mCenter=new Static3D(0,0,0);
75
76 e8b6aa95 Leszek Koltunski
      CubesActivity act = (CubesActivity)v.getContext();
77
78 f6d884d5 Leszek Koltunski
      mTexture = act.getTexture();
79 7fca6741 Leszek Koltunski
      mMesh = act.getMesh();
80 261fe5bd Leszek Koltunski
81 f6d884d5 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
82
      mObjHeight= mTexture.getHeight();
83 8a99c681 Leszek Koltunski
      mObjDepth = mTexture.getDepth(mMesh);
84 261fe5bd Leszek Koltunski
85 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
86
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
87 427ab7bf Leszek Koltunski
      
88 37b324c4 Leszek Koltunski
      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 833685d0 Leszek Koltunski
94 37b324c4 Leszek Koltunski
      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 9c3e749e Leszek Koltunski
      mEffects.apply( new FragmentEffectAlpha(mAlpha));
100 392e16fd Leszek Koltunski
101 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
102 9c3e749e Leszek Koltunski
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
103 427ab7bf Leszek Koltunski
      }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
    public void onDrawFrame(GL10 glUnused) 
108
      {
109 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
110 427ab7bf Leszek Koltunski
      }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
115
      {
116
      mScreenMin = width<height ? width:height;
117 37b324c4 Leszek Koltunski
      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 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
122 427ab7bf Leszek Koltunski
      }
123
124 9c3e749e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
159 fe59d375 Leszek Koltunski
160
      mScreen.detachAll();
161
      mScreen.attach(mTexture,mEffects,mMesh);
162
163 9c3e749e Leszek Koltunski
      FragmentEffectAlpha.enable();
164
165 427ab7bf Leszek Koltunski
      try
166
        {
167 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
168 427ab7bf Leszek Koltunski
        }
169
      catch(Exception ex)
170
        {
171
        android.util.Log.e("Cubes", ex.getMessage() );
172
        }
173
      }
174
}