commit 638b5b5c9bd608318af418f26a7f90d4143bd245
Author: leszek <leszek@koltunski.pl>
Date:   Wed Apr 26 23:37:19 2017 +0100

    More MIPMAP work.

diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index 2515a77..74caf23 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -293,12 +293,16 @@ public class DistortedEffects
     mF.compute(currTime);
 
     float halfZ = halfW*mesh.zFactor;
-    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight);
+
+    float mipmap = 1.0f;//surface.mMipmap;
+    GLES30.glViewport(0, 0, (int)(mipmap*surface.mWidth), (int)(mipmap*surface.mHeight) );
+    //GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight);
 
     mMainProgram.useProgram();
     GLES30.glUniform1i(mMainTextureH, 0);
     surface.setAsOutput(currTime);
     mM.send(surface,halfW,halfH,halfZ);
+    mM.send(surface,halfW,halfH,halfZ);
     mV.send(halfW,halfH,halfZ);
     mF.send(halfW,halfH);
     GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mesh.mMeshPositions);
@@ -317,7 +321,8 @@ public class DistortedEffects
     {
     mBlitProgram.useProgram();
 
-    GLES30.glViewport(0, 0, projection.mWidth, projection.mHeight);
+    float mipmap = 1.0f;//projection.mMipmap;
+    GLES30.glViewport(0, 0, (int)(mipmap*projection.mWidth), (int)(mipmap*projection.mHeight) );
     GLES30.glUniform1i(mBlitTextureH, 0);
     GLES30.glUniform1f( mBlitDepthH , 1.0f-projection.mNear);
     GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index 2dd5cde..a00605f 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -225,23 +225,4 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
     {
     return mColorH[0];
     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Set mipmap level.
- * <p>
- * Trick for speeding up your renders - one can create a pyramid of DistortedFramebuffer objects,
- * each next one some constant FACTOR smaller than the previous (0.5 is the common value), then set
- * the Mipmap Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any
- * scene into such prepared Framebuffer, the library will make sure to scale any Effects used so that
- * the end scene will end up looking identical no matter which object we render to. Identical, that
- * is, except for the loss of quality and gain in speed associated with rendering to a smaller surface.
- *
- * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
- *               does not make any sense (that would result in loss of speed and no gain in quality)
- */
-  public void setMipmap(float mipmap)
-    {
-    mMipmap = mipmap;
-    }
   }
diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index 724e6ca..c21efae 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -28,7 +28,7 @@ import java.util.ArrayList;
 abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave
 {
   private static final int NUM_MIPMAP = 4;
-          static final int CUR_MIPMAP = 0;
+          static final int CUR_MIPMAP = 1;
 
   private static final int ATTACH = 0;
   private static final int DETACH = 1;
@@ -101,7 +101,8 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 
     mBuffer1 = new DistortedFramebuffer[NUM_MIPMAP];
     mBuffer2 = new DistortedFramebuffer[NUM_MIPMAP];
-    mMipmap = 1.0f;
+
+    mMipmap = (this instanceof DistortedScreen ? 0.5f : 1.0f);
 
     createProjection();
     }
@@ -112,11 +113,14 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     {
     if( mWidth>0 && mHeight>1 )
       {
+      float mw = mWidth;//mMipmap*mWidth;
+      float mh = mHeight;//mMipmap*mHeight;
+
       if( mFOV>0.0f )  // perspective projection
         {
         float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
-        float q = mWidth*mNear;
-        float c = mHeight*mNear;
+        float q = mw*mNear;
+        float c = mh*mNear;
 
         float left   = -q/2;
         float right  = +q/2;
@@ -124,20 +128,20 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
         float top    = +c/2;
         float near   =  c/a;
 
-        mDistance    = mHeight/a;
+        mDistance    = mh/a;
         float far    = 2*mDistance-near;
 
         Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
         }
       else             // parallel projection
         {
-        float left   = -mWidth /2.0f;
-        float right  = +mWidth /2.0f;
-        float bottom = -mHeight/2.0f;
-        float top    = +mHeight/2.0f;
-        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
-        mDistance    = mWidth+mHeight;
-        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
+        float left   = -mw/2.0f;
+        float right  = +mw/2.0f;
+        float bottom = -mh/2.0f;
+        float top    = +mh/2.0f;
+        float near   = mw+mh-mh*(1.0f-mNear);
+        mDistance    = mw+mh;
+        float far    = mw+mh+mh*(1.0f-mNear);
 
         Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
         }
@@ -302,6 +306,26 @@ if( !sNew.equals(sOld) )
       }
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Set mipmap level.
+ * <p>
+ * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
+ * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
+ * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
+ * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
+ * scene will end up looking identical no matter which object we render to. Identical, that is, except
+ * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
+ *
+ * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
+ *               does not make any sense (that would result in loss of speed and no gain in quality)
+ */
+  public void setMipmap(float mipmap)
+    {
+    mMipmap = mipmap;
+    createProjection();
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
diff --git a/src/main/java/org/distorted/library/EffectQueueMatrix.java b/src/main/java/org/distorted/library/EffectQueueMatrix.java
index 38dc239..647ccd3 100644
--- a/src/main/java/org/distorted/library/EffectQueueMatrix.java
+++ b/src/main/java/org/distorted/library/EffectQueueMatrix.java
@@ -114,13 +114,15 @@ class EffectQueueMatrix extends EffectQueue
     float x,y,z, sx,sy,sz;
     float mipmap = projection.mMipmap;
 
+    if( mipmap!=1 ) Matrix.scaleM(mViewMatrix, 0, mipmap, mipmap, mipmap);
+
     for(int i=0; i<mNumEffects; i++)
       {
       if (mName[i] == EffectNames.ROTATE.ordinal() )
         {
-        x = mUniforms[NUM_UNIFORMS*i+4]*mipmap;
-        y = mUniforms[NUM_UNIFORMS*i+5]*mipmap;
-        z = mUniforms[NUM_UNIFORMS*i+6]*mipmap;
+        x = mUniforms[NUM_UNIFORMS*i+4];
+        y = mUniforms[NUM_UNIFORMS*i+5];
+        z = mUniforms[NUM_UNIFORMS*i+6];
 
         Matrix.translateM(mViewMatrix, 0, x,-y, z);
         Matrix.rotateM( mViewMatrix, 0, mUniforms[NUM_UNIFORMS*i], mUniforms[NUM_UNIFORMS*i+1], mUniforms[NUM_UNIFORMS*i+2], mUniforms[NUM_UNIFORMS*i+3]);
@@ -128,9 +130,9 @@ class EffectQueueMatrix extends EffectQueue
         }
       else if(mName[i] == EffectNames.QUATERNION.ordinal() )
         {
-        x = mUniforms[NUM_UNIFORMS*i+4]*mipmap;
-        y = mUniforms[NUM_UNIFORMS*i+5]*mipmap;
-        z = mUniforms[NUM_UNIFORMS*i+6]*mipmap;
+        x = mUniforms[NUM_UNIFORMS*i+4];
+        y = mUniforms[NUM_UNIFORMS*i+5];
+        z = mUniforms[NUM_UNIFORMS*i+6];
 
         Matrix.translateM(mViewMatrix, 0, x,-y, z);
         multiplyByQuat(mViewMatrix, mUniforms[NUM_UNIFORMS*i], mUniforms[NUM_UNIFORMS*i+1], mUniforms[NUM_UNIFORMS*i+2], mUniforms[NUM_UNIFORMS*i+3]);
@@ -138,17 +140,17 @@ class EffectQueueMatrix extends EffectQueue
         }
       else if(mName[i] == EffectNames.MOVE.ordinal() )
         {
-        sx = mUniforms[NUM_UNIFORMS*i  ]*mipmap;
-        sy = mUniforms[NUM_UNIFORMS*i+1]*mipmap;
-        sz = mUniforms[NUM_UNIFORMS*i+2]*mipmap;
+        sx = mUniforms[NUM_UNIFORMS*i  ];
+        sy = mUniforms[NUM_UNIFORMS*i+1];
+        sz = mUniforms[NUM_UNIFORMS*i+2];
 
         Matrix.translateM(mViewMatrix, 0, sx,-sy, sz);
         }
       else if(mName[i] == EffectNames.SCALE.ordinal() )
         {
-        sx = mUniforms[NUM_UNIFORMS*i  ]*mipmap;
-        sy = mUniforms[NUM_UNIFORMS*i+1]*mipmap;
-        sz = mUniforms[NUM_UNIFORMS*i+2]*mipmap;
+        sx = mUniforms[NUM_UNIFORMS*i  ];
+        sy = mUniforms[NUM_UNIFORMS*i+1];
+        sz = mUniforms[NUM_UNIFORMS*i+2];
 
         Matrix.scaleM(mViewMatrix, 0, sx, sy, sz);
         }
@@ -158,9 +160,9 @@ class EffectQueueMatrix extends EffectQueue
         sy = mUniforms[NUM_UNIFORMS*i+1];
         sz = mUniforms[NUM_UNIFORMS*i+2];
 
-        x  = mUniforms[NUM_UNIFORMS*i+4]*mipmap;
-        y  = mUniforms[NUM_UNIFORMS*i+5]*mipmap;
-        z  = mUniforms[NUM_UNIFORMS*i+6]*mipmap;
+        x  = mUniforms[NUM_UNIFORMS*i+4];
+        y  = mUniforms[NUM_UNIFORMS*i+5];
+        z  = mUniforms[NUM_UNIFORMS*i+6];
 
         Matrix.translateM(mViewMatrix, 0, x,-y, z);
 
