Question about shader and camera

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.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Question about shader and camera

Post by pgimeno »

I have to apologize. I screwed up big time when I told you about what to do with the camera, because I didn't really think it through.

To make it up to you, here's the full corrected camera.lua. I've renamed some functions so their names express more accurately what they actually do. That's because e.g. 'scale' suggest a scaling relative to the previous scale, but that's not true. 'setScale' suggests it's setting an absolute scale value, as it's actually doing.

Code: Select all

-- Author : Lionel Leeser
-- Date : 15.02.19
-- Goal : Simplified camera for demonstrating.

-- Changes by Pedro Gimeno donated to the public domain.

local Camera = {}

    Camera.x = 0
    Camera.y = 0
    Camera.scaleX = 1
    Camera.scaleY = 1
    Camera.rotation = 0
    Camera.transform = nil

    function Camera:init(x, y)
        self.transform = love.math.newTransform(x, y)
    end -- function

    function Camera:set()
        love.graphics.push()
        self.transform:setTransformation(love.graphics.getWidth()/2, love.graphics.getHeight()/2, self.rotation, self.scaleX, self.scaleY, self.x, self.y)
        love.graphics.applyTransform(self.transform)
    end

    function Camera:unset()
        love.graphics.pop()
    end

    function Camera:setPosition(x, y)
        self.x = x
        self.y = y
    end

    function Camera:move(dx, dy)
        self.x = self.x + dx
        self.y = self.y + dy
    end

    function Camera:setRotation(dr)
        self.rotation = dr
    end

    function Camera:setScale(sx, sy)
        self.scaleX = sx or 1
        self.scaleY = sy or sx or 1
    end

    function Camera:getViewMatrix()
        return self.transform:getMatrix()
    end -- function

return Camera
With this new code, you pass the camera the coordinates where you want to place centre of the screen, rather than the corner. I believe that's much better than having to compensate manually for the screen size as you were doing. If you want to use a viewport, that will need some extra code to set and use the viewport in the camera object.

I don't believe you need a Camera:move() for relative movement. Doing so has the risk of a desynch between camera and player. Instead, you use Camera:setPosition() to e.g. follow the player.

[Edit: Forgot to say: in this version, Camera:getViewMatrix() is only valid right after Camera:set(), not before. I guess it's obvious from the code but I'm making it explicit just in case.]

The previous shader was OK. It only needs one modification (apart from the extern):

Code: Select all

                vec2 norm_pos = (viewMatrix * vec4(light.position, 0.0, 1.0)).xy / screen;
I think you can take over from here :)
User avatar
ChicoGameDev
Citizen
Posts: 70
Joined: Thu Feb 14, 2019 6:02 pm
Location: Switzerland
Contact:

Re: Question about shader and camera

Post by ChicoGameDev »

Hello,

I was surely on the wrong lane hahaha.

I was missing and misunderstanding the importance of Transform:setTransformation. Thanks for your contribution to this project! I think that my light library will include this camera with maybe some more features like screenShake and so on. I'll be happy to give you credit and you're an awesome teacher.

I will meditate on your code to understand how it work, especially :

Code: Select all

vec2 norm_pos = (viewMatrix * vec4(light.position, 0.0, 1.0)).xy / screen;
I've totally miss the possibility to just take the xy of a vec4 to divide by a vec2... That's a real good trick !!!

Thanks a lot and I hope I will be able to help others with what I've learn these last days, cheers!


Regards
Lionel Leeser

Luven : https://github.com/chicogamedev/Luven

--

Always keep Game Dev as a passion.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 55 guests