Java 拒绝渲染三角形的OpenGL函数
这可能与我的转换有关,但现在我无法弄清楚这一点,这让我感到很快。我已经包装了绘图代码,以便可以轻松定义新的三角形。然而,当我把它放入一个函数中时,它只显示一个灰色屏幕。Te功能代码如下所示:Java 拒绝渲染三角形的OpenGL函数,java,android,opengl-es,Java,Android,Opengl Es,这可能与我的转换有关,但现在我无法弄清楚这一点,这让我感到很快。我已经包装了绘图代码,以便可以轻松定义新的三角形。然而,当我把它放入一个函数中时,它只显示一个灰色屏幕。Te功能代码如下所示: public void Draw(float[] mViewMatrix, float[] mModelMatrix, float[] mProjectionMatrix, int mPositionHandle, int mColorHandle, int mMVPMatrixHandle) {
public void Draw(float[] mViewMatrix, float[] mModelMatrix, float[] mProjectionMatrix, int mPositionHandle, int mColorHandle, int mMVPMatrixHandle)
{
long time = SystemClock.uptimeMillis() % 10000L;
float angleInDegrees = (360.0f / 10000.0f) * ((int) time);
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
//aBuffer.position(mPositionOffset);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
aBuffer.position(mColorOffset);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
// (which now contains model * view * projection).
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
正在运行的代码是:
public void onDrawFrame(GL10 glUnused)
{
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
// Do a complete rotation every 10 seconds.
long time = SystemClock.uptimeMillis() % 10000L;
float angleInDegrees = (360.0f / 10000.0f) * ((int) time);
// Draw the triangle facing straight on.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle1Vertices);
// Draw one translated a bit down and rotated to be flat on the ground.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.0f, -1.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, 90.0f, 1.0f, 0.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle2Vertices);
// Draw one translated a bit to the right and rotated to be facing to the left.
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 1.0f, 0.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, 90.0f, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
drawTriangle(mTriangle3Vertices);
*/
/*
for (int x = 0; x < staticHolder.objectList.size(); x++)
{
staticHolder.objectList.get(x).Draw(mViewMatrix, mModelMatrix, mProjectionMatrix, mPositionHandle, mColorHandle, mMVPMatrixHandle);
}
*/
}
/**
* Draws a triangle from the given vertex data.
*
* @param aTriangleBuffer The buffer containing the vertex data.
*/
private void drawTriangle(final FloatBuffer aTriangleBuffer)
{
// Pass in the position information
aTriangleBuffer.position(mPositionOffset);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
aTriangleBuffer.position(mColorOffset);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
// (which now contains model * view * projection).
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
接收类别为:
public class Triangle extends shape
{
public Triangle(float[] data)
{
verts = data;
}
}
在以下代码位之后:
aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
必须将顶点放入缓冲区(否则,缓冲区为空!):
这在代码中不起作用的原因是,这三组顶点的缓冲区是预先分配的,然后(在初始化时)将顶点放入其中。它们每次都被简单地传递给该方法,因此它们不必再次被put()
在这一点上,您需要避免在
Draw
方法中进行分配,因为它每帧调用多次,可能会导致渲染速度变慢。分配aBuffer
一次,每次将新顶点放入其中。在以下代码位之后:
aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
必须将顶点放入缓冲区(否则,缓冲区为空!):
这在代码中不起作用的原因是,这三组顶点的缓冲区是预先分配的,然后(在初始化时)将顶点放入其中。它们每次都被简单地传递给该方法,因此它们不必再次被put()
在这一点上,您需要避免在
Draw
方法中进行分配,因为它每帧调用多次,可能会导致渲染速度变慢。分配aBuffer
一次,每次将新的顶点放入其中。为什么要注释掉aBuffer.position(mPositionOffset)代码>?这是一行必需的代码。我必须把它注释掉,因为我在尝试让它工作时弄得一团糟。如果在您取消注释它时它仍然不工作,那么麻烦制造者代码就在别处(可能在曲面或GL对象的初始化中)。为什么第一种方法有效而第二种方法无效?问题可能出在传递给staticHolder.objectList
的缓冲区中。我相信您已经注意到,draw代码本身是工作代码的完美克隆(除了aBuffer
的名称)。您是否验证了正在调用Draw
?为什么要注释掉aBuffer.position(mpositiononoffset)代码>?这是一行必需的代码。我必须把它注释掉,因为我在尝试让它工作时弄得一团糟。如果在您取消注释它时它仍然不工作,那么麻烦制造者代码就在别处(可能在曲面或GL对象的初始化中)。为什么第一种方法有效而第二种方法无效?问题可能出在传递给staticHolder.objectList
的缓冲区中。我相信您已经注意到,draw代码本身是工作代码的完美克隆(除了aBuffer
的名称)。您是否验证了正在调用Draw
?太棒了!非常感谢你!我对GL(在C++上有一个OpenGL大学的课程,这绝对没有任何可教的东西)是非常新的。我已经寻找了几个小时的解决方案!我欠你一个人情!没问题!我也一直在学习GL。。。2.0真是太痛苦了,不是吗?祝你旅途好运!:)呃,由于某种原因,大学/高中对教我数学没什么兴趣,所以几何编程就像一片茂密的森林。你有什么资源可以让我快速了解这一点吗?我发现大多数openGL教程都是由“哦,你应该已经知道这一点了,笨蛋!”这类人编写的,他们的教程完全没有用处,我不知道我在这方面会有多大帮助;我很快就学会了几何(无线性代数…幸运的是,这主要是针对3D的)。我想说的是,你最好的资源要么是书籍(在图书馆、亚马逊或谷歌游戏中查看),要么是非Android OpenGL 2.0教程。与数学无关,但非常有帮助,至少对我来说是这样。太棒了!非常感谢你!我对GL(在C++上有一个OpenGL大学的课程,这绝对没有任何可教的东西)是非常新的。我已经寻找了几个小时的解决方案!我欠你一个人情!没问题!我也一直在学习GL。。。2.0真是太痛苦了,不是吗?祝你旅途好运!:)呃,由于某种原因,大学/高中对教我数学没什么兴趣,所以几何编程就像一片茂密的森林。你有什么资源可以让我快速了解这一点吗?我发现大多数openGL教程都是由“哦,你应该已经知道这一点了,笨蛋!”这类人编写的,他们的教程完全没有用处,我不知道我在这方面会有多大帮助;我很快就学会了几何(无线性代数…幸运的是,这主要是针对3D的)。我想说的是,你最好的资源要么是书籍(在图书馆、亚马逊或谷歌游戏中查看),要么是非Android OpenGL 2.0教程。与数学无关,但至少对我很有帮助。
aBuffer.put(verts);