3d vectors math help

Started by RemiD, October 11, 2019, 17:52:23

Previous topic - Next topic

RemiD

hi,

let me explain briefly the situation, and what i want to achieve :
i have a surface made of vertices and triangles (let say a cube of 1w1h1d...)
each triangle has a 3d normal (=3d vector with a size of 1.0 divided in the 3 axises, example +0.2x,-0.5y,+0.3z)
i have uvmapped this surface, so that the texel size is 0.1u (for a lightmap texture)
so for the 1w1h1d cube, i have a 10x10texels area on each "face" (square made of 2 triangles pointing in a direction)
i have coded a procedure to calculate/store for each texel :
its 2d texture position : TX,TY
its 3d world position : WX,WY,WZ
its 3d normal vector (direction) : NX,NY,NZ

until now i planned to position each shape without rotating it, so that i don't have to recalculate each texel 3d position and 3d normal.

however, as you may know me, i like some challenge :P

so to calculate a new 3d position (after rotating the mesh), of a 3d point on a mesh (before rotating the mesh), i think that i can use tformpoint() and it should work (not tested yet)

but to calculate a new 3d normal vector (after rotating the mesh), of a 3d normal vector (before rotating the mesh), i have no idea, is there a magic formula to do that with 3d vector maths ?
i know the 3d normal vector before rotating
i know the mesh orientation before rotating (that i can probably translate to a 3d vector using tformvector())
i know the mesh orientation after rotating (that i can probably translate to a 3d vector using tformvector())

any idea ? thanks

Matty

Hi RemiD.  You want to use the tformnormal command which is very similar tformpoint.

Plug in the original normals of a non rotated entity and then convert between its local space and the world space after you have performed the rotation.

Does that make sense to you?


RemiD

@Matty>>mmm, let me rephrase it and you tell me if it is what you mean :

the mesh is not rotated (0pitch, 0yaw, 0roll)
i know the texel normal (same as the triangle normal it is in)
i rotate the mesh
i use tformnormal(texelnx,texelny,texelnz,mesh,0)
newnx = tformedx() : newny = tformedy() : newnz = tformedz()
and this should convert the not rotated texel normal to the rotated texel normal ?


if yes, this was easy, i will try asap. thanks

GaborD

In case the other thing doesn't work out, a generic rotation matrix for 3D is something like this:

mat3 rot(vec3 ang) {
   mat3 x = mat3(1.0 ,0.0, 0.0, 0.0, cos(ang.x), -sin(ang.x), 0.0, sin(ang.x), cos(ang.x));
   mat3 y = mat3(cos(ang.y), 0.0, sin(ang.y), 0.0, 1.0, 0.0, -sin(ang.y), 0.0, cos(ang.y));
   mat3 z = mat3(cos(ang.z), -sin(ang.z), 0.0, sin(ang.z), cos(ang.z), 0.0, 0.0, 0.0, 1.0);
   return x*y*z;
}

Obviously, that's the GLSL version, but it should be easy to convert.


RemiD


Steve Elliott

lol far too much maths for a Friday.   :o
Win11 64Gb 12th Gen Intel i9 12900K 5.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 4.4Ghz Nvidia RTX 2050 8Gb
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.6GHz Nvidia GeForce GTX 1050 2Gb
Linux Mint 8Gb Celeron 2.6Ghz UHD Graphics600
macOS 64Gb M4 Max 16C GPU 40C
Spectrum Next 2Mb

Matty


RemiD


RemiD

#8
ok, i have tested to use tformpoint() to convert the texel world 3d position of a not rotated mesh (0pitch,0yaw,0roll) to the texel world 3d position of a rotated mesh

and to use tformnormal() to convert the texel 3d normal of a not rotated mesh (0pitch,0yaw,0roll) to the texel world 3d normal of a rotated mesh

but it does not seem to work...


;copy the shape
OShapeLSR = CopyMesh(ShapeLSR)
;copy the ls texture
OShapeLST = CopyTexture(ShapeLST) : TextureCoords(OShapeLST,0) : EntityTexture(OShapeLSR,OShapeLST,0,0)
;change its orientation
RotateEntity(OShapeLSR,Rand(-180,+180),Rand(-180,+180),Rand(-180,+180),True)
;change its position
PositionEntity(OShapeLSR,1.5,1.5,1.5,True)
;calculate for each texel of the ls texture
For I% = 1 To TexelsCount Step 1
OTexelsCount = OTexelsCount + 1 : OI% = OTexelsCount
;same texture 2d position
OTexelTX(OI) = TexelTX(I) : OTexelTY(OI) = TexelTY(I)
;new world 3d position wx,wy,wz (using tformpoint())
TFormPoint(TexelWX(I),TexelWY(I),TexelWZ(I),ShapeLSR,OShapeLSR)
OTexelWX(OI) = TFormedX() : OTexelWY(OI) = TFormedY() : OTexelWZ(OI) = TFormedZ()
;new world 3d normal nx,ny,nz (using tformnormal())
TFormNormal(TexelNX(I),TexelNY(I),TexelNZ(I),ShapeLSR,OShapeLSR)
OTexelNX(OI) = TFormedX() : OTexelNY(OI) = TFormedY() : OTexelNZ(OI) = TFormedZ()
Next

i already know that the texels world 3d positions and the texels world 3d normals of the not rotated mesh, are correct...

and i made sure to use rotateentity() and positionentity() so that the rotated mesh has a transform different than 0x,0y,0z 0pitch,0yaw,0roll (with rotatemesh() and positionmesh(), only the vertices are rotated / positionned)

any suggestion ?
thanks

STEVIE G

If the ShapeLSR entity is scaled using scaleentity then the results from tform commands scale by the same.  If you change the ShapeLSR to 0 (for global) when this might work.

S.


RemiD

@StevieG >>the shape is not scaled

i will try to change it to 0 as you say

RemiD

ok it works ! 8)


the correct way to write it is :

;new world 3d position wx,wy,wz (using tformpoint())
TFormPoint(TexelWX(I),TexelWY(I),TexelWZ(I),OShapeLSR,0)
OTexelWX(OI) = TFormedX() : OTexelWY(OI) = TFormedY() : OTexelWZ(OI) = TFormedZ()
;new world 3d normal nx,ny,nz (using tformnormal())
TFormNormal(TexelNX(I),TexelNY(I),TexelNZ(I),OShapeLSR,0)
OTexelNX(OI) = TFormedX() : OTexelNY(OI) = TFormedY() : OTexelNZ(OI) = TFormedZ()

from positionned, rotated, transform of OShape (the new shape), it "projects" the point and the normal which were not positionned (0x0y0z), not rotated (0pitch,0yaw,0roll)

thanks guys !

GaborD

Very cool, great that you got it to work. Very useful commands, make it so much easier than having to do it manually.

Kryzon

@GaborD in that GLSL rot() function you posted, why is the input angle a vec3 value? Shouldn't it be theta (a scalar), so that all cos/sin use the same angle?

GaborD

It's the three angles for the x, y and z rotations.