del_4901
2004-07-01, 20:15:39
Also ich wollte jetzt meine Lichtberechnung im EyeSpace vollziehen. Nur leider krieg ichs nicht gebacken meine Position der Lichtquelle in EyeSpace Koordinaten umzurechnen. ich bin jetzt bei der 3en (wohlgemerkt unoptimierten) variante und es sieht verdammt .... bescheiden aus. Das dumme ist, das mein Licht die Position radikal verändert, wenn ich mich drehe un nah am zu beleuchtenden obj. bin. So langsam krieg ich Knotem im Kopf, ich weiß ja nichtmal mehr wie rum das Cross eigendl. sinn macht ... (rechte oder linke Hand)... vor lauter "umcrossen" ... deswegen auch die vielen - die eigendl. da nicht hingehören, aber so siehts noch am besten aus ... Ich weiß Try&Error ist misst, aber wird gern angewand ;)
version TransformationsMatrix
Aufruf:
WorldToEye = WorldToEye.viewMatrix(g_Camera.m_vPosition,g_Camera.m_vPosition - g_Camera.m_vView ,g_Camera.m_vStrafe);//Vector3f(0,0,1));
Light = WorldToEye*(Vector3f(lx,ly,lz))
cgGLSetParameter3f(shader.parameterv[shader.parameterv.getident(20)]->param, Light.x ,Light.y , Light.z);
Matrix
Matrix::viewMatrix(Vector3f eye,Vector3f gaze,Vector3f up)
{
Matrix ret = identityMatrix();
Vector3f w = (gaze.Normalize());
Vector3f u = up;
u.Normalize();
Vector3f v = w*u;
v.Normalize();
/* Vector3f w = -(gaze.Normalize());
Vector3f u = (up*w);
u.Normalize();
Vector3f v = w*u;
v.Normalize();*/
ret.x[0][0] =u.x;
ret.x[0][1] =u.y;
ret.x[0][2] =u.z;
ret.x[1][0] =v.x;
ret.x[1][1] =v.y;
ret.x[1][2] =v.z;
ret.x[2][0] =w.x;
ret.x[2][1] =w.y;
ret.x[2][2] =w.z;
Matrix move = identityMatrix();
move.x[0][3] = -(eye.x);
move.x[1][3] = -(eye.y);
move.x[2][3] = -(eye.z);
ret = ret *move;
return ret;
}
Matrix
Matrix::identityMatrix()
{
Matrix ret;
ret.x[0][0] = 1.0f;
ret.x[0][1] = 0.0f;
ret.x[0][2] = 0.0f;
ret.x[0][3] = 0.0f;
ret.x[1][0] = 0.0f;
ret.x[1][1] = 1.0f;
ret.x[1][2] = 0.0f;
ret.x[1][3] = 0.0f;
ret.x[2][0] = 0.0f;
ret.x[2][1] = 0.0f;
ret.x[2][2] = 1.0f;
ret.x[2][3] = 0.0f;
ret.x[3][0] = 0.0f;
ret.x[3][1] = 0.0f;
ret.x[3][2] = 0.0f;
ret.x[3][3] = 1.0f;
return ret;
}
Matrix
Matrix::operator* (/*const Matrix& left_op,*/ const Matrix& right_op)
{
Matrix ret;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
{
float subt = 0.0;
for(int k = 0; k<4; k++)
subt += x[i][k] * right_op.x[k][j];
ret.x[i][j] = subt;
}
return ret;
}
Vector3f
Matrix::operator *(/*const Matrix& left_op,*/ /*const*/ Vector3f& right_op)
{
Vector3f ret;
float temp;
ret.x = (right_op.x * x[0][0])+(right_op.y * x[0][1])+(right_op.z * x[0][2])+ x[0][3];
ret.y = (right_op.x * x[1][0])+(right_op.y * x[1][1])+(right_op.z * x[1][2])+ x[1][3];
ret.z = (right_op.x * x[2][0])+(right_op.y * x[2][1])+(right_op.z * x[2][2])+ x[2][3];
temp = (right_op.x * x[3][0])+(right_op.y * x[3][1])+(right_op.z * x[3][2])+ x[3][3];
ret = ret/temp;
return ret;
}
Version Optimiert
Aufruf:
Light = TranformWorldEye(Vector3f(lx,ly,lz),g_Camera.m_vPosition, -g_Camera.m_vView - g_Camera.m_vPosition , -g_Camera.m_vStrafe);
//zum anzeigen meines Vektorraumes ONB
Vector3f EyeX;
Vector3f EyeY;
Vector3f EyeZ;
Vector3f TranformWorldEye(Vector3f LightPos, Vector3f EyePos, Vector3f ViewPos, Vector3f EyeR)
{
EyeZ = g_Camera.m_vView - g_Camera.m_vPosition ;///g_Camera.m_vView - g_Camera.m_vPosition; //EyePos - ViewPos;
EyeZ.Normalize();
//EyeX = Vector3f(0,0,1)*EyeZ;
EyeX = -g_Camera.m_vStrafe;
EyeX.Normalize();
Vector3f LightDir = EyePos - LightPos;
//LightDir.Normalize();
float help = 1/(LightDir%LightDir);
EyeY = EyeX*EyeZ; //verkehrt??
//EyeZ = EyeX*EyeY;
//EyeY = EyeY;
EyeY.Normalize();
//EyeZ = EyeZ;
//EyeZ.Normalize();
Vector3f blah(( EyeX%LightDir ) * help, ( EyeY%LightDir ) * help, (EyeZ%LightDir ) * help);
blah.Normalize();
blah = blah * LightDir.Magnitude(LightDir);
return blah;
}
Vector3f class
float x;
float y;
float z;
Vector3f(float ex, float ey, float ez)
{
x = ex;
y = ey;
z = ez;
}
inline Vector3f operator+(Vector3f vVector)
{
// Return the added vectors result.
return Vector3f(vVector.x + x, vVector.y + y, vVector.z + z);
}
inline Vector3f operator-(Vector3f vVector)
{
// Return the subtracted vectors result
return Vector3f(x - vVector.x, y - vVector.y, z - vVector.z);
}
inline Vector3f operator-()
{
// Return the subtracted vectors result
return Vector3f(-x,-y,-z);
}
//invert S-Multiplication
inline Vector3f operator/(float scalar)
{
return Vector3f(x/scalar, y/scalar, z/scalar);
}
//S-Multiplication
inline Vector3f operator*(float scalar)
{
return Vector3f(x*scalar, y*scalar, z*scalar);
}
//crossProduct
inline Vector3f operator*(Vector3f v2)
{
return Vector3f((y*v2.z - z*v2.y), (z*v2.x - x*v2.z), (x*v2.y - y*v2.x));
}
//dotProduct
inline float operator%(Vector3f v2)
{
return float(x*v2.x + y*v2.y + z*v2.z);
}
Vertexshader:
void main(float3 position : POSITION,
float3 normal : NORMAL,
float3 tangent : TEXCOORD1,
float3 binormal : TEXCOORD2,
float2 texcoord0 : TEXCOORD0,
out float4 oposition : POSITION,
out float2 ntexcoord : TEXCOORD0,
out float3 lightdir : TEXCOORD1,
uniform float3 lightvector,
uniform float3 eyevector,
uniform float4x4 ModelViewProj,
uniform float4x4 ModelViewInvers,
uniform float4x4 ModelViewMatrix)
{
oposition = mul(ModelViewProj, float4(position.xyz, 1));
// normalize(lightvector);
float3 eposition = mul(ModelViewMatrix, float4(position.xyz, 1));
normalize(eposition);
// lightvector.xyz =mul(ModelViewInvers,float4(lightvector.xyz,1));
//normalize(lightdir);
lightdir = lightvector.xyz - eposition.xyz;
// normalize(lightdir);
// if(lightdir.x < 0) lightdir.x = -lightdir.x;
// if(lightdir.y < 0) lightdir.y = -lightdir.y;
// if(lightdir.z < 0) lightdir.z = -lightdir.z;
//lightdir = -lightdir;
//normalize(lightdir);
//float3 bino = cross(tangent,normal);
//normalize(bino);
// float3x3 rotation = float3x3(tangent,
// binormal,
// normal);
// lightdir = mul(rotation, lightdir);
ntexcoord = texcoord0;
}
version TransformationsMatrix
Aufruf:
WorldToEye = WorldToEye.viewMatrix(g_Camera.m_vPosition,g_Camera.m_vPosition - g_Camera.m_vView ,g_Camera.m_vStrafe);//Vector3f(0,0,1));
Light = WorldToEye*(Vector3f(lx,ly,lz))
cgGLSetParameter3f(shader.parameterv[shader.parameterv.getident(20)]->param, Light.x ,Light.y , Light.z);
Matrix
Matrix::viewMatrix(Vector3f eye,Vector3f gaze,Vector3f up)
{
Matrix ret = identityMatrix();
Vector3f w = (gaze.Normalize());
Vector3f u = up;
u.Normalize();
Vector3f v = w*u;
v.Normalize();
/* Vector3f w = -(gaze.Normalize());
Vector3f u = (up*w);
u.Normalize();
Vector3f v = w*u;
v.Normalize();*/
ret.x[0][0] =u.x;
ret.x[0][1] =u.y;
ret.x[0][2] =u.z;
ret.x[1][0] =v.x;
ret.x[1][1] =v.y;
ret.x[1][2] =v.z;
ret.x[2][0] =w.x;
ret.x[2][1] =w.y;
ret.x[2][2] =w.z;
Matrix move = identityMatrix();
move.x[0][3] = -(eye.x);
move.x[1][3] = -(eye.y);
move.x[2][3] = -(eye.z);
ret = ret *move;
return ret;
}
Matrix
Matrix::identityMatrix()
{
Matrix ret;
ret.x[0][0] = 1.0f;
ret.x[0][1] = 0.0f;
ret.x[0][2] = 0.0f;
ret.x[0][3] = 0.0f;
ret.x[1][0] = 0.0f;
ret.x[1][1] = 1.0f;
ret.x[1][2] = 0.0f;
ret.x[1][3] = 0.0f;
ret.x[2][0] = 0.0f;
ret.x[2][1] = 0.0f;
ret.x[2][2] = 1.0f;
ret.x[2][3] = 0.0f;
ret.x[3][0] = 0.0f;
ret.x[3][1] = 0.0f;
ret.x[3][2] = 0.0f;
ret.x[3][3] = 1.0f;
return ret;
}
Matrix
Matrix::operator* (/*const Matrix& left_op,*/ const Matrix& right_op)
{
Matrix ret;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
{
float subt = 0.0;
for(int k = 0; k<4; k++)
subt += x[i][k] * right_op.x[k][j];
ret.x[i][j] = subt;
}
return ret;
}
Vector3f
Matrix::operator *(/*const Matrix& left_op,*/ /*const*/ Vector3f& right_op)
{
Vector3f ret;
float temp;
ret.x = (right_op.x * x[0][0])+(right_op.y * x[0][1])+(right_op.z * x[0][2])+ x[0][3];
ret.y = (right_op.x * x[1][0])+(right_op.y * x[1][1])+(right_op.z * x[1][2])+ x[1][3];
ret.z = (right_op.x * x[2][0])+(right_op.y * x[2][1])+(right_op.z * x[2][2])+ x[2][3];
temp = (right_op.x * x[3][0])+(right_op.y * x[3][1])+(right_op.z * x[3][2])+ x[3][3];
ret = ret/temp;
return ret;
}
Version Optimiert
Aufruf:
Light = TranformWorldEye(Vector3f(lx,ly,lz),g_Camera.m_vPosition, -g_Camera.m_vView - g_Camera.m_vPosition , -g_Camera.m_vStrafe);
//zum anzeigen meines Vektorraumes ONB
Vector3f EyeX;
Vector3f EyeY;
Vector3f EyeZ;
Vector3f TranformWorldEye(Vector3f LightPos, Vector3f EyePos, Vector3f ViewPos, Vector3f EyeR)
{
EyeZ = g_Camera.m_vView - g_Camera.m_vPosition ;///g_Camera.m_vView - g_Camera.m_vPosition; //EyePos - ViewPos;
EyeZ.Normalize();
//EyeX = Vector3f(0,0,1)*EyeZ;
EyeX = -g_Camera.m_vStrafe;
EyeX.Normalize();
Vector3f LightDir = EyePos - LightPos;
//LightDir.Normalize();
float help = 1/(LightDir%LightDir);
EyeY = EyeX*EyeZ; //verkehrt??
//EyeZ = EyeX*EyeY;
//EyeY = EyeY;
EyeY.Normalize();
//EyeZ = EyeZ;
//EyeZ.Normalize();
Vector3f blah(( EyeX%LightDir ) * help, ( EyeY%LightDir ) * help, (EyeZ%LightDir ) * help);
blah.Normalize();
blah = blah * LightDir.Magnitude(LightDir);
return blah;
}
Vector3f class
float x;
float y;
float z;
Vector3f(float ex, float ey, float ez)
{
x = ex;
y = ey;
z = ez;
}
inline Vector3f operator+(Vector3f vVector)
{
// Return the added vectors result.
return Vector3f(vVector.x + x, vVector.y + y, vVector.z + z);
}
inline Vector3f operator-(Vector3f vVector)
{
// Return the subtracted vectors result
return Vector3f(x - vVector.x, y - vVector.y, z - vVector.z);
}
inline Vector3f operator-()
{
// Return the subtracted vectors result
return Vector3f(-x,-y,-z);
}
//invert S-Multiplication
inline Vector3f operator/(float scalar)
{
return Vector3f(x/scalar, y/scalar, z/scalar);
}
//S-Multiplication
inline Vector3f operator*(float scalar)
{
return Vector3f(x*scalar, y*scalar, z*scalar);
}
//crossProduct
inline Vector3f operator*(Vector3f v2)
{
return Vector3f((y*v2.z - z*v2.y), (z*v2.x - x*v2.z), (x*v2.y - y*v2.x));
}
//dotProduct
inline float operator%(Vector3f v2)
{
return float(x*v2.x + y*v2.y + z*v2.z);
}
Vertexshader:
void main(float3 position : POSITION,
float3 normal : NORMAL,
float3 tangent : TEXCOORD1,
float3 binormal : TEXCOORD2,
float2 texcoord0 : TEXCOORD0,
out float4 oposition : POSITION,
out float2 ntexcoord : TEXCOORD0,
out float3 lightdir : TEXCOORD1,
uniform float3 lightvector,
uniform float3 eyevector,
uniform float4x4 ModelViewProj,
uniform float4x4 ModelViewInvers,
uniform float4x4 ModelViewMatrix)
{
oposition = mul(ModelViewProj, float4(position.xyz, 1));
// normalize(lightvector);
float3 eposition = mul(ModelViewMatrix, float4(position.xyz, 1));
normalize(eposition);
// lightvector.xyz =mul(ModelViewInvers,float4(lightvector.xyz,1));
//normalize(lightdir);
lightdir = lightvector.xyz - eposition.xyz;
// normalize(lightdir);
// if(lightdir.x < 0) lightdir.x = -lightdir.x;
// if(lightdir.y < 0) lightdir.y = -lightdir.y;
// if(lightdir.z < 0) lightdir.z = -lightdir.z;
//lightdir = -lightdir;
//normalize(lightdir);
//float3 bino = cross(tangent,normal);
//normalize(bino);
// float3x3 rotation = float3x3(tangent,
// binormal,
// normal);
// lightdir = mul(rotation, lightdir);
ntexcoord = texcoord0;
}