Sign in to follow this  

OpenGL Model Shading Problem

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): Here is what the result looks like with the Direct3D/HLSL-vertex-shader (ignore the little ball inside the model): 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; = 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;

    // 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; = 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
	avConstTable->SetMatrix( deviceHandle,avCToCHandle, &mat ); 

	//set normal matrix (inverse transpose of upper-left 3x3 matrix) 
	float det = D3DXMatrixDeterminant(&wv);

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this