How to correct set render when win resizes?

Hi, I implement this custom rendering window, to customize resolution x window size, it seems to be working fine, but when you resize, all methods I tried didnt work :

// code :

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Quad;
import com.jme3.system.AppSettings;
import com.jme3.texture.FrameBuffer;
import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;

public class CustomApplication  extends SimpleApplication  {
   
   //public static int screen_width  = 1000; public static int screen_heigth = 600;
   //public static int screen_width  = 800; public static int screen_heigth = 600;
   //public static int screen_width  = 1024; public static int screen_heigth = 768;
   public static int screen_width  = 640; public static int screen_heigth = 480;
   //public static int screen_width  = 1920; public static int screen_heigth = 920;
   //public static int screen_width  = 1000; public static int screen_heigth = 100;
       
   private Geometry offBox;
   private float angle = 0;

   private int lastwidth;
   private int lastheight;
   
   public int render_width = 512;
   public int render_height = 512;    

   public static AppSettings getSettings() {
       AppSettings _settings = new AppSettings(true);
       _settings.setResolution( screen_width , screen_heigth );  
       _settings.setResizable(true);        
       return _settings;
   }
   
   public static void main(String[] args){
       CustomApplication app = new CustomApplication();
       app.setSettings( getSettings() );
       app.setDisplayFps(true);
       app.setDisplayStatView(false);
       app.setShowSettings(false);        
       app.start();
       app.getFlyByCamera().setEnabled(false);
   }    

   // TestRenderToTexture
   public ViewPort setupOffscreenView(){
       cam.setLocation(new Vector3f(0, 0, 1.22f));
       cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
       
       Camera offCamera = new Camera(render_width, render_height);
       ViewPort offView = renderManager.createPreView("Offscreen View", offCamera);
       offView.setClearFlags(true, true, true);
       offView.setBackgroundColor(ColorRGBA.DarkGray);

       // setup framebuffer
       FrameBuffer offBuffer = new FrameBuffer(render_width, render_height, 1);
       offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f);
       offCamera.setLocation(new Vector3f(0f, 0f, -5f));
       offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
       Texture2D offTex = new Texture2D(render_width, render_height, Format.RGBA8);
       offTex.setMinFilter(Texture.MinFilter.Trilinear);
       offTex.setMagFilter(Texture.MagFilter.Bilinear);
       offBuffer.setDepthBuffer(Format.Depth);
       offBuffer.setColorTexture(offTex);
       offView.setOutputFrameBuffer(offBuffer);
               
       float cAspect = (float)screen_width / (float)screen_heigth;                
       Geometry quad = new Geometry("box", new Quad(cAspect, 1));
       quad.setLocalTranslation( new Vector3f(-1*(cAspect/2f), -1*(1/2f), 0f) );
       Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
       mat.setTexture("ColorMap", offTex);
       quad.setMaterial(mat);
       rootNode.attachChild(quad);
               
       return offView;
   }

   @Override
   public void simpleInitApp() {
       this.lastwidth  = this.settings.getWidth();
       this.lastheight = this.settings.getHeight();
           
       screen_width = this.settings.getWidth();
       screen_heigth = this.settings.getHeight();    
       
       getFlyByCamera().setEnabled(false);

       ViewPort offView = setupOffscreenView();

       Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
       material.setColor("Color" , ColorRGBA.Blue);
       offBox = new Geometry("box", new Box(Vector3f.ZERO, 1,1,1));
       offBox.setMaterial(material);
       offView.attachScene(offBox);
   }

   @Override
   public void simpleUpdate(float tpf){
              
       if(this.settings.getWidth() != this.lastwidth || this.settings.getHeight()!= this.lastheight){
           this.lastwidth  = this.settings.getWidth();
           this.lastheight = this.settings.getHeight();            
           // restart();   //---->>> bug the app and make it unsizeble, also dont streach correctly
           simpleInitApp();    // ------>working, but slow
           
       }
       
       Quaternion q = new Quaternion();
       
       //if (offView.isEnabled()) {
           angle += tpf;
           angle %= FastMath.TWO_PI;
           q.fromAngles(angle, 0, angle);
           
           offBox.setLocalRotation(q);
           offBox.updateLogicalState(tpf);
           offBox.updateGeometricState();
       //}
   }

   
}

Any way to dynamically reset the scene to the new resolution ?

1 Like

Nevermind, I found the bug, I forgot to refresh the screen_width and screen_heigth variables.
The code in this thread is fixed if anyone wants to test it.

1 Like

Incidentally:

…will let your code block show up properly.

Thanks, I fixed.

By the way, this code is working, but its decreasing the general fps everytime windows resizes, I think I should reset some objects, it seems its rendering stuff it should not, any tips ?