package org.helioviewer.gl3d.scenegraph.visuals;

import org.helioviewer.base.physics.Constants;
import org.helioviewer.gl3d.scenegraph.GL3DDrawBits.Bit;
import org.helioviewer.gl3d.scenegraph.GL3DGroup;
import org.helioviewer.gl3d.scenegraph.GL3DState;
import org.helioviewer.gl3d.scenegraph.math.GL3DMat4d;
import org.helioviewer.gl3d.scenegraph.math.GL3DQuatd;
import org.helioviewer.gl3d.scenegraph.math.GL3DVec3d;
import org.helioviewer.gl3d.scenegraph.math.GL3DVec4d;
import org.helioviewer.gl3d.scenegraph.math.GL3DVec4f;

public class GL3DGrid extends GL3DGroup {
    private final int xticks;
    private final int yticks;
    private final GL3DVec4f color;
    private final GL3DVec4d textColor;
    private String font;

    public GL3DGrid(String name, int xticks, int yticks, GL3DVec4f color, GL3DVec4d textColor) {
        super(name);
        this.xticks = xticks;
        this.yticks = yticks;
        this.color = color;
        this.textColor = textColor;
        this.loadGrid();
    }

    private void loadGrid() {
        GL3DSphere sphere = new GL3DSphere(Constants.SunRadius * 1.02, this.xticks, this.yticks, this.color);
        sphere.getDrawBits().on(Bit.Wireframe);
        this.addNode(sphere);
        GL3DText rect;
        double letterSize = 0.05 * Constants.SunRadius;
        double size = Constants.SunRadius * 1.1;
        double zdist = Constants.SunRadius * 0.001;

        for (int i = 1; i < this.xticks; i++) {
            double angle = i * Math.PI / this.xticks;
            String angleStringx = "" + (int) (90 - 1.0 * i / this.xticks * 180);
            rect = new GL3DText(letterSize, letterSize, Math.sin(angle) * size, Math.cos(angle) * size, zdist, angleStringx, this.textColor);
            this.addNode(rect);
            String angleStringy = "" + (int) (90 - 1.0 * i / this.xticks * 180);
            rect = new GL3DText(letterSize, letterSize, -Math.sin(angle) * size, Math.cos(angle) * size, zdist, angleStringy, this.textColor);
            this.addNode(rect);
        }
        for (int i = 1; i < this.yticks; i++) {
            double angle = i * Math.PI / (this.yticks / 2.0);
            String angleString = "" + (int) (90 - 1.0 * i / (this.yticks / 2.0) * 180);
            rect = new GL3DText(letterSize, letterSize, Math.cos(angle) * size, 0.0, Math.sin(angle) * size, angleString, this.textColor);
            this.addNode(rect);
        }
    }

    private void reloadGrid() {
        this.deleteAll(GL3DState.get());
        this.loadGrid();
    }

    @Override
    public void shapeDraw(GL3DState state) {
        double differentialRotation = state.getActiveCamera().getDifferentialRotation();
        this.m = GL3DMat4d.identity();
        this.m.multiply(GL3DQuatd.createRotation(differentialRotation, new GL3DVec3d(0, 1, 0)).toMatrix());
        //System.out.println(rotation);
        super.shapeDraw(state);
        //this.m.multiply(GL3DQuatd.createRotation(differentialRotation, new GL3DVec3d(0, 1, 0)).toMatrix().inverse());

    }
}

@@ -247,16 +247,71 @@
 DState;%0A
+import org.helioviewer.gl3d.scenegraph.math.GL3DMat3d;%0A
 import o
@@ -2562,25 +2562,24 @@
 id();%0A    %7D%0A
-%0A
     @Overrid
@@ -2596,25 +2596,22 @@
 ic void 
-shapeDraw
+update
 (GL3DSta
@@ -2618,24 +2618,123 @@
 te state) %7B%0A
+        if (!this.isInitialised) %7B%0A            this.init(state);%0A        %7D%0A        state.pushMV();%0A
         doub
@@ -2969,69 +2969,126 @@
     
-//System.out.println(rotation);%0A        super.shapeDraw(state
+this.wm = (this.m);%0A        state.buildInverseAndNormalMatrix();%0A        this.wmI = new GL3DMat4d(state.getMVInverse()
 );%0A 
@@ -3098,121 +3098,235 @@
     
-//
 this.
-m.multiply(GL3DQuatd.createRotation(differentialRotation, new GL3DVec3d(0, 1, 0)).toMatrix().inverse()
+wmN = new GL3DMat3d(state.normalMatrix);%0A        this.shapeUpdate(state);%0A        this.buildAABB();%0A        state.popMV();%0A%0A    %7D%0A%0A    @Override%0A    public void shapeDraw(GL3DState state) %7B%0A        super.shapeDraw(state
 );%0A
-%0A
     

Set matrix before