aliens

Author Topic: [Solved] Let there be light  (Read 32240 times)

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
[Solved] Let there be light
« on: July 06, 2010, 12:10:13 am »
I've been playing around with the code
https://www.youtube.com/watch?v=VnQjsGGmeNk

SupSuper, a few things I noted when building this:
  • Variable Globe::_save is never populated (I added it to the constructor)
  • Day to night palette appears to be 6 shades

You're probably aware of these, but thought I'd mention if not  :D
« Last Edit: July 21, 2010, 09:40:31 am by pmprog »

Offline SupSuper

  • Lazy Developer
  • Administrator
  • Commander
  • *****
  • Posts: 2162
    • View Profile
Re: Let there be light
« Reply #1 on: July 06, 2010, 05:33:02 am »
What, showing off without a hint of code??? How dare you! :P

Very nice work! ;D Looks a lot less gradual than the X-Com effect though, I presume you're not done yet?

I've been playing around with the code
https://www.youtube.com/watch?v=VnQjsGGmeNk

Quote
Variable Globe::_save is never populated (I added it to the constructor)
It's populated by Globe::setSavedGame.

Quote
Day to night palette appears to be 6 shades
I just tried it myself in the original, you're right that the ocean has 6 shades, but each land polygon seems to have 9-10 shades.

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
Re: Let there be light
« Reply #2 on: July 06, 2010, 09:22:01 am »
What, showing off without a hint of code??? How dare you! :P
Hehe, I was actually too embarressed to show the code, whilst the effect looked "okay", the code was pretty sloppy.

Very nice work! ;D Looks a lot less gradual than the X-Com effect though, I presume you're not done yet?
There are a number of issues with it at the moment, the main one being more than 1 sunrise in a day; secondly, I was getting the last longitude of each polygon and calculating a shade based of longitude and hour. This will also contribute to the relatively "jerky" sunrise.

It's populated by Globe::setSavedGame.
Good call (pardon the pun), I didn't see that  :)

I just tried it myself in the original, you're right that the ocean has 6 shades, but each land polygon seems to have 9-10 shades.
Hmm, perhaps I need to dump the palette out so I can see what I've got to work with. I'm sure if my "shade" variable went negative, or above 6, the globe went psychodellic.

Actually, I dumped the palette out:

At least 16 shades, which will also make a smoother transition.

On with the thinking cap.


Also, one last thing. As you noted that there are shades of ocean, you're just drawing a blue circle. Are there ocean polygons that are just not being drawn yet? or is that going to need some wizardry to paint?

Offline SupSuper

  • Lazy Developer
  • Administrator
  • Commander
  • *****
  • Posts: 2162
    • View Profile
Re: Let there be light
« Reply #3 on: July 06, 2010, 08:34:06 pm »
Hmm, perhaps I need to dump the palette out so I can see what I've got to work with. I'm sure if my "shade" variable went negative, or above 6, the globe went psychodellic.

Actually, I dumped the palette out:

At least 16 shades, which will also make a smoother transition.
Well the real globe has actual textures (I haven't applied them because they cause terrible performance for some mysterious reason), which is probably why there's 16 shades of each colour but only 9-10 used for the day/light effect.

If necessary you can just have a min/max value to prevent the globe going psychedelic.

On with the thinking cap.


Also, one last thing. As you noted that there are shades of ocean, you're just drawing a blue circle. Are there ocean polygons that are just not being drawn yet? or is that going to need some wizardry to paint?
There are no ocean polygons (except in TFTD) stored in the game files. If I had to guess, the game might just generate a bunch of "ocean quads" itself that scroll horizontally along the flat map depending on the time, and get mapped as a "semi-sphere" on the globe. Or they use some wizardry involving circles or something, what do I know? :P

For future reference, I'd suggest checking out:
https://www.ufopaedia.org/index.php?title=WORLD.DAT
https://www.ufopaedia.org/index.php?title=PALETTES.DAT

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
Re: Let there be light
« Reply #4 on: July 06, 2010, 11:13:50 pm »
Well the real globe has actual textures (I haven't applied them because they cause terrible performance for some mysterious reason)
Yeah, totally forgot about them. In fact, I've been having a bit of a look at textures tonight, I can't seem to render them. They either don't draw anything, or crashes in the Surface::getPixel with _surface->pixels = 0x00000000. Odd.

The main reason I was looking was if there were different light levels per texture, but interestingly not, so you'd have to update the texture as your blitting it. Should make things a little more interesting. Either that, or as you read in TEXTURE.DAT, you create several SurfaceSets with the modified palettes to give your different light levels, then you can just blit from whichever source you need.

There are no ocean polygons (except in TFTD) stored in the game files. If I had to guess, the game might just generate a bunch of "ocean quads" itself that scroll horizontally along the flat map depending on the time, and get mapped as a "semi-sphere" on the globe. Or they use some wizardry involving circles or something, what do I know? :P
If it were me, I'd probably create a hardcoded full sphere polygon list (landscape can draw over the top), but this way you could texture the sea too if you wanted.

Offline SupSuper

  • Lazy Developer
  • Administrator
  • Commander
  • *****
  • Posts: 2162
    • View Profile
Re: Let there be light
« Reply #5 on: July 07, 2010, 01:38:22 am »
Well the real globe has actual textures (I haven't applied them because they cause terrible performance for some mysterious reason)
Yeah, totally forgot about them. In fact, I've been having a bit of a look at textures tonight, I can't seem to render them. They either don't draw anything, or crashes in the Surface::getPixel with _surface->pixels = 0x00000000. Odd.
Enabling the poor texture rendering is an exercise in horror and shock and possibly frustration, but here goes:

1. Go to the Globe::draw function in Globe.cpp.
2. Comment out the filledPolygonColor(...) line and uncomment the texturedPolygon(...) line.
(if you're having fun you can also uncomment the polygonColor(...) line instead and see the globe in wireframe-o-vision!)

This would be enough, but because of how SDL works, surfaces need to be locked for pixel-level access and unlocked for blitting. So you also need to:

3. Comment out the lock() and unlock() lines so the blitting works.
4. Comment out the whole "base markers" drawing since anything with getPixel/setPixel will probably crash now since the surface is unlocked.
5. Comment out everything in Globe::blit except the Surface::blit(...) line so it doesn't redraw itself every few ticks (I'll optimize this eventually).

After all this you should have successfully revealed the beast:



Any attempt at rotation will take seconds and the textures don't even match the terrain and there's gaps in the polygons and oh gawd it's just so horrible that I just commented it out to never see the light of day while I cried in a corner for weeks and weeks :'( (seriously though with the huge performance problem there's no point in fixing the rest so I just moved on).

The main reason I was looking was if there were different light levels per texture, but interestingly not, so you'd have to update the texture as your blitting it. Should make things a little more interesting. Either that, or as you read in TEXTURE.DAT, you create several SurfaceSets with the modified palettes to give your different light levels, then you can just blit from whichever source you need.
Yeah palette wizardry is very common in X-Com, and since colors go from light to dark in the palette, you would just shift the palette a bit to the left for each polygon to make it darker. In OpenXcom you can achieve a similar effect with the Surface::offset function.

There are no ocean polygons (except in TFTD) stored in the game files. If I had to guess, the game might just generate a bunch of "ocean quads" itself that scroll horizontally along the flat map depending on the time, and get mapped as a "semi-sphere" on the globe. Or they use some wizardry involving circles or something, what do I know? :P
If it were me, I'd probably create a hardcoded full sphere polygon list (landscape can draw over the top), but this way you could texture the sea too if you wanted.
Yeah that would be the more sensible option today, but those were different times with only 32K of memory that you couldn't afford to waste or something who knows. :P Plus it makes the day/night effect look smoother over the ocean.
« Last Edit: July 07, 2010, 01:41:42 am by SupSuper »

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
Re: Let there be light
« Reply #6 on: July 07, 2010, 11:56:51 am »
Enabling the poor texture rendering is an exercise in horror and shock and possibly frustration, but here goes:

1. Go to the Globe::draw function in Globe.cpp.
2. Comment out the filledPolygonColor(...) line and uncomment the texturedPolygon(...) line.
(if you're having fun you can also uncomment the polygonColor(...) line instead and see the globe in wireframe-o-vision!)

This would be enough, but because of how SDL works, surfaces need to be locked for pixel-level access and unlocked for blitting. So you also need to:

3. Comment out the lock() and unlock() lines so the blitting works.
4. Comment out the whole "base markers" drawing since anything with getPixel/setPixel will probably crash now since the surface is unlocked.
5. Comment out everything in Globe::blit except the Surface::blit(...) line so it doesn't redraw itself every few ticks (I'll optimize this eventually).
Actually, I did these instructions, it just left me with a blue globe (tho I might have forgot to change the surface lock). I'll have another look later

I can't see the image at work (it's blocked), but I'll fiddle with the code

Any attempt at rotation will take seconds and the textures don't even match the terrain and there's gaps in the polygons and oh gawd it's just so horrible that I just commented it out to never see the light of day while I cried in a corner for weeks and weeks :'( (seriously though with the huge performance problem there's no point in fixing the rest so I just moved on).
Hmmm, I might take a look at this, see if I can help out any, as I have a cunning plan how to perform this operation quickly...

Yeah palette wizardry is very common in X-Com, and since colors go from light to dark in the palette, you would just shift the palette a bit to the left for each polygon to make it darker. In OpenXcom you can achieve a similar effect with the Surface::offset function.
Thanks for the heads up on that.

Yeah that would be the more sensible option today, but those were different times with only 32K of memory that you couldn't afford to waste or something who knows. :P Plus it makes the day/night effect look smoother over the ocean.
Oh I knew why they didn't do it UFO, but I'm saying for OpenXcom you should. That said, TFTD had polygons for the seas, although I don't think they had as many for the land.

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
Re: Let there be light
« Reply #7 on: July 09, 2010, 05:03:44 pm »
https://www.pmprog.co.uk/tmp/xcomlight.swf

Code is in VB.NET for now (just checking my maths); Will convert to C++ later

Edit: SupSuper, if you think I'm "helping out" too much, let me know. I don't want you to feel like I'm railroading your project  ;)
« Last Edit: July 09, 2010, 05:09:20 pm by pmprog »

Offline SupSuper

  • Lazy Developer
  • Administrator
  • Commander
  • *****
  • Posts: 2162
    • View Profile
Re: Let there be light
« Reply #8 on: July 09, 2010, 06:23:58 pm »
https://www.pmprog.co.uk/tmp/xcomlight.swf

Code is in VB.NET for now (just checking my maths); Will convert to C++ later
I couldn't make much sense of the code, but the result looks alright. If you're making a light calculation based on time, something you should remember:
- Even though X-Com longitude goes from 0..360 (as opposed to -180..180), the GMT line is still at 0 longitude.
- Therefore, the "daylight" is centered at 0 at 12:00GMT, and the "nightlight" is centered at 0 at 0:00GMT (24-hour-format).

Edit: SupSuper, if you think I'm "helping out" too much, let me know. I don't want you to feel like I'm railroading your project  ;)
It's fine. As long as you keep working on the things I really don't feel like working on and would postpone for weeks (UGH GLOBES MATHS TRIGNOMETRIES THAT CAN WAIT), things get done faster. :P
« Last Edit: July 09, 2010, 06:27:34 pm by SupSuper »

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
Re: Let there be light
« Reply #9 on: July 09, 2010, 09:53:41 pm »
I couldn't make much sense of the code, but the result looks alright. If you're making a light calculation based on time, something you should remember:
- Even though X-Com longitude goes from 0..360 (as opposed to -180..180), the GMT line is still at 0 longitude.
- Therefore, the "daylight" is centered at 0 at 12:00GMT, and the "nightlight" is centered at 0 at 0:00GMT (24-hour-format).
That's fine, I had accounted for that.

It's fine. As long as you keep working on the things I really don't feel like working on and would postpone for weeks (UGH GLOBES MATHS TRIGNOMETRIES THAT CAN WAIT), things get done faster. :P
hehe, okay. I'll post a topic before I start looking at anything.

Oh, on a completely backwards note. we're going to have to undo a portion of the texture update. Whilst splitting out the globe and overlays might look cleaner anyway, the whole buffering of the globe was based on the fact that if you didn't rotate, it stayed the same. However, that completely neglected daylight rotation...


Here's the first look...
https://www.youtube.com/watch?v=JY5eJ_m2DlA

Edit: Attached patch
« Last Edit: July 09, 2010, 09:55:59 pm by pmprog »

Offline SupSuper

  • Lazy Developer
  • Administrator
  • Commander
  • *****
  • Posts: 2162
    • View Profile
Re: Let there be light
« Reply #10 on: July 10, 2010, 04:10:41 pm »
Oh, on a completely backwards note. we're going to have to undo a portion of the texture update. Whilst splitting out the globe and overlays might look cleaner anyway, the whole buffering of the globe was based on the fact that if you didn't rotate, it stayed the same. However, that completely neglected daylight rotation...
That's alright, it's still better this way since the globe still won't have to update every engine tick, but only every game tick, even if the difference is negligible. :P

Here's the first look...
https://www.youtube.com/watch?v=JY5eJ_m2DlA

Edit: Attached patch
Very nice! I'll integrate it later today if you're done tweaking it.

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
Re: Let there be light
« Reply #11 on: July 10, 2010, 08:09:31 pm »
Well, it still needs the ocean shading, and I think between texturing and lighting, there needs to be some optimisation; and I know exactly where to start...

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
Re: Let there be light
« Reply #12 on: July 10, 2010, 10:44:40 pm »
New patch.

Changes:
  • New SurfaceSet constructor for cloning texture sets
  • SurfaceSet.getFrames() to return number of frames in Set
  • Fixed performance
  • The oceans are still not shaded
  • Light distribution on the planet is inaccurate (lighter and darker areas should cover more area)

« Last Edit: July 10, 2010, 10:51:44 pm by pmprog »

Offline SupSuper

  • Lazy Developer
  • Administrator
  • Commander
  • *****
  • Posts: 2162
    • View Profile
Re: Let there be light
« Reply #13 on: July 13, 2010, 05:32:16 am »
I wouldn't worry too much about the accuracy of the light at this point, probably better to move on to more pressing issues first.

Also there's an SDL function for duplicating surfaces which is probably more efficient than just re-blitting it from scratch.


Edit: I commited your progress to the SVN. One thing to note, your "s" variable went all the way up to 8 while the array is only 0..7. ;) I increased the array to fix it.
« Last Edit: July 13, 2010, 09:49:35 pm by SupSuper »

Offline pmprog

  • Commander
  • *****
  • Posts: 647
  • Contributor
    • View Profile
    • Polymath Programming
Re: Let there be light
« Reply #14 on: July 15, 2010, 09:25:33 pm »
I wouldn't worry too much about the accuracy of the light at this point, probably better to move on to more pressing issues first.
Okaydoke, back to textures...

Also there's an SDL function for duplicating surfaces which is probably more efficient than just re-blitting it from scratch.
Ah, even better. I'll be honest, it's been a while since I properly played with SDL (and that was when I was looking to make my own XCOM style game)

Edit: I commited your progress to the SVN. One thing to note, your "s" variable went all the way up to 8 while the array is only 0..7. ;) I increased the array to fix it.
The s variable wasn't in patch 2. Hmmm, maybe I should have made it obvious that they are my patch revision numbers, not that you need to apply them both.