Skip to content

Commit

Permalink
Polished lesson six some more.
Browse files Browse the repository at this point in the history
  • Loading branch information
learnopengles committed Oct 10, 2011
1 parent 06c308b commit 015f818
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 65 deletions.
2 changes: 1 addition & 1 deletion android/AndroidOpenGLESLessons/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnopengles.android"
android:versionCode="6" android:versionName="1.0.4.1">
android:versionCode="7" android:versionName="1.0.5">
<uses-sdk
android:minSdkVersion="8" />
<!-- We require OpenGL ES 2.0 -->
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.learnopengles.android.R;

Expand All @@ -24,6 +25,12 @@ public class LessonSixActivity extends Activity
private static final int MIN_DIALOG = 1;
private static final int MAG_DIALOG = 2;

private int mMinSetting = -1;
private int mMagSetting = -1;

private static final String MIN_SETTING = "min_setting";
private static final String MAG_SETTING = "mag_setting";

@Override
public void onCreate(Bundle savedInstanceState)
{
Expand Down Expand Up @@ -74,6 +81,16 @@ public void onClick(View v)
showDialog(MAG_DIALOG);
}
});

// Restore previous settings
if (savedInstanceState != null)
{
mMinSetting = savedInstanceState.getInt(MIN_SETTING, -1);
mMagSetting = savedInstanceState.getInt(MAG_SETTING, -1);

if (mMinSetting != -1) { setMinSetting(mMinSetting); }
if (mMagSetting != -1) { setMagSetting(mMagSetting); }
}
}

@Override
Expand All @@ -94,6 +111,79 @@ protected void onPause()
mGLSurfaceView.onPause();
}

@Override
protected void onSaveInstanceState (Bundle outState)
{
outState.putInt(MIN_SETTING, mMinSetting);
outState.putInt(MAG_SETTING, mMagSetting);
}

private void setMinSetting(final int item)
{
mMinSetting = item;

mGLSurfaceView.queueEvent(new Runnable()
{
@Override
public void run()
{
final int filter;

if (item == 0)
{
filter = GLES20.GL_NEAREST;
}
else if (item == 1)
{
filter = GLES20.GL_LINEAR;
}
else if (item == 2)
{
filter = GLES20.GL_NEAREST_MIPMAP_NEAREST;
}
else if (item == 3)
{
filter = GLES20.GL_NEAREST_MIPMAP_LINEAR;
}
else if (item == 4)
{
filter = GLES20.GL_LINEAR_MIPMAP_NEAREST;
}
else // if (item == 5)
{
filter = GLES20.GL_LINEAR_MIPMAP_LINEAR;
}

mRenderer.setMinFilter(filter);
}
});
}

private void setMagSetting(final int item)
{
mMagSetting = item;

mGLSurfaceView.queueEvent(new Runnable()
{
@Override
public void run()
{
final int filter;

if (item == 0)
{
filter = GLES20.GL_NEAREST;
}
else // if (item == 1)
{
filter = GLES20.GL_LINEAR;
}

mRenderer.setMagFilter(filter);
}
});
}

@Override
protected Dialog onCreateDialog(int id)
{
Expand All @@ -110,41 +200,7 @@ protected Dialog onCreateDialog(int id)
@Override
public void onClick(final DialogInterface dialog, final int item)
{
mGLSurfaceView.queueEvent(new Runnable()
{
@Override
public void run()
{
final int filter;

if (item == 0)
{
filter = GLES20.GL_NEAREST;
}
else if (item == 1)
{
filter = GLES20.GL_LINEAR;
}
else if (item == 2)
{
filter = GLES20.GL_NEAREST_MIPMAP_NEAREST;
}
else if (item == 3)
{
filter = GLES20.GL_NEAREST_MIPMAP_LINEAR;
}
else if (item == 4)
{
filter = GLES20.GL_LINEAR_MIPMAP_NEAREST;
}
else // if (item == 5)
{
filter = GLES20.GL_LINEAR_MIPMAP_LINEAR;
}

mRenderer.setMinFilter(filter);
}
});
setMinSetting(item);
}
});

Expand All @@ -160,25 +216,7 @@ else if (item == 4)
@Override
public void onClick(final DialogInterface dialog, final int item)
{
mGLSurfaceView.queueEvent(new Runnable()
{
@Override
public void run()
{
final int filter;

if (item == 0)
{
filter = GLES20.GL_NEAREST;
}
else // if (item == 1)
{
filter = GLES20.GL_LINEAR;
}

mRenderer.setMagFilter(filter);
}
});
setMagSetting(item);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public boolean onTouchEvent(MotionEvent event)
{
if (mRenderer != null)
{
float deltaX = (x - mPreviousX) / mDensity;
float deltaY = (y - mPreviousY) / mDensity;
float deltaX = (x - mPreviousX) / mDensity / 2f;
float deltaY = (y - mPreviousY) / mDensity / 2f;

mRenderer.mDeltaX += deltaX;
mRenderer.mDeltaY += deltaY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public class LessonSixRenderer implements GLSurfaceView.Renderer
private int mBrickDataHandle;
private int mGrassDataHandle;

/** Temporary place to save the min and mag filter, in case the activity was restarted. */
private int mQueuedMinFilter;
private int mQueuedMagFilter;

// These still work without volatile, but refreshes are not guaranteed to happen.
public volatile float mDeltaX;
public volatile float mDeltaY;
Expand Down Expand Up @@ -435,6 +439,16 @@ public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
mGrassDataHandle = TextureHelper.loadTexture(mActivityContext, R.drawable.noisy_grass_public_domain);
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);

if (mQueuedMinFilter != 0)
{
setMinFilter(mQueuedMinFilter);
}

if (mQueuedMagFilter != 0)
{
setMagFilter(mQueuedMagFilter);
}

// Initialize the accumulated rotation matrix
Matrix.setIdentityM(mAccumulatedRotation, 0);
}
Expand Down Expand Up @@ -557,18 +571,32 @@ public void onDrawFrame(GL10 glUnused)

public void setMinFilter(final int filter)
{
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBrickDataHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, filter);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mGrassDataHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, filter);
if (mBrickDataHandle != 0 && mGrassDataHandle != 0)
{
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBrickDataHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, filter);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mGrassDataHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, filter);
}
else
{
mQueuedMinFilter = filter;
}
}

public void setMagFilter(final int filter)
{
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBrickDataHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, filter);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mGrassDataHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, filter);
if (mBrickDataHandle != 0 && mGrassDataHandle != 0)
{
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBrickDataHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, filter);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mGrassDataHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, filter);
}
else
{
mQueuedMagFilter = filter;
}
}

/**
Expand Down

0 comments on commit 015f818

Please sign in to comment.