Skip to content

Commit

Permalink
none
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin20150405 committed Feb 25, 2018
1 parent 582c0b7 commit a775a21
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 94 deletions.
Binary file modified pano360demo/release/pano360demo-release.apk
Binary file not shown.
75 changes: 40 additions & 35 deletions vrlib/src/main/java/com/martin/ads/vrlib/PanoRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.martin.ads.vrlib.filters.advanced.FilterFactory;
import com.martin.ads.vrlib.filters.base.AbsFilter;
import com.martin.ads.vrlib.filters.base.DrawImageFilter;
import com.martin.ads.vrlib.filters.base.FBO;
import com.martin.ads.vrlib.filters.base.FilterGroup;
import com.martin.ads.vrlib.filters.base.OESFilter;
import com.martin.ads.vrlib.filters.base.OrthoFilter;
Expand All @@ -27,10 +28,6 @@ public class PanoRender
implements GLSurfaceView.Renderer {
public static String TAG = "PanoRender";

public static final int FILTER_MODE_NONE=0x0001;
public static final int FILTER_MODE_BEFORE_PROJECTION=0x0002;
public static final int FILTER_MODE_AFTER_PROJECTION=0x0003;

//If set to RENDER_SIZE_TEXTURE, the rendering window except the last filter
//will be resized to fit the size of input texture.
public static final int RENDER_SIZE_VIEW=0x0004;
Expand All @@ -44,11 +41,13 @@ public class PanoRender
private int surfaceWidth, surfaceHeight;
private int textureWidth, textureHeight;

private FBO fbo;
private PassThroughFilter screenDrawer;

private boolean imageMode;
private boolean planeMode;
private boolean saveImg;
private int filterMode;
private AbsFilter customizedFilter;
private FilterGroup customizedFilters;
private Bitmap bitmap;
private int renderSizeType;

Expand All @@ -59,7 +58,7 @@ private PanoRender() {
public PanoRender init(){
saveImg=false;
filterGroup=new FilterGroup();
customizedFilter=new PassThroughFilter(statusHelper.getContext());
customizedFilters=new FilterGroup();

if(!imageMode) {
firstPassFilter = new OESFilter(statusHelper.getContext());
Expand All @@ -77,11 +76,9 @@ public void notifyTextureSizeChanged(int width, int height) {
});
}
filterGroup.addFilter(firstPassFilter);
if(filterMode==FILTER_MODE_BEFORE_PROJECTION){
//the code is becoming more and more messy ┗( T﹏T )┛
filterGroup.addFilter(customizedFilter);
}

spherePlugin=new Sphere2DPlugin(statusHelper);

//TODO: this should be adjustable
final OrthoFilter orthoFilter=new OrthoFilter(statusHelper,
AdjustingMode.ADJUSTING_MODE_FIT_TO_SCREEN);
Expand All @@ -99,21 +96,15 @@ public void notifyTextureSizeChanged(int width, int height) {
}else{
filterGroup.addFilter(orthoFilter);
}
if(filterMode==FILTER_MODE_AFTER_PROJECTION){
filterGroup.addFilter(customizedFilter);
filterGroup.addPreDrawTask(new Runnable() {
@Override
public void run() {
customizedFilter=new PassThroughFilter(statusHelper.getContext());
alignRenderingAreaWithTexture();
}
});
}
customizedFilters.addFilter(new PassThroughFilter(statusHelper.getContext()));
filterGroup.addFilter(customizedFilters);
screenDrawer=new PassThroughFilter(statusHelper.getContext());
return this;
}
@Override
public void onSurfaceCreated(GL10 glUnused,EGLConfig config) {
filterGroup.init();
screenDrawer.init();
if(!imageMode)
panoMediaPlayerWrapper.setSurface(((OESFilter)firstPassFilter).getGlOESTexture().getTextureId());
}
Expand All @@ -131,7 +122,9 @@ public void onDrawFrame(GL10 glUnused) {
if(!imageMode){
panoMediaPlayerWrapper.doTextureUpdate(((OESFilter)firstPassFilter).getSTMatrix());
}
filterGroup.onDrawFrame(0);
filterGroup.drawToFBO(0,fbo);
fbo.unbind();
screenDrawer.onDrawFrame(fbo.getFrameBufferTextureId());

if (saveImg){
BitmapUtils.sendImage(surfaceWidth, surfaceHeight,statusHelper.getContext());
Expand All @@ -146,23 +139,40 @@ public void onDrawFrame(GL10 glUnused) {
public void onSurfaceChanged(GL10 glUnused, int surfaceWidth, int surfaceHeight) {
this.surfaceWidth =surfaceWidth;
this.surfaceHeight =surfaceHeight;
screenDrawer.onFilterChanged(surfaceWidth,surfaceHeight);
alignRenderingAreaWithTexture();
}

public void onTextureSizeChanged(int textureWidth,int textureHeight){
Log.d(TAG, "onTextureSizeChanged: "+textureWidth+" "+textureHeight);
this.textureWidth=textureWidth;
this.textureHeight=textureHeight;
alignRenderingAreaWithTexture();
}

public void alignRenderingAreaWithTexture(){
if(renderSizeType==PanoRender.RENDER_SIZE_TEXTURE && textureWidth>surfaceWidth) {
private int resolvedWidth, resolvedHeight;

private void alignRenderingAreaWithTexture(){
if(surfaceWidth==0 && textureWidth==0) throw new RuntimeException();
else if(surfaceWidth==0 || textureWidth==0) return;
if(renderSizeType==PanoRender.RENDER_SIZE_TEXTURE) {
double ratio=(double)textureWidth/surfaceWidth;
filterGroup.onFilterChanged(textureWidth, (int) (surfaceHeight*ratio));
AbsFilter filter=filterGroup.getLastFilter();
if(filter!=null) filter.onFilterChanged(surfaceWidth,surfaceHeight);
}else filterGroup.onFilterChanged(surfaceWidth,surfaceHeight);
resolvedWidth=textureWidth;
resolvedHeight=(int) (surfaceHeight*ratio);
}else{
resolvedWidth =surfaceWidth;
resolvedHeight =surfaceHeight;
}
filterGroup.addPreDrawTask(new Runnable() {
@Override
public void run() {
//only can run in gl thread
//create here will cause flashing on drawing
fbo=FBO.newInstance().create(resolvedWidth, resolvedHeight);
filterGroup.onFilterChanged(resolvedWidth, resolvedHeight);
}
});

Log.d(TAG, "alignRenderingAreaWithTexture: "+surfaceWidth+" "+surfaceHeight+" "+textureWidth+" "+textureHeight+" "+ resolvedWidth +" "+ resolvedHeight);
}

public void saveImg(){
Expand Down Expand Up @@ -197,11 +207,6 @@ public PanoRender setPlaneMode(boolean planeMode) {
return this;
}

public PanoRender setFilterMode(int filterMode) {
this.filterMode = filterMode;
return this;
}

public PanoRender setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
return this;
Expand All @@ -220,7 +225,7 @@ public void switchFilter(){
filterGroup.addPreDrawTask(new Runnable() {
@Override
public void run() {
customizedFilter=FilterFactory.randomlyCreateFilter(statusHelper.getContext());
customizedFilters.switchLastFilter(FilterFactory.randomlyCreateFilter(statusHelper.getContext()));
alignRenderingAreaWithTexture();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ else if((mimeType & MimeType.RAW)!=0)
.setImageMode(imageMode)
.setPlaneMode(planeMode)
.setBitmap(bitmap)
.setFilterMode(PanoRender.FILTER_MODE_AFTER_PROJECTION)
.setRenderSizeType(PanoRender.RENDER_SIZE_TEXTURE)
.init();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public void onFilterChanged(int surfaceWidth, int surfaceHeight){
this.surfaceHeight=surfaceHeight;
}

public int getSurfaceWidth() {
return surfaceWidth;
void setViewport(){
GLES20.glViewport(0, 0, surfaceWidth, surfaceHeight);
}

public int getSurfaceHeight() {
return surfaceHeight;
public FBO createFBO(){
return FBO.newInstance().create(surfaceWidth,surfaceHeight);
}

abstract public void onDrawFrame(final int textureId);
Expand Down
66 changes: 66 additions & 0 deletions vrlib/src/main/java/com/martin/ads/vrlib/filters/base/FBO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.martin.ads.vrlib.filters.base;

import android.opengl.GLES20;

public class FBO {
private int frameBuffer;
private int frameBufferTexture;

private FBO(){}

public static FBO newInstance(){
return new FBO();
}

public FBO create(int width,int height){
int[] frameBuffers = new int[1];
int[] frameBufferTextures = new int[1];
GLES20.glGenFramebuffers(1, frameBuffers, 0);

GLES20.glGenTextures(1, frameBufferTextures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, frameBufferTextures[0]);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA,
width,height, 0,
GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_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.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers[0]);
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
GLES20.GL_TEXTURE_2D, frameBufferTextures[0], 0);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

frameBuffer=frameBuffers[0];
frameBufferTexture=frameBufferTextures[0];
return this;
}

public void destroy(){
GLES20.glDeleteTextures(1, new int[]{frameBufferTexture}, 0);
GLES20.glDeleteFramebuffers(1, new int[]{frameBuffer}, 0);
}

public void bind(){
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffer);
}

public void unbind(){
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
}

public int getFrameBufferTextureId() {
return frameBufferTexture;
}

public int getFrameBuffer() {
return frameBuffer;
}
}
Loading

0 comments on commit a775a21

Please sign in to comment.