我正在使用opengl(GLES20)开发一个Android应用程序。 我成功地渲染了正方形多边形模型 但是我不能让他们看着相机。
我阅读了下面的帖子,但我没抓住重点。
Orientating Figures to look at the Camera with OpenGL
view.SampleGLSurfaceViewRenderer.kt
class SampleGLSurfaceViewRenderer() : GLSurfaceView.Renderer {
var vertexShader: Int = -1
var fragmentShader: Int = -1
var shaderProgram: Int = -1
var attributeHandlers: MutableMap<String, Int> = mutableMapOf()
var uniformHandlers: MutableMap<String, Int> = mutableMapOf()
var sampleData: SampleGLData = SampleGLData()
var modelMatrix: FloatArray = FloatArray(16)
var viewMatrix: FloatArray = FloatArray(16)
var perspectiveMatrix: FloatArray = FloatArray(16)
var aspect: Float = 0.0f
override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
initShadersAndProgram()
initLocationHandlers()
}
override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
GLES20.glViewport(0, 0, width, height)
aspect = width * 1.0f / height
}
override fun onDrawFrame(gl: GL10?) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT)
GLES20.glEnable(GLES20.GL_DEPTH_TEST)
GLES20.glUseProgram(shaderProgram)
attributeHandlers["position"]?.also { positionHandle ->
GLES20.glEnableVertexAttribArray(positionHandle)
GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, sampleData.vertexBuffer)
resetMvpMatrix()
applyMvpMatrix()
GLES20.glDrawElements(GLES20.GL_TRIANGLES, sampleData.indicesBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, sampleData.indicesBuffer)
resetMvpMatrix()
Matrix.translateM(modelMatrix, 0, 0.0f, -4.0f, 0.0f)
applyMvpMatrix()
GLES20.glDrawElements(GLES20.GL_TRIANGLES, sampleData.indicesBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, sampleData.indicesBuffer)
GLES20.glDisableVertexAttribArray(positionHandle)
}
}
private fun applyMvpMatrix() {
uniformHandlers["mvpMatrix"]?.also { mvpMatrixHandler ->
val mvpMatrix: FloatArray = FloatArray(16)
Matrix.multiplyMM(mvpMatrix, 0, perspectiveMatrix, 0, viewMatrix, 0)
Matrix.multiplyMM(mvpMatrix, 0, mvpMatrix, 0, modelMatrix, 0)
GLES20.glUniformMatrix4fv(mvpMatrixHandler, 1, false, mvpMatrix, 0)
}
}
private fun resetMvpMatrix() {
Matrix.setIdentityM(modelMatrix, 0)
Matrix.setLookAtM(viewMatrix, 0,
0.0f, 0.4f, -1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f)
Matrix.perspectiveM(perspectiveMatrix, 0,
120.0f,
aspect,
0.1f, 10.0f)
}
private fun initLocationHandlers() {
attributeHandlers.put("position", GLES20.glGetAttribLocation(shaderProgram, "position"))
uniformHandlers.put("mvpMatrix", GLES20.glGetUniformLocation(shaderProgram, "mvpMatrix"))
}
private fun initShadersAndProgram() {
val vertexShaderSource = """
attribute vec3 position;
uniform mat4 mvpMatrix;
void main(void) {
gl_Position = mvpMatrix * vec4(position, 1.0);
}
""".trimIndent()
val fragmentShaderSource = """
precision mediump float;
void main(void) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
""".trimIndent()
vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER)
GLES20.glShaderSource(vertexShader, vertexShaderSource)
GLES20.glCompileShader(vertexShader)
fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER)
GLES20.glShaderSource(fragmentShader, fragmentShaderSource)
GLES20.glCompileShader(fragmentShader)
shaderProgram = GLES20.glCreateProgram()
GLES20.glAttachShader(shaderProgram, vertexShader)
GLES20.glAttachShader(shaderProgram, fragmentShader)
GLES20.glLinkProgram(shaderProgram)
}
}
data.SampleGLData.kt
class SampleGLData {
var vertexCoords: Int = 4
val vertexArray: FloatArray
val vertexBuffer: FloatBuffer
val indicesArray: ShortArray
val indicesBuffer: ShortBuffer
init {
vertexArray = floatArrayOf(
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f
)
vertexBuffer = createFloatBuffer(vertexArray)
indicesArray = shortArrayOf(
0, 1, 2,
0, 2, 3
)
indicesBuffer = createShortBuffer(indicesArray)
}
fun createFloatBuffer(array: FloatArray): FloatBuffer {
return ByteBuffer.allocateDirect(array.size * 4).run {
order(ByteOrder.nativeOrder())
asFloatBuffer().apply {
put(array)
position(0)
}
}
}
fun createShortBuffer(array: ShortArray): ShortBuffer {
return ByteBuffer.allocateDirect(array.size * 2).run {
order(ByteOrder.nativeOrder())
asShortBuffer().apply {
put(array)
position(0)
}
}
}
}
当前,多边形与XY曲面平行渲染。 我想使其旋转成与摄影机的视线垂直。
我画了我想做什么。 像“最佳”部分一样,我希望所有多边形都看着相机。 谢谢。
答案 0 :(得分:1)
当前,多边形与XY曲面平行渲染。我想使其旋转成与摄影机的视线垂直。
您必须向模型添加一个billboard矩阵,该矩阵在视图矩阵上的方向为inverse。 这会将模型定向为与视口平行:
Matrix.invertM(billboard, 0, viewMatrix, 0)
billboard[12] = 0.0f
billboard[13] = 0.0f
billboard[14] = 0.0f
如果要使其垂直于视口,则必须绕X轴额外旋转90度。
Matrix.setIdentityM(rotateX, 0)
Matrix.rotateM(rotateX, 0, 90.0f, 1.0f, 0.0f, 0.0f)
将此转换应用于模型视图矩阵,如下所示:
private fun applyMvpMatrix() {
uniformHandlers["mvpMatrix"]?.also { mvpMatrixHandler ->
val rotateX: FloatArray = FloatArray(16)
val billboard: FloatArray = FloatArray(16)
val mvpMatrix: FloatArray = FloatArray(16)
Matrix.setIdentityM(rotateX, 0)
Matrix.rotateM(rotateX, 0, 90.0f, 1.0f, 0.0f, 0.0f)
Matrix.invertM(billboard, 0, viewMatrix, 0)
billboard[12] = 0.0f
billboard[13] = 0.0f
billboard[14] = 0.0f
Matrix.multiplyMM(mvpMatrix, 0, perspectiveMatrix, 0, viewMatrix, 0)
Matrix.multiplyMM(mvpMatrix, 0, mvpMatrix, 0, modelMatrix, 0)
Matrix.multiplyMM(mvpMatrix, 0, mvpMatrix, 0, rotateX, 0)
Matrix.multiplyMM(mvpMatrix, 0, mvpMatrix, 0, billboard, 0)
GLES20.glUniformMatrix4fv(mvpMatrixHandler, 1, false, mvpMatrix, 0)
}
}
如果您希望模型始终面对modelMatrix
定义的平移镜头,则必须更改应用modelMatrix
和{{1}的方式}矩阵。由于模型应与视口平行,因此在这种情况下,旋转90度(billboard
)的旋转是无用的:
rotateX