Problems when rendering a texture in JOGL

Hello,

I have a problem when trying to use the createGraphics() method in the TextureRenderer class. I have no exceptions, but I see nothing (only the OpenGL background). What I am doing is:

  • creating a texture renderer once at the beginning of the program (I also get an external OpenGL context, and it’s working fine, as I’m able to get the viewport width and height, and correctly use OpenGL commands in my Java program with JOGL):

GLDrawableFactory factory = GLDrawableFactory.getFactory();
context = factory.createExternalGLContext();
context.makeCurrent();

renderer = new TextureRenderer(framewidth, frameheight, true);
GL gl = context.getGL();
gl.glEnable(GL.GL_TEXTURE_2D);

  • when I want to draw:

GL gl = context.getGL();
glEnable2D();

Graphics2D g2d = renderer.createGraphics();
g2d.setColor(Color.MAGENTA);
g2d.fillRect(0, 0, 50, 50);
g2d.dispose();

texture = renderer.getTexture();
gl.glBindTexture(GL.GL_TEXTURE_2D, texture.getTarget());
texture.enable();
texture.bind();

gl.glEnable(GL_BLEND);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

gl.glBegin(GL.GL_QUADS);
    gl.glTexCoord2f(0, 0);  // Texture Coord (Bottom Left)
    gl.glVertex2i(0, 0);      // Vertex Coord (Bottom Left)
    gl.glTexCoord2f(framewidth, 0);  // Texture Coord (Bottom Right)
    gl.glVertex2i(framewidth, 0);      // Vertex Coord (Bottom Right)
    gl.glTexCoord2f(framewidth, frameheight);  // Texture Coord (Top Right)
    gl.glVertex2i(framewidth, frameheight);      // Vertex Coord (Top Right)
    gl.glTexCoord2f(0, frameheight);    // Texture Coord (Top Left)
    gl.glVertex2i(0, frameheight);      // Vertex Coord (Top Left)
gl.glEnd();          // Done Building Our Quad (Character)            
texture.disable();

gl.glTranslatef(100f,100f,0.0f);
   gl.glBegin(GL_TRIANGLES);
   gl.glColor3f(0.0f,1.0f,0.0f);
   gl.glVertex2f( 0.0f, 60.0f);
   gl.glVertex2f(-60.0f,-60.0f);
   gl.glVertex2f( 60.0f,-60.0f);
gl.glEnd();

glDisable2D();

  • The green triangle is drawing fine on the external context, but I don’t see the magenta rectangle.

Remark: I use this glEnable2D() method to be able to draw “HUD style” with pixel coordinates:


private void glEnable2D() {
  GL gl = context.getGL();
  gl.glMatrixMode(GL_PROJECTION);
  gl.glPushMatrix();
  gl.glLoadIdentity();

  gl.glOrtho(vPort[0], vPort[0] + vPort[2], vPort[1], vPort[1] + vPort[3], -1, 1);
  gl.glMatrixMode(GL_MODELVIEW);
  gl.glPushMatrix();
  gl.glLoadIdentity();
  gl.glTranslatef(0.375f, 0.375f, 0f);

  gl.glPushAttrib(GL_DEPTH_BUFFER_BIT); 
  gl.glDisable(GL_DEPTH_TEST);
}

What is wrong in my code ? (again the JOGL OpenGL drawing is OK apart from the texture, and I have no exceptions).

Also changing the draw() code to:


texture = renderer.getTexture();
gl.glEnable(GL_BLEND);            
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
texture.bind();
texture.enable();

gl.glBegin(GL.GL_QUADS);
    gl.glTexCoord2f(0, 0);  // Texture Coord (Bottom Left)
    gl.glVertex2i(0, 0);      // Vertex Coord (Bottom Left)
    gl.glTexCoord2f(vportwidth, 0);  // Texture Coord (Bottom Right)
    gl.glVertex2i(vportwidth, 0);      // Vertex Coord (Bottom Right)
    gl.glTexCoord2f(vportwidth, vportheight);  // Texture Coord (Top Right)
    gl.glVertex2i(vportwidth, vportheight);      // Vertex Coord (Top Right)
    gl.glTexCoord2f(0, vportheight);    // Texture Coord (Top Left)
    gl.glVertex2i(0, vportheight);      // Vertex Coord (Top Left)
gl.glEnd();          // Done Building Our Quad (Character)
texture.disable();

(where vportwidth vportheight are the real width and height of the OpenGL viewport) does not change anything.

OK, again, it’s working now…