Opengl ES系列学习--光照贴图

     本节我们来学习一下光照贴图的知识,还是在之前的基础上不断完善,效果越来越真实,本节实现的效果如下。

     一共四个效果,上面两个是只有漫反射贴图的效果,下面两个是漫反射贴图和光照贴图一起的效果,左侧两个是加了光源随时间变化的影响。大部分的代码都和上一节相同,修改比较小,可以看到作者的思路就是把所有能完善的细节一步步的完善,这样就能得到更好的效果。

     本节的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

          
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红-旺永福

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值