android定义形状,定义形状  |  Android 开发者  |  Android Developers

本文介绍了如何使用OpenGLES在Android设备上定义和绘制图形对象,特别是三角形和方形。首先讲解了OpenGLES坐标系,并展示了如何通过顶点数组定义逆时针顺序的三角形坐标。接着,通过两个三角形的组合来定义一个方形。内容还涵盖了顶点缓冲区的初始化和图形绘制顺序的重要性。
摘要由CSDN通过智能技术生成

能够定义要在 OpenGL ES 视图环境中绘制的形状是创建高端图形作品的第一步。在没有掌握 OpenGL ES 期望您如何定义图形对象的一些基本知识的情况下,使用 OpenGL ES 绘制可能有些棘手。

本课介绍了相对于 Android 设备屏幕的 OpenGL ES 坐标系,以及定义形状、形状面、三角形和方形的基础知识。

定义三角形

通过 OpenGL ES,您可以使用三维空间中的坐标定义绘制的对象。因此,要绘制三角形,您必须先定义其坐标。在 OpenGL 中,执行此操作的典型方式是为坐标定义浮点数的顶点数组。为了最大限度地提高工作效率,您可以将这些坐标写入

Kotlin

// number of coordinates per vertex in this array

const val COORDS_PER_VERTEX = 3

var triangleCoords = floatArrayOf( // in counterclockwise order:

0.0f, 0.622008459f, 0.0f, // top

-0.5f, -0.311004243f, 0.0f, // bottom left

0.5f, -0.311004243f, 0.0f // bottom right

)

class Triangle {

// Set color with red, green, blue and alpha (opacity) values

val color = floatArrayOf(0.63671875f, 0.76953125f, 0.22265625f, 1.0f)

private var vertexBuffer: FloatBuffer =

// (number of coordinate values * 4 bytes per float)

ByteBuffer.allocateDirect(triangleCoords.size * 4).run {

// use the device hardware's native byte order

order(ByteOrder.nativeOrder())

// create a floating point buffer from the ByteBuffer

asFloatBuffer().apply {

// add the coordinates to the FloatBuffer

put(triangleCoords)

// set the buffer to read the first coordinate

position(0)

}

}

}Java

public class Triangle {

private FloatBuffer vertexBuffer;

// number of coordinates per vertex in this array

static final int COORDS_PER_VERTEX = 3;

static float triangleCoords[] = { // in counterclockwise order:

0.0f, 0.622008459f, 0.0f, // top

-0.5f, -0.311004243f, 0.0f, // bottom left

0.5f, -0.311004243f, 0.0f // bottom right

};

// Set color with red, green, blue and alpha (opacity) values

float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

public Triangle() {

// initialize vertex byte buffer for shape coordinates

ByteBuffer bb = ByteBuffer.allocateDirect(

// (number of coordinate values * 4 bytes per float)

triangleCoords.length * 4);

// use the device hardware's native byte order

bb.order(ByteOrder.nativeOrder());

// create a floating point buffer from the ByteBuffer

vertexBuffer = bb.asFloatBuffer();

// add the coordinates to the FloatBuffer

vertexBuffer.put(triangleCoords);

// set the buffer to read the first coordinate

vertexBuffer.position(0);

}

}

默认情况下,OpenGL ES 会假定一个坐标系,其中 [0,0,0] (X,Y,Z) 指定 OpenGL ES 开发者指南。

请注意,此形状的坐标是按照逆时针顺序定义的。绘制顺序非常重要,因为它定义了哪一边是形状的正面(您通常想要绘制的那一面),哪一边是背面(您可以使用 OpenGL ES 面剔除功能选择不绘制的那一面)。如需详细了解面和剔除功能,请参阅 OpenGL ES 开发者指南。

定义方形

在 OpenGL 中定义三角形非常简单,但如果您希望定义稍微复杂一点的图形呢?假设定义方形呢?有多种方式可以执行此操作,但在 OpenGL ES 中绘制此类形状的典型方式是使用两个绘制在一起的三角形:

7488c0ff070402f7790e6ce4329c7b54.png

图 1. 使用两个三角形绘制一个方形。

同样,对于表示该形状的两个三角形,您应按逆时针顺序定义顶点,并将这些值放入

Kotlin

// number of coordinates per vertex in this array

const val COORDS_PER_VERTEX = 3

var squareCoords = floatArrayOf(

-0.5f, 0.5f, 0.0f, // top left

-0.5f, -0.5f, 0.0f, // bottom left

0.5f, -0.5f, 0.0f, // bottom right

0.5f, 0.5f, 0.0f // top right

)

class Square2 {

private val drawOrder = shortArrayOf(0, 1, 2, 0, 2, 3) // order to draw vertices

// initialize vertex byte buffer for shape coordinates

private val vertexBuffer: FloatBuffer =

// (# of coordinate values * 4 bytes per float)

ByteBuffer.allocateDirect(squareCoords.size * 4).run {

order(ByteOrder.nativeOrder())

asFloatBuffer().apply {

put(squareCoords)

position(0)

}

}

// initialize byte buffer for the draw list

private val drawListBuffer: ShortBuffer =

// (# of coordinate values * 2 bytes per short)

ByteBuffer.allocateDirect(drawOrder.size * 2).run {

order(ByteOrder.nativeOrder())

asShortBuffer().apply {

put(drawOrder)

position(0)

}

}

}Java

public class Square {

private FloatBuffer vertexBuffer;

private ShortBuffer drawListBuffer;

// number of coordinates per vertex in this array

static final int COORDS_PER_VERTEX = 3;

static float squareCoords[] = {

-0.5f, 0.5f, 0.0f, // top left

-0.5f, -0.5f, 0.0f, // bottom left

0.5f, -0.5f, 0.0f, // bottom right

0.5f, 0.5f, 0.0f }; // top right

private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices

public Square() {

// initialize vertex byte buffer for shape coordinates

ByteBuffer bb = ByteBuffer.allocateDirect(

// (# of coordinate values * 4 bytes per float)

squareCoords.length * 4);

bb.order(ByteOrder.nativeOrder());

vertexBuffer = bb.asFloatBuffer();

vertexBuffer.put(squareCoords);

vertexBuffer.position(0);

// initialize byte buffer for the draw list

ByteBuffer dlb = ByteBuffer.allocateDirect(

// (# of coordinate values * 2 bytes per short)

drawOrder.length * 2);

dlb.order(ByteOrder.nativeOrder());

drawListBuffer = dlb.asShortBuffer();

drawListBuffer.put(drawOrder);

drawListBuffer.position(0);

}

}

通过此示例,您可以简单了解如何使用 OpenGL 绘制比较复杂的形状。通常,您会使用各种三角形绘制对象。在下一节课中,您将学习如何在屏幕上绘制这些形状。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值