Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ c523e2ea

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

    
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.library.effect.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.main.Distorted;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshCubes;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37

    
38
import javax.microedition.khronos.egl.EGLConfig;
39
import javax.microedition.khronos.opengles.GL10;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class RubikRenderer implements GLSurfaceView.Renderer
44
{
45
    private static final int CUBE_SIZE = 3;
46
    private static final int VERTICES  = 5;
47
    private static final int SIZE      = 200;
48

    
49
    private GLSurfaceView mView;
50
    private DistortedTexture mTexture;
51
    private DistortedScreen mScreen;
52
    private Static3D mMove, mScale, mCenter;
53
    private MeshCubes[][][] mCubes;
54
    private DistortedEffects[][][] mEffects;
55

    
56
    Static4D mQuat1, mQuat2;
57
    int mScreenMin;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
    RubikRenderer(GLSurfaceView v)
62
      {
63
      mView = v;
64

    
65
      mScreen = new DistortedScreen();
66
   // mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
67

    
68
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
69
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
70

    
71
      mCubes = new MeshCubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
72
      mEffects = new DistortedEffects[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
73
      Static3D[][][] cubeVectors = new Static3D[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
74

    
75
      mMove  = new Static3D(0,0,0);
76
      mScale = new Static3D(1,1,1);
77
      mCenter= new Static3D(0,0,0);
78

    
79
      MatrixEffectMove       move  = new MatrixEffectMove(mMove);
80
      MatrixEffectScale      scale = new MatrixEffectScale(mScale);
81
      MatrixEffectQuaternion quat1 = new MatrixEffectQuaternion(mQuat1, mCenter);
82
      MatrixEffectQuaternion quat2 = new MatrixEffectQuaternion(mQuat2, mCenter);
83

    
84
      // 3x2 bitmap = 6 squares:
85
      //
86
      // RED     GREEN   BLUE
87
      // YELLOW  WHITE   BROWN
88

    
89
      final float ze = 0.0f;
90
      final float ot = 1.0f/3.0f;
91
      final float tt = 2.0f/3.0f;
92
      final float oh = 1.0f/2.0f;
93
      final float of = 1.0f/40.0f;
94

    
95
      final Static4D mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
96
      final Static4D mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
97
      final Static4D mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
98
      final Static4D mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
99
      final Static4D mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
100
      final Static4D mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
101

    
102
      final Static4D mapBlack = new Static4D(ze,ze, ze+of,ze+of);
103

    
104
      Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom;
105

    
106
      for(int x=0; x<CUBE_SIZE; x++)
107
        for(int y=0; y<CUBE_SIZE; y++)
108
          for(int z=0; z<CUBE_SIZE; z++)
109
            {
110
            tmpLeft  = (x==          0 ? mapLeft  :mapBlack);
111
            tmpRight = (x==CUBE_SIZE-1 ? mapRight :mapBlack);
112
            tmpFront = (z==CUBE_SIZE-1 ? mapFront :mapBlack);
113
            tmpBack  = (z==          0 ? mapBack  :mapBlack);
114
            tmpTop   = (y==CUBE_SIZE-1 ? mapTop   :mapBlack);
115
            tmpBottom= (y==          0 ? mapBottom:mapBlack);
116

    
117
            mCubes[x][y][z] = new MeshCubes(VERTICES,VERTICES,VERTICES, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
118

    
119
            cubeVectors[x][y][z] = new Static3D( SIZE*(x-0.5f*(CUBE_SIZE-1)), SIZE*(y-0.5f*(CUBE_SIZE-1)), SIZE*(z-0.5f*(CUBE_SIZE-1)) );
120

    
121
            mEffects[x][y][z] = new DistortedEffects();
122

    
123
            mEffects[x][y][z].apply(move);
124
            mEffects[x][y][z].apply(scale);
125
            mEffects[x][y][z].apply(quat1);
126
            mEffects[x][y][z].apply(quat2);
127
            mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
128
            }
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
   
133
    public void onDrawFrame(GL10 glUnused) 
134
      {
135
      mScreen.render( System.currentTimeMillis() );
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
141
      {
142
      mScreenMin = width<height ? width:height;
143

    
144
      float w = mTexture.getWidth();
145
      float h = mTexture.getHeight();
146
      float d = mTexture.getDepth(mCubes[0][0][0]);
147

    
148
      float factor = 0.6f*(width>height ? height/h:width/w)/CUBE_SIZE;
149

    
150
      mCenter.set(w/2,h/2,d/2);
151
      mMove.set( (width-factor*w)/2 , (height-factor*h)/2 , -factor*d/2 );
152
      mScale.set(factor,factor,factor);
153

    
154
      mScreen.resize(width, height);
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
    
159
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
160
      {
161
      Bitmap bitmap;
162

    
163
      final int S = 128;
164
      final int W = 3*S;
165
      final int H = 2*S;
166
      final int R = S/10;
167
      final int M = S/20;
168

    
169
      Paint paint = new Paint();
170
      bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
171
      Canvas canvas = new Canvas(bitmap);
172

    
173
      paint.setAntiAlias(true);
174
      paint.setTextAlign(Paint.Align.CENTER);
175
      paint.setStyle(Paint.Style.FILL);
176

    
177
      // 3x2 bitmap = 6 squares:
178
      //
179
      // RED     GREEN   BLUE
180
      // YELLOW  WHITE   BROWN
181

    
182
      paint.setColor(0xff000000);                                  // BLACK BACKGROUND
183
      canvas.drawRect(0, 0, W, H, paint);                          //
184

    
185
      paint.setColor(0xffff0000);                                  // RED
186
      canvas.drawRoundRect(  0+M, 0+M,   S-M,   S-M, R, R, paint); //
187
      paint.setColor(0xff00ff00);                                  // GREEN
188
      canvas.drawRoundRect(  S+M, 0+M, 2*S-M,   S-M, R, R, paint); //
189
      paint.setColor(0xff0000ff);                                  // BLUE
190
      canvas.drawRoundRect(2*S+M, 0+M, 3*S-M,   S-M, R, R, paint); //
191
      paint.setColor(0xffffff00);                                  // YELLOW
192
      canvas.drawRoundRect(  0+M, S+M,   S-M, 2*S-M, R, R, paint); //
193
      paint.setColor(0xffffffff);                                  // WHITE
194
      canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
195
      paint.setColor(0xffb5651d);                                  // BROWN
196
      canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
197

    
198
      if( mTexture==null ) mTexture = new DistortedTexture(SIZE,SIZE);
199
      mTexture.setTexture(bitmap);
200

    
201
      mScreen.detachAll();
202

    
203
      for(int x=0; x<CUBE_SIZE; x++)
204
        for(int y=0; y<CUBE_SIZE; y++)
205
          for(int z=0; z<CUBE_SIZE; z++)
206
             mScreen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
207

    
208
      try
209
        {
210
        Distorted.onCreate(mView.getContext());
211
        }
212
      catch(Exception ex)
213
        {
214
        android.util.Log.e("Rubik", ex.getMessage() );
215
        }
216
      }
217
}
(2-2/3)