Pyramid

Description

The following code shows how to create pyramid.

Example

Main Activity Java code


package com.java2s.myapplication3.app;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
//  ww w  . java2s. c  o  m
import javax.microedition.khronos.opengles.GL10;

import javax.microedition.khronos.egl.EGLConfig;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

    private GLSurfaceView glView;
    private ColouredPyramid pyramid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        glView = new GLSurfaceView(this);
        glView.setRenderer(new MyOpenGLRenderer());
        setContentView(glView);
    }

    class MyOpenGLRenderer implements Renderer {
        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            gl.glViewport(0, 0, width, height);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
            gl.glMatrixMode(GL10.GL_MODELVIEW);
            gl.glLoadIdentity();
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            pyramid = new ColouredPyramid();
        }
        @Override
        public void onDrawFrame(GL10 gl) {
            gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glLoadIdentity();
            gl.glTranslatef(0.0f, 0.0f, -10.0f);
            pyramid.draw(gl);
        }
    }
}

/**
 * @author tamas
 *
 */
class ColouredPyramid {

    private FloatBuffer vertexBuffer;
    private static final int VERTEX_SIZE = (3 + 4) * 4;
    private float vertices[] = {
            0.0f, 1.0f, 0.0f, 1, 0, 0, 1, // V1 - red
            -1.0f, 0.0f, 0.0f, 1, 0, 0, 1, // V2 - red
            0.0f, 0.0f, -1.0f, 1, 0, 0, 1, // V3 - red

            0.0f, 1.0f, 0.0f, 0, 1, 0, 1, // V1 - green
            0.0f, 0.0f, -1.0f, 0, 1, 0, 1, // V3 - green
            1.0f, 0.0f, 0.0f, 0, 1, 0, 1, // V4 - green

            0.0f, 1.0f, 0.0f, 0, 0, 1, 1, // V1 - blue
            1.0f, 0.0f, 0.0f, 0, 0, 1, 1, // V4 - blue
            -1.0f, 0.0f, 0.0f, 0, 0, 1, 1, // V2 - blue
    };

    private float rotation = 0.1f;

    public ColouredPyramid() {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(ColouredPyramid.VERTEX_SIZE * 3 * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuffer.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.flip();
    }

    public void draw(GL10 gl) {
        rotation += 1.0f;
        gl.glRotatef(rotation, 1f, 1f, 1f);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        vertexBuffer.position(0);
        gl.glVertexPointer(3, GL10.GL_FLOAT, ColouredPyramid.VERTEX_SIZE, vertexBuffer);
        vertexBuffer.position(3);
        gl.glColorPointer(4, GL10.GL_FLOAT, ColouredPyramid.VERTEX_SIZE, vertexBuffer);

        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3 * 3);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    }
}
Pyramid




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView