• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
tcige

how to draw a circle with 'Bresenham'

17 posts in this topic

first i use D3DFVF_XYZRHW, the effect is ok with D3DPT_POINTLIST, but i do not know how to arrange the vertex to draw the solid circle with D3DPT_TRIANGLELIST

 

then i move to D3DFVF_XYZ, i have to change the parameter of D3DXMatrixLookAtLH to suit the radius, but the drawing effect is awful

0

Share this post


Link to post
Share on other sites

Why not with triangle fan?

 

 

D3DXVECTOR2* D3DXVec2Rotate(D3DXVECTOR2* vOut, D3DXVECTOR2* vIn, float rad, const D3DXVECTOR2* vCenter)
{
    float cr = std::cos(rad);
    float sr = std::sin(rad);

    float X = vIn->x;
    float Y = vIn->y;

    X -= vCenter->x;
    Y -= vCenter->y;

    vOut->x = X * cr - Y * sr;
    vOut->y = X * sr + Y * cr;

    vOut->x += vCenter->x;
    vOut->y += vCenter->y;

    return vOut;
}
 
...
struct VERTEX
    {
        D3DXVECTOR4 p;
    };

    static const std::size_t numPts = 20; // points cnt on circle
    std::vector<VERTEX> vVertices;
    d3d9device->SetFVF(D3DFVF_XYZRHW); // pre-tranformed vertex
    VERTEX center; // vertex in center of circle
    center.p = D3DXVECTOR4(500.0f, 400.0f, 0.0f, 1.0f); // set circle center on screen center (1000x800) / 2
    D3DXVECTOR2 ct(center.p.x, center.p.y);
    vVertices.push_back(center); // add center first beause of triangle fan "rule"
    static const float rad = 200.0f; // circle radius
    for(std::size_t i = 0; i < numPts; ++i)
    {
        float ang = (D3DX_PI * 2.0f) * ((float)i / (float)numPts);
        D3DXVECTOR2 pt(center.p.x + rad, center.p.y + rad);
        D3DXVec2Rotate(&pt, &pt, ang, &ct);
        VERTEX vert;
        vert.p = D3DXVECTOR4(pt.x, pt.y, 0.0f, 1.0f);
        vVertices.push_back(vert);
    }
    vVertices.push_back(vVertices[1]); // add first point (on circle) to close
    d3d9device->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, numPts, &vVertices[0], sizeof(VERTEX) );
Edited by belfegor
1

Share this post


Link to post
Share on other sites

Check this thread: 

 

http://www.gamedev.net/topic/259860-drawing-circle-in-directx/

 

Maybe the source code does what you are looking for.

 

[edit] How many thousand / million circles you are planning to draw in order to be bottle necked by sin/cos ??? I'd say that sin&cos isn't the biggest problem here. Especially if you pre-create your geometry, the sin&cos won't have any effect while drawing.

 

Cheers!

Edited by kauna
1

Share this post


Link to post
Share on other sites

+1 What kauna said.

 

Alternative fast sin/cos

 

float fast_sin(float rad)
{
    static const float B = 4 / D3DX_PI;
    static const float C = -4 / (D3DX_PI * D3DX_PI);

    float y = B * rad + C * rad * std::abs(rad);
    return y;
}

float fast_cos(float rad)
{
    static const float B = 4 / D3DX_PI;
    static const float C = -4 / (D3DX_PI * D3DX_PI);

    rad += 1.57079632f;
    float y = B * rad + C * rad * std::abs(rad);
    return y;
}

 

http://devmaster.net/forums/topic/4648-fast-and-accurate-sinecosine/

 

My code above is just an example, you could precalculate most stuff at init time, no need to do that every frame.

Edited by belfegor
0

Share this post


Link to post
Share on other sites

i want to use Bresenham and D3DFVF_XYZ and D3DFILL_WIREFRAME, but no idea

0

Share this post


Link to post
Share on other sites

Why not draw a line list then? You don't need to draw triangles in wireframe to get lines.

 

Cheers!

0

Share this post


Link to post
Share on other sites

wireframe is an example

 

using Bresenham, the vertex is out of order, i have no idea how to arrange the vertex

 

using cosf and sinf according to the angle is simple, but not effecitve

0

Share this post


Link to post
Share on other sites

What is the thing with Bresenham's algorithm? Can you make a drawing for us what you want to accomplish?

 

You want to draw a circle using points? That's highly inefficient (even more inefficient than using sin and cos btw). 

 

Have you made some performance analysis which point out that sin and cos are your problem?

 

Best regards!

1

Share this post


Link to post
Share on other sites

that algorithm is simple, and it works fine with D3DFVF_XYZRHW and D3DPT_POINTLIST

 

i want to apply it to D3DFVF_XYZ and solid circle

0

Share this post


Link to post
Share on other sites

So do I understand you correctly that you want to draw a filled shape (circle in this case) and use Bresenham's algorithm (which is used mostly for drawing circle contour) ?

 

The difference between D3DFVF_XYZRHW and D3DFVF_XYZ is that the RHW version skips vertex shader / projection part ie. it assumes that the data is already transformed. D3DFVF_XYZ doesn't skip transformation and projection so you'll need to provide a correct world, view and projection matrix in order to draw correctly.

 

If you are drawing 2d shapes on the screen then the RHW version should do the trick. However, you may accomplish the same with D3DFVD_XYZ but you'll need correct matrices for it to work correctly. Do you need to draw circles in 3d space?

 

Cheers!

0

Share this post


Link to post
Share on other sites

So do I understand you correctly that you want to draw a filled shape (circle in this case) and use Bresenham's algorithm (which is used mostly for drawing circle contour) ?

 

The difference between D3DFVF_XYZRHW and D3DFVF_XYZ is that the RHW version skips vertex shader / projection part ie. it assumes that the data is already transformed. D3DFVF_XYZ doesn't skip transformation and projection so you'll need to provide a correct world, view and projection matrix in order to draw correctly.

 

If you are drawing 2d shapes on the screen then the RHW version should do the trick. However, you may accomplish the same with D3DFVD_XYZ but you'll need correct matrices for it to work correctly. Do you need to draw circles in 3d space?

 

Cheers!

 

 

yeah, that's what i mean, in 3d space

 

even i increase the radius and adjust the parameter of D3DXMatrixLookAtLH to suit the radius, the effect is awful

0

Share this post


Link to post
Share on other sites

+1 What kauna said.

 

Alternative fast sin/cos

 

float fast_sin(float rad)
{
    static const float B = 4 / D3DX_PI;
    static const float C = -4 / (D3DX_PI * D3DX_PI);

    float y = B * rad + C * rad * std::abs(rad);
    return y;
}

float fast_cos(float rad)
{
    static const float B = 4 / D3DX_PI;
    static const float C = -4 / (D3DX_PI * D3DX_PI);

    rad += 1.57079632f;
    float y = B * rad + C * rad * std::abs(rad);
    return y;
}

 

http://devmaster.net/forums/topic/4648-fast-and-accurate-sinecosine/

 

My code above is just an example, you could precalculate most stuff at init time, no need to do that every frame.

 

 

is this fast cos/sin authoritative and cos/sin acceptable in reality?

0

Share this post


Link to post
Share on other sites

I am using it for my particle system, for me visually there is no noticeable difference between these and standard sin/cos. Why not try and see?

Edited by belfegor
0

Share this post


Link to post
Share on other sites

I am using it for my particle system, for me visually there is no noticeable difference between these and standard sin/cos. Why not try and see?

 

thanks, i will try and this remind me of the 'hacker's delight'

 

then what about Bresenham, can it be used in D3DFVF_XYZ

Edited by tcige
0

Share this post


Link to post
Share on other sites

then what about Bresenham, can it be used in D3DFVF_XYZ

 

I don't think so. The Bresenham's circle algorithm is suitable to drawing a circles in 2d. It was developed when drawing single pixels to the screen was the slowest thing ever... that's like some decades ago. 

 

Just create a mesh using the given functions (as smooth as required) and I can guarantee that the sin and cos functions aren't the performance bottle neck. If you store the geometry inside a vertex buffer, it'll be enough to create the mesh once at the start up.

 

Give some screenshots to show what you want and what you have accomplished. It is really difficult to read people's mind you know. 

 

Cheers!

Edited by kauna
1

Share this post


Link to post
Share on other sites

then what about Bresenham, can it be used in D3DFVF_XYZ

 

I don't think so. The Bresenham's circle algorithm is suitable to drawing a circles in 2d. It was developed when drawing single pixels to the screen was the slowest thing ever... that's like some decades ago. 

 

Just create a mesh using the given functions (as smooth as required) and I can guarantee that the sin and cos functions aren't the performance bottle neck. If you store the geometry inside a vertex buffer, it'll be enough to create the mesh once at the start up.

 

Give some screenshots to show what you want and what you have accomplished. It is really difficult to read people's mind you know. 

 

Cheers!

 

thanks a lot

 

i'm new to directx, and someone says cosf/sinf's efficiency is much slower than Bresenham

 

finally i find difficult to apply it to 3d, and i'll be glad to use cosf/sinf because it needs only a few lines

0

Share this post


Link to post
Share on other sites

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  
Followers 0