Trangle in openGl with Android
[pullquote align="normal"]
[/pullquote]
Trangle in openGl with Android
package com.technegames.openglestutorial;
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 android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
public class TriangleActivity extends Activity implements android.opengl.GLSurfaceView.Renderer
{
private static final String TAG = TriangleActivity.class.getSimpleName();
private static final int Coordinates_In_A_Vertex = 2;
private static final int Vertices_In_A_Triangle = 3;
private static final int Bits_In_A_Byte = 8;
private static final int Bits_In_A_Float = Float.SIZE;
private static final int Bytes_In_A_Float = Bits_In_A_Float / Bits_In_A_Byte;
private GLSurfaceView glSurfaceView;
private FloatBuffer vertices;
private int width;
private int height;
/*
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setRenderer(this);
setContentView(glSurfaceView);
}
/*
* Called when the activity is resumed.
* The activity is resumed whenever it is being presented to user (from the background).
*/
@Override
public void onResume()
{
super.onResume();
glSurfaceView.onResume();
}
/*
* Called when the activity is paused.
* The activity is paused whenever it is placed into the background. Various things can cause this (pressing the home button, receiving a phone call, turning off the display, etc.).
*/
@Override
public void onPause()
{
glSurfaceView.onPause();
super.onPause();
}
@Override
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glColor4f(0.64313725490196f, 0.77647058823529f, 0.22352941176471f, 1);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertices);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
Log.d(TAG, "Surface changed! width = " + String.valueOf(width) + ", height = " + String.valueOf(height));
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
Log.d(TAG, "Surface Created!");
width = glSurfaceView.getWidth();
height = glSurfaceView.getHeight();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, 1, 0, 1, 1, -1);
gl.glClearColor(1, 1, 1, 1);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(Coordinates_In_A_Vertex * Vertices_In_A_Triangle * Bytes_In_A_Float);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
vertices.put(new float[]
{
0.0f, 0.0f,
0.5f, 1.0f,
1.0f, 0.0f
}
);
vertices.flip();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
}
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 android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
public class TriangleActivity extends Activity implements android.opengl.GLSurfaceView.Renderer
{
private static final String TAG = TriangleActivity.class.getSimpleName();
private static final int Coordinates_In_A_Vertex = 2;
private static final int Vertices_In_A_Triangle = 3;
private static final int Bits_In_A_Byte = 8;
private static final int Bits_In_A_Float = Float.SIZE;
private static final int Bytes_In_A_Float = Bits_In_A_Float / Bits_In_A_Byte;
private GLSurfaceView glSurfaceView;
private FloatBuffer vertices;
private int width;
private int height;
/*
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setRenderer(this);
setContentView(glSurfaceView);
}
/*
* Called when the activity is resumed.
* The activity is resumed whenever it is being presented to user (from the background).
*/
@Override
public void onResume()
{
super.onResume();
glSurfaceView.onResume();
}
/*
* Called when the activity is paused.
* The activity is paused whenever it is placed into the background. Various things can cause this (pressing the home button, receiving a phone call, turning off the display, etc.).
*/
@Override
public void onPause()
{
glSurfaceView.onPause();
super.onPause();
}
@Override
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glColor4f(0.64313725490196f, 0.77647058823529f, 0.22352941176471f, 1);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertices);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
Log.d(TAG, "Surface changed! width = " + String.valueOf(width) + ", height = " + String.valueOf(height));
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
Log.d(TAG, "Surface Created!");
width = glSurfaceView.getWidth();
height = glSurfaceView.getHeight();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, 1, 0, 1, 1, -1);
gl.glClearColor(1, 1, 1, 1);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(Coordinates_In_A_Vertex * Vertices_In_A_Triangle * Bytes_In_A_Float);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
vertices.put(new float[]
{
0.0f, 0.0f,
0.5f, 1.0f,
1.0f, 0.0f
}
);
vertices.flip();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
}
----------------------------***********************------------------------
output
----------------------------**************************-----------------------
goood work
ReplyDelete