Struct problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Struct problem

Hybrid View

  1. #1
    Join Date
    Jan 2011
    Location
    Athens, GR
    Posts
    3

    Question Struct problem

    I'm using C++ and DirectX.
    I want to make a tunnel in which the camera moves into the screen. It won't be a large tunnel, this is just for my game intro. This is the code I first wrote:



    // global declarations
    LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
    LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
    LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer

    //texture declerations
    LPDIRECT3DTEXTURE9 texture_1; // the wall texture

    struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR; FLOAT U, V;};
    #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

    // this is the function used to render a single frame
    void render_frame(void)
    {
    static float movement = 0.0f;

    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    // select which vertex format we are using
    d3ddev->SetFVF(CUSTOMFVF);

    // SET UP THE PIPELINE

    D3DXMATRIX matView; // the view transform matrix

    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 0.0f, movement), // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, movement+10.0f), // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction

    movement+=0.1f;

    d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView

    D3DXMATRIX matProjection; // the projection transform matrix

    D3DXMatrixPerspectiveFovLH(&matProjection,
    D3DXToRadian(45), // the horizontal field of view
    (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
    1.0f, // the near view-plane
    100.0f); // the far view-plane

    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection

    // select the vertex buffer to display
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

    // set the texture
    d3ddev->SetTexture(0, texture_1);

    // copy the vertex buffer to the back buffer
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2);

    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 24, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 28, 2);

    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);

    return;
    }

    // this is the function that puts the 3D models into video RAM
    void init_graphics(void)
    {
    // load the texture we will use

    D3DXCreateTextureFromFile(d3ddev,
    L"wall.png",
    &texture_1);

    // create the vertices using the CUSTOMVERTEX struct
    struct CUSTOMVERTEX vertices[]=
    {
    //tunnel #1
    //left wall
    { -3.0f, 3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255),1 , 0,},
    { -3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255),0 , 0,},
    { -3.0f, -3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
    { -3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1},

    //ceiling
    { -3.0f, 3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
    { 3.0f, 3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 0 , 0,},
    { -3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
    { 3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},

    //right wall
    { 3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
    { 3.0f, 3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0,},
    { 3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
    { 3.0f, -3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},

    //floor
    { -3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
    { 3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0,},
    { -3.0f, -3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
    { 3.0f, -3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},

    //tunnel #2
    //left wall
    { -3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255),1 , 0,},
    { -3.0f, 3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255),0 , 0,},
    { -3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
    { -3.0f, -3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1},

    //ceiling
    { -3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
    { 3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0 , 0,},
    { -3.0f, 3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
    { 3.0f, 3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},

    //right wall
    { 3.0f, 3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
    { 3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0,},
    { 3.0f, -3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
    { 3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},

    //floor
    { -3.0f, -3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
    { 3.0f, -3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0,},
    { -3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
    { 3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},

    };

    // create a vertex buffer interface called v_buffer
    d3ddev->CreateVertexBuffer(32*sizeof(CUSTOMVERTEX),
    0,
    CUSTOMFVF,
    D3DPOOL_MANAGED,
    &v_buffer,
    NULL);

    VOID* pVoid; // a void pointer

    // lock v_buffer and load the vertices into it
    v_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    v_buffer->Unlock();

    return;
    }



    As you can see, I seperated the vertices of the tunnel into 2 tunnels, because the texture would stretch too much if I had one tunnel of 100.0 depth. However, it is still somewhat stretched. That's why I decided to make the tunnel a combination of multiple small tunnels. I also wanted to make it a bit longer. So, I changed the code as follows:



    // global declarations
    LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
    LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
    LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer

    //texture declerations
    LPDIRECT3DTEXTURE9 texture_1; // the wall texture

    struct CUSTOMVERTEX
    {
    CUSTOMVERTEX(FLOAT fx, FLOAT fy, FLOAT fz, DWORD dwcolor, FLOAT fu, FLOAT fv) :
    X(fx), Y(fy), Z(fz), COLOR(dwcolor), U(fu), V(fv) {}
    FLOAT X, Y, Z;
    DWORD COLOR;
    FLOAT U, V;
    };

    #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

    // this is the function used to render a single frame
    void render_frame(void)
    {
    static float movement = 0.0f;

    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    // select which vertex format we are using
    d3ddev->SetFVF(CUSTOMFVF);

    // SET UP THE PIPELINE
    D3DXMATRIX matView; // the view transform matrix

    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 0.0f, movement), // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, movement+10.0f), // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction

    movement+=0.1f;

    d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView

    D3DXMATRIX matProjection; // the projection transform matrix

    D3DXMatrixPerspectiveFovLH(&matProjection,
    D3DXToRadian(45), // the horizontal field of view
    (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
    1.0f, // the near view-plane
    100.0f); // the far view-plane

    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection

    // select the vertex buffer to display
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

    // set the texture
    d3ddev->SetTexture(0, texture_1);

    // copy the vertex buffer to the back buffer
    int i;
    for (i=0;i<40;i++)
    {
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, i*4, 2);
    }


    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);

    return;
    }

    // this is the function that puts the 3D models into video RAM
    void init_graphics(void)
    {
    // load the texture we will use

    D3DXCreateTextureFromFile(d3ddev,
    L"wall.png",
    &texture_1);

    float depth = 10.0f;
    struct CUSTOMVERTEX* vertices[160];

    // create the vertices using the CUSTOMVERTEX struct
    int i;
    for (i=0;i < 145;i+=16)
    {
    //left wall
    vertices[i] = new CUSTOMVERTEX(-3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255),1 , 0);

    vertices[i+1] = new CUSTOMVERTEX(-3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255),0 , 0);
    vertices[i+2] = new CUSTOMVERTEX(-3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
    vertices[i+3] = new CUSTOMVERTEX(-3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1);

    //ceiling
    vertices[i+4] = new CUSTOMVERTEX(-3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
    vertices[i+5] = new CUSTOMVERTEX(3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0 , 0);
    vertices[i+6] = new CUSTOMVERTEX(-3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
    vertices[i+7] = new CUSTOMVERTEX(3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1);

    //right wall
    vertices[i+8] = new CUSTOMVERTEX(3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
    vertices[i+9] = new CUSTOMVERTEX(3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 0);
    vertices[i+10] = new CUSTOMVERTEX(3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
    vertices[i+11] = new CUSTOMVERTEX(3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 1);

    //floor
    vertices[i+12] = new CUSTOMVERTEX(-3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
    vertices[i+13] = new CUSTOMVERTEX(3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0);
    vertices[i+14] = new CUSTOMVERTEX(-3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
    vertices[i+15] = new CUSTOMVERTEX(3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 1);

    depth +=25.0f;
    }

    // create a vertex buffer interface called v_buffer
    d3ddev->CreateVertexBuffer(160*sizeof(CUSTOMVERTEX),
    0,
    CUSTOMFVF,
    D3DPOOL_MANAGED,
    &v_buffer,
    NULL);

    VOID* pVoid; // a void pointer

    // lock v_buffer and load the vertices into it
    v_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    v_buffer->Unlock();

    return;
    }



    I have left out the code that has to do with the window and the D3D initializations and I've only included the functions that I have made changes in.
    My first try (with the 2 seperate tunnels) worked just fine. But the second code just gives an empty window. One of my assumptions is it may have to do with the fact that in the second code "vertices" is a pointer. But it threw an error if i just declared a variable.

    Anyone knows what the problem is?

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Struct problem

    What is the error? And please edit your post, and add code tags.

    Viggy

  3. #3
    Join Date
    Jan 2011
    Location
    Athens, GR
    Posts
    3

    Re: Struct problem

    I'm sorry about that.
    There's no error but while with the first code it runs as expected, the second one just gives a blank (black) window.


    Code:
      // global declarations
      LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
      LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
      LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer
       
      //texture declerations
      LPDIRECT3DTEXTURE9 texture_1; // the wall texture
       
      struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR; FLOAT U, V;};
      #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
       
      // this is the function used to render a single frame
      void render_frame(void)
      {
      static float movement = 0.0f;
       
      d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
      d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
       
      d3ddev->BeginScene();
       
      // select which vertex format we are using
      d3ddev->SetFVF(CUSTOMFVF);
       
      // SET UP THE PIPELINE
       
      D3DXMATRIX matView; // the view transform matrix
       
      D3DXMatrixLookAtLH(&matView,
      &D3DXVECTOR3 (0.0f, 0.0f, movement), // the camera position
      &D3DXVECTOR3 (0.0f, 0.0f, movement+10.0f), // the look-at position
      &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
       
      movement+=0.1f;
       
      d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
       
      D3DXMATRIX matProjection; // the projection transform matrix
       
      D3DXMatrixPerspectiveFovLH(&matProjection,
      D3DXToRadian(45), // the horizontal field of view
      (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
      1.0f, // the near view-plane
      100.0f); // the far view-plane
       
      d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
       
      // select the vertex buffer to display
      d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
       
      // set the texture
      d3ddev->SetTexture(0, texture_1);
       
      // copy the vertex buffer to the back buffer
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2);
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2);
       
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2);
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2);
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 24, 2);
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 28, 2);
       
      d3ddev->EndScene();
       
      d3ddev->Present(NULL, NULL, NULL, NULL);
       
      return;
      }
       
      // this is the function that puts the 3D models into video RAM
      void init_graphics(void)
      {
      // load the texture we will use
       
      D3DXCreateTextureFromFile(d3ddev,
      L"wall.png",
      &texture_1);
       
      // create the vertices using the CUSTOMVERTEX struct
      struct CUSTOMVERTEX vertices[]=
      {
      //tunnel #1
      //left wall
      { -3.0f, 3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255),1 , 0,},
      { -3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255),0 , 0,},
      { -3.0f, -3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
      { -3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1},
       
      //ceiling
      { -3.0f, 3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
      { 3.0f, 3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 0 , 0,},
      { -3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
      { 3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},
       
      //right wall
      { 3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
      { 3.0f, 3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0,},
      { 3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
      { 3.0f, -3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},
       
      //floor
      { -3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
      { 3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0,},
      { -3.0f, -3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
      { 3.0f, -3.0f, 10.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},
       
      //tunnel #2
      //left wall
      { -3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255),1 , 0,},
      { -3.0f, 3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255),0 , 0,},
      { -3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
      { -3.0f, -3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1},
       
      //ceiling
      { -3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
      { 3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0 , 0,},
      { -3.0f, 3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
      { 3.0f, 3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},
       
      //right wall
      { 3.0f, 3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
      { 3.0f, 3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0,},
      { 3.0f, -3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
      { 3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},
       
      //floor
      { -3.0f, -3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0,},
      { 3.0f, -3.0f, 100.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0,},
      { -3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1,},
      { 3.0f, -3.0f, 55.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1,},
       
      };
       
      // create a vertex buffer interface called v_buffer
      d3ddev->CreateVertexBuffer(32*sizeof(CUSTOMVERTEX),
      0,
      CUSTOMFVF,
      D3DPOOL_MANAGED,
      &v_buffer,
      NULL);
       
      VOID* pVoid; // a void pointer
       
      // lock v_buffer and load the vertices into it
      v_buffer->Lock(0, 0, (void**)&pVoid, 0);
      memcpy(pVoid, vertices, sizeof(vertices));
      v_buffer->Unlock();
       
      return;
      }
    And this is what I'm trying now:

    Code:
        // global declarations
      LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
      LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
      LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer
       
      //texture declerations
      LPDIRECT3DTEXTURE9 texture_1; // the wall texture
       
      struct CUSTOMVERTEX
      {
      CUSTOMVERTEX(FLOAT fx, FLOAT fy, FLOAT fz, DWORD dwcolor, FLOAT fu, FLOAT fv) :
      X(fx), Y(fy), Z(fz), COLOR(dwcolor), U(fu), V(fv) {}
      FLOAT X, Y, Z;
      DWORD COLOR;
      FLOAT U, V;
      };
       
      #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
       
      // this is the function used to render a single frame
      void render_frame(void)
      {
      static float movement = 0.0f;
       
      d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
      d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
       
      d3ddev->BeginScene();
       
      // select which vertex format we are using
      d3ddev->SetFVF(CUSTOMFVF);
       
      // SET UP THE PIPELINE
      D3DXMATRIX matView; // the view transform matrix
       
      D3DXMatrixLookAtLH(&matView,
      &D3DXVECTOR3 (0.0f, 0.0f, movement), // the camera position
      &D3DXVECTOR3 (0.0f, 0.0f, movement+10.0f), // the look-at position
      &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
       
      movement+=0.1f;
       
      d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
       
      D3DXMATRIX matProjection; // the projection transform matrix
       
      D3DXMatrixPerspectiveFovLH(&matProjection,
      D3DXToRadian(45), // the horizontal field of view
      (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
      1.0f, // the near view-plane
      100.0f); // the far view-plane
       
      d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
       
      // select the vertex buffer to display
      d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
       
      // set the texture
      d3ddev->SetTexture(0, texture_1);
       
      // copy the vertex buffer to the back buffer
      int i;
      for (i=0;i<40;i++)
      {
      d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, i*4, 2);
      }
       
       
      d3ddev->EndScene();
       
      d3ddev->Present(NULL, NULL, NULL, NULL);
       
      return;
      }
       
      // this is the function that puts the 3D models into video RAM
      void init_graphics(void)
      {
      // load the texture we will use
       
      D3DXCreateTextureFromFile(d3ddev,
      L"wall.png",
      &texture_1);
       
      float depth = 10.0f;
      struct CUSTOMVERTEX* vertices[160];
       
      // create the vertices using the CUSTOMVERTEX struct
      int i;
      for (i=0;i < 145;i+=16)
      {
      //left wall
      vertices[i] = new CUSTOMVERTEX(-3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255),1 , 0);
       
      vertices[i+1] = new CUSTOMVERTEX(-3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255),0 , 0);
      vertices[i+2] = new CUSTOMVERTEX(-3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
      vertices[i+3] = new CUSTOMVERTEX(-3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1);
       
      //ceiling
      vertices[i+4] = new CUSTOMVERTEX(-3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
      vertices[i+5] = new CUSTOMVERTEX(3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0 , 0);
      vertices[i+6] = new CUSTOMVERTEX(-3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
      vertices[i+7] = new CUSTOMVERTEX(3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1);
       
      //right wall
      vertices[i+8] = new CUSTOMVERTEX(3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
      vertices[i+9] = new CUSTOMVERTEX(3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 0);
      vertices[i+10] = new CUSTOMVERTEX(3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
      vertices[i+11] = new CUSTOMVERTEX(3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 1);
       
      //floor
      vertices[i+12] = new CUSTOMVERTEX(-3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
      vertices[i+13] = new CUSTOMVERTEX(3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0);
      vertices[i+14] = new CUSTOMVERTEX(-3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
      vertices[i+15] = new CUSTOMVERTEX(3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 1);
       
      depth +=25.0f;
      }
       
      // create a vertex buffer interface called v_buffer
      d3ddev->CreateVertexBuffer(160*sizeof(CUSTOMVERTEX),
      0,
      CUSTOMFVF,
      D3DPOOL_MANAGED,
      &v_buffer,
      NULL);
       
      VOID* pVoid; // a void pointer
       
      // lock v_buffer and load the vertices into it
      v_buffer->Lock(0, 0, (void**)&pVoid, 0);
      memcpy(pVoid, vertices, sizeof(vertices));
      v_buffer->Unlock();
       
      return;
      }
    As I said before, I've left out the code that has to do with the window and d3d initializations.

  4. #4
    Join Date
    Jan 2011
    Location
    Athens, GR
    Posts
    3

    Re: Struct problem

    Ok, I found out myself.
    Actually, I thought that texture coordinates could only be between 0 and 1 but I found out that if you give coordinates more than 1 the texture is tiled on the primitive. So no need for more than 1 tunnels.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center