Sign in to follow this  
LordFallout

FPS Crosshair question

Recommended Posts

I'm currently learning how to uses direct x and direct input and i've managed to program myself a little app with a floor and a camera, at the moment i use direct input to move the camera around on the floor (walk) with w a s and d keys, im using the mouse to move the camera around as in a FPS(look) im trying to figure out a way to draw a crosshair, which is going to stay at the center of the screen to aim with, an idea i had was to draw a quad in the center of the screen and move it along with the camera, and just keep flipping the mouse back to the center of the screen everytime it moved, so that when shooting in the future i can use picking to detect where i shoot. i tried using the fucntion setCursorPosition but it doesnt seem to set the cursor position at all and i have no idea how i'm going to implement the crosshair to display and stay in the center of the screen. any advice from anyone or any links to tutorials would be greatly appreciated.

Share this post


Link to post
Share on other sites
I understand what you are saying and what the artical says but i am having trouble implementing it, how would i make it as an overlay so it will stay in the center of my screen at all times, no matter what the direction of the camera for instance, help is greatly appreciated as the crosshair oads up in one place, the center of my screen but stays in that same place so if i spin the camera around i can see the crosshair from all different angles as it doesnt move like it should.

Share this post


Link to post
Share on other sites
As DvDmanDT said, the way is use transformed vertices (D3DFVF_XYZRHW). The transformed vertices are not affected by IDirect3DDevice::SetTransform(). They stay at their original positions. Its just you create a quad (four vertices/two triangles) and put it on the center of your screen and then use IDIrect3DDevice::DrawPrimitive() (or something like) to render your two triangles. But first, put some texture on your quad with also some transparency and you'll get a very nice crosshair ;)

Hope it helps...good luck

Share this post


Link to post
Share on other sites
Thanks for all your replies, i spent last night and this morning trying to figure this problem out i tried setting the rhw value for the floor quad to 0.0f and the crosshair to 1.0f but im still having the same problem, anyway im gonna keep at it, thanks for you helps

Share this post


Link to post
Share on other sites
Ok i think im on my way to sorting it out, its just driving me crazy now i cant see my textured surface at all i know its going to be a stupid typing error or a mistake with my fvf or something dead easy to spot i just cant, would love some help with sorting this out.

Any clue or a hint would be greatly appreciated, also if my code is laid out correctly please let me know so i can have a shot at making eveything decently laid out

my struct and initialization code is as follows


struct VertexCursor
{
VertexCursor(){}
VertexCursor(float x, float y, float z, float rhw, DWORD colour, float u, float v)
{
_x = x; _y = y; _z = z; _rhw = rhw;
_colour = colour;
_u = u; _v = v;
}
float _x, _y, _z, _rhw;
DWORD _colour;
float _u, _v;

static const DWORD FVF;
};

#define D3DFVF_VERTEXCURSOR (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)






device->CreateVertexBuffer( 6 * sizeof(VertexCursor), D3DUSAGE_WRITEONLY, D3DFVF_VERTEXCURSOR, D3DPOOL_MANAGED, &CURSORVB, 0 );

VertexCursor* vertices;

/* Must lock before carrying out operations */
CURSORVB->Lock(0, 0, (void**)&vertices, 0);

/* vertices of the Crosshair */
vertices[0] = VertexCursor( 100.0f, 100.0f, 0.0f, 1.0f, 0xffffffff, 0.0f, 1.0f);
vertices[1] = VertexCursor( 100.0f, 105.0f, 0.0f, 1.0f, 0xffffffff, 0.0f, 0.0f);
vertices[2] = VertexCursor( 105.0f, 105.0f, 0.0f, 1.0f, 0xffffffff, 1.0f, 0.0f);
vertices[3] = VertexCursor( 100.0f, 100.0f, 0.0f, 1.0f, 0xffffffff, 0.0f, 1.0f);
vertices[4] = VertexCursor( 105.0f, 105.0f, 0.0f, 1.0f, 0xffffffff, 1.0f, 0.0f);
vertices[5] = VertexCursor( 105.0f, 100.0f, 0.0f, 1.0f, 0xffffffff, 1.0f, 1.0f);

/* Done so unlock now */
CURSORVB->Unlock();

/* Load Texture */
if( FAILED( D3DXCreateTextureFromFileEx( device, L"crosshair.png", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DUSAGE_DYNAMIC,
D3DFMT_A8R8G8B8 , D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, BLACK, NULL, NULL, &TexCursor ) ) )
{
MessageBox( 0, L"Creating texture cursor from file - FAILED", 0, 0 );
return false;
}
device->SetTexture( 0, TexCursor );







device->SetStreamSource(0, CURSORVB, 0, sizeof(VertexCursor));
device->SetFVF(D3DFVF_VERTEXCURSOR);

/* Draw primitives using presently enabled texture. */
device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);



Share this post


Link to post
Share on other sites
There are several possible causes of invisible geometry, each as frustrating as the other. Here are a few things you should always try out in this situation.

It may be being culled: set D3DRS_CULLMODE to D3DCULL_NONE.
It may be behind something else in the scene: set D3DRS_ZENABLE to FALSE.
It may be invisible: set D3DRS_ALPHABLENDENABLE to FALSE.

Failing these, it just may be out of view. It looks to me as though your quad is being defined in pixels when it should be in dimensionless units: Remember that a transformed vertex (x, y, z, rhw) must have -1 < x, y, z < 1 and 0 < rhw < 1 for it to be inthe viewing frustum. This is actually beneficial, because you no longer need to know the resolution in order to define the HUD. By the way, those hard-coded values you're using are an accident waiting to happen.

Regards
Admiral

Share this post


Link to post
Share on other sites
I will try out what you have said although reading through the dx9 sdk im pretty sure it says the dimensions are in pixels, i'll double check and i dont intend to keep the values hard coded thats just a temporary thing until i get some basic testing out of the way, thanks for the reply.

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