Sign in to follow this  

Lighting messed up after creating Model class

Recommended Posts

Linkandzelda    110

A few days ago I setup lighting (2 directional lights, an ambient light and a point light) on a project within the main.cpp. Here's the piece of code in the main that handles the lighting setup after setting all the vector values.

XMMATRIX view = XMMatrixIdentity();

// Assign view to camera
view *= player_camera->getViewMatrix();

// Transform model world space
world = XMMatrixRotationX(XMConvertToRadians(x_degrees));
world *= XMMatrixRotationY(XMConvertToRadians(y_degrees));
world *= XMMatrixRotationZ(XMConvertToRadians(z_degrees));
world *= XMMatrixTranslation(noOf2 + -5.0f, noOf3 * -1.3f, noOf * 5.0f);

// Create transpose of model's world space
transpose = XMMatrixTranspose(world); // model world matrix

// Set constant buffer lighting values
cb0_values.directional_light_colour = g_directional_light_colour;
cb0_values.directional_light_colour_2 = g_directional_light_colour_2;
cb0_values.ambient_light_colour = g_ambient_light_colour;

// Transpose directional light
cb0_values.directional_light_vector = XMVector3Transform(g_directional_light_shines_from, transpose);
cb0_values.directional_light_vector = XMVector3Normalize( cb0_values.directional_light_vector );

// Second directional light
transpose = XMMatrixTranspose(world); // model world matrix
cb0_values.directional_light_vector_2 = XMVector3Transform(g_directional_light_shines_from_2, transpose);
cb0_values.directional_light_vector_2 = XMVector3Normalize( cb0_values.directional_light_vector_2 );

// Inverse for point light
inverse = XMMatrixInverse(&determinant, world);
cb0_values.point_light_vector = XMVector3Transform(point_light0_position, inverse);

// WVP into constant buffer
cb0_values.WorldViewProjection = world * view * projection;

// upload the new values for the constant buffer
g_pImmediateContext->UpdateSubresource(g_pConstantBuffer0, 0.1f, 0, &cb0_values, 0, 0);

// Load texture and sampler into resource
g_pImmediateContext->PSSetSamplers(0, 1, &g_pSampler0);
g_pImmediateContext->PSSetShaderResources(0, 1, &g_pTexture0);
g_pImmediateContext->PSSetShaderResources(1, 1, &g_pTexture1);

// Draw the vertex buffer to the back buffer
g_pImmediateContext->Draw(sizeof(vertices), 0);

Below is my Vertex shader code working along side this.

VOut output;

// calculate point light
float4 lightvector = point_light_vector - position;
float point_amount = dot(normalize(lightvector), normal); 				
point_amount =  saturate(point_amount);

// Assign position of vertex
output.position = mul(WVPMatrix, position);

// calculate first directional light
float diffuse_amount = dot(directional_light_vector, normal);
diffuse_amount =  saturate(diffuse_amount);

// second dir light
float diffuse_amount_2 = dot(directional_light_vector_2, normal);
diffuse_amount_2 =  saturate(diffuse_amount_2);

// add all the lights together
output.color = ambient_light_colour + ((directional_light_colour * diffuse_amount) + (directional_light_colour_2 * diffuse_amount_2)) + (point_amount * point_light_colour);

output.texcoord = texcoord;
output.normal = normal;

return output;

The above works 100% and I was rendering multiple of them using a for loop and moving the light sources throughout the world while rotating each model.


Then comes my problem, I adapted the above code into a model class that lets me set and update all the values like you would do with a model class. It also has its own vertex buffer and constant buffer per object, and I copied the code I had inside my main.cpp to handle that. Here's the Draw() function of the model class.

void model::Draw(XMMATRIX *view, XMMATRIX *projection) {
	XMMATRIX world, inverse, transpose;
	XMVECTOR determinant;
	MODEL_CONSTANT_BUFFER model_cb_values;

	world = XMMatrixRotationX(XMConvertToRadians(m_xangle));
	world *= XMMatrixRotationY(XMConvertToRadians(m_yangle));
	world *= XMMatrixRotationZ(XMConvertToRadians(m_zangle));
	world *= XMMatrixTranslation(m_x, m_y, m_z);
	world *= XMMatrixScaling(m_scale, m_scale, m_scale);

	model_cb_values.directional_light_vector =  XMVectorSet(m_dl_x, m_dl_y, m_dl_z, 0.0f);
	model_cb_values.directional_light_colour = XMVectorSet(m_dl_r, m_dl_g, m_dl_b, 0.0f);

	transpose = XMMatrixTranspose(world);
	model_cb_values.directional_light_vector = XMVector3Transform(model_cb_values.directional_light_vector, transpose);
	model_cb_values.directional_light_vector = XMVector3Normalize(model_cb_values.directional_light_vector);

	inverse = XMMatrixInverse(&determinant, world);
	model_cb_values.point_light_vector = XMVector3Transform(XMVectorSet(m_pl_x, m_pl_y, m_pl_z, 0.0f), inverse);

	model_cb_values.ambient_light_colour = XMVectorSet(m_al_r, m_al_g, m_al_b, 0.0f);
	model_cb_values.point_light_colour = XMVectorSet(m_pl_r, m_pl_g, m_pl_b, 0.0f);

	model_cb_values.WorldViewProjection = world*(*view)*(*projection);

	// Set the shader objects as active
    m_pImmediateContext->VSSetShader(m_pVShader, 0, 0);
    m_pImmediateContext->PSSetShader(m_pPShader, 0, 0);
	m_pImmediateContext->VSSetConstantBuffers(0, 1, &m_pConstantBuffer);

	// Load texture and sampler into resource
	m_pImmediateContext->PSSetSamplers(0, 1, &m_pSampler);
	m_pImmediateContext->PSSetShaderResources(0, 1, &m_pTexture);

	m_pImmediateContext->UpdateSubresource(m_pConstantBuffer, 0, 0, &model_cb_values, 0, 0);


It takes pointers of view and projection which is calculated before drawing and passed in. This is the updated shader code, which is pretty much identical as models were adapted to use a new shader.

cbuffer CB0
	matrix WVPMatrix;	// 64 bytes
	float4 directional_light_vector;	// 16 bytes
	float4 directional_light_colour;	// 16 bytes
	float4 ambient_light_colour;	// 16 bytes
	float4 point_light_vector;	// 16 bytes
	float4 point_light_colour;	// 16 bytes
	float4 packing; // 16 bytes
} // TOTAL SIZE = 112 bytes

Texture2D texture0;
SamplerState sampler0;

struct VOut
	float4 position : SV_POSITION;
	float4 color : COLOR;
	float2 texcoord :TEXCOORD;

VOut ModelVS(float4 position : POSITION,  float2 texcoord : TEXCOORD, float3 normal : NORMAL)
	VOut output;

	float4 default_color = {1.0f, 1.0f, 1.0f, 1.0f };

	float4 lightvector = point_light_vector - position;
	float point_amount = dot(normalize(lightvector), normal); 						
	point_amount =  saturate(point_amount);

	output.position = mul(WVPMatrix, position);

	output.texcoord = texcoord;

	float diffuse_amount = dot(directional_light_vector, normal);
	diffuse_amount =  saturate(diffuse_amount);

	output.color = ambient_light_colour + (directional_light_colour * diffuse_amount) + (point_amount * point_light_colour);
	return output;

float4 ModelPS(float4 position : SV_POSITION, float4 color : COLOR, float2 texcoord : TEXCOORD) : SV_TARGET 
	return texture0.Sample(sampler0, texcoord) * color;

Then I updated my main.cpp and set all the respective values identical to the ones from when it was running in the main like so.

Model1->setDirLight(-2.0f, 0.0f, -5.0f, lighting0_colour[0], lighting0_colour[1], lighting0_colour[2]);
Model1->setPtLight(3, 3, 0, 0.0f, 0.0f, 2.0f);
Model1->setAmbLight(0.1f, 0.1f, 0.1f);



Model1->Draw(&view, &projection);

The above results in the video here:


You can see when I'm setting up the light positions that there's a directional light in the front left, (which I set to white), and a point light on 3,3,0 which is the right side of the object, and its set to blue. As soon as the video starts the object is in position 0 0 0 and no rotation, so everything looks fine. Then I begin to rotate the object and the lighting goes completely screwed and inverted and all sorts else. In the main.cpp version I was able to move the point light around the object (without doing any rotations) and the effects were what you expect. I mapped the point light in the model example (not in the video) and the results are not what you would expect at all, so something is off.


Is there anything I could have missed out to cause this? I've made this adaptation from main.cpp to model.cpp 3 times and the results are the same so I don't think I'm missing any code. If needed I can supply more code.


Thanks in advance, this was a long post with lots of code.


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