Jump to content

Rotating Image around center with Context:SetRotation


GorzenDev
 Share

Recommended Posts

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)

 

 

Link to comment
Share on other sites

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

RotatingImage.jpg.106856f78e809dce73cf79748173a8ea.jpg

Video of this and other similar shaders:

 

  • Like 3

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

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...