Advertisement

How to make a Galaxy Generator in C++ using OpenGL and SDL.

Started by March 11, 2024 09:51 PM
18 comments, last by Acosix 9 months, 1 week ago

biell2015 said:
I don't see any tutorial about galaxies, but there is also not a lot of games with galaxy, which is a good point.

Go to shadertoy, search for ‘galaxy’. I can see some promising examples.

Btw, i think a space game showing galaxies could be awesome. Travelling at infinite speeds so you can zoom out to see how galaxies spread over space, then zooming in to see unique stars would surely generate a sensation. A big opportunity without doubt.

But ofc. you need to generate everything on the fly as you zoom in and out. Even storing the positions of all stars would take too much storage, i guess.
But it should be possible. Render each galaxy to a texture to cache it for many frames. Rendering a universe would be just some alpha blended billboards. Some of the billboards get an update per frame to adapt to current camera position.

biell2015 said:
And to be fair, the players will be able to tell whether or not the galaxy was generated correctly.

No. I have never seen a galaxy, so no way to judge correctness.

But i have seen a planet, raising certain expectations.

biell2015 said:
a guy took 7 hours to travel 26,000 km at 1060 m/s, which is the coolest thing ever.

Or the most boring thing ever. Please give me infinite speed in space games. ; )

Advertisement

biell2015 said:
I would have to use ECS and Dots, but it doesn't work on my computer at the moment, according to the forum it's something related to shader model. So the only option for me, is to make a game from scratch, without a game engine and then later learn Data Oriented Programming to optimize the game.

The only rational option is to find a framework that works and/or a computer that works rather than submitting to the enormous detour of doing a lot more development requiring a lot more learning.

Omae Wa Mou Shindeiru

JoeJ said:
And i would agree to a to down philosophy, as i usually think the same way. But here it does not apply because there is a gap of nothing between the scales: Universe → galaxies → solar systems → planets. The gap is so huge, having sorted out the larger scale does not help at all to create the smaller scales.

It isn't only a gap in interesting things that can be rendered procedurally (starfields vs. planets), but also in relevant things in a game: above the scale of, roughly, a solar system it's hard to find reasons to model positions precisely in 3D space because practical travel needs to be FTL or just skipped and places become abstract (locations you have coordinates for, distances and costs to go there…). True flight implies a relatively small environment.

Omae Wa Mou Shindeiru

Even in procedurally generated worlds, in the games I've worked on we've never needed to make anything major.

We've either procedurally generated a large rectangle, or procedurally generated a maze. The maze is easier, drop the start and the end, drop a few control points, apply a bunch of rules that give horizontal and vertical interest, and fill in with a bunch of assets and segments. Pretty easy to build a “world” of jungle, desert, swamps, plains, or similar that has a bunch of POI elements and plays well for a 30 minute to 3 hour play area. Larger worlds will take more to fill out convincingly in a way that is fun, but it is a fairly mechanical process once you break down the logic. The systems require quite a few full-time development months, but they're relatively common enough.

It gets back to the question of why you're building what you're building. What is your game? What exactly is the fun?

@joej

JoeJ said:
Go to shadertoy, search for ‘galaxy’. I can see some promising examples.

I don't know a lot about shader, but if I'm not mistaken, you either can't calculate physics on the gpu or it's bad to do it, inefficient. There is a guy on Youtube that was trying to replicate the volumetric smoke from Couter Strike 2.0, and he did it all on the gpu (if I'm not mistaken, I'm not sure), so if you shoot it, the gpu would do all the calculations and it would make a hole. And there is another guy too, that optimezed mesh generation so much, that he could put a lot of objects on screen. So, the gpu is good to make parallel stuff, drawing a lot of stuff at once, and the cpu is good to make nonparallel stuff, like calculating physics. That is what is cool about shaders and gpu rendering optimization, is that if you want you could actually draw millions of stars in the screen, maybe billions. Here: https://www.youtube.com/watch?v=6mNj3M1il_c​​ , I found the link, he is using Unity and cubes, but maybe if you just use points, that don't tecnically have vertices, you could maybe render billions of points​. But I don't know, since I want physics and real location, maybe shaders wouldn't be a good option.

JoeJ said:
Btw, i think a space game showing galaxies could be awesome. Travelling at infinite speeds so you can zoom out to see how galaxies spread over space, then zooming in to see unique stars would surely generate a sensation. A big opportunity without doubt.

Yes, I also think. My original plan is to make the game happens on this galaxy. But I could and it would make sense to show other galaxies, but that would make the emptyness of space problema more huge, maybe I could find a way around it. Yes, seeing unique stars would be good, but No Man's Skys teached us that, exploring random planets empty of things can be boring.

JoeJ said:
But ofc. you need to generate everything on the fly as you zoom in and out. Even storing the positions of all stars would take too much storage, i guess. But it should be possible. Render each galaxy to a texture to cache it for many frames. Rendering a universe would be just some alpha blended billboards. Some of the billboards get an update per frame to adapt to current camera position.

Well, that is where Data Oriented Programming comes in, you wouldn't need to generate everything everytime, and Data Oriented Programming is much faster than Object Oriented Programming, so even loading stuff I guess would be faster. I don't think it would take a loot of storage to be honest, you could generalize a lot of this stuff, even more about space, you could for example use the same texture for stars and just change colors, Data Oriented Programming do a lot of this too, instead of creating textures when you render stuff, you could use the safe stuff, without saying the rendering that I just talk about. Universe in the sense like our observable universe? Maybe it would be even easier, because galaxies are so tiny that they could be represented as points also, then you would have just make branches.

@lorenzogatti

LorenzoGatti said:
The only rational option is to find a framework that works and/or a computer that works rather than submitting to the enormous detour of doing a lot more development requiring a lot more learning.

Well I love learning stuff, but it would be easier if people actually taught stuff. And I would only upgrade my computer if I couldn't optimize things to run on it, otherwise I would optimize even if I needed poor graphics or something like that.

LorenzoGatti said:
It isn't only a gap in interesting things that can be rendered procedurally (starfields vs. planets), but also in relevant things in a game: above the scale of, roughly, a solar system it's hard to find reasons to model positions precisely in 3D space because practical travel needs to be FTL or just skipped and places become abstract (locations you have coordinates for, distances and costs to go there…). True flight implies a relatively small environment.

Star Citizens seems like the models are really precisely positioned in the space, if you see the video, the guy actually travelled without Faster Than Light and it took a lot of time, so it have a lot of space.

@frob

frob said:
Larger worlds will take more to fill out convincingly in a way that is fun, but it is a fairly mechanical process once you break down the logic.

I have a plan 😉.

frob said:
It gets back to the question of why you're building what you're building. What is your game? What exactly is the fun?

Well, immersion maybe, it's a space game, you will be in space, that's why I will have planets, solar systems, etc. It would have faster than light travel too, so you would be able to travel to another places in the galaxy too. But, maybe it would be boring or not fun if you don't like space games.

Advertisement

biell2015 said:
Well, immersion maybe, it's a space game, you will be in space, that's why I will have planets, solar systems, etc. It would have faster than light travel too, so you would be able to travel to another places in the galaxy too. But, maybe it would be boring or not fun if you don't like space games.

I've played a LOT of space games over the years, too many thousand hours. Mostly they tend to work between a balance of dogfighting and trading. Neither has needed a realistic scale of distances. Even games like EVE Online were just small zones and nodes rather than attempting to simulate an entire universe.

If you've got some kind of plan then go for it, but it might be worth more exploration of the ideas especially considering your programming experience level. Either way, you'll learn from the experience.

biell2015 said:
I don't know a lot about shader, but if I'm not mistaken, you either can't calculate physics on the gpu or it's bad to do it, inefficient.

Shadertoy us a very educational resource, surely better than YT.

Regarding inefficiency your assumptions are just wrong. In general GPUs are good for parallel algorithms, but related functionality is available only in compute shaders.
Shadertoy is about pixel shaders, so each thread operates isolated form others. As this rules out any form of efficient data share, many examples form shadertoy are indeed inefficient and have to do a lot of redundant work. But that's irrelevant. You can still learn how various methods and algorithms work.

Since what you want to do requires to generate massive amounts of data, often only for visual purposes but not affecting game interactions and physics, there is probably no way around of using the power of the GPU. If you generate stuff on CPU this might be just too slow, and even if not it might be too slow to upload the data to GPU each frame. Memory transfer is a big bottleneck.

Personally i really like how GPUs work, but doing actual research and development on GPU is very tedious and cumbersome. Thus i prefer to develop everything on CPU first, and port it to GPU only after it works and i have figured everything out. This works well, but ofc. i must know how GPUs work precisely in advance to do so. When you feel ready, i recommend the computer shader chapter from the OpenGL Super Bible book to learn it, which explains the concepts of parallel programming with very good examples.

biell2015 said:
So, the gpu is good to make parallel stuff, drawing a lot of stuff at once, and the cpu is good to make nonparallel stuff, like calculating physics.

Mostly yes, but the quote is much too general. Firstly, ‘parallel stuff’ sounds a bit like ‘do shitloads of brute force’, as gfx programmers traditionally do for most. But that's not what parallel programming is about. GPUs can run complex algorithms, they can traverse trees, they can do hierarchical processing, reduce work to what's really needed, etc. There are certain limitations making programming harder, but they are general purpose processors these days, no longer just pixel accelerators.
It would make sense to solve the constraints from a physics engine on GPU for example, while detecting narrow phase collisions and generating contacts from various shapes would be pretty hard to handle efficiently, but possible as well.

biell2015 said:
I found the link, he is using Unity and cubes, but maybe if you just use points, that don't tecnically have vertices, you could maybe render billions of points​.

A billion points would take something like 6 GB of VRAM. :D

You don't want to do that!

You want to have a representation of density, e.g. in a volume grid representing a galaxy. Each cell has density, representing the mass of glowing stars in it. That's enough. No need to know it's 5172 stars which make up this mass of stars in the cell.
The need to know this only once your spaceship travels close enough to the cell. Then you generate 5172 points, distributed procedurally respecting given density.
Only when you travel close to one of these points, you generate a star and it's planets, moons, mountains, 3 legged alien fish, strands of grass…

That's just an example of how to do it.
But you won't get there if you think in terms of ‘how many millions of cubes can Unity render?’, which is a pretty useless test anyway imo.

If you want to play god, you have to be smart. But brute force isn't smart. ; )

biell2015 said:
but No Man's Skys teached us that, exploring random planets empty of things can be boring.

Yeah. But well, many of us have this dream to generate an entire universe to explore, out of nothing.
And i think this dream is important to make us move forward.
So let's just assume we will manage to make it interesting and fun somehow, better than all the others before us did. :D

(I'm not in this camp btw. For me the Ubisoft scale of game worlds is already more than challenging enough. :D )

biell2015 said:
Data Oriented Programming is much faster than Object Oriented Programming

Really? Well - personally i don't read so much marketing claims about programming paradigms, Dots, or whatever people come up to extend their former limitations.
I do not even know if my work is more like the former or like the latter from these two buzzwords. : )

biell2015 said:
Well I love learning stuff, but it would be easier if people actually taught stuff.

Like Lorenzo, i also thought at first: Just upgrade so you can still use Unity.
But then i forgot to mention this. Maybe because i'm always happy to see people still attempting to do it all on their own, from scratch, from start to finish, without help and restrictions from U engines or now AI generated code.

But i want to warn you. I will take you many years to achieve your goals. Depending on ambitions. For me it's decades already. After that time i now feel i can do anything i want, finally. But i might be already too old to complete the game of my dreams. Lifetime is finite, and that's the limit.

A galaxy is a really large place.

This topic is closed to new replies.

Advertisement