Kamerafunktion verbessert, div. Fehler korrigiert
parent
c6aafb8cc9
commit
29a1bdea77
|
@ -6,122 +6,84 @@ using OpenTK;
|
|||
|
||||
namespace org.nhengine.graphics
|
||||
{
|
||||
public class GLCamera
|
||||
public class GLCamera : GLSceneOrientation
|
||||
{
|
||||
Vector3 vPosition; // Position der Kamera
|
||||
Vector3 vDirection; // Blickrichtung der Kamera
|
||||
float fov;
|
||||
float width, height;
|
||||
|
||||
float fDistance; // Zusätzliche Distanz der Kamera entgegen der Blickrichtung
|
||||
|
||||
Matrix4 mTranslation; // Matrix für Translation
|
||||
Matrix4 mRotation; // Matrix für Rotation
|
||||
Matrix4 mDistance; // Matrix für Rückzug entgegen der Blickrichtung
|
||||
Matrix4 mProjection; // Projektionsmatrix
|
||||
Vector3 vPosition,
|
||||
vView,
|
||||
vTop;
|
||||
|
||||
public GLCamera()
|
||||
{
|
||||
vPosition = new Vector3(0,0,0);
|
||||
vDirection = Vector3.UnitZ;
|
||||
fDistance = 0.0f;
|
||||
fov = MathHelper.PiOver2;
|
||||
width = 100;
|
||||
height = 100;
|
||||
|
||||
mProjection = Matrix4.CreatePerspectiveFieldOfView(
|
||||
MathHelper.PiOver4,
|
||||
1.0f,
|
||||
50.0f,
|
||||
100000.0f
|
||||
);
|
||||
vPosition = new Vector3(0,0,1000);
|
||||
vView = new Vector3(0,0,-1);
|
||||
vTop = new Vector3(0,1,0);
|
||||
|
||||
mRotation = Matrix4.Identity;
|
||||
|
||||
glPrepare();
|
||||
buildProjection();
|
||||
buildMatrices();
|
||||
}
|
||||
|
||||
public void setViewport(int width, int height)
|
||||
{
|
||||
mProjection = Matrix4.CreatePerspectiveFieldOfView(
|
||||
MathHelper.PiOver4,
|
||||
width / (float)height,
|
||||
this.width = (float)width;
|
||||
this.height = (float)height;
|
||||
|
||||
buildProjection();
|
||||
}
|
||||
public void setFoV(float fov){
|
||||
this.fov = fov;
|
||||
buildProjection();
|
||||
}
|
||||
|
||||
private void buildProjection(){
|
||||
_mProjection = Matrix4.CreatePerspectiveFieldOfView(
|
||||
fov,
|
||||
width / height,
|
||||
50.0f,
|
||||
100000.0f
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
private void glPrepare(){
|
||||
this.mTranslation = Matrix4.CreateTranslation(vPosition * -1.0f);
|
||||
this.mDistance = mRotation * Matrix4.CreateTranslation(0, 0, -fDistance);
|
||||
|
||||
// Console.WriteLine("View Direction: {0}", vDirection);
|
||||
// Console.WriteLine("Rotation Matrix: {0}", mRotation);
|
||||
|
||||
public Vector3 Position
|
||||
{
|
||||
get { return new Vector3(vPosition); }
|
||||
set { this.vPosition = new Vector3(value); buildMatrices(); }
|
||||
}
|
||||
public Vector3 View
|
||||
{
|
||||
get { return new Vector3(vView); }
|
||||
set { this.vView = new Vector3(value).Normalized(); buildMatrices(); }
|
||||
}
|
||||
public Vector3 Top
|
||||
{
|
||||
get { return new Vector3(vTop); }
|
||||
set { this.vTop = new Vector3(value).Normalized(); buildMatrices(); }
|
||||
}
|
||||
|
||||
private void glUpdate(){
|
||||
private void buildMatrices(){
|
||||
|
||||
}
|
||||
|
||||
public void use(ShaderProgram sp){
|
||||
glUpdate();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Matrix4 MTranslation
|
||||
{
|
||||
get
|
||||
{
|
||||
return mTranslation;
|
||||
}
|
||||
}
|
||||
|
||||
public Matrix4 MRotation
|
||||
{
|
||||
get
|
||||
{
|
||||
return mRotation;
|
||||
}
|
||||
set {
|
||||
this.mRotation = value;
|
||||
}
|
||||
}
|
||||
|
||||
public Matrix4 MDistance
|
||||
{
|
||||
get
|
||||
{
|
||||
return mDistance;
|
||||
}
|
||||
}
|
||||
|
||||
public Matrix4 MProjection
|
||||
{
|
||||
get
|
||||
{
|
||||
return mProjection;
|
||||
}
|
||||
}
|
||||
|
||||
public float Distance
|
||||
{
|
||||
get
|
||||
{
|
||||
return fDistance;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
fDistance = value;
|
||||
glPrepare();
|
||||
}
|
||||
}
|
||||
/*
|
||||
public Vector3 Direction {
|
||||
get { return this.vDirection; }
|
||||
set { this.vDirection = value; glPrepare(); }
|
||||
}
|
||||
*/
|
||||
public Vector3 Position {
|
||||
get { return this.vPosition; }
|
||||
set { this.vPosition = value; glPrepare(); }
|
||||
Matrix4 mTranslation = Matrix4.CreateTranslation( -this.vPosition );
|
||||
Vector3 x,y,z;
|
||||
|
||||
z = -vView;
|
||||
x = Vector3.Cross( vTop, z ).Normalized();
|
||||
y = Vector3.Cross( z, x );
|
||||
|
||||
Matrix4 mRotation = new Matrix4(
|
||||
new Vector4(x,0),
|
||||
new Vector4(y,0),
|
||||
new Vector4(z,0),
|
||||
new Vector4(0,0,0,1)
|
||||
);
|
||||
mRotation.Transpose();
|
||||
|
||||
_mCamera = mTranslation * mRotation;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -24,6 +24,8 @@ namespace org.nhengine.graphics
|
|||
nbo; // Normals Buffer
|
||||
|
||||
Vector3 position;
|
||||
|
||||
Matrix4 matrix;
|
||||
|
||||
protected bool colorEnabled,
|
||||
normalsEnabled,
|
||||
|
@ -42,6 +44,9 @@ namespace org.nhengine.graphics
|
|||
normalsEnabled = false;
|
||||
uvEnabled = false;
|
||||
|
||||
this.position = new Vector3();
|
||||
this.matrix = Matrix4.Identity;
|
||||
|
||||
prepareGL();
|
||||
}
|
||||
|
||||
|
@ -160,14 +165,6 @@ namespace org.nhengine.graphics
|
|||
setNormal((3 * n), normal);
|
||||
setNormal((3 * n) + 1, normal);
|
||||
setNormal((3 * n) + 2, normal);
|
||||
|
||||
Console.WriteLine("setTriangle(): ");
|
||||
Console.WriteLine("a: {0}", a);
|
||||
Console.WriteLine("b: {0}", b);
|
||||
Console.WriteLine("c: {0}", c);
|
||||
Console.WriteLine("n: {0}", normal);
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected void setColor(Vector4 c)
|
||||
|
@ -188,12 +185,12 @@ namespace org.nhengine.graphics
|
|||
|
||||
public void paint()
|
||||
{
|
||||
paint(GlobalDefaults.instance().ActiveCamera);
|
||||
paint(GlobalDefaults.instance().ActiveScene);
|
||||
}
|
||||
|
||||
public void paint(GLCamera camera)
|
||||
public void paint(GLSceneOrientation scene)
|
||||
{
|
||||
shaderProgram.use(camera);
|
||||
shaderProgram.use(Matrix,scene);
|
||||
|
||||
GL.BindVertexArray(this.vao);
|
||||
|
||||
|
@ -201,7 +198,10 @@ namespace org.nhengine.graphics
|
|||
|
||||
GL.BindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
protected void buildMatrix(){
|
||||
this.matrix = Matrix4.CreateTranslation( this.position );
|
||||
}
|
||||
|
||||
|
||||
public ShaderProgram ShaderProgram
|
||||
|
@ -212,7 +212,12 @@ namespace org.nhengine.graphics
|
|||
|
||||
public Vector3 Position{
|
||||
get { return this.position; }
|
||||
set { this.position = value; }
|
||||
set { this.position = value; buildMatrix(); }
|
||||
}
|
||||
|
||||
public Matrix4 Matrix {
|
||||
get { return this.matrix; }
|
||||
set { this.matrix = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,32 @@
|
|||
using System;
|
||||
namespace org.nhengine
|
||||
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
|
||||
namespace org.nhengine.graphics
|
||||
{
|
||||
public class GLSceneOrientation
|
||||
{
|
||||
|
||||
|
||||
protected Matrix4
|
||||
_mCamera,
|
||||
_mProjection;
|
||||
|
||||
public GLSceneOrientation()
|
||||
{
|
||||
_mCamera = Matrix4.Identity;
|
||||
_mProjection = Matrix4.CreatePerspectiveFieldOfView(
|
||||
MathHelper.PiOver4,
|
||||
1.0f,
|
||||
50.0f,
|
||||
100000.0f
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Matrix4 mCamera(){
|
||||
return this._mCamera;
|
||||
}
|
||||
public Matrix4 mProjection(){
|
||||
return this._mProjection;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace org.nhengine.graphics
|
|||
private ShaderProgram
|
||||
defShaderProgram;
|
||||
|
||||
private GLCamera activeCamera;
|
||||
private GLSceneOrientation activeScene;
|
||||
|
||||
private GlobalDefaults(){
|
||||
|
||||
|
@ -27,8 +27,7 @@ namespace org.nhengine.graphics
|
|||
|
||||
defShaderProgram = new ShaderProgram(defVertexShader, defFragmentShader);
|
||||
|
||||
|
||||
activeCamera = new GLCamera();
|
||||
activeScene = new GLSceneOrientation();
|
||||
}
|
||||
|
||||
public ShaderProgram DefaultShaderProgram {
|
||||
|
@ -45,9 +44,9 @@ namespace org.nhengine.graphics
|
|||
get { return defFragmentShader; }
|
||||
}
|
||||
|
||||
public GLCamera ActiveCamera {
|
||||
get { return this.activeCamera; }
|
||||
set { this.activeCamera = value; }
|
||||
public GLSceneOrientation ActiveScene {
|
||||
get { return this.activeScene; }
|
||||
set { this.activeScene = value; }
|
||||
}
|
||||
|
||||
|
||||
|
@ -58,13 +57,12 @@ in vec4 iv_color;
|
|||
in vec3 iv_normal;
|
||||
in vec2 iv_uv;
|
||||
|
||||
uniform mat4 mTranslation;
|
||||
uniform mat4 mRotation;
|
||||
uniform mat4 mWorld;
|
||||
uniform mat4 mDistance;
|
||||
uniform mat4 mObject;
|
||||
uniform mat4 mCamera;
|
||||
uniform mat4 mObjectCamera;
|
||||
uniform mat4 mObjectCameraIT;
|
||||
uniform mat4 mProjection;
|
||||
uniform mat4 mComplete;
|
||||
uniform mat4 mScene;
|
||||
|
||||
out vec4 color;
|
||||
out vec3 norm;
|
||||
|
@ -72,18 +70,17 @@ out vec2 uv;
|
|||
|
||||
void main()
|
||||
{
|
||||
gl_Position = mComplete * vec4( iv_position, 1 );
|
||||
gl_Position = mScene * vec4( iv_position, 1 );
|
||||
|
||||
norm = normalize( ( mRotation * vec4( iv_normal, 1 ) ).xyz );
|
||||
norm = normalize( ( mObjectCameraIT * vec4( iv_normal, 1 ) ).xyz );
|
||||
vec3 camray = normalize( -gl_Position.xyz );
|
||||
|
||||
float cosTheta = dot( norm , camray );
|
||||
|
||||
float visibility = 1.0;
|
||||
if (cosTheta < 0){
|
||||
visibility = 1.0;
|
||||
}
|
||||
color = vec4( iv_color.xyz * (0.5 + (0.5 * cosTheta * cosTheta)), 0.5f );
|
||||
color = vec4( iv_color.xyz * (0.5 + (0.5 * cosTheta * cosTheta)), visibility );
|
||||
}
|
||||
|
||||
";
|
||||
|
@ -99,7 +96,6 @@ out vec4 _color;
|
|||
void main()
|
||||
{
|
||||
_color = color;
|
||||
gl_FragDepth = gl_FragCoord.z;
|
||||
}
|
||||
|
||||
";
|
||||
|
|
|
@ -19,23 +19,20 @@ using OpenTK;
|
|||
* To render the scene another coordinate space is used, which is called "CameraSpace"
|
||||
* The transformation matrix used to translate from WorldSpace to CameraSpace is called "mCamera"
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* CameraSpace = (mObject * mCamera) * ObjectSpace
|
||||
*
|
||||
* As convention the following data will be given to shaders:
|
||||
*
|
||||
* Uniform:
|
||||
*
|
||||
* mTranslation World Coordinate Translation
|
||||
* mRotation Rotation to camera view direction
|
||||
* mWorld Transformation Matrix to world coordinate system
|
||||
* mObject Transformation Matrix ObjectSpace -> WorldSpace
|
||||
* mCamera Transformation Matrix WorldSpace -> CameraSpace
|
||||
* mProjection Projection Matrix for 2D Rendering
|
||||
*
|
||||
* mDistance Camera Distance translation (Z)
|
||||
* mCamera Transformation to camera space
|
||||
* mProjection Projection Matrix
|
||||
* mObjectCamera mObject * mCamera
|
||||
*
|
||||
* mObjectCameraIT (mObjectCamera^-1)^T
|
||||
*
|
||||
* mComplete Result of all Matrices
|
||||
*
|
||||
* Stream Data:
|
||||
*
|
||||
|
@ -58,13 +55,12 @@ namespace org.nhengine.graphics
|
|||
|
||||
int id;
|
||||
|
||||
int nm_Translation,
|
||||
nm_Rotation,
|
||||
nm_World,
|
||||
nm_Distance,
|
||||
int nm_Object,
|
||||
nm_Camera,
|
||||
nm_Projection,
|
||||
nm_Complete;
|
||||
nm_ObjectCamera,
|
||||
nm_ObjectCameraIT,
|
||||
nm_Scene;
|
||||
|
||||
public ShaderProgram(Shader vertexShader, Shader fragmentShader)
|
||||
{
|
||||
|
@ -105,13 +101,14 @@ namespace org.nhengine.graphics
|
|||
GL.LinkProgram(this.id);
|
||||
Console.WriteLine("Shader Program Result: {0}", GL.GetProgramInfoLog(this.id));
|
||||
|
||||
nm_Translation = GL.GetUniformLocation(this.ID, "mTranslation");
|
||||
nm_Rotation = GL.GetUniformLocation(this.ID, "mRotation");
|
||||
nm_World = GL.GetUniformLocation(this.ID, "mWorld");
|
||||
nm_Distance = GL.GetUniformLocation(this.ID, "mDistance");
|
||||
nm_Object = GL.GetUniformLocation(this.ID, "mObject");
|
||||
nm_Camera = GL.GetUniformLocation(this.ID, "mCamera");
|
||||
nm_ObjectCamera = GL.GetUniformLocation(this.ID, "mObjectCamera");
|
||||
nm_ObjectCameraIT
|
||||
= GL.GetUniformLocation(this.ID, "mObjectCameraIT");
|
||||
|
||||
nm_Projection = GL.GetUniformLocation(this.ID, "mProjection");
|
||||
nm_Complete = GL.GetUniformLocation(this.ID, "mComplete");
|
||||
nm_Scene = GL.GetUniformLocation(this.ID, "mScene");
|
||||
}
|
||||
|
||||
public int ID
|
||||
|
@ -119,38 +116,13 @@ namespace org.nhengine.graphics
|
|||
get { return this.id; }
|
||||
}
|
||||
|
||||
public int nmTranslation
|
||||
public int nmObject
|
||||
{
|
||||
get
|
||||
{
|
||||
return nm_Translation;
|
||||
return nm_Object;
|
||||
}
|
||||
}
|
||||
|
||||
public int nmRotation
|
||||
{
|
||||
get
|
||||
{
|
||||
return nm_Rotation;
|
||||
}
|
||||
}
|
||||
|
||||
public int nmWorld
|
||||
{
|
||||
get
|
||||
{
|
||||
return nm_World;
|
||||
}
|
||||
}
|
||||
|
||||
public int nmDistance
|
||||
{
|
||||
get
|
||||
{
|
||||
return nm_Distance;
|
||||
}
|
||||
}
|
||||
|
||||
public int nmCamera
|
||||
{
|
||||
get
|
||||
|
@ -159,6 +131,21 @@ namespace org.nhengine.graphics
|
|||
}
|
||||
}
|
||||
|
||||
public int nmObjectCamera
|
||||
{
|
||||
get
|
||||
{
|
||||
return nm_ObjectCamera;
|
||||
}
|
||||
}
|
||||
|
||||
public int nmObjectCameraIT
|
||||
{
|
||||
get
|
||||
{
|
||||
return nm_ObjectCameraIT; }
|
||||
}
|
||||
|
||||
public int nmProjection
|
||||
{
|
||||
get
|
||||
|
@ -167,39 +154,45 @@ namespace org.nhengine.graphics
|
|||
}
|
||||
}
|
||||
|
||||
public int nmComplete
|
||||
public int nmScene
|
||||
{
|
||||
get { return this.nm_Complete; }
|
||||
get
|
||||
{
|
||||
return nm_Scene;
|
||||
}
|
||||
}
|
||||
|
||||
public void use(GLCamera camera){
|
||||
|
||||
public void use(GLSceneOrientation scene){
|
||||
use(Matrix4.Identity, scene);
|
||||
}
|
||||
|
||||
public void use(Matrix4 _mObject,GLSceneOrientation scene){
|
||||
GL.UseProgram(this.id);
|
||||
|
||||
Matrix4
|
||||
mTranslate,
|
||||
mRotation,
|
||||
mWorld,
|
||||
mDistance,
|
||||
Matrix4 mObject,
|
||||
mCamera,
|
||||
mObjectCamera,
|
||||
mObjectCameraIT,
|
||||
mProjection,
|
||||
mComplete;
|
||||
mScene;
|
||||
|
||||
mTranslate = camera.MTranslation;
|
||||
mRotation = camera.MRotation;
|
||||
mWorld = mTranslate * mRotation;
|
||||
mDistance = camera.MDistance;
|
||||
mCamera = mWorld * mDistance;
|
||||
mProjection = camera.MProjection;
|
||||
mComplete = mCamera * mProjection;
|
||||
|
||||
GL.UniformMatrix4(nm_Translation, false, ref mTranslate);
|
||||
GL.UniformMatrix4(nm_Rotation, false, ref mRotation);
|
||||
GL.UniformMatrix4(nm_World, false, ref mWorld);
|
||||
GL.UniformMatrix4(nm_Distance, false, ref mDistance);
|
||||
GL.UniformMatrix4(nm_Camera, false, ref mCamera);
|
||||
GL.UniformMatrix4(nm_Projection, false, ref mProjection);
|
||||
GL.UniformMatrix4(nm_Complete, false, ref mComplete);
|
||||
mObject = _mObject;
|
||||
mCamera = scene.mCamera();
|
||||
mObjectCamera = mObject * mCamera;
|
||||
|
||||
mObjectCameraIT = mObjectCamera.Inverted();
|
||||
mObjectCameraIT.Transpose();
|
||||
|
||||
mProjection = scene.mProjection();
|
||||
mScene = mObjectCamera * mProjection;
|
||||
|
||||
GL.UniformMatrix4(nm_Object, false, ref mObject);
|
||||
GL.UniformMatrix4(nm_Camera, false, ref mCamera);
|
||||
GL.UniformMatrix4(nm_ObjectCamera, false, ref mObjectCamera);
|
||||
GL.UniformMatrix4(nm_Projection, false, ref mProjection);
|
||||
GL.UniformMatrix4(nm_ObjectCameraIT,false, ref mObjectCameraIT);
|
||||
GL.UniformMatrix4(nm_Scene, false, ref mScene);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,14 +16,14 @@ namespace org.nhengine.graphics.primitives
|
|||
dy = (Vector3.UnitY * width / 2);
|
||||
dz = (Vector3.UnitZ * width / 2);
|
||||
|
||||
a = position - dx - dy - dz;
|
||||
b = position + dx - dy - dz;
|
||||
c = position + dx + dy - dz;
|
||||
d = position - dx + dy - dz;
|
||||
e = position - dx - dy + dz;
|
||||
f = position + dx - dy + dz;
|
||||
g = position + dx + dy + dz;
|
||||
h = position - dx + dy + dz;
|
||||
a = - dx - dy - dz;
|
||||
b = dx - dy - dz;
|
||||
c = dx + dy - dz;
|
||||
d = - dx + dy - dz;
|
||||
e = - dx - dy + dz;
|
||||
f = dx - dy + dz;
|
||||
g = dx + dy + dz;
|
||||
h = - dx + dy + dz;
|
||||
|
||||
normalsEnabled = true;
|
||||
|
||||
|
@ -42,6 +42,8 @@ namespace org.nhengine.graphics.primitives
|
|||
|
||||
setColor(new Vector4(0.8f, 0.3f, 0.5f, 1.0f));
|
||||
|
||||
Position = position;
|
||||
|
||||
updateGL();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue