Public Group

Learning DirectX yet can't find an answer to a problem

This topic is 3845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey guys, this is my first (and hopefully not last) post on GD. I thought this would be the best place to turn to. First of all, I would probably say I'm a beginner/intermediate C++ programmer, I'm relatively experienced. However, I've recently started learning DirectX9 for the first time, I haven't got a problem with the code, I understand that fine. It's just the coordinate system that DirectX uses. I've been making triangles and quads fine, but I just don't understand how to color both sides of the polygon. For example, here's the code I'm using from directxtutorial.com // left gun { 3.2f, -1.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), }, { 3.2f, -1.0f, 11.0f, D3DCOLOR_XRGB(0, 255, 0), }, { 2.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(255, 0, 0), }, ... // opposite side of left gun { 3.2f, -1.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), }, { 2.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(255, 0, 0), }, { 3.2f, -1.0f, 11.0f, D3DCOLOR_XRGB(0, 255, 0), }, I've read that the vertices should be declared clockwise but I just can't visualise how the above code is set out clockwise. /confusing rant Lol any help will be greatly appreciated. Ray

Share on other sites
When they say clockwise, they generally mean from the point of view of the camera. If you have a 3D object, this means that is all the triangles are ordered that way, only the side visible to the camera get drawn. This can help with performance, as generally you won't be wasting you time drawing the triangles that are on the back side of an object. If you do have a 2D object or something that you want both side to be drawn, you can turn off or even reverse the culling. If you use the line (IIRC, I am a slave to intellisense these days):

device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

you can turn off culling so both sides will be drawn. You can also change the direction to CW or CCW by using D3DCULL_CW and D3DCULL_CCW respectively. It is usually a bit easier to understand if you are rendering the triangle in 2D directly facing your camera.

Share on other sites
CW or CCW is relative to the viewpoint. Imagine that you're drawing a triangle CW and you're standing directly in front of it, now slowly move around behind it and with the same order of drawing it, it's now drawn in CCW order. Rendering pipeline use this to tell whether polygons are facing away from the camera, if they are then they get cull out.

Share on other sites
Ok, so it's from the camera's viewpoint, it's still confusing me a bit. Well I know what culling is, but haven't "learnt" about it in the tutorials. The thing that's getting to me is well, if you've got the camera pointing at 0, 0, 0. How would you work out what is clockwise if you've got two vertices with the same x and y position but different z position.

Sorry about all the questions, I realise this is probably easier than I think.

Ray

Share on other sites
Quote:
 Original post by raydeyHow would you work out what is clockwise if you've got two vertices with the same x and y position but different z position.

It would depend where the third vertex of the triangle is located.

[Edited by - MJP on July 14, 2008 10:13:39 AM]

Share on other sites
Well, what I'm currently experimenting with is just adding a tail fin to this amateurish spaceship. I'm using a single triangle for the fin but nothing's showing up. My camera is at position 0, 0, 30 and it's facing 0, 0, 0. The fin's coordinates are at (0, 0, 0) (0, 5, -5) and (0, 3, -3). Since there are only changes in the y and z directions and the viewpoint is directly along the z-axis, how would I work out what clockwise is relevant to these 3 points?

Ray

Any ideas?

Share on other sites
If you are looking directly down the Z-axis, the triangle would just be a line (if I am understanding this correctly). I'm not 100% how DX handles culling in this situation. If you rotate the camera or the world matrix in either direction (around the y axis), does the triangle show up? Is everything else being drawn correctly?

Edit: Can you post the triangle creation code as well as the matrix creation/rendering code?

Share on other sites
That's exactly what I'm interested in finding out. Before I tried putting a quad as the tail fin, it *partly* showed up. Then when I switched to a single triangle, nothing showed up, apart from a single vertex flickering when it rotated about the y-axis.

Here's the vertex buffer code:

CUSTOMVERTEX OurVertices[] =
{
// fuselage
{ 3.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 0.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 0.0f, 0.0f, 10.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ -3.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },

// THIS IS THE MAIN BIT I WOULD LIKE EXPLANATION ON
// tail fin
{ 0.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 0.0f, 0.5f, -5.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 0.0f, 4.0f, -6.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 0.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), },

// opp side of tail fin
{ 0.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 0.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 0.0f, 4.0f, -6.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 0.0f, 0.5f, -5.0f, D3DCOLOR_XRGB(0, 255, 255), },

// left gun
{ 3.2f, -1.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 3.2f, -1.0f, 11.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 2.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(255, 0, 0), },

// right gun
{ -3.2f, -1.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ -3.2f, -1.0f, 11.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ -2.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(255, 0, 0), },

// bottom of fuselage
{ 0.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 3.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ -3.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 0.0f, 0.0f, 10.0f, D3DCOLOR_XRGB(255, 0, 0), },

// opposite side of left gun
{ 3.2f, -1.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 2.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 3.2f, -1.0f, 11.0f, D3DCOLOR_XRGB(0, 255, 0), },

// opposite side of right gun
{ -3.2f, -1.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ -2.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ -3.2f, -1.0f, 11.0f, D3DCOLOR_XRGB(0, 255, 0), }
};

d3ddev->CreateVertexBuffer(28*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_DEFAULT,
&t_buffer,
NULL);

And here's the rendering code:

void render_frame(void)
{
// clear the window to a deep blue
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);

// begin the 3d scene
d3ddev->BeginScene();

// do 3d rendering on the backbuffer here

d3ddev->SetFVF(CUSTOMFVF);

// SET UP THE 3D PIPELINE //

// the view transform matrix
D3DXMATRIX matView;

D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 0.0f, 30.0f), &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));

d3ddev->SetTransform(D3DTS_VIEW, &matView);

// the projection transform matrix
D3DXMATRIX matProjection;

(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,
1.0f,
300.0f);

d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);

// the world transform matrix

D3DXMATRIX matRotateY;
static float index = 0.0f;
index += 0.03f;
D3DXMatrixRotationY(&matRotateY, index);
d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);

d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));

// draw primitives

d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); // fuselage
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2); // tail fin
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2); // opp side of tail
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 1); // left gun
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 15, 1); // right gun
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 18, 2); // bottom of fuselage
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 22, 1); // opp side of left
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 25, 1); // opp side of right

// ends the 3d scene
d3ddev->EndScene();

// displays the created frame
d3ddev->Present(NULL, NULL, NULL, NULL);

return;
}

EDIT: I thought a couple of screenshots to show what happened might be useful.

One side:

Other side:

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• Forum Statistics

• Total Topics
634134
• Total Posts
3015751
×