Skip to content

Instantly share code, notes, and snippets.

@ogenodisho
Created June 29, 2014 02: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 ogenodisho/58fb2f32ce2115dd9195 to your computer and use it in GitHub Desktop.
Save ogenodisho/58fb2f32ce2115dd9195 to your computer and use it in GitHub Desktop.
Ogen's non-working triangle app.
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
public abstract class GLRenderer implements Renderer {
private boolean mFirstDraw;
private boolean mSurfaceCreated;
private int mWidth;
private int mHeight;
private long mLastTime;
private int mFPS;
public GLRenderer() {
mFirstDraw = true;
mSurfaceCreated = false;
mWidth = -1;
mHeight = -1;
mLastTime = System.currentTimeMillis();
mFPS = 0;
}
@Override
public void onSurfaceCreated(GL10 notUsed,
EGLConfig config) {
if (Util.DEBUG) {
Log.i(Util.LOG_TAG, "Surface created.");
}
mSurfaceCreated = true;
mWidth = -1;
mHeight = -1;
}
@Override
public void onSurfaceChanged(GL10 notUsed, int width,
int height) {
if (!mSurfaceCreated && width == mWidth
&& height == mHeight) {
if (Util.DEBUG) {
Log.i(Util.LOG_TAG,
"Surface changed but already handled.");
}
return;
}
if (Util.DEBUG) {
// Android honeycomb has an option to keep the
// context.
String msg = "Surface changed width:" + width
+ " height:" + height;
if (mSurfaceCreated) {
msg += " context lost.";
} else {
msg += ".";
}
Log.i(Util.LOG_TAG, msg);
}
mWidth = width;
mHeight = height;
onCreate(mWidth, mHeight, mSurfaceCreated);
mSurfaceCreated = false;
}
@Override
public void onDrawFrame(GL10 notUsed) {
onDrawFrame(mFirstDraw);
if (Util.DEBUG) {
mFPS++;
long currentTime = System.currentTimeMillis();
if (currentTime - mLastTime >= 1000) {
mFPS = 0;
mLastTime = currentTime;
}
}
if (mFirstDraw) {
mFirstDraw = false;
}
}
public int getFPS() {
return mFPS;
}
public abstract void onCreate(int width, int height,
boolean contextLost);
public abstract void onDrawFrame(boolean firstDraw);
}
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
private GLSurfaceView mSurfaceView;
private GLSurfaceView mGLView;
private Triangle t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (hasGLES20()) {
mGLView = new GLSurfaceView(this);
mGLView.setEGLContextClientVersion(2);
mGLView.setPreserveEGLContextOnPause(true);
mGLView.setRenderer(new GLES20Renderer());
} else {
// Time to get a new phone, OpenGL ES 2.0 not
// supported.
}
setContentView(mGLView);
}
private boolean hasGLES20() {
ActivityManager am = (ActivityManager)
getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo info = am.getDeviceConfigurationInfo();
return info.reqGlEsVersion >= 0x20000;
}
@Override
protected void onResume() {
super.onResume();
/*
* The activity must call the GL surface view's
* onResume() on activity onResume().
*/
if (mSurfaceView != null) {
mSurfaceView.onResume();
}
}
@Override
protected void onPause() {
super.onPause();
/*
* The activity must call the GL surface view's
* onPause() on activity onPause().
*/
if (mSurfaceView != null) {
mSurfaceView.onPause();
}
}
public class GLES20Renderer extends GLRenderer {
@Override
public void onCreate(int width, int height,
boolean contextLost) {
GLES20.glClearColor(0f, 0f, 0f, 1f);
t = new Triangle();
}
@Override
public void onDrawFrame(boolean firstDraw) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT
| GLES20.GL_DEPTH_BUFFER_BIT);
t.draw();
}
}
}
/*
* Copyright (C) 2011 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 java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
/**
* A two-dimensional triangle for use as a drawn object in OpenGL ES 1.0/1.1.
*/
public class Triangle {
private final String vertexShaderCode =
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
private final FloatBuffer vertexBuffer;
private int mProgram, mPositionHandle, mColorHandle;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 2;
static float triangleVertexData[];
static float triangleCoords[] = {
// in counterclockwise order:
0.0f, 0.622008459f,// top
-0.5f, -0.311004243f,// bottom left
0.5f, -0.311004243f // bottom right
};
float color[] = { 1.0f, 0.0f, 0.0f, 1.0f, //r
0.0f, 1.0f, 0.0f, 1.0f, //g
0.0f, 0.0f, 1.0f, 1.0f,}; //b
/**
* Sets up the drawing object data for use in an OpenGL ES context.
*/
public Triangle() {
triangleVertexData = concat(triangleCoords, color);
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
triangleVertexData.length * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(triangleVertexData);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
mProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
// Bind attributes
GLES20.glBindAttribLocation(mProgram, 0, "vPosition");
GLES20.glBindAttribLocation(mProgram, 1, "vColor");
GLES20.glLinkProgram(mProgram); // creates OpenGL ES program executables
}
protected static int loadShader(int type, String shaderCode){
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
/**
* Encapsulates the OpenGL ES instructions for drawing this shape.
*
* @param gl - The OpenGL ES context in which to draw this shape.
*/
public void draw() {
GLES20.glUseProgram(mProgram);
// get handles
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
vertexBuffer.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, 2, GLES20.GL_FLOAT, false,
0, vertexBuffer); // NOTE: A stride of 0 since the data is packed.
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
vertexBuffer.position(6);
GLES20.glVertexAttribPointer(mColorHandle, 4, GLES20.GL_FLOAT, false,
0, vertexBuffer); // NOTE: A stride of 0 since the data is packed.
GLES20.glEnableVertexAttribArray(mColorHandle);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
private float[] concat(float[] A, float[] B) {
int aLen = A.length;
int bLen = B.length;
float[] C= new float[aLen+bLen];
System.arraycopy(A, 0, C, 0, aLen);
System.arraycopy(B, 0, C, aLen, bLen);
return C;
}
}
/**
* Copyright 2013 Per-Erik Bergman (per-erik.bergman@jayway.com)
*
* 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.
*/
public interface Util {
public static final boolean DEBUG = true;
public static final String LOG_TAG = "gles20tut";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment