Struggling with Vertex Shaders

I am learning to render objects with vertex shaders after just getting the hang of rendering objects with the fixed pipeline. I successfully have been able to render the same objects through using a shader, but the problem I have now is that the objects are not transforming to the new positions when I update the world matrix.

This is a part of my Render function focused on the shader:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//setup the shader
m_pEffect->SetTechnique(m_pTech);
UINT numPasses = 0;
m_pEffect->Begin(&numPasses, 0);
for( UINT i = 0; i < numPasses; ++i) {

	m_pEffect->BeginPass(i);


	//setup the camera
	m_pEffect->SetMatrix("worldViewProjMat", &(m_V*m_proj));

		

	/* first cube */
	m_pD3DDevice->SetStreamSource(0, VertexBuffer, 0, sizeof(Vertex));
	m_pD3DDevice->SetIndices(IndexBuffer);
	m_pD3DDevice->SetVertexDeclaration(VertexDecl);

	m_pEffect->SetTexture("tex", m_pTexture[0]);

	D3DXMATRIX Trans;
	D3DXMATRIX RotY;
	D3DXMATRIX Scale;
	D3DXMATRIX World;
	D3DXMatrixIdentity(&Trans);
	D3DXMatrixIdentity(&RotY);
	D3DXMatrixIdentity(&Scale);
	D3DXMatrixIdentity(&World);
	D3DXMatrixTranslation(&Trans, 6.0f, 0.0f, 0.0f);
	D3DXMatrixRotationY(&RotY, D3DXToRadian(timeGetTime() * 0.1f));
	D3DXMatrixScaling(&Scale, 1.0f, 1.0f, 1.0f);
	D3DXMatrixMultiply(&Scale, &Scale, &RotY);
	D3DXMatrixMultiply(&World, &Scale, &Trans);
	m_pEffect->SetMatrix("worldMat", &World);
		

	//allows changing a shader effect during a single pass
	m_pEffect->CommitChanges();

	//draws cube with applied transformations
	m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);


	/* second cube */
	m_pD3DDevice->SetStreamSource(0, VertexBuffer, 0, sizeof(Vertex));
	m_pD3DDevice->SetIndices(IndexBuffer);
	m_pD3DDevice->SetVertexDeclaration(VertexDecl);

	m_pEffect->SetTexture("tex", m_pTexture[2]);


	D3DXMATRIX Trans2;
	D3DXMATRIX RotY2;
	D3DXMATRIX Scale2;
	D3DXMATRIX World2;
	D3DXMatrixIdentity(&Trans2);
	D3DXMatrixIdentity(&RotY2);
	D3DXMatrixIdentity(&Scale2);
	D3DXMatrixIdentity(&World2);
	D3DXMatrixTranslation(&Trans2, -6.0f, 0.0f, 0.0f);
	D3DXMatrixRotationY(&RotY2, -D3DXToRadian(timeGetTime() * 0.1f));
	D3DXMatrixScaling(&Scale2, 1.0f, 1.0f, 1.0f);
	D3DXMatrixMultiply(&Scale2, &Scale2, &RotY2);
	D3DXMatrixMultiply(&World2, &Scale2, &Trans2);
	m_pEffect->SetMatrix("worldMat", &World2);

	//allows changing a shader effect during a single pass
	m_pEffect->CommitChanges();

	//draws cube with applied transformations
	m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);


	/* controllable pyramid */
	m_pD3DDevice->SetStreamSource(0, VertexBuffer2, 0, sizeof(Vertex));
	m_pD3DDevice->SetIndices(IndexBuffer2);
	m_pD3DDevice->SetVertexDeclaration(VertexDecl);

	m_pEffect->SetTexture("tex", m_pTexture[2]);

	//transformations that happen in Update() affect here
	m_pEffect->SetMatrix("worldMat", &World3);

	//allows changing a shader effect during a single pass
	m_pEffect->CommitChanges();

	//draw pyramid with applied transformations
	m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 16, 0, 6);


	m_pEffect->EndPass();
}


All three primitives are currently being drawn on top of each other (it looks like a Christmas tree), but I want the two cubes to transform from the origin position to the new coordinates I put in the World matrix. What is wrong with my logic here?
Last edited on
I figured out how to translate the rendered objects with a shader through looking through the examples provided on chapter 8 of this book: Introduction To 3D Game Programming With Directx 9.0C: A Shader Approach.

Only thing I do not understand now is how to position the camera.
Topic archived. No new replies allowed.