Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / [SOLVED] struggling with coordinate systems between AGK and shaders

Author
Message
Dale Schultz
3
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 12th Nov 2021 23:25
Ok I seem to be going round in circles searching the web and search the forum and AppGameKit docs for clues... but am getting nowhere fast.

My AppGameKit app is using:

so all my x & y values are 0 - 100 %

I have my blur shader working fine and it blurs a sprite in AppGameKit nicely. It applies the blur to the whole image so I have no issues with coordinates.

My next shader has a point of interest in a 'fixed' position in it. i.e. 50% across in the x and 15% up from the bottom. I have developed and tested the shader at shadertoy.com and then adapted it to run in AppGameKit where is compiles and runs, but I cant work out how to normalize the coordinates so that they run from 0.0 to 1.0 on both axes.

I can obtain the AppGameKit screen resolution with:
vec2 agk_resolution
but my sprite I am shading does not use the whole screen. (Currently it uses the full width at the top of the screen, but I imagine I may need to position a sprite elsewhere or not use the whole width)
vec2 agk_spritesize
gives me I don't know what as I cant work out what part of my image I am seeing in the result I get. (Some small area)


There is a list of parameters at https://www.appgamekit.com/documentation/guides/13_shaders.htm but I am confused by what coordinate system I am using and which one I need to use in my shader.

My sprite size in AppGameKit is x=100 and y = -1
What is the best way to get a shader xy of 0.5, 0.5 to be in the middle of my AppGameKit sprite when using SetDisplayAspect () ?

Side question: Is there a way to render a value as text in a shader so I can 'see' a value I get in the shader?

Thanks in advance!


The author of this post has marked a post as an answer.

Go to answer

PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 12th Nov 2021 23:36 Edited at: 12th Nov 2021 23:37
Quote: "What is the best way to get a shader xy of 0.5, 0.5 to be in the middle of my AppGameKit sprite when using SetDisplayAspect () ?"


divide by self, multiply by 1.0

Quote: "Side question: Is there a way to render a value as text in a shader so I can 'see' a value I get in the shader?"


no, dont think so, the shader manipulates the existing texture, I dont think it can generate text.
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 12th Nov 2021 23:43
Actually, GLSL has a "normalize" function, just call that on your inputs
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Dale Schultz
3
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 12th Nov 2021 23:44
sorry for not getting "divide by self".
I think my problem is I am trying to determine 'self'
(and multiply by 1.0 can only mean its a float result)
Dale Schultz
3
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 12th Nov 2021 23:47
the normalize functions takes a vector input, I am trying to find out what input I need.

which AKG shader variable are your suggesting I pass into it?
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 12th Nov 2021 23:54 Edited at: 12th Nov 2021 23:56
Sorry, not by self, by 100.... speed reply I got it wrong

Print( 56.0 / 100.0 * 1.0 ) << gives the normalised position for the shader, make sure you cast the values to floats first

Quote: "which AKG shader variable are your suggesting I pass into it?"


Got me there, I dont do much shader work, I edit, not create lol
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Dale Schultz
3
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 13th Nov 2021 00:04
56.0/100.0 * 1.0 = 0.56 I can just code 0.56 I am not asking how to calculate a percentage.

The issue is the divisor is unknown. (100 in your example)

Given a sprite which is 100% wide, within the shader, what divisor would provide a pixel at point 0.5 that corresponds to the middle. 100.0 is not the answer.


blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 13th Nov 2021 00:57
Almost ALL shader values are between 0 and 1
So .5 would give you the middle
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Nov 2021 01:10
56.0 was an example but your right I was dividing by the percentage resolution not the real resolution, try using "agk_resolution" as the divider in your shader

read this: https://www.appgamekit.com/documentation/guides/13_shaders.htm, its all there



Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Dale Schultz
3
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 13th Nov 2021 01:40
@PartTimeCoder You are quoting a link I provided in my OP plus suggesting I use something which again I explained would be incorrect in the OP.

@blink0k Of course 0.5 is half way, but you read to read the rest of the sentence.... the 0.5 was just to pose the question in the most simple terms possible.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Nov 2021 01:49 Edited at: 13th Nov 2021 01:49
Well you could have just googled it!!

1st link!
normalized = (x-min(x))/(max(x)-min(x))

https://stats.stackexchange.com/questions/70801/how-to-normalize-data-to-0-1-range


Note to self: Stop trying to help people!!
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 13th Nov 2021 02:00 Edited at: 13th Nov 2021 02:02
Ched80 posted a small shader suite a while back. there's a centre blur shader in there. it's not quite what you're asking for but skimming through those shader files might unlock some doors for you.

btw, if you haven't noticed it yet, there's a directory of what i'd consider "essential" AppGameKit stuff that is worth your time to review: https://forum.thegamecreators.com/thread/226258.

sorry i can't be more help in the shader department, otherwise
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 13th Nov 2021 02:13
50/100 = x / 1
To remove a value from one side multiply/divide both sides (depending on where your unknown value is)
50 * 1 / 100 = x * 1
50 / 100 = .5


Dale Schultz
3
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 13th Nov 2021 02:46
guys, I know how to do simple arithmetic. I know how to divide and make fractions. (Am I being treated like an imbecile because this is the newcomers section?)

The problem is the number to divide by is unknown. I am trying to find out where to get the divisor.

How many of you actually read every word of my OP? Here is the TL/DR of the post:
Quote: "What is the best way to get a shader xy of 0.5, 0.5 to be in the middle of my AppGameKit sprite when using SetDisplayAspect () ?"


@virtual nomad, thanks, I'll look over you post of links, but as I said I have spent hours googling and searching on this topic before I posted.


PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Nov 2021 03:00 Edited at: 13th Nov 2021 04:23
Quote: " (Am I being treated like an imbecile because this is the newcomers section?)"


No ~VN
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Dale Schultz
3
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 13th Nov 2021 03:10 Edited at: 13th Nov 2021 04:23
No ~VN
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Nov 2021 03:30 Edited at: 13th Nov 2021 04:25
NO! ~VN
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 13th Nov 2021 03:35 Edited at: 13th Nov 2021 04:25
SetDisplayAspect () has absolutely no bearing on the co-ordinate system used by shaders.

Please keep things civil

YES! ~VN

Attachments

Login to view attachments
Dale Schultz
3
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 13th Nov 2021 03:51 Edited at: 13th Nov 2021 04:24
This post has been marked by the post author as the answer.
Here is a working solution:



and a demonstration shader:



YES! ~VN
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 27th Nov 2021 19:51
Quote: "The problem is the number to divide by is unknown. I am trying to find out where to get the divisor."


agk_Resolution are preset values always available to you within GLSL Shaders (see: Working with Shaders from the AppGameKit Help for a full list of Preset Values populated by AGK)

Normalisation of a value can be obtained one of two ways... but a 0.0 to 1.0 is a simple case of Value / MaximumValue

CurrentValue / MaximumValue

i.e.

514 (Pixel Position) / 1080 (Screen Width) = 0.470

try using this:


Oh as a note I forget which does it., but OpenGL and Vulkan use slightly different Texture Spaces (one is -0.5 to +0.5 while the other is 0.0 to 1.0) and AppGameKit doesn't differentiate. So you might need a pre-processor condition with U -= 0.5; V -= 0.5; for converting to the correct texture space., else the values might be shifted when using converted Screen Space values.

Now as a keynote... "Resolution" DOES NOT refer to the Screen Resolution!
It refers to the Image Resolution., meaning it will be the same Resolution as the Output Render Target (which in the case of the Shader is the Texture Image Resolution)

This is specifically why we use Texture (Cartesian Space) instead of World (Euclidian Space) Coordinates., because the actual Resolution of what we're using is typically going to be an unknown; but we don't really NEED it... think about it., we NEED our Shader to work on Multiple Resolutions to support things like Mipmapping, Anisotropic Filtering, etc.
And actually we can do some quite clever things with manipulation of Mipmaps in regards to things like Blur, Reflection, etc.

You say you want exact positions of things in regards to the UV... alright., you know the percentages; which translate to U = 0.5 and V = 1.0 - 0.15 // 0.85
There's no additional step to take with Cartesian Space; as the image is 0.0 to 1.0 in terms of coordinates.

If we were using Pixel Space., well we'd NEED to know the Resolution.
As a 2048x2048 image... 50% is 1048 and 85% is 1741 pixels; where-as with a 1024x1024., these values become 512 and 870 respectively.

In Cartesian Coordinates... well regardless of the image size., we always want coordinates 0.5 and 0.85 respectively.
I'd argue learning this coordinate system and how to calculate it in AppGameKit is a good idea for things like Scalable UI.

Login to post a reply

Server time is: 2024-11-23 09:38:19
Your offset time is: 2024-11-23 09:38:19