Skip to content

Instantly share code, notes, and snippets.

@yamanetoshi
Created February 5, 2015 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamanetoshi/235e53698857435f0b1c to your computer and use it in GitHub Desktop.
Save yamanetoshi/235e53698857435f0b1c to your computer and use it in GitHub Desktop.
背景グラデーションなサンプルコード
package fuga.glsurfaceviewsample;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.TextView;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private GLSurfaceView mGLSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGLSurfaceView = (MyGLSurfaceView)findViewById(R.id.glsurfaceview);
mGLSurfaceView.setRenderer(new GLRenderer());
}
@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mGLSurfaceView.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
abstract class GLRendererBase implements GLSurfaceView.Renderer {
/**
* ログ出力を行う。
*
* @param message
*/
public void log(String message) {
Log.i("TAG", message);
}
};
class GLRenderer extends GLRendererBase {
private int screenWidth, screenHeight;
/**
* OpenGLから割り当てられたテクスチャ名
*/
// private int texture;
private int [] textures;
private int [] widths;
private int [] heights;
private int textureWidth, textureHeight;
private float angle;
/**
* サーフェイス作成時の処理。
*
* @param gl10
* @param eglconfig
*/
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglconfig) {
}
/**
* サーフェイスが変更になった。
*
* @param gl10
* @param w
* 画面横の長さ(ピクセル)
* @param h
* 画面縦の長さ(ピクセル)
*/
@Override
public void onSurfaceChanged(GL10 gl10, int w, int h) {
gl10.glViewport(0, 0, w, h);
screenWidth = w;
screenHeight = h;
angle = -3f;
final int [] resources = { R.drawable.centerlogo };
textures = new int[resources.length];
widths = new int[resources.length];
heights = new int[resources.length];
gl10.glEnable(GL10.GL_TEXTURE_2D);
gl10.glGenTextures(resources.length, textures, 0);
for (int i = 0; i < resources.length; i++) {
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeResource(getResources(), resources[i]);
widths[i] = bitmap.getWidth();
heights[i] = bitmap.getHeight();
gl10.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
gl10.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl10.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
bitmap.recycle();
}
}
private void setTexture(GL10 gl10, int index) {
if (index < 0 || index >= textures.length) {
gl10.glDisable(GL10.GL_TEXTURE_2D);
} else {
gl10.glEnable(GL10.GL_TEXTURE_2D);
gl10.glBindTexture(GL10.GL_TEXTURE_2D, textures[index]);
textureWidth = widths[index];
textureHeight = heights[index];
}
}
private void setTextureArea(GL10 gl10, int x, int y, int w, int h) {
float left = ((float) x / (float) textureWidth), top = ((float) y / (float) textureHeight);
float right = left + ((float) w / (float) textureWidth);
float bottom = top + ((float) h / (float) textureHeight);
// ! 位置情報
float uv[] = {
// ! u v
left, top, // !< 左上
left, bottom, // !< 左下
right, top, // !< 右上
right, bottom, // !< 右下
};
ByteBuffer bb = ByteBuffer.allocateDirect(uv.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(uv);
fb.position(0);
gl10.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl10.glTexCoordPointer(2, GL10.GL_FLOAT, 0, fb);
}
private void drawQuad(GL10 gl10, int x, int y, int w, int h) {
float left = ((float) x / (float) screenWidth) * 2.0f - 1.0f, top = ((float) y / (float) screenHeight) * 2.0f - 1.0f;
float right = left + ((float) w / (float) screenWidth) * 2.0f;
float bottom = top + ((float) h / (float) screenHeight) * 2.0f;
// ! 上下を反転させる
top = -top;
bottom = -bottom;
// ! 位置情報
float positions[] = {
// ! x y z
left, top, 0.0f, // !< 左上
left, bottom, 0.0f, // !< 左下
right, top, 0.0f, // !< 右上
right, bottom, 0.0f, // !< 右下
};
ByteBuffer bb = ByteBuffer.allocateDirect(positions.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(positions);
fb.position(0);
gl10.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl10.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl10.glVertexPointer(3, GL10.GL_FLOAT, 0, fb);
gl10.glMatrixMode(GL10.GL_MODELVIEW);
final float aspect = (float)screenWidth / (float)screenHeight;
gl10.glLoadIdentity();
gl10.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
private void drawBackground(GL10 gl10) {
float left = -1f;
float top = 1f;
float right = 1f;
float bottom = -1f;
// ! 上下を反転させる
top = -top;
bottom = -bottom;
// ! 位置情報
float positions[] = {
// ! x y z
left, top, 0.0f, // !< 左上
left, bottom, 0.0f, // !< 左下
right, top, 0.0f, // !< 右上
right, bottom, 0.0f, // !< 右下
};
ByteBuffer bb = ByteBuffer.allocateDirect(positions.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(positions);
fb.position(0);
float colors [] = {
4f / 255f, 68f / 255f, 154f / 255f, 1f, // 左下
25f / 255f, 140f / 255f, 255f / 255f, 1f, // 左上
3f / 255f, 67f / 255f, 153f / 255f, 1f, // 右下
25f / 255f, 140f / 255f, 255f / 255f, 1f, // 右上
};
ByteBuffer colorBuffer = ByteBuffer.allocateDirect(colors.length*4);
colorBuffer.order(ByteOrder.nativeOrder());
FloatBuffer mColorBuffer = colorBuffer.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
gl10.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl10.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl10.glVertexPointer(3, GL10.GL_FLOAT, 0, fb);
gl10.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
gl10.glMatrixMode(GL10.GL_MODELVIEW);
gl10.glLoadIdentity();
gl10.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
/**
* 毎フレーム描画処理。
*
* @param gl10
*/
@Override
public void onDrawFrame(GL10 gl10) {
gl10.glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
gl10.glClear(GL10.GL_COLOR_BUFFER_BIT);
// ! テクスチャ全体のUVを指定する。
{
// ! UV情報
float uv[] = {
// ! u v
0.0f, 0.0f, // !< 左上
0.0f, 1.0f, // !< 左下
1.0f, 0.0f, // !< 右上
1.0f, 1.0f, // !< 右下
};
ByteBuffer bb = ByteBuffer.allocateDirect(uv.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(uv);
fb.position(0);
gl10.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl10.glTexCoordPointer(2, GL10.GL_FLOAT, 0, fb);
}
{
gl10.glEnable(GL10.GL_BLEND);
gl10.glEnable(GL10.GL_ALPHA);
gl10.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
}
gl10.glPushMatrix();
setTexture(gl10, -1);
drawBackground(gl10);
gl10.glPopMatrix();
gl10.glPushMatrix();
setTexture(gl10, 0);
setTextureArea(gl10, 0, 0, textureWidth, textureHeight);
drawQuad(gl10,
screenWidth / 2 - textureWidth / 2,
screenHeight / 9 - textureHeight / 2,
textureWidth,
textureHeight);
gl10.glPopMatrix();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment