Generic Shader for DirectX 11?

Started by
2 comments, last by MJP 11 years ago

I'm working on loading different models with the Assimp library and was wondering if anyone could offer any help on a generic shader, if that is possible. I have very little shader experience and was wondering if there was a way to create a generic shader that my engine could load and use it to display different models. I was thinking it could take the vertices of the model I load, as well as the optional normals and texture coordinates and display the model with just ambient light. I know that for more advanced things I would need custom shaders, but I was hoping it would be possible to take 3D file that Assimp loads and just display the object in my scene with no extra effects. Is it possible for a generic shader like this, or would I need to create a shader for every file format I want to load and use that shader for each model I load. If any of my questions don't make much sense or aren't possible just let me know, I'm pretty new to the whole shader thing at the moment and haven't really found a resource like I'm describing. Any help would be greatly appreciated.

Advertisement

I have no idea about HLSL specifics but as far as I know, anything teaching you a shading language will start with what you asked, what you need to display a model, any model. That includes the various spaces you're going to work on, shading language specifics and how to set up and upload the data to your GPU (which is often the most cumbersome work, at this level your shader is going to be a few lines long, ie, vertexPos = MVP * inputPos; and color = inputColor. thats it).

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This is the ImmediateGeometrySolid.hlsl file from the Hieroglyph 3 code repository:


//--------------------------------------------------------------------------------
// ImmediateGeometrySolid.hlsl
//
//
// Copyright (C) 2012 Jason Zink.  All rights reserved.
//--------------------------------------------------------------------------------

cbuffer WorldTransforms
{
	matrix WorldMatrix;
	matrix WorldViewProjMatrix;	
};

cbuffer ObjectMaterialInfo
{
	//float4 Ka;
	//float4 Kd;
	float4 Ks;
};

cbuffer PointLightInfo
{
	float4 LightPosition;
	float4 Ia;
	float4 Id;
	float4 Is;
};

cbuffer SceneInfo
{
	float4 ViewPosition;
};




struct VS_INPUT
{
	float3 position : POSITION;
	float3 normal   : NORMAL;
	float4 color    : COLOR;
};

struct VS_OUTPUT
{
	float4 position : SV_Position;
	float4 worldPos : POSITION;
	float3 normal   : NORMAL;
	float4 color    : COLOR;
};


VS_OUTPUT VSMAIN( in VS_INPUT input )
{
	VS_OUTPUT output;
	
	// Lighting calculations done in world space
	float4 P = mul( float4( input.position, 1.0f ), WorldMatrix );
	float3 N = mul( input.normal, (float3x3)WorldMatrix );

	output.position = mul( float4( input.position, 1.0f ), WorldViewProjMatrix );
	output.worldPos = P;	
	output.normal = N;
	output.color = input.color;
	
	return output;
}


float4 PSMAIN( in VS_OUTPUT input ) : SV_Target
{
	float3 P = input.worldPos.xyz;// / input.worldPos.w;
	float3 N = normalize( input.normal );
	float3 L = normalize( LightPosition.xyz - P.xyz );
	float3 V = normalize( ViewPosition.xyz - P.xyz );
	float3 H = normalize( L + V );

	float NdotL = dot( L, N );
	float4 ambientIntensity = input.color * Ia;
	float4 diffuseIntensity = input.color * Id * clamp( NdotL, 0.0f, 1.0f );
	float4 specularIntensity = Ks * Is * clamp( pow( abs( dot( H, N ) ), 64.0f ), 0.0f, 1.0f );

	float4 output = ambientIntensity;

	if ( NdotL > 0.0f ) {
		output += diffuseIntensity;
		output += specularIntensity;
	}

	output.a = 1.0f;


	return( output );
}


It has a specular and diffuse portion, but even if those end up contributing zero then you can still output something with the ambient term. I hope that helps, and if you need guidance on how it works, feel free to ask!

You can't really have a single generic vertex shader, since your vertex buffer(s) need to provide every element expected by the vertex shader. So if the vertex shader expects positions, normals, and texture coordinates, you need to provide all of those otherwise your call to create the input layout will fail. So you either need to use different shaders, or you would need to provide "dummy" vertex data if the element is present.

Anyway I would highly recommend that you check out BasicEffect from DirectXTK. It should do exactly what you want, and the library also provides a few other effects if you want to go a little more advanced. All of the source code is available as well, so you can see how they do it.

This topic is closed to new replies.

Advertisement