Author Topic: Using enableGLCanvasRenderer  (Read 6498 times)

Offline Mr.Marbles

  • int
  • **
  • Posts: 81
    • View Profile
Using enableGLCanvasRenderer
« on: January 18, 2006, 05:18:30 pm »
I need to render into a JPanel component, so I'm using the following code:
Code: [Select]

Canvas myCanvas = frameBuffer.enableGLCanvasRenderer(IRenderer.MODE_OPENGL);
JPanel panel = new JPanel();
panel.add(myCanvas);

Now if I want to switch to a software renderer I can do the following:
Code: [Select]

frameBuffer.disableRenderer(IRenderer.RENDERER_OPENGL);
frameBuffer.enableRenderer(IRenderer.RENDERER_SOFTWARE, IRenderer.MODE_OPENGL);

My question is what's the difference, when using software rendering, to render to a Canvas or to a Graphics object such as:
Code: [Select]

Graphics g = panel.getGraphics();
frameBuffer.display(g);

Is one method better than the other in terms of performance?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Using enableGLCanvasRenderer
« Reply #1 on: January 18, 2006, 09:51:16 pm »
The AWTGLCanvas is a Canvas by design. The software renderer renders into an Image. You can either use that directly, or let the FrameBuffer itself draw it into a Graphics context or even use the pixels array....
To be honest, i don't really understand the question... :?:

Offline Mr.Marbles

  • int
  • **
  • Posts: 81
    • View Profile
Using enableGLCanvasRenderer
« Reply #2 on: January 19, 2006, 04:58:46 pm »
EgonOlsen,

I'm sorry if my question was unclear, I will try to elaborate. Is there a "best" way for doing software rendering? I noticed that using a Canvas:
Code: [Select]
Canvas myCanvas = frameBuffer.enableGLCanvasRenderer(IRenderer.MODE_OPENGL);
JPanel panel = new JPanel();
panel.add(myCanvas);
.
.
.
private void display()
{
    frameBuffer.clear();
    theWorld.renderScene(frameBuffer);
    theWorld.draw(frameBuffer);
    frameBuffer.update();

    myCanvas.refresh();
}

and using the Graphics context:
Code: [Select]
JPanel panel = new JPanel();
Graphics g = panel.getGraphics();
.
.
.
private void display()
{
    frameBuffer.clear();
    theWorld.renderScene(frameBuffer);
    theWorld.draw(frameBuffer);
    frameBuffer.update();

    frameBuffer.display(g);
}

do exactly the same thing. Which is the "better" way?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Using enableGLCanvasRenderer
« Reply #3 on: January 19, 2006, 07:16:44 pm »
The first example (except that it's Canvas.repaint(), not refresh()) doesn't do software rendering but uses the hardware renderer. Only the second one is software. I guess that's why i don't fully understand the question.
Anyway, they are not doing the same thing. The first one uses passive rendering, the second one active. I.e. in the second example, the pixels are being painted when you call display(g). You can rely on that after the call, the Panel shows the current frame. This isn't the case in the first example. repaint() just schedules the component for a repaint, it doesn't actually paint anything. Painting is done in the awt event dispatch thread when the VM thinks it's time to.
Ignoring the fact that we are comparing apples (software) to bananas (hardware) here, the former way of doing things is mandatory when using a GLCanvasRenderer and usefull for frames that should integrate into Swing/AWT-GUIs (by putting the display(g) call into the paint()-method of the component) when using software rendering.
The latter way is better when using software rendering without any further GUI elements because it gives you total control of what to paint when.

Offline Mr.Marbles

  • int
  • **
  • Posts: 81
    • View Profile
Using enableGLCanvasRenderer
« Reply #4 on: January 19, 2006, 07:57:11 pm »
What if I create the  Canvas like this:
Code: [Select]
Canvas myCanvas = frameBuffer.enableGLCanvasRenderer(IRenderer.RENDERER_SOFTWARE);
Won't this imply software rendering into the Canvas object?

Offline Mr.Marbles

  • int
  • **
  • Posts: 81
    • View Profile
Using enableGLCanvasRenderer
« Reply #5 on: January 19, 2006, 08:12:20 pm »
I think I understand now. After reviewing the javadoc for the IRenderer class I see that the "mode" only determines what type of lighting model the renderer will use. So enableGLCanvasRenderer will always be rendering in hardware and never in software. Therefore there are no 'Canvas-type' components in which software rendering can be done. It seems the only way to render via software is to use the FrameBuffer.draw(Graphics). Furthermore, there are exactly two ways to render via hardware; using enableGLCanvasRenderer, or using FrameBuffer.displayGLOnly() (with a FrameBuffer.enableRenderer(IRenderer.RENDERER_OPENGL, IRenderer.MODE_OPENGL) call).

Offline Melssj5

  • double
  • *****
  • Posts: 724
    • View Profile
Using enableGLCanvasRenderer
« Reply #6 on: January 19, 2006, 10:28:15 pm »
Of course you can do it.

If you want to render by software inside a canvas you just need to use a comon awt canvas and use display (Graphics g); method to render.

To make the images appear on the canvas you must put the rendering code inside the paint method of the canvas.

public void paint (Graphics g) {
//your rendering code.
}
Nada por ahora

Offline Mr.Marbles

  • int
  • **
  • Posts: 81
    • View Profile
Using enableGLCanvasRenderer
« Reply #7 on: January 19, 2006, 10:42:07 pm »
Melssj5,

Yes, you're absolutely right. I didn't write my statement properly. I meant that there are no 'Canvas-type' components that are returned by a class in the jpct lib into which a software renderer will automatically draw a rendered frame; i.e. without calling a FrameBuffer.draw(Graphics).

Just to add to your post: any subclass of java.awt.Component can be potentially used as a rendering host for FrameBuffer.draw(Graphics)  :wink:

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Using enableGLCanvasRenderer
« Reply #8 on: January 19, 2006, 11:08:10 pm »
Quote from: "Mr.Marbles"
It seems the only way to render via software is to use the FrameBuffer.draw(Graphics). Furthermore, there are exactly two ways to render via hardware; using enableGLCanvasRenderer, or using FrameBuffer.displayGLOnly() (with a FrameBuffer.enableRenderer(IRenderer.RENDERER_OPENGL, IRenderer.MODE_OPENGL) call).
You may also get the rendered Image from the FrameBuffer and do something with that and even the pixels-int[]-array is available.
You are right about hardware rendering. The two ways use completely different renderers. The reason why the enableGL...exist is, that is has to return a Canvas to be of any use while the enableRenderer can't do that.