Author Topic: How to setup environment  (Read 3239 times)

Offline gugugu

  • byte
  • *
  • Posts: 3
    • View Profile
How to setup environment
« on: March 06, 2008, 12:32:58 pm »
Hello JPCT Community!
I have big trouble in understanding how to get some kind of "canvas" to render my 3D Scenes to.

I need a component to integrate into my GUI, I was thinking about a class that extends JPanel and is able to display Software and Hardware Rendering.

It also has to lbe able to listen to Mouse and Keyboard events..
Can Anyone give me a hint?
I am as far as this, but there is no rendering viewable:
Code: [Select]
import com.threed.jpct.*;

import java.awt.Graphics;
import java.io.PrintStream;
import java.net.URL;
import javax.swing.JPanel;

public class Panel3D extends JPanel
{

    private World _world;
    private FrameBuffer _buffer;
private Camera _camera;

    public Panel3D()
    {
        //this.addMouseListener(arg0);
        //this.addKeyListener(arg0);
    Logger.setLogLevel(2);
        Config.useMultipleThreads = true;
        Config.fadeoutLight = false;
        Config.linearDiv = 100F;
        Config.farPlane = 500F;
        _world = new World();
        _world.setAmbientLight(250, 250, 250);
       
     
        _buffer = new FrameBuffer(this.getHeight(), this.getWidth(), 0);
        _buffer.enableRenderer(IRenderer.RENDERER_OPENGL);
       
        _camera = _world.getCamera();
      }

}

I just found the HelloWorld-Apps in the jpctapi116rc2.zip.

Is ist possible to use OpenGL or Hardwaremode to render into a JPanel?
« Last Edit: March 06, 2008, 01:02:43 pm by gugugu »

Offline Jonas

  • byte
  • *
  • Posts: 41
    • View Profile
Re: How to setup environment
« Reply #1 on: March 06, 2008, 03:09:13 pm »
Hi

If you want to use software just use the components graphics object to paint the framebuffer on to the component..

frambuffer.display(component.getGraphics());

For the hardware mode you could use a canvas like this and add it to a jpanel(if you want to use both, soft and hardware modus you can then just replace the canvas in the jpanel by its software counterpart). I think there could be some problems with that because of Swing/awt integration..this one needs to call render() after every update to the world you want rendered...:

Code: [Select]
public class HardwareCanvas extends Canvas
{

   private FrameBuffer _buffer;
   private World _world;

   private Canvas myCanvas;
   
   private boolean isWireframeMode = false;
 
   public HardwareCanvas(World myWorld)
   {
     
      this.myWorld = myWorld;
      createCanvas(800, 800);
     
   }
   
   /**
    *  Set up the panel and listener, and create a framebuffer
    *  in hardwarerendering mode.
    */

   private void createCanvas(int height, int width) {
     
      if(myCanvas == null)
      {
         myFramebuffer = new FrameBuffer(
                 width,
                 height,
                 FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY);
         myFramebuffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);

         myCanvas = myFramebuffer.enableGLCanvasRenderer(); // Enable hardware rendering

         /* Your listeners go here*/
         
         myCanvas.addKeyListener( new KeyAdapter() {} );
       
         myCanvas.requestFocus();
      }
   }
/* Render the world...could use a render loop as well so you dont have to call it after every update */

   public void render() {

         myFramebuffer.clear();
         //Render the scene onto an image
         myWorld.renderScene(myFramebuffer);

         if (!isWireframeMode) {
            myWorld.draw(myFramebuffer);
         } else {
            myWorld.drawWireframe(myFramebuffer, Color.white);
         }

         myFramebuffer.update();
         
         SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
               myFramebuffer.displayGLOnly();
               myCanvas.repaint();
            }
         
         });
     
   }

   public void changeWireframeMode()
   {
      isWireframeMode = !isWireframeMode;
      this.render();
   }
     
}
Simple things should be simple, complex things should be possible - Alan Kay

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: How to setup environment
« Reply #2 on: March 06, 2008, 04:08:56 pm »
I would suggest exactly what Jonas said.

If you want software use framebuffer.display(yourjpanelorcanvas.getGraphics())

If you need hardware then call framebuffer.enableGLRenderer() put the Canvas that it returns into your gui and starting rendering into it.

Also,  I wouldn't try changing renderers while running the program. The renderer you start out with Software or OpenGL is the renderer you should stay with. Sometimes changing the renderer works fine, and other times it makes my program crash. ???

Jman