Rick Posted January 17, 2010 Share Posted January 17, 2010 Does anyone have any code to scale/rotate a texture painted on a model? Say just a cube? Quote Link to comment Share on other sites More sharing options...
Masterxilo Posted January 17, 2010 Share Posted January 17, 2010 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(); } Quote Hurricane-Eye Entertainment - Site, blog. Link to comment Share on other sites More sharing options...
Masterxilo Posted January 17, 2010 Share Posted January 17, 2010 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(); } Quote Hurricane-Eye Entertainment - Site, blog. Link to comment Share on other sites More sharing options...
Rick Posted January 17, 2010 Author Share Posted January 17, 2010 Very nice. I'll put this in my Thingoids. Thanks! Quote Link to comment Share on other sites More sharing options...
Masterxilo Posted January 22, 2010 Share Posted January 22, 2010 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); } Quote Hurricane-Eye Entertainment - Site, blog. Link to comment Share on other sites More sharing options...
Recommended Posts
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.