package com.bn.Sample4_3_V2;
import static com.bn.Sample4_3_V2.Constant.SCREEN_HEIGHT;
import static com.bn.Sample4_3_V2.Constant.SCREEN_WIDTH;
import static com.bn.Sample4_3_V2.Constant.SHADOW_TEX_HEIGHT;
import static com.bn.Sample4_3_V2.Constant.SHADOW_TEX_WIDTH;
import android.opengl.GLSurfaceView;
import android.opengl.GLES20;
import android.view.MotionEvent;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
class MySurfaceView extends GLSurfaceView
{
private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
private SceneRenderer mRenderer;//场景渲染器
private float mPreviousY;//上次的触控位置Y坐标
private float mPreviousX;//上次的触控位置X坐标
//摄像机位置相关
float cx=0;
float cy=20;
float cz=50;
float cAngle=0;
final float cR=50;
//灯光位置
float lx=0;
final float ly=10;
float lz=45;
float lAngle=0;
final float lR=45;
final float cDis=15;
//光源总变换矩阵
float[] mMVPMatrixGY;
public MySurfaceView(Context context) {
super(context);
this.setEGLContextClientVersion(2); //设置使用OPENGL ES2.0
mRenderer = new SceneRenderer(); //创建场景渲染器
setRenderer(mRenderer); //设置渲染器
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
new Thread()
{
public void run()
{
while(true)
{
lAngle += 0.5;//设置沿x轴旋转角度
lx=(float) Math.sin(Math.toRadians(lAngle))*lR;
lz=(float) Math.cos(Math.toRadians(lAngle))*lR;
try {
Thread.sleep(80);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
//触摸事件回调方法
@Override
public boolean onTouchEvent(MotionEvent e)
{
float y = e.getY();
float x = e.getX();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dy = y - mPreviousY;//计算触控笔Y位移
float dx = x - mPreviousX;//计算触控笔X位移
cAngle += dx * TOUCH_SCALE_FACTOR;//设置沿x轴旋转角度
cx=(float) Math.sin(Math.toRadians(cAngle))*cR;
cz=(float) Math.cos(Math.toRadians(cAngle))*cR;
cy+= dy/10.0f;//设置沿z轴移动
requestRender();//重绘画面
}
mPreviousY = y;//记录触控笔位置
mPreviousX = x;//记录触控笔位置
return true;
}
private class SceneRenderer implements GLSurfaceView.Renderer
{
//从指定的obj文件中加载对象
LoadedObjectVertexNormal lovo_pm;//平面
LoadedObjectVertexNormal lovo_ch;//茶壶
LoadedObjectVertexNormal lovo_cft;//长方体
LoadedObjectVertexNormal lovo_qt;//球体
LoadedObjectVertexNormal lovo_yh;//圆环
//TextureRect tr;
int frameBufferId;
int shadowId;// 动态产生的阴影纹理Id
int renderDepthBufferId;// 动态产生的阴影纹理Id
boolean isBegin=true;
//初始化帧缓冲和渲染缓冲
public void initFRBuffers()
{
int[] tia=new int[1];
GLES20.glGenFramebuffers(1, tia, 0);
frameBufferId=tia[0];
if(isBegin)
{
GLES20.glGenRenderbuffers(1, tia, 0);
renderDepthBufferId=tia[0];
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderDepthBufferId);
GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, SHADOW_TEX_WIDTH, SHADOW_TEX_HEIGHT);
isBegin=false;
}
int[] tempIds = new int[1];
GLES20.glGenTextures
(
1, //产生的纹理id的数量
tempIds, //纹理id的数组
0 //偏移量
);
shadowId=tempIds[0];
}
//通过绘制产生阴影纹理
public void generateShadowImage()
{
initFRBuffers();
GLES20.glViewport(0, 0, SHADOW_TEX_WIDTH, SHADOW_TEX_HEIGHT);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBufferId);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, shadowId);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
GLES20.glFramebufferTexture2D
(
GLES20.GL_FRAMEBUFFER,
GLES20.GL_COLOR_ATTACHMENT0,
GLES20.GL_TEXTURE_2D,
shadowId,
0
);
GLES20.glTexImage2D
(
GLES20.GL_TEXTURE_2D,
0,
GLES20.GL_RGB,
SHADOW_TEX_WIDTH,
SHADOW_TEX_HEIGHT,
0,
GLES20.GL_RGB,
GLES20.GL_UNSIGNED_SHORT_5_6_5,
null
);
GLES20.glFramebufferRenderbuffer
(
GLES20.GL_FRAMEBUFFER,
GLES20.GL_DEPTH_ATTACHMENT,
GLES20.GL_RENDERBUFFER,
renderDepthBufferId
);
//清除深度缓冲与颜色缓冲
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
//调用此方法产生摄像机9参数位置矩阵
MatrixState.setCamera(lx,ly,lz,0f,0f,0f,0f,1,0);
MatrixState.setProjectFrustum(-1, 1, -1.0f, 1.0f, 1.5f, 400);
mMVPMatrixGY=MatrixState.getViewProjMatrix();
//绘制最下面的平面
lovo_pm.drawSelfForShadow();
//绘制球体
MatrixState.pushMatrix();
MatrixState.translate(-cDis, 0, 0);
//若加载的物体部位空则绘制物体
lovo_qt.drawSelfForShadow();
MatrixState.popMatrix();
//绘制圆环
MatrixState.pushMatrix();
MatrixState.translate(cDis, 0, 0);
MatrixState.rotate(30, 0, 1, 0);
//若加载的物体部位空则绘制物体
lovo_yh.drawSelfForShadow();
MatrixState.popMatrix();
//绘制长方体
MatrixState.pushMatrix();
MatrixState.translate(0, 0, -cDis);
//若加载的物体部位空则绘制物体
lovo_cft.drawSelfForShadow();
MatrixState.popMatrix();
//绘制茶壶
MatrixState.pushMatrix();
MatrixState.translate(0, 0, cDis);
//若加载的物体部位空则绘制物体
lovo_ch.drawSelfForShadow();
MatrixState.popMatrix();
}
public void drawScene(GL10 gl)
{
//设置视窗大小及位置
GLES20.glViewport(0, 0, (int)SCREEN_WIDTH, (int)SCREEN_HEIGHT);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
//清除深度缓冲与颜色缓冲
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glClearColor(1f,0.3f,0.3f,1.0f);
//调用此方法产生摄像机9参数位置矩阵
//如果要显示距离纹理的话,要改动摄像机位置及观察视角
/
没有合适的资源?快使用搜索试试~ 我知道了~
ShadowMapping(阴影映射)
共74个文件
class:31个
java:11个
png:9个
1星 需积分: 50 41 下载量 89 浏览量
2016-05-20
07:52:49
上传
评论
收藏 191KB RAR 举报
温馨提示
在安卓下面实现openGL的Shadow Mapping,运用到GLSL 有很好的参考作用 代码博客: http://blog.csdn.net/xiaoge132/article/details/51458489
资源推荐
资源详情
资源评论
收起资源包目录
ShadowMapping.rar (74个子文件)
ShadowMapping
.project 848B
project.properties 446B
src
com
bn
Sample4_3_V2
ShaderUtil.java 4KB
Constant.java 341B
MatrixState.java 4KB
LoadedObjectVertexNormal.java 8KB
TextureRect.java 5KB
MySurfaceView.java 11KB
Sample4_3_Activity.java 1KB
LoadUtil.java 6KB
Normal.java 1KB
AndroidManifest.xml 742B
res
raw
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
drawable-xhdpi
values
strings.xml 114B
.settings
org.eclipse.jdt.core.prefs 629B
assets
cft.obj 454B
frag_tex.sh 316B
frag_shadow.sh 596B
frag.sh 2KB
yh.obj 16KB
ch.obj 29KB
vertex_shadow.sh 336B
vertex_tex.sh 431B
vertex.sh 2KB
qt.obj 27KB
pm.obj 1KB
gen
com
bn
Sample4_3_V2
BuildConfig.java 161B
R.java 493B
.classpath 475B
bin
resources.ap_ 35KB
classes
com
bn
Sample4_3_V2
LoadedObjectVertexNormal.class 4KB
Sample4_3_Activity.class 1KB
Constant.class 489B
BuildConfig.class 347B
R$drawable.class 403B
R$attr.class 343B
ShaderUtil.class 3KB
MatrixState.class 3KB
MySurfaceView.class 2KB
MySurfaceView$1.class 1KB
LoadUtil.class 5KB
TextureRect.class 3KB
R.class 444B
R$string.class 401B
MySurfaceView$SceneRenderer.class 5KB
Normal.class 1KB
AndroidManifest.xml 742B
dexedLibs
annotations-208bc076e0325a436b7d8b4d8545823c.jar 1KB
jarlist.cache 120B
com
bn
Sample15_8_V2
LoadedObjectVertexNormal.class 4KB
Constant.class 491B
R$drawable.class 406B
R$attr.class 346B
ShaderUtil.class 3KB
Sample15_8_Activity.class 1KB
MatrixState.class 3KB
MySurfaceView.class 2KB
MySurfaceView$1.class 1KB
LoadUtil.class 5KB
TextureRect.class 3KB
R.class 449B
R$string.class 404B
MySurfaceView$SceneRenderer.class 5KB
Normal.class 1KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 2KB
crunch
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 2KB
Sample4_3_V2.apk 49KB
classes.dex 23KB
共 74 条
- 1
资源评论
- maxos2019-03-06运行到手机上效果很差,几乎看不见图像,起不到参考作用。
xiaogeformax
- 粉丝: 101
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功