gpt4 book ai didi

android - OpenGL ES 顶点/索引

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:01 25 4
gpt4 key购买 nike

我刚刚开始学习 OpenGL ES,但在理解顶点和索引的工作原理时遇到了一些困难。我目前的理解是顶点是形状本身上的一个点,索引代表顶点内的“三角形”。我正在按照教程定义顶点和索引点,如下所示...

顶点数据

-1.0f, -1.0f1.0f, -1.0f-1.0f, 1.0f1.0f, 1.0f

指数数据

0,3,1,0,2,3

我知道定义索引应该总是从一个顶点开始,但对我来说这些数字只是不相加。当我在纸上画这个时,看起来实际绘制的图像应该是两个三角形在一起,形成一个“皇冠”形状。有人可以解释为什么这实际上是在绘制正方形而不是我期望的“皇冠”吗?

Square 类的源代码:

public class Square {

private FloatBuffer mFVertexBuffer;
private ByteBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;

public Square() {

// 2D Points
float[] square = {

-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,

};

byte maxColor = (byte) 225;

/**
* Each line below represents RGB + Alpha transparency
*/
byte colors[] = {

0, maxColor, 0, maxColor,
0, maxColor, maxColor, maxColor,
0, 0, 0, maxColor,
maxColor, 0, maxColor, maxColor,

};

//triangles
byte[] indicies = {

0,3,1,
0,2,3

};

/**
* Make sure that bytes are in correct order, otherwise they might be
* drawn backwards
*/
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(square.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
mFVertexBuffer = byteBuffer.order(ByteOrder.nativeOrder())
.asFloatBuffer();
mFVertexBuffer.put(square);
mFVertexBuffer.position(0);

mColorBuffer = ByteBuffer.allocateDirect(colors.length);
mColorBuffer.put(colors);
mColorBuffer.position(0);

mIndexBuffer = ByteBuffer.allocateDirect(indicies.length);
mIndexBuffer.put(indicies);
mIndexBuffer.position(0);
}

public void draw(GL10 gl) {

/**
* Make open GL only draw the front of the triangle (GL_CW = Graphics
* Library Clockwise)
*
* Back of triangle will not be drawn
*/
gl.glFrontFace(GL11.GL_CW);

/**
* specifies number of elements per vertex
*
* specifies floating point type
*
* Sets stride = 0 bytes* (Stride allows to use different types of data
* interchangably with opengl )
*/
gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);

// 4 because we are using 4 colors in our color bufer array
gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);

/**
* draws the image
*
* first argument specifies geomety format
*/
gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE,
mIndexBuffer);

// Reset to CounterClockwise
gl.glFrontFace(GL11.GL_CCW);

}

如果需要更多信息,请告诉我...

最佳答案

您定义了四个顶点:

2    3

0 1

然后您的索引定义了两个三角形,0-3-1:

     .
...
....
.....

和 0-2-3:

.....
....
...
.

将它们放在一起形成一个正方形。

关于android - OpenGL ES 顶点/索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18723950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com