JOGL problem when drawing on canvas

Scripting
2010-08-28
2013-06-05
  • Guillermo Pérez

    Peter:

    You know i'm using JOGl without calling the CanvasDrawer. I'm doing so because you explained me that there's no native way to draw on canvas from a plugin using the canvasDrawer (hope that could change in future versions ;o)

    Problem is that even when the text shows ok, i discovered a conflict with reference images. Every time i render text using JOGL, the reference images gets distorted:



    Maybe is a problem with the image being allocated in memory, and being rendered once again when i display the text?

    This is my code, more or less:

    private class CanvasListener implements GLEventListener
              {
                public void init(GLAutoDrawable drawable)
                {
                  Font font = view.getComponent().getFont();
                  renderer = new TextRenderer(font);
                  renderer.setColor(ViewerCanvas.lineColor);
                }
                public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
                {
                }
                public void display(GLAutoDrawable drawable)
                {
    
                    // draw text for JOGL
                    renderer.beginRendering(drawable.getWidth(), drawable.getHeight());
                    doCount();
    
                    if (showHideCheckBox)
                    {
                        renderer.draw(selectedMessage, 50, drawable.getHeight()-45); // x - y
                        renderer.draw(statsMessage, drawable.getWidth()-statsWidth-50, drawable.getHeight()-45);    
                    }       
                    renderer.endRendering();
    
                }
                public void displayChanged(GLAutoDrawable drawable, boolean arg1, boolean arg2)
                {
                }
              }
    

    The code belongs to "countSelected" plugin. It only distorts the picture once. But in the BonesNames plugins, the images distorts each time i create a bone (and it's named). It's a cycle of distortions: after 5 or 6 times, the images gets restored. (?)

     
  • Peter Eastman

    Peter Eastman - 2010-09-01

    I don't know exactly what TextRenderer is doing, but I suspect it's modifying some aspect of the OpenGL state that GLCanvasDrawer never modifies and therefore doesn't reset.  Given the behavior you're seeing, the texture matrix seems like a likely candidate.  Try surrounding your code with a glPushMatrix()/glPopMatrix() to save and restore it.

    Peter

     
  • Guillermo Pérez

    I just tried it, but it's still distorting the image. Maybe i should give you more info about the way i use JOGL (anyway, the code is published in the forum):

    usingOpenGL = ArtOfIllusion.getPreferences().getUseOpenGL(); // Jogl is on?
    
                /*** SoftwareCanvasDrawer Event ***/
                if (!(usingOpenGL))
                {
                canvas.addEventLink(RepaintEvent.class, this, "getCanvasAndProceed");           
                }
    
                /*** Check for OpenGL ***/
                try
                {
                  Class.forName("javax.media.opengl.GLCanvas");
                  openGLAvailable = true; // exists.
    
                  if (!(usingOpenGL)) return;
                  
                  JOGLdrawer =  canvas.getCanvasDrawer();
                  gCanvas = (GLCanvas)((GLCanvasDrawer)JOGLdrawer).getGLCanvas();
                  gl = gCanvas.getGL();
                  
                  // GLCanvasDrawer Event 
                  gCanvas.addGLEventListener(new CanvasListener());
    
                } catch(Exception e){}
    

    Then , i added you you suggested (under display(GLAutoDrawable drawable) section ):

    public void display(GLAutoDrawable drawable)
                {
    
                    // draw text for JOGL
                    renderer.beginRendering(drawable.getWidth(), drawable.getHeight());
                    doCount();
    
                    if (showHideCheckBox)
                    {
                        gl.glPushMatrix();
                        renderer.draw(selectedMessage, 50, drawable.getHeight()-45); // x - y
                        renderer.draw(statsMessage, drawable.getWidth()-statsWidth-50, drawable.getHeight()-45);    
                        gl.glPopMatrix();
                    }       
                    renderer.endRendering();
    
                }
    

    Also i tried plcaing the methods this way:

    gl.glPushMatrix();
                    renderer.beginRendering(drawable.getWidth(), drawable.getHeight());
                    doCount();
    
                    if (showHideCheckBox)
                    {
    
                        renderer.draw(selectedMessage, 50, drawable.getHeight()-45); // x - y
                        renderer.draw(statsMessage, drawable.getWidth()-statsWidth-50, drawable.getHeight()-45);
    
                    }       
                    renderer.endRendering();
                    gl.glPopMatrix();
    

    I don't get a word about jogl, and there's no much information to start with. Besides, learning such a exclusive language is out of my scope. Hope you can find the issue.

    Here's the textRenderer specification, btw:
    http://www.cse.unsw.edu.au/~cs3421/jogl/javadoc_public/com/sun/opengl/util/j2d/TextRenderer.html

     
  • Peter Eastman

    Peter Eastman - 2010-09-02

    Before any matrix manipulation routine, you first have to tell in which matrix to operate on.  At the beginning you want to call

    gl.glMatrixMode(GL.GL_TEXTURE);
    gl.glPushMatrix();

    and then at the end

    gl.glMatrixMode(GL.GL_TEXTURE);
    gl.glPopMatrix();

    Peter

     
  • Guillermo Pérez

    I see. I tried it, but still makes no improvement.

    note: i'm publishing some notes from the textRenderer class. Maybe that can lead us to solve the issue?

    public void display(GLAutoDrawable drawable)
                {
    
                    // draw text for JOGL
                    gl.glMatrixMode(GL.GL_TEXTURE);
                    gl.glPushMatrix();
                    renderer.beginRendering(drawable.getWidth(), drawable.getHeight());
                    doCount();
    
                    if (showHideCheckBox)
                    {
    
                        renderer.draw(selectedMessage, 50, drawable.getHeight()-45); // x - y
                        renderer.draw(statsMessage, drawable.getWidth()-statsWidth-50, drawable.getHeight()-45);
    
                    }       
                    renderer.endRendering();
                    gl.glMatrixMode(GL.GL_TEXTURE);
                    gl.glPopMatrix();
                }
    

    Unless you are sharing textures and display lists between OpenGL contexts, you do not need to call the dispose method of the TextRenderer; the OpenGL resources it uses internally will be cleaned up automatically when the OpenGL context is destroyed.

    Note that the TextRenderer may cause the vertex and texture coordinate array buffer bindings to change, or to be unbound. This is important to note if you are using Vertex Buffer Objects (VBOs) in your application.

    Internally, the renderer uses a rectangle packing algorithm to pack both glyphs and full Strings' rendering results (which are variable size) onto a larger OpenGL texture. The internal backing store is maintained using a TextureRenderer. A least recently used (LRU) algorithm is used to discard previously rendered strings; the specific algorithm is undefined, but is currently implemented by flushing unused Strings' rendering results every few hundred rendering cycles, where a rendering cycle is defined as a pair of calls to beginRendering / endRendering.

    http://www.cse.unsw.edu.au/~cs3421/jogl/javadoc_public/com/sun/opengl/util/j2d/TextRenderer.html

     
  • Harald G

    Harald G - 2010-10-25

    Mayid,
    is there an solution for this?

    I use the verticecount plugin always - but now I´ve seen that distortion and it took me a while to remember this thread.

    Harald

     
  • Guillermo Pérez

    By my side, there's no solution. Jogl / OpenGL is such different from other languages that i just cannot dig on it. I tried Peter's suggestions to clean buffers, but i couldn't get it working.

    A future solution could be a (new) public method to use the canvas from plugins / scripts or something. By the time, we could focus on getting this JOGL implementation to work. I'd really like to solve incompatibilities issues.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks