First person question

This is a discussion on First person question within the Game Programming forums, part of the General Programming Boards category; Hi Folks; I'm attempting my hand at some first person game programming and the nature of the game requires a ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    First person question

    Hi Folks;

    I'm attempting my hand at some first person game programming and the nature of the game requires a reticule in the middle of the screen. To change my view of the world I do some rotations but I can't seem to keep my reticule in the screen. Can anyone offer some advice on this?

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    The easiest way to do it would be to put your reticle at a certain pixel coordinate after the 3d calculations are done-in essence you already have your picture of the 3d view, and then you paste the reticule over it. I haven't done it but the setup of opengl should make this easy to do. If you're using directx I have no idea what it would take.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Here is what it takes in DirectX:

    Code:
    #ifndef C2DOVERLAY
    #define C2DOVERLAY
    
    #include <string>
    #include <d3d9.h>
    
    struct TLVertex
    {
      D3DXVECTOR3  m_Position;
      float                  m_fRhw;
      float                  m_fTu,m_fTv;
    
      static const DWORD m_dwFVF;
      TLVertex(D3DXVECTOR3 pos,float u,float v):m_Position(pos),m_fRhw(1.0f),m_fTu(u),m_fTv(v) {}
    
    };
    
    class C2DOverlay
    {
       TLVertex      m_pVerts[4];
       IDirect3DTexture9      *m_pTexture;
       IDirect3DDevice9        *m_pDevice;
    
       float m_fSize;
       
       public:
          C2DOverlay(void):m_pTexture(NULL),m_pDevice(NULL),m_fSize(0.0f) {}
          ~C2DOverlay(void)
         {
            //If we sucessfully allocated an object from COM, hand it back now
            if (Texture) Texture->Release();
            Texture=NULL;
         }
    
         void Create(IDirect3DDevice9 *_device,float _size);
         void Render(float _timeDelta);
    };
    
    #endif
    Code:
    #include "C2DOverlay.h"
    
    const DWORD TLVertex::FVF=D3DFVF_XYZRHW | D3DFVF_TEX1;
    
    void C2DOverlay::Create(IDirect3DDevice9 *_device,float _size,std::string _TextureFile)
    {
        m_pDevice=_device;
        m_fSize=_size;
    
        float size2=m_fSize*.5f;
    
        m_pVerts[0]=TLVertex(D3DXVECTOR3(-size2,-size2,0.0f,0.0f,0.0f);
        m_pVerts[1]=TLVertex(D3DXVECTOR3(size2,-size2,0.0f,1.0f,0.0f);
        m_pVerts[2]=TLVertex(D3DXVECTOR3(-size2,size2,0.0f,0.0f,1.0f);
        m_pVerts[3]=TLVertex(D3DXVECTOR3(size2,size2,0.0f,1.0f,1.0f);
    
        if (FAILED(D3DXCreateTextureFromFile(m_pDevice,_TextureFile.c_str(),&m_pTexture)))
       {
           MessageBox(0,0,"C2DOverlay::Create() - Failed to load texture");
           return;
       }
    }
    
    void C2DOverlay::Render(float _timeDelta)
    {
       //Insert code to translate overlay here
       //.....
       //.....
    
       //Render overlay
       m_pDevice->SetTexture(0,&m_pTexture);
       m_pDevice->SetFVF(TLVertex::FVF);
       m_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2 ,m_pVerts,sizeof(TLVertex));
    }
    That should do the trick. Render your vertices as XYZRHW. This tells Direct3D NOT to send the vertices down the rendering pipeline. In essence you are saying 'I've already transformed these vertexes to screen space....don't touch them.' What this translates to is that the x and y components of your vertices are rendered as is - in screen space - just like in a normal 2D game. So it's fairly straightforward how to move the object around in screen space so I omitted that code.

    You can also use irregular shaped textures by using the correct alpha blending states.

    m_pDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA );
    m_pDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALP HA);

    You probably don't want to do this in each object's render function as it would be too slow. Turn alpha blending on, render all objects that use that type of alpha blending and then turn it off if need be or simply change the blending states to do different types of alpha blending.

    Hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 07:09 AM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Another Quick Question....I'm a really curious Person
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-02-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21