Revision 67038dda
Added by Leszek Koltunski over 5 years ago
| src/main/java/org/distorted/examples/monalisa/MonaLisaRenderer.java | ||
|---|---|---|
| 51 | 51 |
private MeshRectangles mMesh; |
| 52 | 52 |
private DistortedScreen mScreen; |
| 53 | 53 |
private Static3D mScale; |
| 54 |
private float mBmpRatio; |
|
| 54 | 55 |
|
| 55 | 56 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 56 | 57 |
|
| ... | ... | |
| 59 | 60 |
mView = v; |
| 60 | 61 |
|
| 61 | 62 |
// MonaLisa bitmap is 320x366, this is thus (90,108) pixels from the lower-left corner |
| 62 |
Static3D centerLeft = new Static3D( 90-320/2, 108-366/2,0);
|
|
| 63 |
Static3D centerLeft = new Static3D( ( 90-320*0.5f)/320.0f, (108-366*0.5f)/366.0f, 0);
|
|
| 63 | 64 |
// (176,111) from the lower left |
| 64 |
Static3D centerRight = new Static3D(176-320/2, 111-366/2,0);
|
|
| 65 |
Static3D centerRight = new Static3D( (176-320*0.5f)/320.0f, (111-366*0.5f)/366.0f, 0);
|
|
| 65 | 66 |
|
| 66 | 67 |
// two regions defining the areas affected by the Distort effect |
| 67 |
Static4D rLeft = new Static4D(-10, 10, 0, 25);
|
|
| 68 |
Static4D rRight = new Static4D( 10, 5, 0, 25);
|
|
| 68 |
Static4D rLeft = new Static4D( -10/320.0f, 10/366.0f, 0, 25/320.0f);
|
|
| 69 |
Static4D rRight = new Static4D( 10/320.0f, 5/366.0f, 0, 25/320.0f);
|
|
| 69 | 70 |
|
| 70 | 71 |
// two dynamics for interpolating through vectors of distortion. Interpolate |
| 71 | 72 |
// every 1000 miliseconds, indefinately ('0.0f').
|
| ... | ... | |
| 73 | 74 |
Dynamic3D dRight= new Dynamic3D(1000,0.0f); |
| 74 | 75 |
|
| 75 | 76 |
// two vectors of distortion the left tip of the mouth gets distorted with - |
| 76 |
// interpolated from (0,0,0) - i.e. no distortion - to (-20,20,0), i.e.
|
|
| 77 |
// interpolated from (0,0,0) - i.e. no distortion - to (-20 pixels, 20 pixels, 0), i.e.
|
|
| 77 | 78 |
// slightly to the top left. |
| 78 |
dLeft.add ( new Static3D( 0, 0, 0) );
|
|
| 79 |
dLeft.add ( new Static3D(-20, 20, 0) );
|
|
| 79 |
dLeft.add ( new Static3D( 0, 0, 0) );
|
|
| 80 |
dLeft.add ( new Static3D(-20/320.0f, 20/366.0f, 0) );
|
|
| 80 | 81 |
|
| 81 | 82 |
// likewise two vectors the right tip is distorted with. |
| 82 |
dRight.add( new Static3D( 0, 0, 0) );
|
|
| 83 |
dRight.add( new Static3D( 20, 10, 0) );
|
|
| 83 |
dRight.add( new Static3D( 0, 0, 0) );
|
|
| 84 |
dRight.add( new Static3D( 20/320.0f, 10/366.0f, 0) );
|
|
| 84 | 85 |
|
| 86 |
// Equip MonaLisa with the Effects we want to draw her with - i.e. two Distorts of the mouth |
|
| 85 | 87 |
mEffects = new DistortedEffects(); |
| 86 | 88 |
mEffects.apply( new VertexEffectDistort(dLeft , centerLeft , rLeft ) ); |
| 87 | 89 |
mEffects.apply( new VertexEffectDistort(dRight, centerRight, rRight) ); |
| 88 | 90 |
|
| 91 |
// ... and a Scale - so far by (1,1,1), i.e. no scale. The mScale point will be computed |
|
| 92 |
// later, in onSurfaceChanged, when we actually know the size of the screen and know how |
|
| 93 |
// much to scale the initial Mesh (which is 1x1x0 in size) |
|
| 89 | 94 |
mScale= new Static3D(1,1,1); |
| 90 | 95 |
mEffects.apply(new MatrixEffectScale(mScale)); |
| 91 | 96 |
|
| ... | ... | |
| 103 | 108 |
|
| 104 | 109 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
| 105 | 110 |
{
|
| 106 |
float horiRatio = (float)width / mMesh.getStretchX(); |
|
| 107 |
float vertRatio = (float)height/ mMesh.getStretchY(); |
|
| 108 |
float factor = horiRatio > vertRatio ? vertRatio : horiRatio; |
|
| 111 |
float min= width>height ? height : width; |
|
| 109 | 112 |
|
| 110 |
mScale.set( factor,factor,factor );
|
|
| 113 |
mScale.set( min, min*mBmpRatio, 1 );
|
|
| 111 | 114 |
mScreen.resize(width, height); |
| 112 | 115 |
} |
| 113 | 116 |
|
| ... | ... | |
| 131 | 134 |
catch(IOException e) { }
|
| 132 | 135 |
} |
| 133 | 136 |
|
| 134 |
int bmpHeight = bitmap.getHeight(); |
|
| 135 |
int bmpWidth = bitmap.getWidth(); |
|
| 137 |
mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth(); |
|
| 136 | 138 |
|
| 137 | 139 |
// We could have gotten here after the activity went to the background |
| 138 | 140 |
// for a brief amount of time; in this case mTexture is already created. |
| 139 |
// Do not leak memory by creating it the second time around.
|
|
| 141 |
// Do not create it the second time around then.
|
|
| 140 | 142 |
if( mTexture==null ) mTexture = new DistortedTexture(); |
| 141 | 143 |
|
| 142 | 144 |
// likewise the Mesh |
| 143 |
// This will make the Mesh stretched by bmpWidth x bmpHeight even before any effects |
|
| 144 |
// are applied to it (the Mesh - MeshRectangles - is flat, so the third parameter does not |
|
| 145 |
// not matter). bmpWight x bmpHeight is the size of the Bitmap, thus this means that we can |
|
| 146 |
// conveniently work with Effects thinking in Bitmap's native size in pixels. The origin is |
|
| 147 |
// in Bitmap's lower-left corner, thus e.g. a rotation which is meant to rotate the bitmap |
|
| 148 |
// around its center has to be centered at (bmpWidth/2, bmpHeight/2, 0). |
|
| 149 |
// Without this call, the default size of the Mesh is 1x1x0 ( or 1x1x1 in case of not-flat |
|
| 150 |
// Meshes) so we would need to be rotating around (0.5,0.5,0.0). |
|
| 151 |
if( mMesh==null ) |
|
| 152 |
{
|
|
| 153 |
mMesh = new MeshRectangles(9,9*bmpHeight/bmpWidth); |
|
| 154 |
mMesh.setStretch(bmpWidth,bmpHeight,0); |
|
| 155 |
} |
|
| 145 |
// Create an underlying Mesh of 9x10 vertices - this is for the Distort |
|
| 146 |
// effect to have vertices to distort. We multiply by mBmpRatio here so |
|
| 147 |
// that the Mesh's cells are as close to squares as possible (when they |
|
| 148 |
// finally be rendered with a Scale - see mScale) |
|
| 149 |
if( mMesh==null ) mMesh = new MeshRectangles(9, (int)(9*mBmpRatio) ); |
|
| 156 | 150 |
|
| 157 | 151 |
// even if mTexture wasn't null, we still need to call setTexture() on it |
| 158 | 152 |
// because every time activity goes to background, its OpenGL resources |
| ... | ... | |
| 160 | 154 |
// to recreate the internal OpenGL textures. |
| 161 | 155 |
mTexture.setTexture(bitmap); |
| 162 | 156 |
|
| 157 |
// Build the Scene Graph - attach all Objects we want to be rendered to the Screen. |
|
| 163 | 158 |
mScreen.detachAll(); |
| 164 | 159 |
mScreen.attach(mTexture,mEffects,mMesh); |
| 165 | 160 |
|
| 166 |
// All effects are by default disabled! |
|
| 161 |
// All effects are by default disabled! Enable all we need (and better only those) |
|
| 162 |
// before the call to DistortedLibrary.onCreate(). Best done here. |
|
| 167 | 163 |
VertexEffectDistort.enable(); |
| 168 | 164 |
|
| 169 | 165 |
try |
Also available in: Unified diff
Correct the Rubik app for the recent changes to the library's Node.