Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 950511ed

1 08f92d82 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
20
package org.distorted.examples.vertex3d;
21
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLES20;
25
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 794e6c4f Leszek Koltunski
import org.distorted.library.DistortedBitmap;
30 261fe5bd Leszek Koltunski
import org.distorted.library.DistortedObject;
31 08f92d82 Leszek Koltunski
import org.distorted.library.EffectTypes;
32 950511ed Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
33 833685d0 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
34 08f92d82 Leszek Koltunski
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36
37
import java.io.IOException;
38
import java.io.InputStream;
39
40
import javax.microedition.khronos.egl.EGLConfig;
41
import javax.microedition.khronos.opengles.GL10;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
class Vertex3DRenderer implements GLSurfaceView.Renderer
46
{
47
    private GLSurfaceView mView;
48 d2337a3a Leszek Koltunski
    private DistortedObject mObject;
49 56cbe1cf Leszek Koltunski
    private DistortedBitmap mBackground;
50 950511ed Leszek Koltunski
    private DistortedBitmap mCenter;
51 261fe5bd Leszek Koltunski
    private int mObjWidth, mObjHeight;
52 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
53 950511ed Leszek Koltunski
54
    private Dynamic3D mMoveInter;
55
    private Static3D mMovePoint;
56
    private float mFactorObj;
57
58 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
59 d2337a3a Leszek Koltunski
    int mScreenMin;
60 833685d0 Leszek Koltunski
61 950511ed Leszek Koltunski
62 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
    public Vertex3DRenderer(GLSurfaceView v)
65
      {
66
      mView = v;
67 d40cfeb2 Leszek Koltunski
68 56cbe1cf Leszek Koltunski
      mObject     = ((Vertex3DActivity)v.getContext()).getObject();
69
      mBackground = new DistortedBitmap(100, 100, 1);
70 950511ed Leszek Koltunski
      mCenter     = new DistortedBitmap(100, 100, 1);
71 d40cfeb2 Leszek Koltunski
72 261fe5bd Leszek Koltunski
      mObjWidth = mObject.getWidth();
73
      mObjHeight= mObject.getHeight();
74 d40cfeb2 Leszek Koltunski
75 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
76
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
77
78
      mQuatInt1 = new DynamicQuat(0,0.5f);
79
      mQuatInt2 = new DynamicQuat(0,0.5f);
80
81
      mQuatInt1.add(mQuat1);
82
      mQuatInt2.add(mQuat2);
83 950511ed Leszek Koltunski
84
      mMovePoint= new Static3D(0,0,0);
85
      mMoveInter= new Dynamic3D();
86
      mMoveInter.add(mMovePoint);
87
      }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
    public void setCenter(float x, float y)
92
      {
93
      mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
94 08f92d82 Leszek Koltunski
      }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97 56cbe1cf Leszek Koltunski
98
    public void onDrawFrame(GL10 glUnused)
99 08f92d82 Leszek Koltunski
      {
100
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
101
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
102 71d8ad03 Leszek Koltunski
103
      long time = System.currentTimeMillis();
104
105 56cbe1cf Leszek Koltunski
      mBackground.draw(time);
106 71d8ad03 Leszek Koltunski
      mObject.draw(time);
107 950511ed Leszek Koltunski
      mCenter.draw(time);
108 08f92d82 Leszek Koltunski
      }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111 56cbe1cf Leszek Koltunski
112
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
113 08f92d82 Leszek Koltunski
      {
114 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
115
116 261fe5bd Leszek Koltunski
      mObject.abortEffects(EffectTypes.MATRIX);
117 56cbe1cf Leszek Koltunski
      mBackground.abortEffects(EffectTypes.MATRIX);
118 950511ed Leszek Koltunski
      mCenter.abortEffects(EffectTypes.MATRIX);
119
120
      float factorCen;
121
      int centerSize = mCenter.getWidth();
122 d40cfeb2 Leszek Koltunski
123 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
124 d40cfeb2 Leszek Koltunski
        {
125 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
126
        factorCen  = (0.08f*height)/centerSize;
127 261fe5bd Leszek Koltunski
        }
128 d40cfeb2 Leszek Koltunski
      else
129
        {
130 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
131
        factorCen  = (0.08f*width)/centerSize;
132 d40cfeb2 Leszek Koltunski
        }
133
134 950511ed Leszek Koltunski
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
135
      mMovePoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
136
137 2bf60c29 Leszek Koltunski
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
138
139 950511ed Leszek Koltunski
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
140
      mObject.scale(mFactorObj);
141 2bf60c29 Leszek Koltunski
      mObject.quaternion(mQuatInt1, rotateObj);
142
      mObject.quaternion(mQuatInt2, rotateObj);
143 833685d0 Leszek Koltunski
144 950511ed Leszek Koltunski
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
145
146
      mCenter.quaternion(mQuatInt1, rotateCen);
147
      mCenter.quaternion(mQuatInt2, rotateCen);
148
149
      mCenter.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
150
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
151
      mCenter.move(mMoveInter);
152
      mCenter.scale(factorCen);
153
154 56cbe1cf Leszek Koltunski
      int backgroundSize = mBackground.getWidth();
155
      float factorBackX = ((float)width)/backgroundSize;
156
      float factorBackY = ((float)height)/backgroundSize;
157 833685d0 Leszek Koltunski
158 950511ed Leszek Koltunski
      mBackground.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
159 56cbe1cf Leszek Koltunski
      mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
160 71d8ad03 Leszek Koltunski
161 56cbe1cf Leszek Koltunski
      Distorted.onSurfaceChanged(width, height);
162 08f92d82 Leszek Koltunski
      }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165 56cbe1cf Leszek Koltunski
166
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
167 08f92d82 Leszek Koltunski
      {
168 bcc8e016 Leszek Koltunski
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
169
170
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
171 56cbe1cf Leszek Koltunski
      InputStream is2 = act.getResources().openRawResource(R.raw.water);
172 950511ed Leszek Koltunski
      InputStream is3 = act.getResources().openRawResource(R.raw.center);
173 71d8ad03 Leszek Koltunski
174 950511ed Leszek Koltunski
      Bitmap bitmap1,bitmap2,bitmap3;
175 08f92d82 Leszek Koltunski
        
176
      try 
177
        {
178 71d8ad03 Leszek Koltunski
        bitmap1 = BitmapFactory.decodeStream(is1);
179
        bitmap2 = BitmapFactory.decodeStream(is2);
180 950511ed Leszek Koltunski
        bitmap3 = BitmapFactory.decodeStream(is3);
181 71d8ad03 Leszek Koltunski
        }
182 08f92d82 Leszek Koltunski
      finally 
183
        {
184
        try 
185
          {
186 71d8ad03 Leszek Koltunski
          is1.close();
187
          is2.close();
188 950511ed Leszek Koltunski
          is3.close();
189 08f92d82 Leszek Koltunski
          } 
190
        catch(IOException e) { }
191
        }  
192
      
193 71d8ad03 Leszek Koltunski
      mObject.setBitmap(bitmap1);
194 950511ed Leszek Koltunski
      mBackground.setBitmap(bitmap2);
195
      mCenter.setBitmap(bitmap3);
196
197 08f92d82 Leszek Koltunski
      try
198
        {
199
        Distorted.onSurfaceCreated(mView.getContext());
200
        }
201
      catch(Exception ex)
202
        {
203
        android.util.Log.e("Vertex3D", ex.getMessage() );
204
        }
205
      }
206 56cbe1cf Leszek Koltunski
}