basic directx question from an opengl background

Started by
5 comments, last by jollyjeffers 17 years, 6 months ago
hi all, this is what im trying to do. I want a pgn file to be the background to my game. So i created a square and placed it so it took up the whole screen and then mapped a the png onto it. This worked fine. Now what I want to do is add a cylinder into the world. Im using the inbuilt D3DXCreateCylinder api for it. I then want the cylinder to rotate 90 degrees in the Y axis and spin in the Z axis. Problem is that instead of just rotating the cylinder it is also rotating the background (its also mapping the background texture onto the cylinder as well which is undesirable). I used to achieve this in opengl using the PushMatrix() and PopMatrix() functions. However I dont know how to do this in DirectX. This is the code:
[SOURCE]

void render_spin(void);


// this is the function used to render a single frame
void render_frame(void)
{
    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 TRANSFORMS

    D3DXMATRIX matView;    // the view transform matrix
    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 0.0f, 25.0f),    // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),      // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView

    D3DXMATRIX matProjection;    // the projection transform matrix
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               D3DXToRadian(22),    // the horizontal field of view
                               SCREEN_WIDTH / SCREEN_HEIGHT,    // the 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, t_buffer, 0, sizeof(CUSTOMVERTEX));

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

    // draw the textured square
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

	render_spin();
    d3ddev->EndScene(); 

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

    return;
}


void render_spin(void){

	static float index = 0.0f; index+=0.03f;    // an ever-increasing float value
    D3DXMATRIX matRotateY90;
	D3DXMatrixRotationY(&matRotateY90, D3DXToRadian(90.0f));  
	D3DXMATRIX matRotateZ;    // a matrix to store the rotation for each triangle
    D3DXMatrixRotationZ(&matRotateZ, index);    // the rotation matrix
    d3ddev->SetTransform(D3DTS_WORLD, &(matRotateZ * matRotateY90));    // set the world transform

    // draw the cyl
    meshCylinder->DrawSubset(0);
}

[/SOURCE]
[/source][/source] Thanks for any help
Advertisement
Your problem is that when you apply the world matrix to make the cylinder spin, you don't "zero" the matrix back out to render the background. Your code should look something like this:

//Setup Rotation Matrix//Render Cylinder//Setup Identity Matrix  D3DXMATRIX world;D3DXMatrixIdentity(&world);d3ddev->SetTransform(D3DTS_WORLD, &world);//Render Background
I understand and now it works, thanks for the reply. However how do i stop the texture from being mapped on to it?

Thanks
Comment out the d3ddev->SetTexture() call or set the second argument to NULL. If you want wireframe use d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME).
Anthony Rufrano
RealityFactory 2 Programmer
But then the background texture disappears. I want the background texture to stay but i dont want it applied to the cylinder. Thanks
I managed to get it working. Is there anyway that I can choose which objects on the screen is affected by light and which isnt? Thanks
Quote:Original post by Anonymous Poster
Is there anyway that I can choose which objects on the screen is affected by light and which isnt?
If you're using fixed-function, then toggle D3DRS_LIGHTING between calls as appropriate or alter the IDirect3DDevice9::LightEnable() call. For HW T&L you can have any number of lights set to the device, just only 8 active ones...


hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement