如何解决如何在 OpenGL ES 中应用投影和相机视图?
我尝试根据官方文档 (https://developer.android.com/training/graphics/opengl/projection#kotlin) 应用投影和相机视图。不幸的是,我得到了一个空屏幕而不是正三角形。下面是我的 onSurfaceCreated
和 onDrawFrame
函数。我填充了一个不在 onSurfaceChanged
函数中的投影变换矩阵(如文档中所示),因为我从未在我的应用程序中调用过它(在这种情况下,结果是相同的)。
override fun onSurfaceCreated(unused: GL10,config: EGLConfig) {
mTriangle = Triangle()
GLES20.glClearColor(0.0f,0.0f,1.0f)
Matrix.frustumM(projectionMatrix,-ratio,ratio,-1f,1f,3f,7f)
}
override fun onDrawFrame(unused: GL10) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
// Set the camera position (View matrix)
Matrix.setLookAtM(viewMatrix,0f,-3f,1.0f,0.0f)
// Calculate the projection and view transformation
Matrix.multiplyMM(vPMatrix,projectionMatrix,viewMatrix,0)
// Draw shape
mTriangle.draw(vPMatrix)
}
然后我想出了重写我的绘图功能。首先,我按照文档中的说明设置了 vertexshadercode
。
private val vertexshadercode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
// the matrix must be included as a modifier of gl_Position
// Note that the uMVPMatrix factor *must be first* in order
// for the matrix multiplication product to be correct.
" gl_Position = uMVPMatrix * vPosition;" +
"}"
其次我改变了函数本身:
fun draw(mvpMatrix: FloatArray) { // pass in the calculated transformation matrix
// get handle to shape's transformation matrix
vPMatrixHandle = GLES20.glGetUniformlocation(program,"uMVPMatrix")
// Pass the projection and view transformation to the shader
GLES20.gluniformMatrix4fv(vPMatrixHandle,1,false,mvpMatrix,0)
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES,vertexCount)
// disable vertex array
GLES20.gldisabLevertexAttribArray(positionHandle)
}
我得到了一个空屏幕...然后我尝试将上面的代码和它的工作版本结合起来。我得到:
fun draw(mvpMatrix: FloatArray) {
// Add program to OpenGL ES environment
GLES20.gluseProgram(mProgram)
// get handle to vertex shader's vPosition member
positionHandle = GLES20.glGetAttribLocation(mProgram,"vPosition").also {
// Enable a handle to the triangle vertices
GLES20.glEnabLevertexAttribArray(it)
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(
it,COORDS_PER_VERTEX,GLES20.GL_FLOAT,vertexStride,vertexBuffer
)
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformlocation(mProgram,"vColor").also { colorHandle ->
// Set color for drawing the triangle
GLES20.gluniform4fv(colorHandle,color,0)
}
// get handle to shape's transformation matrix
vPMatrixHandle = GLES20.glGetUniformlocation(mProgram,"uMVPMatrix")
// Pass the projection and view transformation to the shader
GLES20.gluniformMatrix4fv(vPMatrixHandle,0)
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES,vertexCount)
// disable vertex array
GLES20.gldisabLevertexAttribArray(it)
}
}
这也不起作用。我怎样才能画出我的三角形?
附言
- 将 3f->2f->1f 更改为
Matrix.frustumM(projectionMatrix,7f)
处的“near”值没有任何作用 - 在具有第二个(大)版本 draw 函数的
gl_Position = vPosition;
中使用gl_Position = uMVPMatrix * vPosition;
而不是vertexshadercode
给出三角形。问题似乎出在矩阵上。
解决方法
好的,终于成功了!
使用了 Matrix.frustumM(projectionMatrix,-1.5f,1.5f,-1f,1f,7f)
,也许一切都和上面一样。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。