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.

DarkBASIC Professional Discussion / [SOLVED] 2D to 3D Conversion (I Got Brain Fog)

Author
Message
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 28th Nov 2024 18:31
I know this has probably been asked a million or so times, but searching is getting me nowhere! (all the returned solutions aren't what I'm looking for!)

I have created a Sky dome using 'MAKE OBJECT SPHERE nOBJ, -1000, 18, 9' the negative value turns it inside out.
I have loaded a texture using 'LOAD IMAGE "MyTexture.png", nIMG'
I then Texture The Object using 'TEXTURE OBJECT nOBJ, nIMG'
Lastly I position the Object using 'POSITION OBJECT nOBJ, 0.0, 0.0, 0.0'
nothing too impressive so far and this displays my image as expected...

The fancy bit that I'm struggling with is, within my image (which is a seamless spherical map) there is a light source (a sun!)

My application scans this image to calculate the position of the sun in X & Y coordinates from the top left of the image
dX = 360.0 * (X.Pos / Image.Width) <-- Returns a Value 0.0 to 1.0 * 360.0 related to position across image (multiplied into degrees)
dY = 360.0 * (Y.Pos / Image.Height) <-- Returns a Value 0.0 to 1.0 * 360.0 related to position down image (multiplied into degrees)

I'm now attempting convert this into 3D coordinates to position a light, and this is where I'm messing up somewhere
fX = 1000 * SIN(dy) * COS(dx)
fY = 1000 * SIN(dy) * SIN(dx)
fZ = 1000 * COS(dy)

Then I position the light using 'POSITION LIGHT 0, fx, fy, fz'
And finally direct the light using 'POINT LIGHT 0, 0.0, 0.0, 0.0'

As a sanity check (because the results were weren't what I expected!) I created another object and placed it at the generated calculations.
The fX and fY values were both 0.0 so I changed the multiplications between SIN 7 COS with +/- which gave me even weirder results (1.0, 0.0, 500.0)!

For some reason I can't get my code working it looks correct, but it's definitely not working!

Any ideas or references to working solutions would be appreciated

Many thanks Alan

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

Go to answer

TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 29th Nov 2024 18:12
This post has been marked by the post author as the answer.
What a (#ck'n) Idiot
Apologies for the expletives, but hey I am from Glasgow, it is officially part of our language!

After spending the knight going around in circles getting nowhere, I stumbled on the fact that DBP inverts the Image when applied to an inverted sphere!
In addition to that, The X position from the image relates to the Y Angle and the Y position from the image relates to the X Angle and there is more the X Angle ranges from 0.0 degree's to 180.0 degree's Top to Bottom not the 360 I had used.

The problem seems to be fixed, that is using the following function, although I'm not sure this is quite correct (Confidence has taken a blow)



Anyway, I'm off to retrieve my lost boot that has somehow gotten firmly stuck up my own A$$

Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 29th Nov 2024 19:54
Not to mention your camera or the object might be rotated or anchored backwards.

Also COS/SIN/TAN can in some cases spit out the same values for more than one value. The curve goes up and comes back down.

You might find you need to section off co-ordinate or angle ranges where you are using slightly tweaked conversion formulas to ensure the values come out correctly.
For example if you notice the values work but suddenly glitch out if an angle grows larger than 180 degrees or something. Usually a negative/positive sign swap somewhere sorts that out for that value range.


Mage's Modular Code - Get awesome UI controls and powerful Bitmap Fonts!
Screen Device Recovery - Stop your apps from crashing when minimized/tabbed/screen locked.
CPU Friendly Frame Limiter - Make your app generate less computer heat + noise, and use less battery life!
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 29th Nov 2024 22:41
Cheers Mage

Nothing to scary, This is primarily only to initially position a light where the sun is located in the sky dome after that it'll be tied to the dome's X, Y & Z location and Pointing to it's origin
which is tied to the camera position, this will allow the player to walk around and view the objects dark side. So I don't need it to be updated every frame or any frame for that mater.

Currently I've manually entered the Sunspot's location as I can't find any command to read the pixel brightness from an image



That's the rough idea, and I can Use the LASTCOLOR value as the light colour and position it accordingly, obviously sunspots won't just be one pixel so some crazy averaging algorithm will be needed to separate and centre the sun and filter out any other light sources within the picture, i.e. stars, lightning, street lights etc.

I also have an Idea of using the same function to calculate an average of all the pixel colours measured to be used as the ambient colour and intensity,

eventually I can have a folder that contains a collection of HD Spherical Images that when the application runs, it randomly selects as the background and automatically sets the main and ambient light source colours and levels.

I've created my own randomiser that uses the current system time for Hours, Minutes & Seconds and the timer() Mod 1000 for milliseconds as Timer() stats at zero whenever the application starts using the HMS generates a more random seed to Randomize with.

Sigh, I Forgot how much fun can be had while using DBP

TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 2nd Jan 2025 15:41 Edited at: 2nd Jan 2025 15:51
Not as expected, I thought I had solved this but after a few Christmas sherries, I tried it out with a completely random image I came across while browsing the internet and it didn't work or more accurately isn't working correctly as in the marker is in wrong position.

here is some code that hopefully explains in more detail what I'm trying to achieve


I've tried every variation of above that I can think of, I've offset the angles, I've swapped around the SIN/COS functions the results are all wrong, either the marker is behind the camera, on the right side or below the horizon but never on top of the selected point.

I'm guessing this one is for the Math gurus amongst us, but I'm open to any suggestions or pointers to other posts that may help.

Thanks

Login to post a reply

Server time is: 2026-06-05 07:06:05
Your offset time is: 2026-06-05 07:06:05