Jump to content
  • Advertisement
Sign in to follow this  
null434

Rendering problem

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to render 3 vertices in the vertex buffer to the screen to form a triangle. But instead a black window appeared and there was no vertices. the code is the following..

 

    #include <windows.h>
#include <d3d10.h>
#include<D3DX10.h>
 
#pragma comment( lib, "D3DX10.lib" )
#pragma comment( lib, "D3D10.lib" )
const int width = 640;
const int height = 640; 
 
const char ClassName[] = "myWindowClass";
 
// D3D()
bool D3D();
 
ID3D10Device* mDevice=0;
IDXGISwapChain* mSwapChain=NULL;
ID3D10Effect* effect=0;
ID3D10EffectTechnique *technique=NULL;
ID3D10InputLayout* input=0;
ID3D10Buffer* vbd=0;
 
HWND hwnd; // The HWND to the window
 
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    MSG Msg;
 
    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = L"ClassName";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
 
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"),
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
 
    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        L"ClassName",
        TEXT("The title of my window"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, width, height,
        NULL, NULL, hInstance, NULL);
 
    if(hwnd == NULL)
    {
        MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error!"),
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
D3D();
    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
if (Msg.message == WM_QUIT)
{
break;
}
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
 
 
bool D3D()
    {
DXGI_SWAP_CHAIN_DESC SwapchainDESC;
SwapchainDESC.BufferDesc.Width = width ; //
SwapchainDESC.BufferDesc.Height = height ; //
SwapchainDESC.BufferDesc.RefreshRate.Numerator = 60;
SwapchainDESC.BufferDesc.RefreshRate.Denominator = 1 ;
SwapchainDESC.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
SwapchainDESC.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED ;
SwapchainDESC.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED ;
 
//no Multisampling
SwapchainDESC.SampleDesc.Count = 1;
SwapchainDESC.SampleDesc.Quality = 0;
 
SwapchainDESC.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT ;
SwapchainDESC.BufferCount = 1 ;
SwapchainDESC.OutputWindow =  hwnd; //
SwapchainDESC.Windowed = true ; //
SwapchainDESC.SwapEffect = DXGI_SWAP_EFFECT_DISCARD ;
SwapchainDESC.Flags =  0 ;
 
// Create D3D10Device and SwapChain
 
 
if(FAILED(D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_REFERENCE, NULL, 0, D3D10_SDK_VERSION, &SwapchainDESC, &mSwapChain, &mDevice)))
{MessageBox(hwnd, L"DEVE",L"error",MB_OK);
return FALSE;
}
 
 
 
    D3D10_VIEWPORT vp;
    vp.Width = 1024;
    vp.Height = 1024;
    vp.MinDepth = 0.0f;
    vp.MaxDepth = 1.0f;
    vp.TopLeftX = 0;
    vp.TopLeftY = 0;
    mDevice->RSSetViewports( 1, &vp );
// Create Render Target View and set back buffer as Render Target
 
 
ID3D10RenderTargetView* mRenderTargetView=0;
ID3D10Texture2D* backBuffer=0;
if(FAILED(mSwapChain -> GetBuffer(0,__uuidof(ID3D10Texture2D), reinterpret_cast<void**>(&backBuffer))))
{MessageBox(hwnd, L"swap",L"error",MB_OK);
return FALSE;
}
mDevice -> CreateRenderTargetView ( backBuffer, 0, &mRenderTargetView );
backBuffer->Release();
 
D3D10_TEXTURE2D_DESC depthDESC;
depthDESC.Width = width; //The width of the client area of the window
depthDESC.Height = height;
depthDESC.MipLevels = 1;
depthDESC.ArraySize = 1;
depthDESC.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthDESC.SampleDesc.Count = 1; //multisampling must match
depthDESC.SampleDesc.Quality = 0;
depthDESC.Usage = D3D10_USAGE_DEFAULT;
depthDESC.BindFlags = D3D10_BIND_DEPTH_STENCIL;
depthDESC.CPUAccessFlags = 0;
depthDESC.MiscFlags = 0;
 
// Create the Depth/Stencil Buffer and View
 
ID3D10Texture2D* mDepthStencilBuffer;
ID3D10DepthStencilView* mDepthStencilView;
 
mDevice -> CreateTexture2D( &depthDESC,  0, &mDepthStencilBuffer);
mDevice -> CreateDepthStencilView( mDepthStencilBuffer, 0 , &mDepthStencilView );
 
// Bind the Views to the Outout Merger Stage
 
mDevice -> OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView );
 
 
 
if(FAILED(D3DX10CreateEffectFromFile(L"Empty.fx", NULL, NULL, "fx_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, 
  mDevice, NULL, NULL, &effect, NULL, NULL)))
{
 
MessageBox(hwnd, L"effect,", L"error", MB_OK);
return FALSE;
}
 
 
 
// Obtain technique
 
if(FAILED(technique = effect->GetTechniqueByName("Render")))
{
MessageBox(hwnd, L"technique",L"error",MB_OK);
return FALSE;
}
 
 
// Define layout
 
D3D10_INPUT_ELEMENT_DESC layout[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}
 
};
 
 
UINT num = sizeof(layout)/sizeof(layout[0]);
 
// Create layout
 
D3D10_PASS_DESC PassDesc;
technique->GetPassByIndex(0)->GetDesc(&PassDesc);
mDevice->CreateInputLayout(layout, 1, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &input);
 
mDevice->IASetInputLayout(input);
 
 
// Vertex Struct
 
struct Vertex
{
D3DXVECTOR3 pos;
};
 
// Initialization Vertices
 
Vertex vertices[]=
{
{D3DXVECTOR3 ( 0.0f, 0.5f, 0.5f )},
{D3DXVECTOR3 ( 1.0f, -0.5f, 0.5f)},
{D3DXVECTOR3 ( -1.0f, -0.5f, 0.5f)}
};
// Create Vertex Desc
 
D3D10_BUFFER_DESC bd;
bd.Usage = D3D10_USAGE_DEFAULT;
bd.ByteWidth = sizeof(Vertex)*3;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA Initdata;
Initdata.pSysMem = vertices;
mDevice->CreateBuffer( &bd, &Initdata, &vbd);
 
 
// Set vertex buffer
 
UINT stride = sizeof(Vertex);
UINT offset = 0;
mDevice->IASetVertexBuffers( 0, 1, &vbd, &stride, &offset);
 
// Set Topology
mDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
 
// Render
 
    D3D10_TECHNIQUE_DESC techDesc;
    technique->GetDesc( &techDesc );
    for( UINT p = 0; p < techDesc.Passes; ++p )
    {
 
float col[4]={0.0f, 0.0f, 0.0f, 1.0f};
mDevice->ClearRenderTargetView(mRenderTargetView, col);
mDevice->Draw(3,0);
mSwapChain->Present(0,0);
}
return true;
}
 
 
And the shader code is
 
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
    return Pos;
}
 
 
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( float4 Pos : SV_POSITION ) : SV_Target
{
    return float4( 1.0f, 1.0f, 0.0f, 1.0f );    // Yellow, with Alpha = 1
}
 
 
//--------------------------------------------------------------------------------------
technique10 Render
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
    }
}
 
 
[attachment=17900:pic.jpg]
 
 

And when I executed the exe. file that is inside the Debug folder in the project folder, the window appeared but message box also appeared showing something is wrong with the D3DX10CreateEffectFromFile() in the code. I set up that message box to show when there is error with the function.
 
[attachment=17901:pic2.jpg]
 

 

Share this post


Link to post
Share on other sites
Advertisement

Have you checked to see what errors D3DX10CreateEffectFromFile() spits out? You will need to use the ppErrors parameter, but it will give you a hint as to what is going wrong.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!