package com.jogamp.opengl.util;

import org.junit.Before;
import org.junit.Test;

import javax.media.opengl.fixedfunc.GLMatrixFunc;
import java.nio.FloatBuffer;

import static org.junit.Assert.assertArrayEquals;

/**
 * @author Thomas De Bodt
 */
public class PMVMatrixTest {

  private PMVMatrix fMat;

  @Before
  public void setUp() throws Exception {
    fMat = new PMVMatrix();
  }

  @Test
  public void testLookAtNegZIsNoOp() throws Exception {
    fMat.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    // Look towards -z
    fMat.gluLookAt(
        0, 0, 0,
        0, 0, -1,
        0, 1, 0
    );
    FloatBuffer mvMatrix = fMat.glGetMvMatrixf();
    float[] mvMatrixArr = new float[16];
    mvMatrix.asReadOnlyBuffer().get(mvMatrixArr);
    assertArrayEquals(
        /**
         * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
         */
        new float[] {
            1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
        },
        mvMatrixArr,
        1e-6f
    );
  }
  @Test
  public void testLookAtPosY() throws Exception {
    fMat.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    // Look towards +y
    fMat.gluLookAt(
        0, 0, 0,
        0, 1, 0,
        0, 0, 1
    );
    FloatBuffer mvMatrix = fMat.glGetMvMatrixf();
    float[] mvMatrixArr = new float[16];
    mvMatrix.asReadOnlyBuffer().get(mvMatrixArr);
    assertArrayEquals(
        /**
         * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
         */
        new float[] {
            1, 0, 0, 0,
            0, 0, -1, 0,
            0, 1, 0, 0,
            0, 0, 0, 1
        },
        mvMatrixArr,
        1e-6f
    );
  }
}
