I have just started using DirectX with C++.I have been following DirectX tutorial. I have to draw multiple rectangles on the screen. I have created an array to store the 4 vertices of rectangle. This array of 4 vertices are placed in each index of another newly created array. This array is passed to the VectorBuffer. The VectorBuffer could not calculate the correct size of array passed to it.Due to this when render frame is called nothing can be seen on the screen. How to store multiple objects in single vector buffer.
My code is :
CUSTOMVERTEX** Array = new CUSTOMVERTEX*[2];
CUSTOMVERTEX OurVertices1[] = {
{ 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), }, //meaning x,y,z,RHW,Dword
{ 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
};
CUSTOMVERTEX OurVertices2[] = {
{ 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
};
Array[0] = OurVertices1;// placing vertices in array 0 index
Array[1] = OurVertices2; // placing vertices in array 1 index
d3ddev->CreateVertexBuffer(8* sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // the void* we were talking about
v_buffer->Lock(0, 0, (void**)&pVoid, 0); // locks v_buffer, the buffer we made earlier
memcpy(pVoid, Array,sizeof(Array)); // passing array to vbuffer
v_buffer->Unlock(); // unlock v_buffer
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
//render_frame
void render_frame(LPDIRECT3DDEVICE9 d3ddev, LPDIRECT3DVERTEXBUFFER9 v_buffer)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
// select which vertex format we are using
// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->SetFVF(CUSTOMFVF);
// copy the vertex buffer to the back buffer
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
// copy the vertex buffer to the back buffer
//d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
But could not get two object on the screen. Is there any other method to place multiple object in vector buffer?
You are creating buffer to hold 8 vertex structs, but then store there just a single pointer by calling memcpy(pVoid, Array, sizeof(Array));. Note that Array is just a plain pointer, despite of its name. You should initialize vertex buffer with vertex data:
CUSTOMVERTEX OurVertices[] =
{
// 1
{ 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), }, //meaning x,y,z,RHW,Dword
{ 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
// 2
{ 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
};
memcpy(pVoid, OurVertices, sizeof(OurVertices));
Or you can just initialize those vertices in-place by casting static_cast<CUSTOMVERTEX *>(pVoid).
Related
sorry if I'm not specific enough, I'm new to DirectX programming and to Stack Overflow.
The problem is that when I'm rendering 3D objects in DirectX9, I have to manually add the Vertex Buffers and the Index Buffers (draw the cubes by hand). I'm trying to recreate a 'Minecraft' like world but don't know how I would go about it.
I've tried using 'for loops' but that just glitched the entire screen out. I've also attempted using vectors but I just got way to confused and didn't get anywhere.
This is the code I have. As you can see, this is the way I'd draw 2 cubes. I'm wondering if there is another more efficient way since I'd be wanting to draw hundreds of cubes at a time.
void DirectXClass::init_graphics(void)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX vertices[] =
{
// Square 1
{ 3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ -3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ -3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ -3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ -3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ -3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ -3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ -3.0f, 3.0f, 9.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 3.0f, 3.0f, 9.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ -3.0f, -3.0f, 9.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 3.0f, -3.0f, 9.0f, D3DCOLOR_XRGB(0, 255, 255), },
};
// create a vertex buffer interface called v_buffer
// d3ddev->CreateVertexBuffer(sizeof(vertices) / sizeof(vertices[0]) * sizeof(CUSTOMVERTEX),
d3ddev->CreateVertexBuffer(sizeof(vertices) / sizeof(vertices[0]) * sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID** pVoid;
// 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();
short indices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 0, 6, // side 2
6, 0, 2,
7, 5, 6, // side 3
6, 5, 4,
3, 1, 7, // side 4
7, 1, 5,
4, 5, 0, // side 5
0, 5, 1,
3, 7, 2, // side 6
2, 7, 6,
8, 9, 10, // side 9
10, 9, 11,
12, 8, 14, // side 10
14, 8, 10,
15, 13, 14, // side 11
14, 13, 12,
11, 9, 15, // side 12
15, 9, 13,
12, 13, 8, // side 13
8, 13, 9,
11, 15, 10, // side 14
10, 15, 14,
};
// create a vertex buffer interface called v_buffer
// d3ddev->CreateIndexBuffer(sizeof(indices) / sizeof(indices[0]) * sizeof(short),
d3ddev->CreateIndexBuffer(sizeof(indices) / sizeof(indices[0]) * sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&i_buffer,
NULL);
// lock v_buffer and load the vertices into it
i_buffer->Lock(0, 0, (void**)& pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
i_buffer->Unlock();
}
I have been following this tutorial of DirectX http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-5. I have just started DirectX with C++. I have drawn two rectangles on the screen but cannot translate them.Th rectangles have different position on screen and are stored in Array OurVertices. I have been reading from the tutorial and it says that you first have to apply world transformation to translate it in 3d but I dont want to get into 3d because I have two simple 2d rectangles. How you move simple 2d rectangles in DirectX C++. If I apply World Transformation using the tutorial I dont know why my camera position is tilted. My code is given below:
CUSTOMVERTEX OurVertices[] =
{
// 1
{ 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), }, //meaning x,y,z,Dword
{ 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
};
d3ddev->CreateVertexBuffer(8 * sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // the void* we were talking about
v_buffer->Lock(0, 0, (void**)&pVoid, 0); // locks v_buffer, the buffer we made earlier
memcpy(pVoid, OurVertices, sizeof(OurVertices));
v_buffer->Unlock(); // unlock v_buffer
d3ddev->SetFVF(CUSTOMFVF);
void render_frame()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
// select which vertex format we are using
// select the vertex buffer to display
d3ddev->SetFVF(CUSTOMFVF);
// SET UP THE PIPELINE
D3DXMATRIX matTranslate;
static float index = 0.0f;
index += 0.01f; // an ever-increasing float value
// build a matrix to rotate the model based on the increasing float value
D3DXMatrixTranslation(&matTranslate, 0, index , 0.0f);
// tell Direct3D about our matrix
d3ddev->SetTransform(D3DTS_WORLD, &matTranslate);
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3(0.0f, 0.0f, 10.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(100), // the horizontal field of view
(FLOAT)800 / (FLOAT)600, // 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));
// copy the vertex buffer to the back buffer
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
Is there any way to translate two rectangles?
You are already working in 3D, when you specify the position of your vertices you have an extra z component wich is set to 0 at the moment. You can change it and see the effect on your scene.
{ 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), }, //meaning x,y,z,Dword
{ 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
So now each frame you are translating your vertices by an increasing amount on the y axis.
static float index = 0.0f;
index += 0.01f; // an ever-increasing float value
// build a matrix to rotate the model based on the increasing float value
D3DXMatrixTranslation(&matTranslate, 0, index , 0.0f);
If you print index each frame you will see him growing and you use this value to to build a translation matrix which will be apply before view/projection.
I don't know what is the effect you want.
But all the vertices of your scene will go from bottom to top, which you can look like your camera is going down.
I am learning DirectX and trying to make sure I understand what the functions are doing before I move on to creating an index buffer (which is why I am using repetitive Vertices instead of specific indices). I am trying to render a square but I only have one triangle displaying. I am sure that it is my misunderstanding of the winding order or offsetting the values when passing to the shader but I cant find the issue. Below is the current relevant code and the result at runtime.
void RenderFrame(void) {
// Clear BackBuffer to a color
devContext->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
// select which vertex buffer to display
UINT stride = sizeof(Vertex);
UINT offset = 0;
devContext->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
// select which primtive type we are using
devContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELST);
// draw the vertex buffer to the back buffer
devContext->Draw(6, 0);
// swap buffers
swapchain->Present(0, 0);
}
void ParseGraphics() {
// Create a triangle with the Vertex Struct
Vertex square[] =
{
{ 0.2f, 0.5f, 0.5f, D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f) }, //top-right
{ 0.2f, -0.5f, 0.5f, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f) }, //bottom-right
{ -0.2f, -0.5f, 0.5f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) }, //bottom-left
{ 0.2f, -0.5f, 0.5f, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f) }, //bottom-right
{ -0.2f, -0.5f, 0.5f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) }, //bottom-left
{ -0.2f, 0.5f, 0.5f, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) }, //top-left
};
// Create Vertex Buffer
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(Vertex) * 6;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dev->CreateBuffer(&bd, NULL, &pVBuffer);
// copy vertices into buffer
D3D11_MAPPED_SUBRESOURCE msr;
devContext->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &msr);
memcpy(msr.pData, square, sizeof(square));
devContext->Unmap(pVBuffer, NULL);
}
void BuildPipeline() {
// Load and Compile Shaders
ID3D10Blob *VS, *PS;
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);
// Create shaders from the data in the Blobs Buffer
dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);
// Apply Shaders to the device context
devContext->VSSetShader(pVS, 0, 0);
devContext->PSSetShader(pPS, 0, 0);
// Define the layout of the input given to the shaders
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
devContext->IASetInputLayout(pLayout);
}
{"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}
should be DXGI_FORMAT_R32G32B32_FLOAT as the position has only three members. The AlignedByteOffset can be set to 12 (4x3).
For the topology, you specify D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, so the vertex data is expected to form a triangle strip. However, the data you provide specifies two separate triangles (i.e. a triangle list), so you should probably use
devContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
The essential mistake seems to be in the Draw() call: you specify that you want to draw a total of 3 vertices, while you actually want to draw all 6 vertices from your buffer. I.e.
devContext->Draw(6, 0);
The byte offset of your Color element description is wrong, too. The position takes 3 single precision floats (3*4 = 12 bytes), and is directly followed by the color data.
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
i have a source code :
here's the initialization
//The Direct3d and device object
IDirect3D9 *g_pD3D = NULL;
IDirect3DDevice9 *g_pD3DDevice = NULL;
//the 3-D vertex format and descriptor
typedef struct
{
FLOAT x, y, z; //3-D coordinates
FLOAT nx, ny, nz; //Normals
D3DCOLOR Diffuse; //Colors
}sVertex;
#define VERTEXFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL)
//Vertex buffer
IDirect3DVertexBuffer9 *g_pVB = NULL;
sVertex Verts[16] =
{
{-100.f, 100.0f, -100.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255,255,255,255)},
{ 100.f, 100.0f, -100.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ -100.f, -100.0f, -100.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ 100.f, -100.0f, -100.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ 100.f, 100.0f, -100.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ 100.f, 100.0f, 100.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ 100.f, -100.0f, -100.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ 100.f, -100.0f, 100.0f, 1.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ 100.f, 100.0f, 100.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ -100.f, 100.0f, 100.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ 100.f, -100.0f, 100.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ -100.f, -100.0f, 100.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ -100.f, 100.0f, 100.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ -100.f, 100.0f, -100.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ -100.f, -100.0f, 100.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
{ -100.f, -100.0f, -100.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) }
};
here's the init function
BOOL DoInit()
{
//perform application initialization functions here
//such as those that set up the graphics, sound, network, etc
//Return a value of TRUE for success, FALSE otherwise.
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE d3ddm;
D3DXMATRIX matProj, matView;
D3DLIGHT9 Light;
BYTE *Ptr;
sVertex Verts[16];
//do a windowed mode initialization of Direct3D
if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return FALSE;
if (FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
return FALSE;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pD3DDevice)))
return FALSE;
//set the rendering states
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
//create and set the view matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3(0.0f, 0.0f, -500.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f,0.0f));
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
//create the vertex buffer and set data
g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)* 16, 0, VERTEXFVF, D3DPOOL_DEFAULT, &g_pVB, 0);
g_pVB->Lock(0, 0, (VOID**)&Ptr, 0);
memcpy(Ptr, Verts, sizeof(Verts));
g_pVB->Unlock();
//set light data, color, position, and range
ZeroMemory(&Light, sizeof(Light));
Light.Type = D3DLIGHT_POINT;
Light.Diffuse.r = Light.Ambient.r = 0.5f;
Light.Diffuse.g = Light.Ambient.g = 0.5f;
Light.Diffuse.b = Light.Ambient.b = 0.0f;
Light.Diffuse.a = Light.Ambient.a = 1.0f;
Light.Range = 1000.0f;
Light.Attenuation0 = 0.5f;
Light.Position.x = 300.0f;
Light.Position.y = 0.0f;
Light.Position.z = -600.0f;
//set and enable the light
g_pD3DDevice->SetLight(0, &Light);
g_pD3DDevice->LightEnable(0, TRUE);
return TRUE;
}
and here's the function doFrame
BOOL DoFrame()
{
//Perform per-frame processing, such as rendering.
//Return TRUE on success, FALSE otherwise.
D3DXMATRIX matWorld;
//cleat device backbuffer
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0);
//begin scene
if (SUCCEEDED(g_pD3DDevice->BeginScene()))
{
//create and set the world transformation matrix
//rotate object along Y-axis
D3DXMatrixRotationY(&matWorld, (float)timeGetTime()/1000.0f);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
//set the vertex stream and shader
g_pD3DDevice->SetStreamSource(0, g_pVB, sizeof(sVertex), 0);
//g_pD3DDevice->SetVertexShader(VERTEXFVF);
g_pD3DDevice->SetFVF(VERTEXFVF);
//Draw the vertex buffer
for (short i = 0; i < 4; i++)
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, i * 4, 2);
//end the scene
g_pD3DDevice->EndScene();
}
//displa the scene
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
return TRUE;
}
when I compile this code, it just shows black background.. where's the problem??
I have a background of opengl. I don't know directx much. Correct me if I am wrong. But I think you need to set the projection matrix. I mean without the viewing frustum GPU won't render the scene.
It seems you have declared projection matrix but you haven't set values to it. (Value like FOV).
I have an Object class that keeps track of the objects scale, translation and everything else, and I want it to set those transform matrices when it draws(obviously), but for some reason, despite that I set the transform and everything, it does not work.
---------- IN OBJECT------------------
vertices = new Vertex[vertexCount];
scaleX = 100.0f;
scaleY = 100.0f;
scaleZ = 100.0f;
vertices[0] = { 100.0f, 0.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0, };
vertices[1] = { 100.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1, };
vertices[2] = { 0.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1, };
vertices[3] = { 0.0f, 0.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0, };
v_buffer = NULL;
i_buffer = NULL;
texture = 0;
d3ddev->CreateVertexBuffer(vertexCount * sizeof(Vertex),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
short indices[] =
{
0, 1, 2, // side 1
2, 3, 0,
};
// create an index buffer interface called i_buffer
d3ddev->CreateIndexBuffer(6 * sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&i_buffer,
NULL);
// lock i_buffer and load the indices into it
i_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
i_buffer->Unlock();
// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(Vertex)* vertexCount);
v_buffer->Unlock();
-----------DRAW FUNCTION --------------
D3DXVECTOR3 pos = { obj.GetX(), obj.GetY(), obj.GetZ() };
d3ddev->SetFVF(CUSTOMFVF);
// select the vertex buffer to display
d3ddev->SetStreamSource(0, obj.GetVBuffer(), 0, sizeof(Vertex));
d3ddev->SetIndices(obj.GetIBuffer());
D3DXMatrixRotationYawPitchRoll(&obj.rotationTransform, obj.GetRotationX(), obj.GetRotationY(), obj.GetRotationZ());
D3DXMatrixTranslation(&obj.translationTransform, obj.GetX(), obj.GetY(), obj.GetZ());
D3DXMatrixScaling(&obj.scalingTransform, obj.GetScaleX(), obj.GetScaleY(), obj.GetScaleZ());
D3DXMatrixMultiply(&obj.worldTransform, &obj.scalingTransform, &obj.translationTransform);
D3DXMatrixMultiply(&obj.worldTransform, &obj.rotationTransform, &obj.worldTransform);
//obj.worldTransform = obj.rotationTransform * obj.scalingTransform * obj.translationTransform;
d3ddev->SetTransform(D3DTS_WORLD, &obj.worldTransform);
D3DXMatrixLookAtRH(&obj.viewTransform, &D3DXVECTOR3(0, 0, 10), &D3DXVECTOR3(0, 0, 0), &D3DXVECTOR3(0, 0, 0));
d3ddev->SetTransform(D3DTS_VIEW, &obj.viewTransform);
D3DXMatrixPerspectiveFovRH(&obj.projectionTransform, D3DXToRadian(90), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 1.0f, 10.0f);
d3ddev->SetTransform(D3DTS_PROJECTION, &obj.projectionTransform);
// copy the vertex buffer to the back buffer
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
Okay, you are doing wrong with the multiplication of the matrices. Matrix multiplication is not commutative, that is A*B != B*A. Change the order you multiply them in from rotation*scale*translate to scale*rotation*translate.
Hope that helps.