android - OpenGL- Render to texture- whole rendered scene -


i implemented fbo on opengl game. , im rendering rendered screen texture, problem rendering texture starts lower left corner. look:

what rendered default frame buffer:

enter image description here

what rendered texture attached fbo:

enter image description here

but want rendered texture is:

enter image description here

i have calculated hypothetical rectangle (red dots) positions. dont know how render area,

how can this? here renderer calass (the fbo operation done in ondrawframe function):

public class curlrenderer implements glsurfaceview.renderer {  // constant requesting right page rect. public static final int page = 1; // set true checking how perspective projection looks. private static final boolean use_perspective_projection = false; // background fill color. private int mbackgroundcolor; // curl meshes used static , dynamic rendering. private curlmesh mcurlmesh; private rectf mmargins = new rectf(); private curlrenderer.observer mobserver; // page rectangles. private rectf mpagerect; // view mode. // screen size. private int mviewportwidth, mviewportheight; // rect render area. private rectf mviewrect = new rectf(); private boolean first = true; int[] fb, rendertex;  int texw = 300;  int texh = 256;  intbuffer texbuffer; int[] buf = new int[texw * texh]; gl11extensionpack gl11ep ; /**  * basic constructor.  */ public curlrenderer(curlrenderer.observer observer) {     mobserver = observer;     mcurlmesh = new curlmesh(0);     mpagerect = new rectf(); }  /**  * adds curlmesh renderer.  */ public synchronized void addcurlmesh(curlmesh mesh) {     mcurlmesh = mesh; }  /**  * returns rect reserved left or right page. value page should  * page_left or page_right.  */ public rectf getpagerect(int page) {         if (page == page) {         return mpagerect;     }     return null; } public void setup(gl10 gl){     fb = new int[1];     rendertex = new int[1];     // generate     ((gl11extensionpack)gl).glgenframebuffersoes(1, fb, 0);      gl.glenable(gl10.gl_texture_2d);     gl.glgentextures(1, rendertex, 0);// generate texture     gl.glbindtexture(gl10.gl_texture_2d, rendertex[0]);     gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_min_filter, gl10.gl_nearest);     gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_mag_filter, gl10.gl_linear);     gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_s, gl10.gl_clamp_to_edge);     gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_t, gl10.gl_clamp_to_edge);    // texbuffer = bytebuffer.allocatedirect(buf.length*4).order(byteorder.nativeorder()).asintbuffer();    // gl.gltexenvf(gl10.gl_texture_env, gl10.gl_texture_env_mode,gl10.gl_modulate);     gl.glteximage2d(gl10.gl_texture_2d, 0, gl10.gl_rgba, texw, texh, 0, gl10.gl_rgba, gl10.gl_unsigned_short_4_4_4_4, null);     gl.gldisable(gl10.gl_texture_2d); } boolean renderstart(gl10 gl){     // bind framebuffer     ((gl11extensionpack)gl).glbindframebufferoes(gl11extensionpack.gl_framebuffer_oes, fb[0]);      // specify texture color attachment     ((gl11extensionpack)gl).glframebuffertexture2does(gl11extensionpack.gl_framebuffer_oes, gl11extensionpack.gl_color_attachment0_oes, gl10.gl_texture_2d, rendertex[0], 0);       int error = gl.glgeterror();     if (error != gl10.gl_no_error) {         log.d("err", "background load glerror: " + error+"      ");     }     int status = ((gl11extensionpack)gl).glcheckframebufferstatusoes(gl11extensionpack.gl_framebuffer_oes);     if (status != gl11extensionpack.gl_framebuffer_complete_oes)     {         log.d("err", "background load glerror: " + status+"      ");;         return true;     }     gl.glclear(gl10.gl_color_buffer_bit);     return true; } void renderend(gl10 gl){     ((gl11extensionpack)gl).glbindframebufferoes(gl11extensionpack.gl_framebuffer_oes, 0);      gl.glclear(gl10.gl_color_buffer_bit | gl10.gl_depth_buffer_bit);      gl.glenable(gl10.gl_texture_2d);     gl.glbindtexture(gl10.gl_texture_2d, rendertex[0]);     gl.glcolor4f(1,1,1,1);     gl.gldisable(gl10.gl_texture_2d); }  @override public synchronized void ondrawframe(gl10 gl) {     if(first){     int h = gles20.glgeterror();     this.setup(gl);     if(h!=0){         log.d("error", "error happend"+h+"");     }     first = false;     }     mobserver.ondrawframe();     //glclearcolor miad rangi ke maa entekhaab kardim ro tooye carde graphic register mikone     gl.glclearcolor(color.red(mbackgroundcolor) / 255f,             color.green(mbackgroundcolor) / 255f,             color.blue(mbackgroundcolor) / 255f,             color.alpha(mbackgroundcolor) / 255f);     //glclear miad oon rangi ke bala register karde boodim ro dige az buffer paak mikone     gl.glclear(gl10.gl_color_buffer_bit);      //miad matris ro mabdaesh barmigardoone, ke bad baraye glrotate va gltranslate moshkeli ijaad nashe     //chon maa asle jaabejaa kardan hamoon baraye safhe, baste makaane avalieye     // kaaghazemoon hast, na oon makani ke dar haale hazer gharaar dare     gl.glloadidentity();      if (use_perspective_projection) {         gl.gltranslatef(0, 0, -6f);     }       renderstart(gl);     mcurlmesh.ondrawframe(gl);     renderend(gl);     mcurlmesh.ondrawframe(gl);  }  @override public void onsurfacechanged(gl10 gl, int width, int height) {     gl.glviewport(0, 0, width, height);     mviewportwidth = width;     mviewportheight = height;      float ratio = (float) width / height;     mviewrect.top = 1.0f;     mviewrect.bottom = -1.0f;     mviewrect.left = -ratio;     mviewrect.right = ratio;     updatepagerects();      gl.glmatrixmode(gl10.gl_projection);     gl.glloadidentity();     if (use_perspective_projection) {         glu.gluperspective(gl, 20f, (float) width / height, .1f, 100f);     } else {         glu.gluortho2d(gl, mviewrect.left, mviewrect.right,                 mviewrect.bottom, mviewrect.top);     }      gl.glmatrixmode(gl10.gl_modelview);     gl.glloadidentity(); }  @override public void onsurfacecreated(gl10 gl, eglconfig config) { //  mcurlmesh.setup(gl);     gl.glclearcolor(0f, 0f, 0f, 1f);     gl.glshademodel(gl10.gl_smooth);     gl.glhint(gl10.gl_perspective_correction_hint, gl10.gl_nicest);     gl.glhint(gl10.gl_line_smooth_hint, gl10.gl_nicest);     //gl.glhint(gl10.gl_polygon_smooth_hint, gl10.gl_nicest);     gl.glenable(gl10.gl_line_smooth);     gl.gldisable(gl10.gl_depth_test);     gl.gldisable(gl10.gl_cull_face); }   /**  * change background/clear color.  */ public void setbackgroundcolor(int color) {     mbackgroundcolor = color; }  /**  * set margins or padding. note: margins proportional. meaning value  * of .1f produce 10% margin.  */ public synchronized void setmargins(float left, float top, float right,         float bottom) {     mmargins.left = left;     mmargins.top = top;     mmargins.right = right;     mmargins.bottom = bottom;     updatepagerects(); } /**  * translates screen coordinates view coordinates.  * mokhtassate ye noghte (masalan pointer position) roye safhe ro, moadele mokhtasaatesh  * rooye curlview tabdil mikene  */ public void translate(pointf pt) {     pt.x = mviewrect.left + (mviewrect.width() * pt.x / mviewportwidth);     pt.y = mviewrect.top - (-mviewrect.height() * pt.y / mviewportheight); }  /**  * recalculates page rectangles.  */ private void updatepagerects() {     if (mviewrect.width() == 0 || mviewrect.height() == 0) {         return;     }     /**      * @ todo inja daghighan hamnoon kaari ke mikham, yani size dadan page ro anjaam mide      * mpagerect... khode meshe  va mviewrect view e layout      */         mpagerect.set(mviewrect);         mpagerect.left += mviewrect.width() * mmargins.left;         mpagerect.right -= mviewrect.width() * mmargins.right;         mpagerect.top += mviewrect.height() * mmargins.top;         mpagerect.bottom -= mviewrect.height() * mmargins.bottom;          int bitmapw = (int) ((mpagerect.width() * mviewportwidth) / mviewrect.width());         int bitmaph = (int) ((mpagerect.height() * mviewportheight) / mviewrect.height());         mobserver.onpagesizechanged(bitmapw, bitmaph);  }  /**  * observer waiting render engine/state updates.  */ public interface observer {     /**      * called ondrawframe called before rendering started.      * intended used animation purposes.      */     public void ondrawframe();      /**      * called once page size changed. width , height tell page size      * in pixels making possible update textures accordingly.      */     public void onpagesizechanged(int width, int height);       } }  

first of all, should understand basic rendering pipeline. in which, there clipping coordinate after multiplying projection matrix. when call gluortho2d() projection matrix, cut object yourself.

in short, rtt (render texture) texture rendered render target (fbo), , draw chopped triangle @ fbo have cut texture don't want get.

to draw object in box after multiplying projection , after w-divide step, x,y, range must between [-1,1]. if hard calculate you, don't multiply projection matrix , make sure x,y , z values in range [-1,1], , w must 1  


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -