<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://jogamp.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.2"
          urlbase="https://jogamp.org/bugzilla/"
          
          maintainer="sgothel@jausoft.com"
>

    <bug>
          <bug_id>1006</bug_id>
          
          <creation_ts>2014-05-06 14:20:37 +0200</creation_ts>
          <short_desc>Transformation applied to a texture is also applied to post render graphics</short_desc>
          <delta_ts>2016-10-26 02:00:42 +0200</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>General</classification>
          <product>Java3D</product>
          <component>core</component>
          <version>unspecified</version>
          <rep_platform>All</rep_platform>
          <op_sys>all</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>---</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Manu">puybaret</reporter>
          <assigned_to name="Phil Jordan">p.j.nz</assigned_to>
          <cc>gouessej</cc>
    
    <cc>p.j.nz</cc>
          
          <cf_type>DEFECT</cf_type>
          <cf_scm_refs></cf_scm_refs>
          <cf_workaround>---</cf_workaround>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>3890</commentid>
    <comment_count>0</comment_count>
    <who name="Manu">puybaret</who>
    <bug_when>2014-05-06 14:20:37 +0200</bug_when>
    <thetext>When a transformation is applied on a texture with a call to TextureAttributes#setTextureTransform, this transformation is also applied to the graphics drawn with a J3DGraphics2D instance returned by Canvas3D#getGraphics2D, when called in an postRender overridden method.

In the following example, the red rectangle drawn in canvas postRender shouldn&apos;t be rotated.

mport java.awt.*;
import java.awt.image.BufferedImage;
import javax.media.j3d.*;
import javax.vecmath.Vector4f;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class TextureTransformTest {
  public static void main(String [] args) {
    // Create a box using a rotated texture
    BranchGroup scene = new BranchGroup();    
    Appearance appearance = new Appearance();
    // Create a simple image with a white line
    BufferedImage image = new BufferedImage(4, 4, BufferedImage.TYPE_INT_ARGB);
    Graphics g = image.getGraphics();
    g.setColor(Color.WHITE);
    g.drawLine(0, 0, 4, 0);
    g.dispose();
    appearance.setTexture(new TextureLoader(image).getTexture());
    // Rotate the texture
    TextureAttributes textureAttributes = new TextureAttributes();
    Transform3D rotation = new Transform3D();
    rotation.rotZ(Math.PI / 4);
    textureAttributes.setTextureTransform(rotation);
    appearance.setTextureAttributes(textureAttributes);
    appearance.setTexCoordGeneration(new TexCoordGeneration(
        TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2, 
        new Vector4f(10, 0, 0, 10), new Vector4f(0, 10, -10, 10)));
    Box box = new Box(0.5f, 0.5f, 0.5f, appearance);
    scene.addChild(box);
    
    // Create a canvas 3D that post renders a red rectangle  
    GraphicsConfiguration configuration = GraphicsEnvironment
        .getLocalGraphicsEnvironment().getDefaultScreenDevice()
        .getBestConfiguration(new GraphicsConfigTemplate3D());
    Canvas3D canvas = new Canvas3D(configuration) {
        @Override
        public void postRender() {
          // Bug: The red rectangle is correct under Java 3D 1.3
          // but is rotated under Java 3D 1.5.2 and 1.6
          J3DGraphics2D g2D = getGraphics2D();
          g2D.setColor(Color.RED);
          g2D.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
          g2D.flush(true);
        }
      };
    SimpleUniverse universe = new SimpleUniverse(canvas);
    universe.getViewingPlatform ().setNominalViewingTransform ();
    universe.addBranchGraph(scene);

    // Build a GUI that displays canvas 
    Frame frame = new Frame(&quot;TextureTransformTest&quot;);
    frame.add(canvas, BorderLayout.CENTER);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>4889</commentid>
    <comment_count>1</comment_count>
    <who name="Phil Jordan">p.j.nz</who>
    <bug_when>2015-08-07 10:55:42 +0200</bug_when>
    <thetext>I can confirm this bug, it was killing me for years, I couldn&apos;t do a decent HUD.
I have a workaround however.
The issue (if it&apos;s the same as I&apos;ve had) only occurs when teh transformation is on the last rendered object, the order of rendering being not controlable in normal usage. 

The work around is to render a trivial object in the postRender call just prior to getting the graphics, in Canvas3D sub class:

	// For reseting the texture binding in the pipeline
	private static Shape3D trivialShape = new Cube(0.01f);

	@Override
	public void postRender()
	{
		//  if the last rendered texture on a canvas3d has a transformation
		// then calls to the J3DGraphics2D will inherit it. Easy way to ensure last texture is plain, render trival cube.
		getGraphicsContext3D().draw(trivialShape);
                J3DGraphics2D g = getGraphics2D();
		//draw hud etc...
	}

Hope this helps someone somewhere.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5960</commentid>
    <comment_count>2</comment_count>
    <who name="Manu">puybaret</who>
    <bug_when>2016-10-18 12:09:45 +0200</bug_when>
    <thetext>Thanks for the workaround, it works well.
Trying to find a cleaner fix, I found that adding the following lines in JoglPipeline#texturemapping() after the call to
  gl.glEnable(GL.GL_TEXTURE_2D); 
would fix the problem:

  gl.glPushAttrib(GL2.GL_TRANSFORM_BIT);
  gl.glMatrixMode(GL.GL_TEXTURE);
  gl.glLoadIdentity();
  gl.glPopAttrib();

I&apos;m not sure that calls to gl.glPushAttrib and gl.glPopAttrib are mandatory. I borrowed this code from JoglPipeline#updateTextureAttributes method.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5968</commentid>
    <comment_count>3</comment_count>
    <who name="Phil Jordan">p.j.nz</who>
    <bug_when>2016-10-26 02:00:42 +0200</bug_when>
    <thetext>Fixed in branch 1.7.0</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>