# 1、概述

OpenGL ES 2.0 显示图形（上）

OpenGL ES 2.0 显示图形（下）

OpenGL ES 3.0（一）综述

OpenGL ES 3.0（二）GLSL与着色器

OpenGL ES 3.0（三）纹理

OpenGL ES 3.0（四）矩阵变换

OpenGL ES 3.0（五）坐标系

# 2、摄像机/观察空间

## 2.1 摄像机位置

``````
public var cameraPos = floatArrayOf (0.0f, 0.0f,3.0f)

``````

z轴是从屏幕指向用户的方向，如果希望摄像机向后移动，就沿着z轴的正方向移动。

## 2.2 摄像机方向

``````
public var cameraTarget = floatArrayOf (0.0f, 0.0f,0f)

``````

## 2.3 右轴

``````
public var up = floatArrayOf(0.0f, 1.0f, 0.0f)

``````

## 2.4 上轴

``````
public var cameraUp = Utils.vector3DCross(cameraDirection, cameraRight)

``````

# 3、LookAt

LookAt矩阵

``````
private val mViewMatrix = FloatArray(16)

fun draw() {

...

Matrix.setLookAtM(mViewMatrix, 0,

0f, 0f, 3.0f,

0.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f

)

for (i in 0..3) {

...

}

...

}

``````

Matrix.setLookAtM()需要一个位置、目标和上向量。它会创建一个观察矩阵。

``````
// Triangle.kt

private val mViewMatrix = FloatArray(16)

fun draw() {

...

mAngle = (System.currentTimeMillis() / 300) % 360

var camX = Math.sin(mAngle.toDouble() ).toFloat() * mRadius

var camZ = Math.cos(mAngle.toDouble() ).toFloat() * mRadius

Matrix.setLookAtM(mViewMatrix, 0,

camX, 0f, camZ,

0.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f

)

for (i in 0..3) {

...

}

...

}

``````

# 4、相机自由移动

``````
// Triangle.kt
public var cameraPos = floatArrayOf(0.0f, 0.0f, 3.0f)

public var cameraFront = floatArrayOf(0.0f, 0.0f, -1.0f)

public var cameraUp = floatArrayOf(0.0f, 1.0f, 3.0f)

``````

``````
// Triangle.kt
fun draw() {

...

Matrix.setLookAtM(mViewMatrix, 0,

cameraPos[0], cameraPos[1], cameraPos[2],

cameraPos[0] + cameraFront[0],

cameraPos[1] + cameraFront[1],

cameraPos[2] + cameraFront[2],

cameraUp[0], cameraUp[1], cameraUp[2]

)

for (i in 0..3) {

...

}

...

}

``````

``````
// MyGLSurfaceView.kt

public var mPreCameraPos = FloatArray(3)

public var mPreCameraFront = FloatArray(3)

public var mPreCameraUp = FloatArray(3)

init {

...

renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY

}

override fun onTouchEvent(e: MotionEvent): Boolean {

val x = e.x

val y = e.y

when (e.action) {

MotionEvent.ACTION_DOWN -> {

mPreCameraPos[0] = mRenderer.mTriangle?.cameraPos?.get(0)!!

mPreCameraPos[1] = mRenderer.mTriangle?.cameraPos?.get(1)!!

mPreCameraPos[2] = mRenderer.mTriangle?.cameraPos?.get(2)!!

mPreCameraFront[0] = mRenderer.mTriangle?.cameraFront?.get(0)!!

mPreCameraFront[1] = mRenderer.mTriangle?.cameraFront?.get(1)!!

mPreCameraFront[2] = mRenderer.mTriangle?.cameraFront?.get(2)!!

mPreCameraUp[0] = mRenderer.mTriangle?.cameraUp?.get(0)!!

mPreCameraUp[1] = mRenderer.mTriangle?.cameraUp?.get(1)!!

mPreCameraUp[2] = mRenderer.mTriangle?.cameraUp?.get(2)!!

...

}

MotionEvent.ACTION_MOVE -> {

var dx = x - mPreviousX

var dy = y - mPreviousY

if (y > height / 2) {

mRenderer.mTriangle?.cameraPos =

mRenderer.mTriangle?.cameraPos =

, (mRenderer.mTriangle?.cameraUp))

, dx / 5))

} else {

...

}

requestRender()

}

MotionEvent.ACTION_UP -> {

mRenderer.mTriangle?.cameraPos?.set(0, mPreCameraPos[0])

mRenderer.mTriangle?.cameraPos?.set(1, mPreCameraPos[1])

mRenderer.mTriangle?.cameraPos?.set(2, mPreCameraPos[2])

mRenderer.mTriangle?.cameraFront?.set(0, mPreCameraFront[0])

mRenderer.mTriangle?.cameraFront?.set(1, mPreCameraFront[1])

mRenderer.mTriangle?.cameraFront?.set(2, mPreCameraFront[2])

mRenderer.mTriangle?.cameraUp?.set(0, mPreCameraUp[0])

mRenderer.mTriangle?.cameraUp?.set(1, mPreCameraUp[1])

mRenderer.mTriangle?.cameraUp?.set(2, mPreCameraUp[2])

requestRender()

}

}

mPreviousX = x

mPreviousY = y

return true

}

``````

# 5、相机视角移动

## 5.1 欧拉角

``````
direction.y = sin(pitch);

``````

``````
direction.x = cos(pitch);

direction.z = cos(pitch);

``````

``````
direction.x = cos(pitch) * cos(yaw);

direction.y = sin(pitch);

direction.z = cos(pitch) * sin(yaw);

``````

## 5.2 视角触摸事件

``````
// MyGLSurfaceView.kt

private var mPitch: Double =0.toDouble()

private var mYaw: Double =0.toDouble()

override fun onTouchEvent(e: MotionEvent): Boolean {

val x = e.x

val y = e.y

when (e.action) {

MotionEvent.ACTION_DOWN -> {

...

}

MotionEvent.ACTION_MOVE -> {

var dx = x - mPreviousX

var dy = y - mPreviousY

if (y > height / 2) {

...

} else {

mPitch += dy/100

mYaw += dx/100

mRenderer.mTriangle?.cameraFront?.set(0, (Math.cos(mPitch) * Math.cos(mYaw)).toFloat())

mRenderer.mTriangle?.cameraFront?.set(1, Math.sin(mPitch).toFloat())

mRenderer.mTriangle?.cameraFront?.set(2, (Math.cos(mPitch) * Math.sin(mYaw)).toFloat())

}

requestRender()

}

MotionEvent.ACTION_UP -> {

...

}

}

...

}

``````