2010年4月29日 星期四

OpenGL學生第一個作業



package se.jayway.opengl.tutorial;

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

import javax.microedition.khronos.opengles.GL10;

public class Square {
// Our vertices.
private float vertices[] = {
-17.0f, 5.0f, 0.0f, // 0,
-17.0f, 0.0f, 0.0f, // 1,
-15.0f, 5.0f, 0.0f, // 2,
-15.0f, 0.0f, 0.0f, // 3,
-17.0f, -5.0f, 0.0f, //4,
-15.0f, -5.0f, 0.0f, //5,
-13.0f, 5.0f, 0.0f, //6
-11.0f, 5.0f, 0.0f, //7
-13.0f, 0.0f, 0.0f, //8
-11.0f, 0.0f, 0.0f, //9
-13.0f, -5.0f, 0.0f, //10
-11.0f, -5.0f, 0.0f, //11
-9.0f, 5.0f, 0.0f, //12
-9.0f, 0.0f, 0.0f, //13
-9.0f, -5.0f, 0.0f, //14
-6.0f, 2.5f, 0.0f, //15
-6.0f, -2.5f, 0.0f, //16
-2.5f, 5.0f, 0.0f, //17
-3.5f, 0.0f, 0.0f, //18
-4.0f, -5.0f, 0.0f, //19
-1.5f, 0.0f, 0.0f, //20
-1.0f, -5.0f, 0.0f, //21
1.0f, 5.0f, 0.0f, //22
1.0f, -5.0f, 0.0f, //23
4.0f, 5.0f, 0.0f, //24
4.0f, -5.0f, 0.0f, //25
6.0f, 5.0f, 0.0f, //26
6.0f, 0.0f, 0.0f, //27
9.0f, 5.0f, 0.0f, //28
9.0f, -5.0f, 0.0f, //29
11.0f, 5.0f, 0.0f, //30
11.0f, -5.0f, 0.0f, //31
14.0f, 5.0f, 0.0f, //32
14.0f, -5.0f, 0.0f, //33
};

// The order we like to connect them.
private short[] indices = { 0, 1, 0, 2, 2, 3, 1, 3, 3, 5, 4, 5,
6, 7, 6, 8, 8, 10, 8, 9, 9, 11, 10, 11,
12, 13, 12, 15, 15, 13, 13, 14, 13, 16, 16, 14,
17, 18, 18, 19, 17, 20, 20, 21, 18, 20,
22, 24, 22, 23, 23, 25, 24, 25,
26, 27, 26, 28, 28, 29,
30, 31, 30, 32, 32, 33, 31, 33};

// Our vertex buffer.
private FloatBuffer vertexBuffer;

// Our index buffer.
private ShortBuffer indexBuffer;

public Square() {
// a float is 4 bytes, therefore we multiply the number if
// vertices with 4.
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);

// short is 2 bytes, therefore we multiply the number if
// vertices with 2.
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
}

/**
* This function draws our square on screen.
* @param gl
*/
public void draw(GL10 gl) {
// Counter-clockwise winding.
gl.glFrontFace(GL10.GL_CCW);
// Enable face culling.
gl.glEnable(GL10.GL_CULL_FACE);
// What faces to remove with the face culling.
gl.glCullFace(GL10.GL_BACK);

// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

gl.glDrawElements(GL10.GL_LINES, indices.length,
GL10.GL_UNSIGNED_SHORT, indexBuffer);

// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
// Disable face culling.
gl.glDisable(GL10.GL_CULL_FACE);
}

}

沒有留言:

張貼留言