Author Topic: some matrix tests  (Read 6273 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
some matrix tests
« on: May 22, 2013 »
hey again guys.

in order too get my other project working im going too need matrix's everywhere i need too get viewdir matrix, camera, model, projection etc.

so i figuared id get stuck back in. i dug out this peice of code i made ages ago and got a feel for it again. trasfered it all into my software engine and it seems too be working good. it gives me a camera and model matrix,

my question is how would i go about building a projection matrix similar to opengls one, for use in a software engine too go along side these?.
and how do you get the view( or eye space matrix ) is this simply the camera matrix inversed? 

in the little example use arrow keys and wsda keys.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: some matrix tests
« Reply #1 on: May 22, 2013 »
ive sussed the projection matrix  :)

Code: [Select]

glViewport 0, 0, 640, 480
glMatrixMode GL_PROJECTION
glLoadIdentity

Dim Shared As Double ProjMatrix( 3,3 )
Dim As Double aspect = 640 / 480
Dim As Double Gtop = tan((45 * 0.5f)*3.1415 / 180.0) * 0.1'tan( fov to deg! )*nearclip
Dim As Double Gbottom = -Gtop
Dim As Double Gright = Gtop * aspect
Dim As Double Gleft = -Gright

projMatrix(0, 0) = (2.0f * 0.1) / (Gright - Gleft)
projMatrix(1, 1) = (2.0f * 0.1) / (Gtop - Gbottom)
projMatrix(2, 2) = -(1000.0 + 0.1) / (1000.0 - 0.1)'far clip + nearclip / farclip - nearclip
projMatrix(2, 3) = -1.0f
projMatrix(3, 2) = (-2.0f * 1000.0 * 0.1) / (1000.0 - 0.1)' 2*farclip * nearclip/farclip-nearclip
projMatrix(3, 3) = 0.0f
glpushmatrix
glmultmatrixd( @ProjMatrix(0,0) )

glMatrixMode GL_MODELVIEW
glLoadIdentity

works as a perfect replacement too ogl's. now i have a camera, object and projection matrix i think that only leaves the view matrix and i should be able too do almost any converting in my softrender
« Last Edit: May 22, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: some matrix tests
« Reply #2 on: May 22, 2013 »
Just two remarks:

double precision for matrices and vectors are pretty wasteful. gpus work in single precision anyway, so doubles need to be converted when passed to opengl.

I've never really needed homogeneous matrices in a software renderer.
It just gets the area between near- and far-clipping plane into a 0..1 range; that's useful to feed a 16bit zbuffer but more or less useless with 32bit precision.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: some matrix tests
« Reply #3 on: May 22, 2013 »
cheers mate,

Quote
double precision for matrices and vectors are pretty wasteful. gpus work in single precision anyway, so doubles need to be converted when passed to opengl.

yeah im in a bad habbit of declaring all floats as doubles. i really need too stop doing that.
i guess its probably a little ott doing it this way for a soft render it just helps me better follow the glsl stuff floating around i.e
Code: [Select]
gl_Position = Projection * View * Model * vec4(vertex, 1.0);
 
    mat3 tbni = inverse(mat3(_tangent, _bitangent, _normal));
 
    vec4 v        = Model * vec4(vertex, 1.0);                  // vertex in model space
    light_vector  = tbni * (inverse(Model) * vec4(light_position - v.xyz, 0)).xyz;  // light vector in tangent space
 
    v = View * Model * vec4(vertex, 1.0);                       // vertex in eye space
    vec4 light_vector_eye  = normalize((View * vec4(light_position, 1.0)) - v); // light vector in eye space
    vec4 viewer_vector_eye = normalize(-v);                     // view vector in eye space
 
    halfway_vector = tbni * (inverse(View * Model) * vec4((light_vector_eye.xyz + viewer_vector_eye.xyz), 0.0)).xyz;
                                            // halfway vector in tangent space
    normal_vector = inverse(transpose(tbni)) * normal;
 
    texture_coord = texcoord.xy;


for me too be able too follow something like this its much easyer too have all the matrixes4x4 too hand. ill probably realize at some point 3x3 is better and that im doing it way over the top :)

i thought the projection matrix was needed to convert too view matrix, but now im messing im not so sure, looks like its just used purely for frustrum culling and ndc calculations so yep not much use too me  :)...
« Last Edit: May 22, 2013 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: some matrix tests
« Reply #4 on: May 29, 2013 »
been messing around with this today trying too emulate a first person shooter style of camera where the camera can be rotated in any direction but only around its own origin not the scene then when you move forward the translation moves in the same direction as the camera faces.

ive got the camera rotating around its own origin no matter where in the scene its moved too but i get kind of stuck when trying too move relative too the direction the camera faces. hope that makes sense :).

so if i spin the camera round 180 using the forward key then moves the camera backward etc. is there a simple way around this with matrixes or maybe a look at sort of method can be used.
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: some matrix tests
« Reply #5 on: May 29, 2013 »
A 3x3 rotation matirx contains three vectors which point to the right, up and front of the new coordinate system.
So when moving around you can simply translate along these vectors instead of (1,0,0), (0,1,0) and (0,0,1).
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: some matrix tests
« Reply #6 on: May 29, 2013 »
cheers hellfire,

i think if i read correctly i have it working off the out vector.

im now doing translation like this.

Code: [Select]
sub multranslmatrix4x4( byval m1 as entity ptr , byval m2 as entity ptr , destination as entity ptr )
   
    dim as double Tx , Ty , Tz
    dim as double cz , cx , cy
    Dim As Single Bx, By, Bz
   
    cx = m2->matrix->m12 'xpos of trans matrix
    cy = m2->matrix->m13 'ypos of trans matrix
    cz = m2->matrix->m14 'zpos of trans matrix
   
    Bx = m1->Matrix->m2*Cx 'up vectorx*trans xpos
    By = m1->Matrix->m6*Cy 'up vectorY*trans ypos
    Bz = m1->Matrix->m10*Cz' up vectorz*trans zpos

    copymatrix4x4( destination , m2 ) 'this just copys rotation matrix too destination addr

    Destination->matrix->m12 = Bx' add out translation too rotation matrix
    Destination->matrix->m13 = By' add out translation too rotation matrix
    Destination->matrix->m14 = Bz' add out translation too rotation matrix

end sub

and this largely works correct but now the camera rotates around the centre of the scene instead of its own origin.and because of this it acts quite strange through rotation.
i had a read about encoded camera origin vector but it was very sketchy material.
« Last Edit: May 29, 2013 by ninogenio »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: some matrix tests
« Reply #7 on: May 30, 2013 »
First of all your matrix code is very hard to read because the elements are numbered sequentially.
Without looking deeper into the code, one cannot know if you started counting at 0 or 1 and if your layout is in column- or row-major order.

If you want to rotate around a given center, you need to:
- translate so that center becomes (0,0,0)
- rotate
- translate (0,0,0) back to center

Each transformation is a matrix; to get all transformations into a single matrix you can simply multiply the three matrices.
This can be optimized quite a bit because most elements of the two translation matrices are 0 or 1.
But for a camera-matrix which you only build once per frame, I really wouldn't care.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: some matrix tests
« Reply #8 on: May 30, 2013 »
cheers hellfire,

been playing around some more tonight. i think i must have a rotation out somewhere because everything i try roughly produces the same effect i can either make the camera rotate round its own axis and move but it pays no attension too its facing direction or it moves in its facing dir but spins round 0,0,0 of the scene.

the matrixes are row major starting 0 and make up a 4x4 like so

m0.m1.m2.m3
m4.m5.m6.m7
m8.m9.m10.m11
m12.m13.m14.m15

with 12.13.14 being translation. i understand its hard too jump into this and find whats going on though. especially with the odd way ive put it together.

ill spend some time striping it back peice by peice too try and find out whats going on cheers.
« Last Edit: May 30, 2013 by ninogenio »
Challenge Trophies Won: