Skip to content

Instantly share code, notes, and snippets.

@niusounds
Last active August 29, 2015 13:59
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 niusounds/10709333 to your computer and use it in GitHub Desktop.
Save niusounds/10709333 to your computer and use it in GitHub Desktop.
Hello OpenGL ES Texture
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.opengl.GLES20;
public class GLToolbox {
public static int loadShader(int shaderType, String source) {
int shader = GLES20.glCreateShader(shaderType);
if (shader != 0) {
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
int[] compiled = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
String info = GLES20.glGetShaderInfoLog(shader);
GLES20.glDeleteShader(shader);
shader = 0;
throw new RuntimeException("Could not compile shader " +
shaderType + ":" + info);
}
}
return shader;
}
public static int createProgram(String vertexSource, String fragmentSource) {
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
if (vertexShader == 0) {
return 0;
}
int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
if (pixelShader == 0) {
return 0;
}
int program = GLES20.glCreateProgram();
if (program != 0) {
GLES20.glAttachShader(program, vertexShader);
checkGlError("glAttachShader");
GLES20.glAttachShader(program, pixelShader);
checkGlError("glAttachShader");
GLES20.glLinkProgram(program);
int[] linkStatus = new int[1];
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
if (linkStatus[0] != GLES20.GL_TRUE) {
String info = GLES20.glGetProgramInfoLog(program);
GLES20.glDeleteProgram(program);
program = 0;
throw new RuntimeException("Could not link program: " + info);
}
}
return program;
}
public static void checkGlError(String op) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
throw new RuntimeException(op + ": glError " + error);
}
}
public static void initTexParams() {
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
}
}
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.apache.commons.io.IOUtils;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
@EBean
public class MyRenderer implements Renderer {
private int[] textures = new int[2];
private int[] buffers = new int[1];
private Bitmap bitmap;
private String vertexSource;
private String fragmentSource;
@RootContext
Context ctx;
@AfterInject
void init() {
try {
vertexSource = IOUtils.toString(ctx.getResources().openRawResource(R.raw.texture_vertex_shader));
fragmentSource = IOUtils.toString(ctx.getResources().openRawResource(R.raw.texture_fragment_shader));
} catch (NotFoundException | IOException e) {
throw new RuntimeException(e);
}
bitmap = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.god);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
int program = GLToolbox.createProgram(vertexSource, fragmentSource);
GLES20.glUseProgram(program);
// 変数ポインターを取得
int u_sampler = GLES20.glGetUniformLocation(program, "u_sampler");
int a_position = GLES20.glGetAttribLocation(program, "a_position");
int a_texcoords = GLES20.glGetAttribLocation(program, "a_texcoords");
// テクスチャーを作成
GLES20.glGenTextures(textures.length, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLES20.glUniform1i(u_sampler, 0);
// 図形座標を設定
FloatBuffer positions = newFloatBuffer(new float[] {
// posX, posY, texS, texT
-0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 1.0f, 0.0f
});
GLES20.glGenBuffers(buffers.length, buffers, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, byteSize(positions), positions, GLES20.GL_STATIC_DRAW);
GLES20.glVertexAttribPointer(a_position, 2, GLES20.GL_FLOAT, false, floatBytes(4), floatBytes(0));
GLES20.glEnableVertexAttribArray(a_position);
GLES20.glVertexAttribPointer(a_texcoords, 2, GLES20.GL_FLOAT, false, floatBytes(4), floatBytes(2));
GLES20.glEnableVertexAttribArray(a_texcoords);
// 画面クリアの色を指定
GLES20.glClearColor(0, 0, 0, 1);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}
private static FloatBuffer newFloatBuffer(float[] val) {
FloatBuffer buffer = ByteBuffer.allocateDirect(val.length * Float.SIZE / Byte.SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
buffer.put(val).position(0);
return buffer;
}
private static int byteSize(FloatBuffer buffer) {
return buffer.limit() * Float.SIZE / Byte.SIZE;
}
private static int floatBytes(int stride) {
return stride * Float.SIZE / Byte.SIZE;
}
}
uniform sampler2D u_sampler;
varying vec2 v_texcoords;
void main() {
gl_FragColor = texture2D(u_sampler, v_texcoords);
}
attribute vec4 a_position;
attribute vec2 a_texcoords;
varying vec2 v_texcoords;
void main() {
gl_Position = a_position;
v_texcoords = a_texcoords;
}
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import android.app.Activity;
import android.opengl.GLSurfaceView;
@EActivity(R.layout.main)
public class TextureTest extends Activity {
@ViewById
GLSurfaceView gl;
@Bean
MyRenderer renderer;
@AfterViews
void init() {
gl.setEGLContextClientVersion(2);
gl.setRenderer(renderer);
gl.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment