Sign in to follow this  

Why don't primitives come out right in DirectX10?

Recommended Posts

Dahkex    99
Going through one of the tutorials and am not understanding why my primitive either doesn't appear or appears wrong.

For this example, I'll use a trianglestrip.

1. If I just set the strip to use 3 vertices just to see a triangle, I don't see anything using the index ordering of 0,1 and 2. Thus, if I graph it out on paper and go from point to point then it is clockwise. Yet, nothing.

2. If I set a 4th vertex, I expect to see a quad but now I see the triangle I originally intended to see with 3 vertices. Vertices 0, 1 and 2 go in clockwise order and then 2,1,3 goes up and around also in clockwise order but I just see one piece of the triangle instead of a square.

Am I visualizing this wrong or something? The same issue occurs if I set it to just using a triangle list.

[code]// include the basic windows header files and the Direct3D header files
#include <windows.h>
#include <windowsx.h>
#include <d3d10.h>
#include <d3dx10.h>

// include the Direct3D Library file
#pragma comment (lib, "d3d10.lib")
#pragma comment (lib, "d3dx10.lib")

// define the screen resolution
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

// global declarations
ID3D10Device* device; // the pointer to our Direct3D device interface
ID3D10Buffer* iBuffer;
ID3D10RenderTargetView* rtv; // the pointer to the render target view
ID3D10DepthStencilView* dsv;
IDXGISwapChain* swapchain; // the pointer to the swap chain class
ID3D10Buffer* pBuffer; // the pointer to the vertex buffer
ID3D10Effect* pEffect; // the pointer to the effect file interface
ID3D10EffectTechnique* pTechnique; // the pointer to the technique interface
ID3D10EffectPass* pPass; // the pointer to the pass interface
ID3D10InputLayout* pVertexLayout; // the pointer to the input layout interface
ID3D10EffectMatrixVariable* pTransform; // the pointer to the effect variable interface
D3D10_PASS_DESC PassDesc; // the pass description struct

struct VERTEX {D3DXVECTOR3 Position; D3DXCOLOR Color;};

// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
void init_geometry(void); // creates geometry to render
void init_pipeline(void); // sets up the pipeline for rendering

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
HWND hWnd;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX); = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = L"WindowClass";


hWnd = CreateWindowEx(NULL,
L"Our Direct3D Program",
0, 0,

ShowWindow(hWnd, nCmdShow);

// set up and initialize Direct3D

// enter the main loop:

MSG msg;

if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

if(msg.message == WM_QUIT)


// clean up DirectX and COM

return msg.wParam;

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
return 0;
} break;

return DefWindowProc (hWnd, message, wParam, lParam);

// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
DXGI_SWAP_CHAIN_DESC scd; // create a struct to hold various swap chain information

ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); // clear out the struct for use

scd.BufferCount = 1; // create two buffers, one for the front, one for the back
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
scd.BufferDesc.Width = SCREEN_WIDTH; // set the back buffer width
scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // tell how the chain is to be used
scd.OutputWindow = hWnd; // set the window to be used by Direct3D
scd.SampleDesc.Count = 1; // set the level of multi-sampling
scd.SampleDesc.Quality = 0; // set the quality of multi-sampling
scd.Windowed = TRUE; // set to windowed or full-screen mode at startup
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching

// create a device class and swap chain class using the information in the scd struct

ZeroMemory(&zbd, sizeof(zbd));
zbd.Width = SCREEN_WIDTH;
zbd.Height = SCREEN_HEIGHT;
zbd.ArraySize = 1;
zbd.SampleDesc.Count = 1;
zbd.Format = DXGI_FORMAT_D32_FLOAT;
zbd.BindFlags = D3D10_BIND_DEPTH_STENCIL;
ID3D10Texture2D* pDepthBuffer;
device->CreateTexture2D(&zbd, NULL, &pDepthBuffer);

ZeroMemory(&dsvd, sizeof(dsvd));
dsvd.Format = DXGI_FORMAT_D32_FLOAT;
dsvd.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
device->CreateDepthStencilView(pDepthBuffer, &dsvd, &dsv);

// get the address of the back buffer and use it to create the render target
ID3D10Texture2D* pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
device->CreateRenderTargetView(pBackBuffer, NULL, &rtv);

// set the back buffer as the render target
device->OMSetRenderTargets(1, &rtv, NULL);

D3D10_VIEWPORT viewport; // create a struct to hold the viewport data

ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT)); // clear out the struct for use

viewport.TopLeftX = 0; // set the left to 0
viewport.TopLeftY = 0; // set the top to 0
viewport.Width = SCREEN_WIDTH; // set the width to the window's width
viewport.Height = SCREEN_HEIGHT; // set the height to the window's height
viewport.MinDepth =0;
viewport.MaxDepth = 1;

device->RSSetViewports(1, &viewport); // set the viewport

// this is the function used to render a single frame
void render_frame(void)
// clear the window to a deep blue
device->ClearRenderTargetView(rtv, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));

device->ClearDepthStencilView(dsv, D3D10_CLEAR_DEPTH, 1.0f, 0);

// select which input layout we are using

// select which primtive type we are using

// select which vertex buffer to display
UINT stride = sizeof(VERTEX);
UINT offset = 0;
device->IASetVertexBuffers(0, 1, &pBuffer, &stride, &offset);
device->IASetIndexBuffer(iBuffer, DXGI_FORMAT_R32_UINT,0);

// increase the time varaible and send to the effect
static float Time = 0.0f; Time += 0.001f;

D3DXMATRIX matRotate, matView, matProjection, matFinal;

D3DXMatrixRotationY(&matRotate, Time);

&D3DXVECTOR3(3.0f, 1.0f, 3.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));

matFinal = matView * matProjection;

// apply the appropriate pass


// display the rendered frame
swapchain->Present(0, 0);

// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
swapchain->SetFullscreenState(FALSE, NULL); // switch to windowed mode

pBuffer->Release(); // close and release the vertex buffer
pVertexLayout->Release(); // close and release the input layout object
swapchain->Release(); // close and release the swap chain
rtv->Release(); // close and release the render target view
device->Release(); // close and release the 3D device

// this is the function that creates the geometry to render
void init_geometry(void)

// create three vertices using the VERTEX struct built earlier
VERTEX OurVertices[] =
{D3DXVECTOR3(-1.0f, 0.0f,0.0f), D3DXCOLOR(1.0f,0.0f,0.0f,1.0f)},
{D3DXVECTOR3(0.0f,1.0f,0.0f), D3DXCOLOR(0.0f,1.0f,0.0f,1.0f)},
{D3DXVECTOR3(0.0f,-1.0f,0.0f), D3DXCOLOR(0.0f,0.0f,1.0f,1.0f)},
{D3DXVECTOR3(1.0f,0.0f,0.0f), D3DXCOLOR(1.0f,0.5f,1.0f,1.0f)},

// create the vertex buffer and store the pointer into pBuffer, which is created globally
bd.Usage = D3D10_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(VERTEX) * 4;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;

device->CreateBuffer(&bd, NULL, &pBuffer);

void* pVoid; // the void pointer

pBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid); // map the vertex buffer
memcpy(pVoid, OurVertices, sizeof(OurVertices)); // copy the vertices to the buffer

DWORD OurIndices[] =

bd.Usage = D3D10_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(DWORD) * 6;
bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;

device->CreateBuffer(&bd, NULL, &iBuffer);

iBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid);
memcpy(pVoid, OurIndices, sizeof(OurIndices));

// this function sets up the pipeline for rendering
void init_pipeline(void)
// load the effect file
D3DX10CreateEffectFromFile(L"effect.fx", 0, 0,
"fx_4_0", 0, 0,
device, 0, 0,
&pEffect, 0, 0);

// get the technique and the pass
pTechnique = pEffect->GetTechniqueByIndex(0);
pPass = pTechnique->GetPassByIndex(0);

// get the TimeElapsed effect variable
pTransform = pEffect->GetVariableByName("Transform")->AsMatrix();

// create the input element descriptions
// first input element: position

// second input element: color

// use the input element descriptions to create the input layout

Share this post

Link to post
Share on other sites
karwosts    840
Your vertices would be clockwise if you just drew the 2D coordinates on a piece of paper, but realize that they are counterclockwise when viewed from the camera's position.

In a left handed matrix, your axes are in the following orientation:

^ /+z
| /
| /
---------> +x


Now realize that your eye is located on the positive z axis, looking towards (0,0,0). This means the XY axis as viewed from your camera (ignoring the xy eye offset) looks like this:

+x <--------

When you look at your vertices (-1,0),(0,1),(0,-1), from this direction, you can see that they are in fact counterclockwise.

However I realize this doesn't understand what you're describing when you draw a 4-vertex strip. I'll have to let someone else comment on that, because what you're describing doesn't make sense to me.

Share this post

Link to post
Share on other sites
Dahkex    99
Should have noted that as going through the tutorial I was just re-typing everything and not realizing to set the camera back -3 on the z which now would probably work.


Share this post

Link to post
Share on other sites
ryan mccabe    161
I just worked through making a triangle with array verts and turning it into a square. The tutorial said to use two triangles, I thought why not try a square first, maybe d3d will fill triangulate it. It doesn't so four verts wont work if I understand things correctly. anyway, code for a square and texture:


//making a square
vertices[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f); // Bottom left.
vertices[0].texture = D3DXVECTOR2(0.0f, 1.0f);

vertices[1].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f); // Top left.
vertices[1].texture = D3DXVECTOR2(0.0f, 0.0f);

vertices[2].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f); // Top right.
vertices[2].texture = D3DXVECTOR2(1.0f, 0.0f);

vertices[3].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f); // Top right.
vertices[3].texture = D3DXVECTOR2(1.0f, 0.0f);

vertices[4].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f); // Bottom right.
vertices[4].texture = D3DXVECTOR2(1.0f, 1.0f);

vertices[5].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f); // Bottom left.
vertices[5].texture = D3DXVECTOR2(0.0f, 1.0f);

Edit: I'm assuming your camera wasn't the only issue as I had a tri show up when I input four verts as well.

Side note. After 3d modeling for years building a square feels very funny lol.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this