Jump to content

Texture scaling/rotation


Rick
 Share

Recommended Posts

I do this for uv scaling (move the mouse to set the uv scaling on the oil drum):

#include "engine.h"
#include "mathlib.h"

#include <string>
using namespace std;

void ScaleMeshUV(TEntity mesh, TVec2 uvscale)
{
for (int iSurf = 1; iSurf <= CountSurfaces(mesh); iSurf++)
{
	TSurface surface = GetSurface(mesh, iSurf);
	for (int iVert = 0; iVert < CountVertices(surface); iVert++)
	{
		for (int texcset = 0; texcset < 2; texcset++)
		{
			TVec2 uv = GetVertexTexCoords(surface, iVert, texcset) * uvscale;
			SetVertexTexCoords(surface, iVert, uv, texcset);
		}
	}
}
UpdateMesh(mesh);
}


int main(void)
{
// Init
Initialize();
Graphics(640,480);
TEntity world = CreateWorld();
TEntity buffer = CreateBuffer(640,480,BUFFER_COLOR0|BUFFER_DEPTH|BUFFER_NORMAL|BUFFER_COLOR2);

// Cam
TEntity camera = CreateCamera();
MoveEntity(camera, Vec3(0,0.5,-1.5) );

// Light
TEntity light  = CreateSpotLight(15,0);
MoveEntity   (light,  Vec3(-1,5,-4) );
RotateEntity (light,  Vec3(45,0,0), 0);
SetShadowmapSize(light,512);
AmbientLight(Vec3(.05));

// Ground
TEntity plane  = CreateCube(0);
ScaleEntity  (plane,  Vec3(100,1,100) );
MoveEntity   (plane,  Vec3(0,-2.5,0) );

// Model
TEntity m1 = LoadMesh("abstract::oildrum.gmf");
// ScaleMeshUV(m1, Vec2(3,2)); // some test scaling

// Main Loop
while(!KeyHit(KEY_ESCAPE) && !AppTerminate())
{
	TVec2 scale = Vec2((1.0f*Max(MouseX(),1)/GraphicsWidth())*10,(1.0f*Max(MouseY(),1)/GraphicsHeight())*10);
	ScaleMeshUV(m1, scale);

	UpdateWorld();

	SetBuffer(buffer);
	RenderWorld();
	SetBuffer(BackBuffer());
	RenderLights(buffer);

	ScaleMeshUV(m1, Vec2(1,1)/scale); // undo above scaling

	Flip(1);
}

// Terminate
return Terminate();
}

Link to comment
Share on other sites

And I just wrote this for rotation (press the up/down keys to rotate the texture on the oil drum):

#include "engine.h"
#include "mathlib.h"

#include <string>
using namespace std;

void RotateMeshUV(TEntity mesh, TVec2 rotationCenter, float angle)
{
angle = deg2rad(angle);
for (int iSurf = 1; iSurf <= CountSurfaces(mesh); iSurf++)
{
	TSurface surface = GetSurface(mesh, iSurf);
	for (int iVert = 0; iVert < CountVertices(surface); iVert++)
	{
		for (int texcset = 0; texcset < 2; texcset++)
		{
			TVec2 uv = GetVertexTexCoords(surface, iVert, texcset);
			uv -= rotationCenter;
			TVec2 rf = Vec2(cos(angle), sin(angle)); // roationFactor
			uv = Vec2(uv.X*rf.X - uv.Y*rf.Y, uv.X*rf.Y + uv.Y*rf.X);
			uv += rotationCenter;

			SetVertexTexCoords(surface, iVert, uv, texcset);
		}
	}
}
UpdateMesh(mesh);
}


int main(void)
{
// Init
Initialize();
Graphics(640,480);
TEntity world = CreateWorld();
TEntity buffer = CreateBuffer(640,480,BUFFER_COLOR0|BUFFER_DEPTH|BUFFER_NORMAL|BUFFER_COLOR2);

// Cam
TEntity camera = CreateCamera();
MoveEntity(camera, Vec3(0,0.5,-1.5) );

// Light
TEntity light  = CreateSpotLight(15,0);
MoveEntity   (light,  Vec3(-1,5,-4) );
RotateEntity (light,  Vec3(45,0,0), 0);
SetShadowmapSize(light,512);
AmbientLight(Vec3(.05));

// Ground
TEntity plane  = CreateCube(0);
ScaleEntity  (plane,  Vec3(100,1,100) );
MoveEntity   (plane,  Vec3(0,-2.5,0) );

// Model
TEntity m1 = LoadMesh("abstract::oildrum.gmf");
float angle = 0.0f;

// Main Loop
while(!KeyHit(KEY_ESCAPE) && !AppTerminate())
{
	if(KeyDown(KEY_UP) || KeyDown(KEY_DOWN))
	{
		float fac = KeyDown(KEY_UP) - KeyDown(KEY_DOWN);
		RotateMeshUV(m1, Vec2(0,0), fac);
		angle += fac;
	}

	UpdateWorld();

	SetBuffer(buffer);
	RenderWorld();
	SetBuffer(BackBuffer());
	RenderLights(buffer);

	DrawText(0,0,"angle: %f", angle);


	Flip(1);
}

// Terminate
return Terminate();
}

Link to comment
Share on other sites

Might actually be useful to define a center for scaling as well. Here some updated versions:

void ScaleMeshUV(TEntity mesh, TVec2 uvscale, TVec2 center = Vec2(0,0))
{
   for (int iSurf = 1; iSurf <= CountSurfaces(mesh); iSurf++)
   {
       TSurface surface = GetSurface(mesh, iSurf);
       for (int iVert = 0; iVert < CountVertices(surface); iVert++)
       {
           for (int texcset = 0; texcset < 2; texcset++)
           {
               TVec2 uv = (GetVertexTexCoords(surface, iVert, texcset) - center) * uvscale + center;
               SetVertexTexCoords(surface, iVert, uv, texcset);
           }
       }
   }
   UpdateMesh(mesh);
}


void RotateMeshUV(TEntity mesh, float angle, TVec2 center = Vec2(0,0))
{
   angle = deg2rad(angle);
   for (int iSurf = 1; iSurf <= CountSurfaces(mesh); iSurf++)
   {
       TSurface surface = GetSurface(mesh, iSurf);
       for (int iVert = 0; iVert < CountVertices(surface); iVert++)
       {
           for (int texcset = 0; texcset < 2; texcset++)
           {
               TVec2 uv = GetVertexTexCoords(surface, iVert, texcset);
               uv -= center;
               TVec2 rf = Vec2(cos(angle), sin(angle)); // roationFactor
               uv = Vec2(uv.X*rf.X - uv.Y*rf.Y, uv.X*rf.Y + uv.Y*rf.X);
               uv += center;

               SetVertexTexCoords(surface, iVert, uv, texcset);
           }
       }
   }
   UpdateMesh(mesh);
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...