I tried 3 methods to draw a simple shape, but none of them work.
Here is the main class.
I have added <uses-feature android:glEsVersion="0x00020000"
android:required="true" /> in manifest.
In fact, gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
really works,
but the shape is not painted.
package viewer;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.opengl.GLSurfaceView;
import android.content.Context;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.view.MotionEvent;
import com.phyard.android.jni.JNI;
public class MainActivity extends Activity {
private GLSurfaceView mGLView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new DemoGLSurfaceView(this);
setContentView(mGLView);
}
}
class DemoGLSurfaceView extends GLSurfaceView {
GLSurfaceView.Renderer mRenderer;
public DemoGLSurfaceView(Context context) {
super(context);
//mRenderer = new NativeRenderer();
mRenderer = new GLES10Renderer();
//mRenderer = new GLES20Renderer ();
setRenderer(mRenderer);
}
}
class NativeRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
JNI.nativeInit();
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
JNI.nativeResize(w, h);
}
public void onDrawFrame(GL10 gl) {
JNI.nativeRender();
}
}
class GLES10Renderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//JNI.nativeInit();
gl.glShadeModel(gl.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glDisable(gl.GL_CULL_FACE);
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
//JNI.nativeResize(w, h);
gl.glViewport(0, 0, w, h);
}
public void onDrawFrame(GL10 gl) {
//JNI.nativeRender();
FloatBuffer vb = ByteBuffer.allocateDirect( 4 *
squareVertices.length ).order(
ByteOrder.nativeOrder() ).asFloatBuffer();
vb.put(squareVertices);
vb.position(0);
FloatBuffer cb = ByteBuffer.allocateDirect( 4 *
squareColours.length ).order(
ByteOrder.nativeOrder() ).asFloatBuffer();
cb.put(squareColours);
cb.position(0);
gl.glClear(gl.GL_COLOR_BUFFER_BIT |
gl.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -6.0f);
gl.glRotatef(0.0f, 0.0f, 1.0f, -6.0f);
gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
gl.glEnableClientState(gl.GL_COLOR_ARRAY); //
NEW
gl.glVertexPointer(3, gl.GL_FLOAT, 0, vb);
gl.glColorPointer(4, gl.GL_FLOAT, 0, cb); // NEW
gl.glDrawArrays(gl.GL_TRIANGLE_FAN, 0, 4);
//gl.glDisableClientState(gl.GL_COLOR_ARRAY); //
NEW
gl.glFlush();
}
private final float[] squareVertices = {
-30.0f, 30.0f, 0.0f, // Top left
-30.0f, -30.0f, 0.0f, // Bottom left
30.0f, -30.0f, 0.0f, // Bottom right
30.0f, 30.0f, 0.0f // Top right
};
private final float[] squareColours = {
1.0f, 0.0f, 0.0f, 1.0f,// Red - top left - colour for
squareVertices[0]
0.0f, 1.0f, 0.0f, 1.0f, // Green - bottom left -
squareVertices[1]
0.0f, 0.0f, 1.0f, 1.0f, // Blue - bottom right -
squareVerticies[2]
0.5f, 0.5f, 0.5f, 1.0f // Grey - top right-
squareVerticies[3]
};
}
class GLES20Renderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
initShapes ();
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |
GLES20.GL_DEPTH_BUFFER_BIT);
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// Prepare the triangle data
GLES20.glVertexAttribPointer(maPositionHandle, 3,
GLES20.GL_FLOAT, false, 12, triangleVB);
GLES20.glEnableVertexAttribArray(maPositionHandle);
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
mProgram = GLES20.glCreateProgram(); // create
empty OpenGL Program
GLES20.glAttachShader(mProgram, vertexShader); // add the
vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the
fragment shader to program
GLES20.glLinkProgram(mProgram); // creates
OpenGL program executables
// get handle to the vertex shader's vPosition member
maPositionHandle = GLES20.glGetAttribLocation(mProgram,
"vPosition");
}
private void initShapes(){
float triangleCoords[] = {
// X, Y, Z
-0.5f, -0.25f, 0,
0.5f, -0.25f, 0,
0.0f, 0.559016994f, 0
};
// initialize vertex Buffer for triangle
ByteBuffer vbb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
triangleCoords.length * 4);
vbb.order(ByteOrder.nativeOrder());// use the device
hardware's native byte order
triangleVB = vbb.asFloatBuffer(); // create a floating point
buffer from the ByteBuffer
triangleVB.put(triangleCoords); // add the coordinates to
the FloatBuffer
triangleVB.position(0); // set the buffer to read
the first coordinate
}
private 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;
}
private FloatBuffer triangleVB;
private final String vertexShaderCode =
"attribute vec4 vPosition; \n" +
"void main(){ \n" +
" gl_Position = vPosition; \n" +
"} \n";
private final String fragmentShaderCode =
"precision mediump float; \n" +
"void main(){ \n" +
" gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625,
1.0); \n" +
"} \n";
private int mProgram;
private int maPositionHandle;
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en