Advertisement

Wireframe mode not drawing all primitives, what am i doing wrong

Started by June 17, 2017 06:53 PM
2 comments, last by Cwhizard 7 years, 5 months ago

The following code is supposed to draw a cube in wireframe mode, but it is only drawing the first 2 primites (6 vertices instead of 36). If its just a typo Ill be mad, but honestly I'm drawing a blank here


DWORD WINAPI MainThread(DWORD);
DWORD WINAPI MainThread(DWORD dwParam){
 
    // Wait for the window to appear
    while(!bQuit && hwnd == NULL) Sleep(1);
    if(bQuit) return 0;
 
 
// Initialize Direct3D
 
// Create a Direct3D Object (starts Direct3D and lets you access its functions)
IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d9 == NULL) bQuit = true;
 
// Get device capabilities (what your card can do)
D3DCAPS9 caps;
d3d9->GetDeviceCaps( 0 , D3DDEVTYPE_HAL , &caps);
 
// Set Vertex Processing type (Hardware or Software)
// Hardware is faster, but not 'all' cards support it
int vp = 0;
switch(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT){
case 0:
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
break;
default:
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
break;
}
 
//Beep(1000,50);
 
// Fill out the Present Parameters structure (What we want the display to look like)
 
d3dpp.BackBufferWidth            = 800;
d3dpp.BackBufferHeight           = 600;
d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount            = 1;
d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality         = 0;
d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow              = hwnd;
d3dpp.Windowed                   = TRUE;
d3dpp.EnableAutoDepthStencil     = TRUE;
d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
d3dpp.Flags                      = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
 
 
// Create the device (start the Direct3D window)
 
IDirect3DDevice9* device;
 
HRESULT hResult = d3d9->CreateDevice(
D3DADAPTER_DEFAULT, // primary adapter
D3DDEVTYPE_HAL,     // device type - We want Hardware, not Software (emulation)
hwnd,               // window associated with device
vp,                 // vertex processing
   &d3dpp,             // present parameters
   &device);            // return created device
if(FAILED(hResult)){ bQuit = true; MessageBox(NULL , "CreateDevice() FAILED" , 0 , 0);}
 
// Create a vertex buffer that holds 8 vertices
IDirect3DVertexBuffer9* VertexBuffer;
hResult = device->CreateVertexBuffer(
8 * sizeof(Vertex),
0 ,
VERTEXFORMAT,
D3DPOOL_MANAGED,
&VertexBuffer,
0);
if(FAILED(hResult)){ bQuit = true; MessageBox(NULL , "CreateVertexBuffer() FAILED" , 0 , 0);}
 
// create an index buffer that holds 36 indices
IDirect3DIndexBuffer9* IndexBuffer;
hResult = device->CreateIndexBuffer(
36 * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&IndexBuffer,
0);
if(FAILED(hResult)){ bQuit = true; MessageBox(NULL , "CreateIndexBuffer() FAILED" , 0 , 0);}
 
// the following 2 sections, accessing the buffers should be merged into a single section
// which loads the model data from files after calculating the necessary buffer sizes
// access the vertex buffer
Vertex* vertices;
VertexBuffer->Lock(0 , 0 , (void**)&vertices , 0);
vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
vertices[1] = Vertex(-1.0f,  1.0f, -1.0f);
vertices[2] = Vertex( 1.0f,  1.0f, -1.0f);
vertices[3] = Vertex( 1.0f, -1.0f, -1.0f);
vertices[4] = Vertex(-1.0f, -1.0f,  1.0f);
vertices[5] = Vertex(-1.0f,  1.0f,  1.0f);
vertices[6] = Vertex( 1.0f,  1.0f,  1.0f);
vertices[7] = Vertex( 1.0f, -1.0f,  1.0f);
VertexBuffer->Unlock();
 
// access the index buffer
WORD* indices;
IndexBuffer->Lock(0 , 0 , (void**)&indices , 0);
indices[0]  = 0; indices[1]  = 1; indices[2]  = 2;
indices[3]  = 0; indices[4]  = 2; indices[5]  = 3;
indices[6]  = 4; indices[7]  = 6; indices[8]  = 5;
indices[9]  = 4; indices[10] = 7; indices[11] = 6;
indices[12] = 4; indices[13] = 5; indices[14] = 1;
indices[15] = 4; indices[16] = 1; indices[17] = 0;
indices[18] = 3; indices[19] = 2; indices[20] = 6;
indices[21] = 3; indices[22] = 6; indices[23] = 7;
indices[24] = 1; indices[25] = 5; indices[26] = 6;
indices[27] = 1; indices[28] = 6; indices[29] = 2;
indices[30] = 4; indices[31] = 0; indices[32] = 3;
indices[33] = 4; indices[34] = 3; indices[35] = 7;
IndexBuffer->Unlock();
 
    // set stream source (attach vertex buffer to rendering pipeline)
    //device->SetStreamSource(0 , VertexBuffer , 0 , sizeof(Vertex));
 
    // set the vertex format
    //device->SetFVF(D3DFVF_XYZ);// | D3DFVF_DIFFUSE);// | D3DFVF_TEX1);
 
    // set the index buffer
    //device->SetIndices(IndexBuffer);
 
    // Position and aim the camera
    // TODO:  This should probably be moved into the main rendering loop, as it may change in game
//D3DXVECTOR3 position(1.0f, 1.0f, -3.0f);
//D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
//D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    //D3DXMATRIX V;
//D3DXMatrixLookAtLH(&V, &position, &target, &up);
 
    //device->SetTransform(D3DTS_VIEW, &V);
 
// Set the projection matrix
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)d3dpp.BackBufferWidth / (float)d3dpp.BackBufferHeight,
1.0f,
1000.0f);
device->SetTransform(D3DTS_PROJECTION, &proj);
 
// set render state - wireframe mode
device->SetRenderState(D3DRS_FILLMODE , D3DFILL_WIREFRAME);
 
 
// Now enter the rendering loop, this renders the game objects
float x,y,dx,dy;
dx=+0.1f;dy=0.0f;x=0.0f;y=0.0f;
 
//D3DXVECTOR3 position((float)x, (float)y, -3.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
 
while(!bQuit){
x+=dx;y+=dy;
if(x>1.0f) dy=0.0f-dx;dx=0.0f;x=1.0f;
if(x<-1.0f) dy=0.0f-dx;dx=0.0f;x=-1.0f;
if(y>1.0f) dx=dy;dy=0.0f;y=1.0f;
if(y<-1.0f) dx=dy;dy=0.0f;y=-1.0f;
D3DXVECTOR3 position(x, y, -3.0f);
 
 
// Position and aim the camera
// TODO:  This should probably be moved into the main rendering loop, as it may change in game
 
D3DXMatrixLookAtLH(&V, &position, &target, &up);
device->SetTransform(D3DTS_VIEW, &V);
 
 
//bQuit = true;
device->Clear(0 , 0 , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER , 0xffffffff , 1.0f , 0);
device->BeginScene();
// draw code goes here
 
   // set stream source (attach vertex buffer to rendering pipeline)
device->SetStreamSource(0 , VertexBuffer , 0 , sizeof(Vertex));
 
// set the vertex format
device->SetFVF(Vertex::FVF); // | D3DFVF_DIFFUSE);// | D3DFVF_TEX1);
 
// set the index buffer
device->SetIndices(IndexBuffer);
 
// draw the primitive, which in this case is a triangle
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST , 0 , 0 , 36 , 0 , 12);
 
 
 
 
 
 
 
 
// done drawing
device->EndScene();
// present the scene, usually copies the backbuffer to the screen
device->Present(0 , 0 , 0 , 0);
 
// be multitask friendly
Sleep(1);
}
 
VertexBuffer->Release();
IndexBuffer->Release();
    device->Release();
d3d9->Release();
return 0;
}

tl;dr, but did you disable face culling?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement

 

14 hours ago, Khatharr said:

tl;dr, but did you disable face culling?

That was it!.  I simply added

device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

and now it works, thanks :)  I thought it was supposed to be off by default, at least that's how OpenGL did it.  but thinking about it, it should be on by default as that is the state most applications will prefer.

 

This topic is closed to new replies.

Advertisement