本节我们来学习一下光照贴图的知识,还是在之前的基础上不断完善,效果越来越真实,本节实现的效果如下。
一共四个效果,上面两个是只有漫反射贴图的效果,下面两个是漫反射贴图和光照贴图一起的效果,左侧两个是加了光源随时间变化的影响。大部分的代码都和上一节相同,修改比较小,可以看到作者的思路就是把所有能完善的细节一步步的完善,这样就能得到更好的效果。
本节的Render渲染类是GlLightMapRender,完整源码如下:
package com.opengl.learn.aric.lightmap;
import android.content.Context;
import android.opengl.GLES32;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.GL_DEPTH_BUFFER_BIT;
import static android.opengl.GLES20.GL_DEPTH_TEST;
public class GlLightMapRender implements GLSurfaceView.Renderer {
private static final String TAG = GlLightMapRender.class.getSimpleName();
private Context mContext;
private int mWidth, mHeight;
private LightMapCube mCube;
private LightMapLight mLight;
public GlLightMapRender(Context context) {
mContext = context;
mCube = new LightMapCube(mContext);
mLight = new LightMapLight(mContext);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
mCube.onSurfaceCreated();
mLight.onSurfaceCreated();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
mWidth = width;
mHeight = height;
mCube.onSurfaceChanged(gl, width, height);
mLight.onSurfaceChanged(gl, width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
GLES32.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLES32.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLES32.glEnable(GL_DEPTH_TEST);
for (int i = 0; i < 4; i++) {
if (i == 0) {
GLES32.glViewport(0, mHeight / 2, mWidth / 2, mHeight / 2);
} else if (i == 1) {
GLES32.glViewport(mWidth / 2, mHeight / 2, mWidth / 2, mHeight / 2);
} else if (i == 2) {
GLES32.glViewport(0, 0, mWidth / 2, mHeight / 2);
} else if (i == 3) {
GLES32.glViewport(mWidth / 2, 0, mWidth / 2, mHeight / 2);
}
mLight.onDrawFrame(i);
mCube.onDrawFrame(i);
}
}
}
调用glViewport变化视口四次,绘制出四个视图,这次因为我们把数据统一了,在Cube中使用到的光照的数据,也是直接调用光照的属性的,所以绘制顺序修改了一下,先调用mLight.onDrawFrame(i)绘制光源,然后再绘制立方体。
光源LightMapLight类的完整源码如下:
package com.opengl.learn.aric.lightmap;
import android.content.Context;
import android.opengl.GLES32;
import android.opengl.Matrix;
import com.opengl.learn.OpenGLUtils;
import com.opengl.learn.R;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import static android.opengl.GLES20.GL_ARRAY_BUFFER;
import static android.opengl.GLES20.GL_FLOAT;
import static android.opengl.GLES20.GL_STATIC_DRAW;
import static android.opengl.GLES20.GL_TRIANGLES;
import static android.opengl.GLES20.glGenBuffers;
import static android.opengl.GLES20.glGetUniformLocation;
public class LightMapLight {
private final float[] mVerticesData =
{
// back face
0.5f, 0.5f, -0.5f, // (5) Top-right far
0.5f, -0.5f, -0.5f, // (7) Bottom-right far
-0.5f, -0.5f, -0.5f, // (6) Bottom-left far
-0.5f, -0.5f, -0.5f, // (6) Bottom-left far
-0.5f, 0.5f, -0.5f, // (4) Top-left far
0.5f, 0.5f, -0.5f, // (5) Top-right far
// front face
-0.5f, 0.5f, 0.5f, // (0) Top-left near
-0.5f, -0.5f, 0.5f, // (2) Bottom-left near
0.5f, -0.5f, 0.5f, // (3) Bottom-right near
0.5f, -0.5f, 0.5f, // (3) Bottom-right near
0.5f, 0.5f, 0.5f, // (1) Top-right near
-0.5f, 0.5f, 0.5f, // (0) Top-left near
// left face
-0.5f, 0.5f, -0.5f, // (4) Top-left far
-0.5f, -0.5f, -0.5f, // (6) Bottom-left far
-0.5f, -0.5f, 0.5f, // (2) Bottom-left near
-0.5f, -0.5f, 0.5f, // (2) Bottom-left near
-0.5f, 0.5f, 0.5f, // (0) Top-left near
-0.5f, 0.5f, -0.5f, // (4) Top-left far
// right face
0.5f, 0.5f, 0.5f, // (1) Top-right near
0.5f, -0.5f, 0.5f, // (3) Bottom-right near
0.5f, -0.5f, -0.5f, // (7) Bottom-right far
0.5f, -0.5f, -0.5f, // (7) Bottom-right far
0.5f, 0.5f, -0.5f, // (5) Top-right far
0.5f, 0.5f, 0.5f, // (1) Top-right near
// bottom face
-0.5f, -0.5f, 0.5f, // (2) Bottom-left near
-0.5f, -0.5f, -0.5f, // (6) Bottom-left far
0.5f, -0.5f, -0.5f, // (7) Bottom-right far
0.5f, -0.5f, -0.5f, // (7) Bottom-right far
0.5f, -0.5f, 0.5f, // (3) Bottom-right near
-0.5f, -0.5f, 0.5f, // (2) Bottom-left near