Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
setting the "origin" of a texture textures; so u can rotate it around center of tex
Topic Started: Oct 1 2008, 09:00 AM (260 Views)
Bazza
Member Avatar
Forum God
[ *  *  *  *  *  * ]
is there a way to set the origin of the tex so when u rotate it, it stays in the same position kind of like how rotating a model works but 2d
My instinct is to hide in this barrel, like the wily fish.
Offline Profile Quote Post Goto Top
 
MysteriXYZ
Member Avatar
Master Matrix Masher
[ *  *  *  *  *  * ]
bazza games
Oct 1 2008, 09:00 AM
is there a way to set the origin of the tex so when u rotate it, it stays in the same position kind of like how rotating a model works but 2d
What are "texture textures" :huh: ?
You mean "billboard texture", don't you ;) ?

If I understood correctly, you want to rotate the texture of a 2D primitive, right? Yes, you could use a billboard, but I am going to show you how you can transform a texture on a polygon instead.
Rotating a texture around its centre is still reasonably simple (you could use simple trigonometry), but the script I will give you will allow you to transform the texture in any way you want.

And yes, this will once again involve matrices and vectors, so you better hold on to your hat :D !

First, create the polygon like this:
Code:
 
vertex_count=4;
BeginPolygon(vertex_count);
AddVertex(-.5,0,-.5,0,0);
AddVertex(-.5,0,.5,0,0);
AddVertex(.5,0,.5,0,0);
AddVertex(.5,0,-.5,0,0);
CreatePolygon();
Step();

Creating it like this should make the polygon easy to use.

Then you call the following script in its Step Event (you don't even need to call Step()!):
Code:
 
//RotateTexture(Angle)
//Sets the texture UV-coordinates of a polygon (which is assumed to lie in
//the XZ-plane) to make its texture rotate around its centre by the given
//Angle.

//Since UV-coordinate (0,0) is the top left corner of the texture map and
//UV-coordinate (1,1) is the bottom right corner, the texture is viewed
//correctly (not mirrored) in "bottom-up" view if the U-axis corresponds to
//the X-axis and the V-axis to the Y-axis.
//From its starting position in the XY-plane, the texture can be imagined
//as being transformed so a particular part of it is visible on the polygon.
//But a texture map cannot be transformed directly. Instead, the UV's of the
//polygon vertices will need to be transformed inversely within the "texture
//space".
//So the first thing to do is to define the transformation matrix that would
//normally be used for the texture map. Then all that remains to be done is
//to transform each of the polygon vertices with the inverse of this matrix.

//In this case, the texture will be rotated around its centre, which will
//be positioned at the origin of the polygon.
var OffsMatr,RotMatr,ScaleMatr,Transform;

//set the pivot of the texture at its centre
OffsMatr=CreateTranslationMatrix(-1,-.5,-.5,0);
//rotate around that pivot
RotMatr=CreateRotationMatrix(-1,0,-argument0,0);
Transform=TransformMatrix(-1,OffsMatr,RotMatr);
//scale up the texture so it doesn't repeat across the polygon
ScaleMatr=CreateScalingMatrix(-1,1.2,1.2,1);
TransformMatrix(Transform,Transform,ScaleMatr);
//align the texture to the polygon; to avoid texture distortion, rotate the
//texture (which lies in the XY-plane) so it's completely parallel to the
//polygon (in the XZ-plane)
CreateRotationMatrix(RotMatr,90,0,0);
TransformMatrix(Transform,Transform,RotMatr);
//optionally, the pivot could be offset from the polygon origin in x and z
/*CreateTranslationMatrix(OffsMatr,OffsX,0,OffsZ);
TransformMatrix(Transform,Transform,OffsMatr);*/
//finally, invert the resulting matrix so it can be used to correctly set
//the UV-coordinates of the polygon vertices
InvertMatrix(Transform,Transform);

//Now the vertices of the polygon can be transformed with the inverse
//transformation matrix that would be applied to the texture map.
//To this end, create a position vector out of each vertex and transform
//it with that inverse matrix. The resulting vector will contain the
//needed values for the U- and V-coordinates as its 1st and 2nd components
//respectively.
//NOTE: if the polygon is not completely planar (not all vertices lie in the
//same plane), or the vectors are not coplanar, then the texture will be
//distorted.
var OffsVect,i,X,Y,Z,U,V;

OffsVect=CreateVector(-1,0,0,0);
for (i=0; i < vertex_count; i+=1)
{
X=GetVertex(i,0);
Y=GetVertex(i,1);
Z=GetVertex(i,2);
CreateVector(OffsVect,X,Y,Z);
TransformVector(OffsVect,OffsVect,Transform);
//since the transformed vector lies in the XY-plane, retrieve the 1st and
//2nd components for the U- and V-coordinates respectively
U=GetVector(OffsVect,1);
V=GetVector(OffsVect,2);
SetVertex(i,X,Y,Z,U,V);
}

ReleaseMatrix(OffsMatr);
ReleaseMatrix(RotMatr);
ReleaseMatrix(ScaleMatr);
ReleaseMatrix(Transform);
ReleaseVector(OffsVect);
U3D is like candy; after extensive consumption, it's Best to brush.
Offline Profile Quote Post Goto Top
 
ashrat3000
Member Avatar
u3d raytracer
[ *  *  *  *  *  * ]
However, if you mean the textures drawn with DrawTex(), its impossible.

Well, maybe not. You may be able to use DrawTexEx() and the clipping things (args 2,3,4,5 [I think]) and then rotate. It may just work.

그대를 사랑해


Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
ashrat3000
Oct 1 2008, 10:02 PM
However, if you mean the textures drawn with DrawTex(), its impossible.

Well, maybe not. You may be able to use DrawTexEx() and the clipping things (args 2,3,4,5 [I think]) and then rotate. It may just work.

Yeah, what you do is you take the length and width, calculate a hypotenuse from that, divide it by two, and use the lengthdir_x and y to move the image to it's center. That way, you'll always have it centered. That's what I do all the time.
Blog|EHS
Offline Profile Quote Post Goto Top
 
« Previous Topic · Questions about Ultimate 3D · Next Topic »
Add Reply