Kamerafunktion verbessert, div. Fehler korrigiert

HWO-base
Harald Christian Joachim Wolff 2017-04-28 11:51:02 +02:00
parent c6aafb8cc9
commit 29a1bdea77
6 changed files with 185 additions and 211 deletions

View File

@ -6,122 +6,84 @@ using OpenTK;
namespace org.nhengine.graphics namespace org.nhengine.graphics
{ {
public class GLCamera public class GLCamera : GLSceneOrientation
{ {
Vector3 vPosition; // Position der Kamera float fov;
Vector3 vDirection; // Blickrichtung der Kamera float width, height;
float fDistance; // Zusätzliche Distanz der Kamera entgegen der Blickrichtung Vector3 vPosition,
vView,
Matrix4 mTranslation; // Matrix für Translation vTop;
Matrix4 mRotation; // Matrix für Rotation
Matrix4 mDistance; // Matrix für Rückzug entgegen der Blickrichtung
Matrix4 mProjection; // Projektionsmatrix
public GLCamera() public GLCamera()
{ {
vPosition = new Vector3(0,0,0); fov = MathHelper.PiOver2;
vDirection = Vector3.UnitZ; width = 100;
fDistance = 0.0f; height = 100;
mProjection = Matrix4.CreatePerspectiveFieldOfView( vPosition = new Vector3(0,0,1000);
MathHelper.PiOver4, vView = new Vector3(0,0,-1);
1.0f, vTop = new Vector3(0,1,0);
50.0f,
100000.0f
);
mRotation = Matrix4.Identity; buildProjection();
buildMatrices();
glPrepare();
} }
public void setViewport(int width, int height) public void setViewport(int width, int height)
{ {
mProjection = Matrix4.CreatePerspectiveFieldOfView( this.width = (float)width;
MathHelper.PiOver4, this.height = (float)height;
width / (float)height,
buildProjection();
}
public void setFoV(float fov){
this.fov = fov;
buildProjection();
}
private void buildProjection(){
_mProjection = Matrix4.CreatePerspectiveFieldOfView(
fov,
width / height,
50.0f, 50.0f,
100000.0f 100000.0f
); );
} }
private void glPrepare(){ public Vector3 Position
this.mTranslation = Matrix4.CreateTranslation(vPosition * -1.0f); {
this.mDistance = mRotation * Matrix4.CreateTranslation(0, 0, -fDistance); get { return new Vector3(vPosition); }
set { this.vPosition = new Vector3(value); buildMatrices(); }
// Console.WriteLine("View Direction: {0}", vDirection); }
// Console.WriteLine("Rotation Matrix: {0}", mRotation); 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(){
} Matrix4 mTranslation = Matrix4.CreateTranslation( -this.vPosition );
Vector3 x,y,z;
public void use(ShaderProgram sp){
glUpdate(); z = -vView;
x = Vector3.Cross( vTop, z ).Normalized();
} y = Vector3.Cross( z, x );
Matrix4 mRotation = new Matrix4(
public Matrix4 MTranslation new Vector4(x,0),
{ new Vector4(y,0),
get new Vector4(z,0),
{ new Vector4(0,0,0,1)
return mTranslation; );
} mRotation.Transpose();
}
_mCamera = mTranslation * mRotation;
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(); }
} }

View File

@ -24,6 +24,8 @@ namespace org.nhengine.graphics
nbo; // Normals Buffer nbo; // Normals Buffer
Vector3 position; Vector3 position;
Matrix4 matrix;
protected bool colorEnabled, protected bool colorEnabled,
normalsEnabled, normalsEnabled,
@ -42,6 +44,9 @@ namespace org.nhengine.graphics
normalsEnabled = false; normalsEnabled = false;
uvEnabled = false; uvEnabled = false;
this.position = new Vector3();
this.matrix = Matrix4.Identity;
prepareGL(); prepareGL();
} }
@ -160,14 +165,6 @@ namespace org.nhengine.graphics
setNormal((3 * n), normal); setNormal((3 * n), normal);
setNormal((3 * n) + 1, normal); setNormal((3 * n) + 1, normal);
setNormal((3 * n) + 2, 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) protected void setColor(Vector4 c)
@ -188,12 +185,12 @@ namespace org.nhengine.graphics
public void paint() 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); GL.BindVertexArray(this.vao);
@ -201,7 +198,10 @@ namespace org.nhengine.graphics
GL.BindVertexArray(0); GL.BindVertexArray(0);
} }
protected void buildMatrix(){
this.matrix = Matrix4.CreateTranslation( this.position );
}
public ShaderProgram ShaderProgram public ShaderProgram ShaderProgram
@ -212,7 +212,12 @@ namespace org.nhengine.graphics
public Vector3 Position{ public Vector3 Position{
get { return this.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; }
} }
} }
} }

View File

@ -1,16 +1,32 @@
using System; using System;
namespace org.nhengine
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace org.nhengine.graphics
{ {
public class GLSceneOrientation public class GLSceneOrientation
{ {
protected Matrix4
_mCamera,
_mProjection;
public GLSceneOrientation() 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;
}
} }
} }

View File

@ -16,7 +16,7 @@ namespace org.nhengine.graphics
private ShaderProgram private ShaderProgram
defShaderProgram; defShaderProgram;
private GLCamera activeCamera; private GLSceneOrientation activeScene;
private GlobalDefaults(){ private GlobalDefaults(){
@ -27,8 +27,7 @@ namespace org.nhengine.graphics
defShaderProgram = new ShaderProgram(defVertexShader, defFragmentShader); defShaderProgram = new ShaderProgram(defVertexShader, defFragmentShader);
activeScene = new GLSceneOrientation();
activeCamera = new GLCamera();
} }
public ShaderProgram DefaultShaderProgram { public ShaderProgram DefaultShaderProgram {
@ -45,9 +44,9 @@ namespace org.nhengine.graphics
get { return defFragmentShader; } get { return defFragmentShader; }
} }
public GLCamera ActiveCamera { public GLSceneOrientation ActiveScene {
get { return this.activeCamera; } get { return this.activeScene; }
set { this.activeCamera = value; } set { this.activeScene = value; }
} }
@ -58,13 +57,12 @@ in vec4 iv_color;
in vec3 iv_normal; in vec3 iv_normal;
in vec2 iv_uv; in vec2 iv_uv;
uniform mat4 mTranslation; uniform mat4 mObject;
uniform mat4 mRotation;
uniform mat4 mWorld;
uniform mat4 mDistance;
uniform mat4 mCamera; uniform mat4 mCamera;
uniform mat4 mObjectCamera;
uniform mat4 mObjectCameraIT;
uniform mat4 mProjection; uniform mat4 mProjection;
uniform mat4 mComplete; uniform mat4 mScene;
out vec4 color; out vec4 color;
out vec3 norm; out vec3 norm;
@ -72,18 +70,17 @@ out vec2 uv;
void main() 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 ); vec3 camray = normalize( -gl_Position.xyz );
float cosTheta = dot( norm , camray ); float cosTheta = dot( norm , camray );
float visibility = 1.0; float visibility = 1.0;
if (cosTheta < 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() void main()
{ {
_color = color; _color = color;
gl_FragDepth = gl_FragCoord.z;
} }
"; ";

View File

@ -19,23 +19,20 @@ using OpenTK;
* To render the scene another coordinate space is used, which is called "CameraSpace" * 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" * 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: * As convention the following data will be given to shaders:
* *
* Uniform: * Uniform:
* *
* mTranslation World Coordinate Translation * mObject Transformation Matrix ObjectSpace -> WorldSpace
* mRotation Rotation to camera view direction * mCamera Transformation Matrix WorldSpace -> CameraSpace
* mWorld Transformation Matrix to world coordinate system * mProjection Projection Matrix for 2D Rendering
* *
* mDistance Camera Distance translation (Z) * mObjectCamera mObject * mCamera
* mCamera Transformation to camera space *
* mProjection Projection Matrix * mObjectCameraIT (mObjectCamera^-1)^T
* *
* mComplete Result of all Matrices
* *
* Stream Data: * Stream Data:
* *
@ -58,13 +55,12 @@ namespace org.nhengine.graphics
int id; int id;
int nm_Translation, int nm_Object,
nm_Rotation,
nm_World,
nm_Distance,
nm_Camera, nm_Camera,
nm_Projection, nm_Projection,
nm_Complete; nm_ObjectCamera,
nm_ObjectCameraIT,
nm_Scene;
public ShaderProgram(Shader vertexShader, Shader fragmentShader) public ShaderProgram(Shader vertexShader, Shader fragmentShader)
{ {
@ -105,13 +101,14 @@ namespace org.nhengine.graphics
GL.LinkProgram(this.id); GL.LinkProgram(this.id);
Console.WriteLine("Shader Program Result: {0}", GL.GetProgramInfoLog(this.id)); Console.WriteLine("Shader Program Result: {0}", GL.GetProgramInfoLog(this.id));
nm_Translation = GL.GetUniformLocation(this.ID, "mTranslation"); nm_Object = GL.GetUniformLocation(this.ID, "mObject");
nm_Rotation = GL.GetUniformLocation(this.ID, "mRotation");
nm_World = GL.GetUniformLocation(this.ID, "mWorld");
nm_Distance = GL.GetUniformLocation(this.ID, "mDistance");
nm_Camera = GL.GetUniformLocation(this.ID, "mCamera"); 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_Projection = GL.GetUniformLocation(this.ID, "mProjection");
nm_Complete = GL.GetUniformLocation(this.ID, "mComplete"); nm_Scene = GL.GetUniformLocation(this.ID, "mScene");
} }
public int ID public int ID
@ -119,38 +116,13 @@ namespace org.nhengine.graphics
get { return this.id; } get { return this.id; }
} }
public int nmTranslation public int nmObject
{ {
get 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 public int nmCamera
{ {
get 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 public int nmProjection
{ {
get 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); GL.UseProgram(this.id);
Matrix4 Matrix4 mObject,
mTranslate,
mRotation,
mWorld,
mDistance,
mCamera, mCamera,
mObjectCamera,
mObjectCameraIT,
mProjection, mProjection,
mComplete; mScene;
mTranslate = camera.MTranslation; mObject = _mObject;
mRotation = camera.MRotation; mCamera = scene.mCamera();
mWorld = mTranslate * mRotation; mObjectCamera = mObject * mCamera;
mDistance = camera.MDistance;
mCamera = mWorld * mDistance; mObjectCameraIT = mObjectCamera.Inverted();
mProjection = camera.MProjection; mObjectCameraIT.Transpose();
mComplete = mCamera * mProjection;
mProjection = scene.mProjection();
GL.UniformMatrix4(nm_Translation, false, ref mTranslate); mScene = mObjectCamera * mProjection;
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);
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);
} }
} }
} }

View File

@ -16,14 +16,14 @@ namespace org.nhengine.graphics.primitives
dy = (Vector3.UnitY * width / 2); dy = (Vector3.UnitY * width / 2);
dz = (Vector3.UnitZ * width / 2); dz = (Vector3.UnitZ * width / 2);
a = position - dx - dy - dz; a = - dx - dy - dz;
b = position + dx - dy - dz; b = dx - dy - dz;
c = position + dx + dy - dz; c = dx + dy - dz;
d = position - dx + dy - dz; d = - dx + dy - dz;
e = position - dx - dy + dz; e = - dx - dy + dz;
f = position + dx - dy + dz; f = dx - dy + dz;
g = position + dx + dy + dz; g = dx + dy + dz;
h = position - dx + dy + dz; h = - dx + dy + dz;
normalsEnabled = true; normalsEnabled = true;
@ -42,6 +42,8 @@ namespace org.nhengine.graphics.primitives
setColor(new Vector4(0.8f, 0.3f, 0.5f, 1.0f)); setColor(new Vector4(0.8f, 0.3f, 0.5f, 1.0f));
Position = position;
updateGL(); updateGL();
} }
} }