Draw a Pyramid with Texture in OpenGL

Description

The following code shows how to Draw a Pyramid with Texture in OpenGL.

Example

Main Activity Java code


package com.java2s.myapplication4.app;
// w w w  .  ja  v  a  2  s  .c  o m
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MainActivity extends Activity {

    private GLSurfaceView glView;
    private TexturedPyramid pyramid;
    private Bitmap texture;

    @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) {
            texture = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            int textureIds[] = new int[1];
            gl.glGenTextures(1, textureIds, 0);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIds[0]);
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
            texture.recycle();
            pyramid = new TexturedPyramid(textureIds[0]);
        }

        @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, -5.0f);
            pyramid.draw(gl);
        }
    }
}

class TexturedPyramid {

    private int textureId;
    private FloatBuffer vertexBuffer;
    private static final int VERTEX_SIZE = (3 + 2) * 4;
    private float vertices[] = {

            0.0f, 1.0f, 0.0f, 0.5f, 0.0f, // V1 + mapping
            -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // V2
            0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // V3

            0.0f, 1.0f, 0.0f, 0.5f, 0.0f, // V1
            0.0f, 0.0f, -1.0f, 0.0f, 1.0f, // V3
            1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // V4

            0.0f, 1.0f, 0.0f, 0.5f, 0.0f, // V1
            1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // V4
            -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // V2

    };

    private float rotation = 0.1f;

    public TexturedPyramid(int textureId) {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(TexturedPyramid.VERTEX_SIZE * 3 * 3);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuffer.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.flip();
        this.textureId = textureId;
    }

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

        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        vertexBuffer.position(0);
        gl.glVertexPointer(3, GL10.GL_FLOAT, TexturedPyramid.VERTEX_SIZE, vertexBuffer);
        vertexBuffer.position(3);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, TexturedPyramid.VERTEX_SIZE, vertexBuffer);

        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3 * 3);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }
}
Draw a Pyramid with Texture in OpenGL




















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