Jump to content
  • Advertisement
Sign in to follow this  
axv4745

OpenGL Model Shading Problem

This topic is 4444 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 wrote an HLSL vertex shader to perform some lighting on a mesh; however, the shading doesn't look right. The vertex shader I wrote is actually a port of a CG-GL shader and the application code to pass in the appropriate parameters is also ported from openGL. Here is what the result looks like with the openGL/Cg-vertex-shader (ignore the little ball in front of the model): http://www.rit.edu/~axv4745/muppetsDX/modelSmoothShaded.jpg Here is what the result looks like with the Direct3D/HLSL-vertex-shader (ignore the little ball inside the model): http://www.rit.edu/~axv4745/muppetsDX/modelFlatShaded.jpg As you can see the shading is coming out flat with the HLSL code and smooth the CGGL code. Here is the code for the CG-GL vertex shader:
void ambient( float4 position		: POSITION,
			float4 spec			: COLOR0,
			float4 texIn		: TEXCOORD0,
			float4 normal		: NORMAL,
			uniform float4x4 mvp : _GL_MVP,
			
			uniform float4x4 clipToCube,
			
			out float4 col		: COLOR0,
			out float4 pos		: POSITION,
			out float4 texOut	: TEXCOORD0,
			out float4 envOut	: TEXCOORD3
			) {
	float3 norm = mul(glstate.matrix.invtrans.modelview[0], normal).xyz;			
	
	texOut = texIn;
	col = spec;
	col.a = 1;
	envOut.xyz = mul((float3x3) clipToCube, norm);
	envOut.w = 1;
		
	pos = mul(mvp, position);
}

envOut points to texture unit 3 which is bound to an ambient light map(the one you see in the background of the images I showed). This is the code snippet I use to bind the ambient light map: ext->mglActiveTextureARB(GL_TEXTURE3_ARB); //ext is a custom class glEnable(GL_TEXTURE_CUBE_MAP_ARB); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, ambientMap); the clipToCube variable is initialized in the openGL application as follows: glGetFloatv(GL_TRANSPOSE_MODELVIEW_MATRIX_ARB, mat); cgGLSetMatrixParameterfc(cg_clipToCube[CG_AMBIENT_VERTEX], mat); Here is the corresponding code for the HLSL vertex shader:
uniform matrix normalMatrix;
matrix WorldViewProjMatrix;
matrix clipToCube;

struct VS_INPUT
{
     vector position : POSITION;
     float2 texIn : TEXCOORD0;     
     float3 normal : NORMAL;
     vector ambientCol  : COLOR;
};

struct VS_OUTPUT
{
     vector position : POSITION;
     vector colOut : COLOR;
     float2 texOut : TEXCOORD0;
     float3 envOut : TEXCOORD1;
};

VS_OUTPUT Main(VS_INPUT input) 
{
    // zero out members of output
    VS_OUTPUT output = (VS_OUTPUT)0;
    
    output.position = mul(input.position, WorldViewProjMatrix);
     
	float3 norm = mul(input.normal,(float3x3)normalMatrix);
	
	output.colOut = input.ambientCol;
	output.colOut.w = 1.0;
	
	output.texOut = input.texIn;
	output.envOut.xyz = mul(norm , (float3x3)clipToCube);
	
	return output;
}

envOut points to texture unit 1 which is bound to an ambient light map (the one you see in the background of the images I showed). This is the code snippet I use to bind the ambient light map: deviceHandle->SetTexture(1, ambientMap); deviceHandle->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE ); deviceHandle->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE ); deviceHandle->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT ); the clipToCube, normalMatrix, and WorldViewProjMatrix variables are initialized in the application as follows:
	D3DXMATRIX w,v,p,wv;
	deviceHandle->GetTransform(D3DTS_WORLD , &w);
	deviceHandle->GetTransform(D3DTS_VIEW , &v);
        deviceHandle->GetTransform(D3DTS_PROJECTION , &p);
	wv = w*v;

        //set World-View-Projection variable
        avConstTable->SetMatrix( deviceHandle,avWVPMatHandle,&(wv*p) );

	//set clipToCube variable
	D3DXMATRIX mat;
	D3DXMatrixTranspose(&mat,&(wv));
	avConstTable->SetMatrix( deviceHandle,avCToCHandle, &mat ); 

	//set normal matrix (inverse transpose of upper-left 3x3 matrix) 
	float det = D3DXMatrixDeterminant(&wv);
	D3DXMatrixInverse(&mat,&det,&wv);
	D3DXMatrixTranspose(&mat,&(mat));
	avConstTable->SetMatrix(deviceHandle,avNormMatHandle,&mat);

As far as I can see I'm pretty much doing the same thing in my Direct3D code that is being done in the OpenGL code and yet its looking smooth in GL and flat in Direct3D. Does anyone see anything wrong or have any thoughts as to what might be going wrong? Thanks,

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!