Sign in to follow this  

How to draw stars or simple circle shapes in DirectX 9?

This topic is 3309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm developing a simple side-scrolling app where stars fly by. I have DirectX working in Visual Studio 2008 and am using Visual Basic as my language. Where I'm really struggling is in displaying small circles, pixels (points), and star shapes (pixels with a glow?). I've scoured the web for examples, but just can't find what I'm looking for. Most examples are ancient and outdated or not tailored to simple circular shapes. Right now I have "Microsoft.DirectX" and "Microsoft.DirectX.Direct3D" as references. Any help would be appreciated! Thanks much, - Matt

Share this post


Link to post
Share on other sites
Well one thing you should be aware of is that the Managed DirectX API is no longer supported. The two main options these days are Microsoft's XNA Framework and the community-supported SlimDX. The XNA Framework is game-creation framework that also includes a wrapper for D3D9, and is cross platform with the Xbox 360. SlimDX is a managed wrapper for both DirectX9 and DirectX10.

Anyway to answer your question, most likely the easiest and best way to do this is to simply have a texture with the star shape you want on it and then draw it as a sprite or billboarded quad. Simple, easy, good performance, good results.

Share this post


Link to post
Share on other sites
Thanks! I guess I'm struggling because there's so much out there. I'm willing to use whatever is today's flavor for accessing DirectX, so SlimDX looks like a good start. Would I still need to install the DirectX SDK?

Regarding the "star shape" I'm looking for, is there not a way to draw just one set of random pixels (space dust, for example)? You can probably tell I'm not the most keen on using sprites, although those would work great as a texture for spherical planets. :-)

Basically, I'm looking to port something I did way back in DOS QuickBasic in high school. I'd like to use DirectX, but so far I've struggled with the lack of documentation I'm finding through searches. Thanks for the help so far though! It's appreciated.

Share this post


Link to post
Share on other sites
I've done a lot of reading and it looks like SlimDX is definitely the way to go. But to be quite frank, documentation is terrible. :-( I just want to find a simple framework I can start from and I can't find a darn thing. It's so frustrating. I've developed in VB 4, VB 6, QuickBasic 4.5, PHP, Perl, and C++, and yet I can't figure this thing out. I feel like people are posting piles of parts and telling newbies to go build a car. Is it that difficult to draw a simple pixel?

Maybe OpenGL is the answer? Think I'm going to give up for tonight...

Share this post


Link to post
Share on other sites
Quote:
Original post by Mateo1041
I just want to find a simple framework I can start from and I can't find a darn thing.


XNA might be a better route to go. They have an entire website dedicated to examples and tutorials. Plus the XNA framework comes with excellant documentation that contains tutorials and 'how to' topics.

Quote:
Original post by Mateo1041
Is it that difficult to draw a simple pixel?


The problem is that your looking into 3D API's (Direct3D and OpenGL). With these API's, you generally do not think in terms of drawing individual pixels. Instead you draw with polygons. The most common way to render 2D graphics (with these API's) is to render a quad and texturize it. Newer video cards are built to render geometry at blazing speeds.

XNA has a SpriteBatch class that makes rendering 2D sprites pretty simple. I would recommend starting there if you look into XNA.

What kind of game are you trying to port? Perhaps we could make some better suggestions for you. :-)

[Edited by - Billr17 on November 17, 2008 9:44:37 PM]

Share this post


Link to post
Share on other sites
When I first implemented glow effects, I used this algorithm for the pixel shader:


float rX = pow(length(ScreenPos.x - iTex.x) * AspectRatio, 2);
float rY = pow(length(ScreenPos.y - iTex.y), 2);
float Radius = pow(1 - pow(saturate((rX + rY) * OuterFactor), InnerExp), 1.25f);



(Where ScreenPos is the center of the circle in screen space, AspectRatio is the relationship between the width and height of your render target and iTex is the pixel's position clamped to [0..1]. Finally, OuterFactor and InnerExp defines the size and shape of the circle)

But, like someone said above, procedurally generating a circle it's quite expensive, you may want to look into textures for doing this without a considerable performance hit.

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by jpventoso
When I first implemented glow effects, I used this algorithm for the pixel shader:


The shader approach may be a bit too advanced if you are just starting to learn how to render with a 3D API. For the sake of discussion, you could also create a 3D mesh shaped like a circle or star. Once you have created the mesh in memory once, you can reuse it to render the same object many times.

So far we have outlined three approaches to solving your problem:
1.) Render a quad that is textured with a image of a circle or star.
2.) Render a quad/geometry that is colorized during run time using a pixel shader.
3.) Create and render a piece of geomtetry that is shaped like a circle or star.

No matter the approach you pick, it will help you learn the basics of working with 3D API's.

Share this post


Link to post
Share on other sites
I really appreciate the helpful comments. Truth be told, I'm trying to port a screen saver. I wasn't sure if this forum was *just* for games, so I guess I wasn't too specific. :-) But maybe that's my fault.

My concept is to take the default boring Windows starfield screen saver and turn it sideways so that stars float by in "3D". The one I developed back in the 1990's was pretty awesome and simply used pixels of various shades of color and at different speeds.

So, that's what I'm looking to do. There might be anywhere from 150 - 1000 stars on the screen at a time. That's why my original thought was just to use pixels. But if I can get some glow going too, this would be pretty awesome.

Billr17, what is the difference between #2 and #3? Is one imported from some kind of model file while the other is generated from code?

Is it easy to do simple spheres? That might be the best option in a 3D sense.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mateo1041
what is the difference between #2 and #3? Is one imported from some kind of model file while the other is generated from code?


With #2 you would apply a pixel shader to the geometry. A pixel shader is a small program that runs on the GPU to finely control the colorization of the outputted pixels. Think of the pixel shader as a material applied to your geometry. Shaders are generally programmed using a high level programming language. Most of the current shader languages have a syntax that resembles the C language. The most common shader language used with XNA and DirectX is HLSL (High Level Shader Language).

You have two options with #3. You could import a 3D model (as you have already stated), or you could programmatically generate the mesh during runtime. It would be fairly simple to create a mesh shaped like a circle programmatically.

Quote:
Original post by Mateo1041
Is it easy to do simple spheres? That might be the best option in a 3D sense.


It would be similar to creating the circle. You could either create it during runtime using an equation to calculate the sphere, or import a 3D model of a sphere.

Share this post


Link to post
Share on other sites
Quote:
Original post by Billr17
Quote:
Original post by Mateo1041
Is it easy to do simple spheres? That might be the best option in a 3D sense.

It would be similar to creating the circle. You could either create it during runtime using an equation to calculate the sphere, or import a 3D model of a sphere.


If you're starting with Direct3D, you may find useful the D3DX library (It comes with the Direct3D SDK). It's a kind of a helper library that simplifies and optimizes a lot of common practices on Direct3D.

Specifically on this case, the D3DXCreateSphere() function can easily create a sphere model for your application.

Share this post


Link to post
Share on other sites
I really appreciate the help! I'm not looking to make any money off this and would be glad to share when done.

Now that I have a few more ideas on how to proceed, I really like the sphere route. Although I'll have to think about that vs. a small pixel-sized sprite as I want the screensaver to run with minimal CPU power. I've tried some screensavers that are too graphical and really take my laptop for a ride.

Finally, I'll need to get started with either VB 2008 or C# (I've heard that's better than C++). I have Visual Studio 2008. I have more experience with VB and it's simpler for me, but maybe this will be an excuse to finally learn C# well. I just hope I have more luck finding decent samples and a framework I can use. The last few days have been brutal. :-)

Share this post


Link to post
Share on other sites

This topic is 3309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this