using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BauzoidNET.math;
namespace BauzoidNET
.graphics.renderstates
{
public class RenderStates
{
public static int NUM_TEXTURE_STAGES
= 2;
public static int MAX_VERTEX_BUFFER_ID
= 8;
public BlendingStates blending
= null;
public CullingStates culling
= null;
public DepthTestStates depthTest
= null;
public ScissorStates scissor
= null;
private TextureStage
[] mTextureStages
= new TextureStage
[NUM_TEXTURE_STAGES
];
private bool mLocked
= false;
public const int MATRIX_STACK_SIZE
= 16;
//public Matrix4 modelViewProjection;
public Matrix4 model
= Matrix4
.createIdentity();
public Matrix4 view
= Matrix4
.createIdentity();
public Matrix4 projection
= Matrix4
.createIdentity();
private Matrix4
[] mModelStack
= new Matrix4
[MATRIX_STACK_SIZE
];
private Matrix4
[] mViewStack
= new Matrix4
[MATRIX_STACK_SIZE
];
private Matrix4
[] mProjectionStack
= new Matrix4
[MATRIX_STACK_SIZE
];
private int mModelStackCurrentIndex
= 0;
private int mViewStackCurrentIndex
= 0;
private int mProjectionStackCurrentIndex
= 0;
/** Constructor. */
public RenderStates
()
{
blending
= new BlendingStates
(this);
culling
= new CullingStates
(this);
depthTest
= new DepthTestStates
(this);
scissor
= new ScissorStates
(this);
for (int i
= 0; i
< NUM_TEXTURE_STAGES
; i
++)
{
mTextureStages
[i
] = new TextureStage
(this, i
);
}
for (int i
= 0; i
< MATRIX_STACK_SIZE
; i
++)
{
mModelStack
[i
] = Matrix4
.createIdentity();
mViewStack
[i
] = Matrix4
.createIdentity();
mProjectionStack
[i
] = Matrix4
.createIdentity();
}
}
/** Initialize render states. */
public void initialize
() //throws BauzoidException
{
blending
.initialize();
culling
.initialize();
depthTest
.initialize();
scissor
.initialize();
for (int i
= 0; i
< NUM_TEXTURE_STAGES
; i
++)
{
mTextureStages
[i
].initialize();
}
}
/** Activate renderstates - calls activate(false). */
public void activate
()
{
activate
(false);
}
/** Activate render states with force parameter. */
public void activate
(bool force
)
{
blending
.activate(force
);
culling
.activate(force
);
depthTest
.activate(force
);
scissor
.activate(force
);
}
/** Reset to default values. */
public void reset
()
{
blending
.reset();
culling
.reset();
depthTest
.reset();
scissor
.reset();
}
/** Deactivate render states. */
public void deactivate
()
{
reset
();
activate
(false);
}
/** Check if the renderstates are locked. */
public bool isLocked
()
{
return mLocked
;
}
/** Lock renderstates. */
public void lockStates
(bool doLock
)
{
mLocked
= doLock
;
}
/** Get a texture stage. */
public TextureStage getTextureStage
(int index
)
{
return mTextureStages
[index
];
}
/** Multiply matrices and return the full model-view-projection matrix. */
public Matrix4 getModelViewProjection
()
{
Matrix4 mvpMatrix
= Matrix4
.multiply(model, view
);
mvpMatrix
= Matrix4
.multiply(mvpMatrix, projection
);
return mvpMatrix
;
}
public void getModelViewProjection
(Matrix4 target
)
{
Matrix4
.multiply(target, model, view
);
Matrix4
.multiply(target, target, projection
);
}
/** Push current model matrix to stack. */
public void pushModelMatrix
()
{
if (mModelStackCurrentIndex
>= MATRIX_STACK_SIZE
)
return;
// Create copy so that calling code can work on a different matrix without affecting anything
mModelStack
[mModelStackCurrentIndex
].copyFrom(model
);
mModelStackCurrentIndex
++;
}
/** Pop current model matrix from stack. */
public void popModelMatrix
()
{
if (mModelStackCurrentIndex
<= 0)
return;
mModelStackCurrentIndex
--;
model
.copyFrom(mModelStack
[mModelStackCurrentIndex
]);
}
/** Push current model matrix to stack. */
public void pushViewMatrix
()
{
if (mViewStackCurrentIndex
>= MATRIX_STACK_SIZE
)
return;
mViewStack
[mViewStackCurrentIndex
].copyFrom(view
);
mViewStackCurrentIndex
++;
}
/** Pop current model matrix from stack. */
public void popViewMatrix
()
{
if (mViewStackCurrentIndex
<= 0)
return;
mViewStackCurrentIndex
--;
view
.copyFrom(mViewStack
[mViewStackCurrentIndex
]);
}
/** Push current model matrix to stack. */
public void pushProjectionMatrix
()
{
if (mProjectionStackCurrentIndex
>= MATRIX_STACK_SIZE
)
return;
mProjectionStack
[mProjectionStackCurrentIndex
].copyFrom(projection
);
mProjectionStackCurrentIndex
++;
}
/** Pop current model matrix from stack. */
public void popProjectionMatrix
()
{
if (mProjectionStackCurrentIndex
<= 0)
return;
mProjectionStackCurrentIndex
--;
projection
.copyFrom(mProjectionStack
[mProjectionStackCurrentIndex
]);
}
}
}