Drawing a Triangle Using DirectX

Started by
2 comments, last by Ultraporing 2 years ago

I wrote a program with the help of one book, which was supposed to draw a triangle on the screen.

An error occurs while executing the program. It tries to compile the vertex shader, but the operation fails. See file TriangleDemo.cpp from line 23.

MY CODE IS HERE https://filebin.net/0l6tj4lx5i2e35sq

Maybe I configured the project incorrectly?

Advertisement

I think I speak for everyone when I say I would prefer not to download random files from a file-sharing website. Can you host the file on something like GitHub that lets us browse the code, or perhaps just post it here instead?

If it's failing to compile the vertex shader, then it's likely you have a compilation error in the shader code. You should look into outputting the error message that the compiler gives you back.

MJP said:

I think I speak for everyone when I say I would prefer not to download random files from a file-sharing website. Can you host the file on something like GitHub that lets us browse the code, or perhaps just post it here instead?

If it's failing to compile the vertex shader, then it's likely you have a compilation error in the shader code. You should look into outputting the error message that the compiler gives you back.

I did scan the zip, unpacked and uploaded it to my GitHub as a repo: https://github.com/Ultraporing/triangle-temp-gamedev

Living on the edge xD. I'm gonna check on the code now and edit this post.

Edit: OP you did not upload the shader file SolidGreenColor.fx in this zip…

I think I found the shader on the Internet and it compiles for me. You are not the only person with problems with this Book. I found a ton of posts from people about broken examples.
Check the "with_shaderfile_and_render_func" branch on Github for the working source and binary (Debug Folder).

arkadysahakian said:
Maybe I configured the project incorrectly?

Looks like it. The Shaderfile was nowhere in the project directory and it needs to be copied into the output folder of your binaries. In example Debug folder which you already included.

here is the shader I found:

// 輸入一個頂點坐標, 類型為 POSITION
// 輸出一個頂點坐標(已着色的), 類型為 SV_POSITION
float4 VS_Main( float4 pos : POSITION ) : SV_POSITION
{
    // 直接返回輸入的參數值 pos
    return pos;
}


//輸入一個 頂點坐標( 已着色的), 類型為 SV_POSITION
//輸出一個 可直接顯示在目標上的 顏色( 已着色的), 類型為 SV_TARGET
float4 PS_Main( float4 pos : SV_POSITION ) : SV_TARGET
{
    // 直接返回 固定的顏色, 輸入值 pos 不做任何處理
    return float4( 0.0f, 1.0f, 0.0f, 1.0f );
}

//注意 , 上述的 ( 已着色的) 是指 shadered.

And after Implementing the Render function it works just fine:

void TriangleDemo::Render()
{
	if (d3dContext_ == 0)
		return;
	float clearColor[4] = { 0.5, 0.5f, 0.5f, 1.0f };
	//Set the background color 
	d3dContext_->ClearRenderTargetView(backBufferTarget_, clearColor);
	unsigned int stride = sizeof(VertexPos);
	unsigned int offset = 0;
	d3dContext_->IASetInputLayout(inputLayout_);
	d3dContext_->IASetVertexBuffers(0, 1, &vertexBuffer_, &stride, &offset);
	d3dContext_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	d3dContext_->VSSetShader(solidColorVS_, 0, 0);
	d3dContext_->PSSetShader(solidColorPS_, 0, 0);
	d3dContext_->Draw(3, 0);
	swapChain_->Present(0, 0);
}

“It's a cruel and random world, but the chaos is all so beautiful.”
― Hiromu Arakawa

This topic is closed to new replies.

Advertisement