Computing transformed coords

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
grump
Party member
Posts: 535
Joined: Sat Jul 22, 2017 7:43 pm

Computing transformed coords

Post by grump » Mon Aug 14, 2017 2:36 pm

Is there a way to have coords transformed by the currently active graphics transformation state and get the transformed result back? I need the axis aligned bounding boxes of a hierarchy of translated, scaled, rotated Drawables and I wonder if I have to implement the transformation stack myself. I also want to compute localized mouse coords for these objects, and possibly perform clipping/culling using the AABBs.

MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Computing transformed coords

Post by MasterLee » Mon Aug 14, 2017 5:52 pm

There is an way using an Canvas with rgba32f format but that is somewhat tricky and slow. But beneath that i did not find any method directly implemented in API.

grump
Party member
Posts: 535
Joined: Sat Jul 22, 2017 7:43 pm

Re: Computing transformed coords

Post by grump » Tue Aug 22, 2017 10:08 pm

I'm not sure how Canvas would help with this problem. Care to elaborate?

If there is no easy way of getting the current transform, is there at least a way to set the transformation matrix directly in one function call, so I can compute it myself and share the transform between the graphics stack and my display objects?

MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Computing transformed coords

Post by MasterLee » Wed Aug 23, 2017 10:52 am

You can't set the matrix but you can use your own matrix:

Code: Select all

local effect=love.graphics.newShader([[
    attribute vec3 vPos;
    attribute vec2 vTex;
    attribute vec3 vNormal;
    extern mat4 matrix;
    varying vec4 v_pos;
    varying vec3 v_tex;
    varying vec3 v_normal;
 
    vec4 position(mat4 transform_projection, vec4 vertex_position )
    {
      v_pos = vec4(vPos.xyz,1.0)*matrix;
      v_tex = vec3(vTex,1.0)/v_pos.w;
      v_pos/=v_pos.w;
      v_normal = vNormal;
      return v_pos;      
    }
  ]],[[
    varying vec4 v_pos;
    varying vec3 v_tex;
    varying vec3 v_normal;
    
    vec4 effect(vec4 color,Image texture,vec2 texture_coords,vec2 pixel_coords)
    {
      return Texel(texture, vec2(v_tex.x/v_tex.z,v_tex.y/v_tex.z));      
    }
  ]])
On the other hand you could write transform_projection matrix to target canvas and then read from there.
Or you could write all points that need to write into canvas apply transform_projection on that values trough shader and then read from target canvas.

grump
Party member
Posts: 535
Joined: Sat Jul 22, 2017 7:43 pm

Re: Computing transformed coords

Post by grump » Wed Aug 23, 2017 5:58 pm

Okay, I need some more clarifications please. This is my display object drawing itself:

Code: Select all

love.graphics.push()
love.graphics.translate(self._x + self._anchorX, self._y + self._anchorY)
love.graphics.scale(self._scaleX, self._scaleY)
love.graphics.rotate(self._rotation)
love.graphics.translate(-self._anchorX, -self._anchorY)
self:draw()
love.graphics.pop()
This happens in a nested fashion (display objects can have child display objects). I'm not using Canvas or Shader right now.

Now if I want to work with coordinates relative to this object, for instance when I want to know on which part of the rotated object the user has clicked, I have to get the transformation matrix from somewhere. I'm currently calculating it a second time by myself, because I can't query it from love.

Are you saying I shouldn't use the love.graphics transform functions, and rather calculate it myself and draw everything with a shader that takes my transformation matrix instead?

Reading stuff back from a Canvas seems pretty hacky and potentially slow.

MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Computing transformed coords

Post by MasterLee » Wed Aug 23, 2017 6:54 pm

Yes that was what i saying. Simple use your own matrix. Oh by the way be warned when setting up your matrix you must deal with the coordinate space transformation yourself.

User avatar
slime
Solid Snayke
Posts: 2811
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Computing transformed coords

Post by slime » Wed Aug 23, 2017 10:46 pm

You can shadow the transformation state functions to be able to convert to and from the coordinate space, camera libraries such as hump.camera and gamera do something similar.

In love 0.11 there will be built-in functions to transform a vector into the current transform's coordinate space, and the inverse.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 14 guests