GorzenDev Posted July 3, 2018 Share Posted July 3, 2018 I'm trying to rotate a image around its center. And since there is no rotation parameter for DrawImage(), i thought i'd use Context:SetRotation() to achieve the result i need. But i think i must be doing something wrong or do i need some math to translate the context everytime i rotate so it stays centered.? Somehow a image always rotates around its topleft. While a rectangle with proper rotation and translation CAN rotate around its center. Can anybody help? I have tried some different approaches as you can see and added rectangles with color to visualize. --cp is the center of the screen (the topleft position of (center - halfimgsize)) --newrot is controlled by the keyboard --Red Rectangle context:SetRotation(newrot) gui:SetColor(1.0, 0.0, 0.0) gui:DrawImage(needle, cp.x + 128, cp.y, 256, 256) gui:DrawRect(cp.x + 128, cp.y, 256, 256, 1, 1) context:SetRotation(0.0) -- --Green Rectangle context:SetRotation(newrot) context:SetTranslation(cp.x, cp.y + 128) gui:SetColor(0.0, 1.0, 0.0) gui:DrawImage(needle, -128, -128, 256, 256) gui:DrawRect(-128, -128, 256, 256, 1, 1) context:SetRotation(0.0) context:SetTranslation(0, 0) -- --Blue Rectangle context:SetTranslation(cp.x, cp.y) context:SetRotation(newrot) gui:SetColor(0.0, 0.0, 1.0) gui:DrawImage(needle, -128, -128, 256, 256) gui:DrawRect(-128, -128, 256, 256, 1, 1) context:SetRotation(0.0) context:SetTranslation(0, 0) Quote Link to comment Share on other sites More sharing options...
macklebee Posted July 3, 2018 Share Posted July 3, 2018 Try using this shader to rotate your images: drawimage_rotating.shader Example script that shows usage: window = Window:Create("draw rotating image",0,0,600,400,Window.Titlebar+Window.Center) context = Context:Create(window) imageshader = Shader:Load("Shaders/Drawing/drawimage_rotating.shader") imageshader:SetVec2("pivotposition", Vec2(150.0, 37.5)) --center of image angle = 0 image = Texture:Load("Materials/Developer/leadwerks.tex") while window:KeyDown(Key.Escape)==false do if window:Closed() then break end context:SetColor(1,.3,0) context:Clear() angle = angle +1 defaultshader = context:GetShader() context:SetBlendMode(Blend.Alpha) --Draw Image context:SetColor(1, 1, 1) context:SetShader(imageshader) imageshader:SetFloat("angle", angle) context:DrawImage(image, 150, 150, 300, 75) context:SetShader(defaultshader) context:DrawText(string.format("FPS: %.2f",Time:UPS()), 0 ,10) context:SetBlendMode(Blend.Solid) context:Sync(true) end Video of this and other similar shaders: 3 Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
GorzenDev Posted July 4, 2018 Author Share Posted July 4, 2018 offcourse i should have thought about that ?♂️. i whas so focused on simply rotating a image that i havent thought of using a shader. thank you very much for your help, i whas about to give up and just use digits. Quote 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.