Advertisement

Software rendering - compose View matrix

Started by January 29, 2024 02:44 PM
0 comments, last by black4 10 months, 4 weeks ago

I study old code for 90s years. There is function compose View Matrix, it's software rendering project. Please explain this calculations, where is there vector Right, Up, Look like in View matrix from DX? How composed View matrix in this case (below)? I dont know this approach.

void Draw_Cube()
{
	RECT Rc;
	GetClientRect(g_hWnd, &Rc);

	//start compose view matrix

	vector3 VecLook = {0.0f, 0.0f, 1.0f};
	vector3 VecCamPos = {0.0f, 10.0f, -18.0f};

	vector3 VecCamPosInv = { VecCamPos.x * -1.0f,
		VecCamPos.y * -1.0f, 
		VecCamPos.z * -1.0f };

	vector3 VecCamPosInvNorm = Vec3_Normalize(VecCamPosInv);

	float Dot = Vec3_Dot(VecLook, VecCamPosInvNorm);

	float AngleX = acos(Dot);

	float x_rot = AngleX;
	
	float y_rot = 0.0f;

	float z_rot = 0.0f;

	float sx = sinf(x_rot);
    float cx = cosf(x_rot);
    
	float sy = sinf(y_rot);
    float cy = cosf(y_rot);
    
	float sz = sinf(z_rot);
    float cz = cosf(z_rot);

	matrix4x4 MatView;
	
	MatView[0][0] = sx * sy * sz + cy * cz;
	MatView[1][0] = cx * sz;
	MatView[2][0] = sx * cy * sz - sy * cz;
	
	MatView[0][1] = sx * sy * cz - cy * sz;
	MatView[1][1] = cx * cz;
	MatView[2][1] = sx * cy * cz + sy * sz;

	MatView[0][2] = cx * sy;
	MatView[1][2] = -sx;
	MatView[2][2] = cx * cy;

	MatView[3][0] = VecCamPos.x;
	MatView[3][1] = VecCamPos.y;
	MatView[3][2] = VecCamPos.z;

	MatView[0][3] = 0.0f;
	MatView[1][3] = 0.0f;
	MatView[2][3] = 0.0f;
	MatView[3][3] = 1.0f;

	//end compose view matrix
	
	//start vertex transformations
	//cube model has 8 vertices
	for ( int i = 0; i < 8; i++ )
	{

		vector3 Vec = Vec3_Mat4x4_Mul(g_VertBuff[i], MatView);

		Vec.x = Vec.x / Vec.z;
		Vec.y = Vec.y / Vec.z;
		Vec.x = Vec.x / ((float)Rc.right / Rc.bottom);

		Vec.x = Vec.x * Rc.right / 2.0f + Rc.right / 2.0f;
		Vec.y =-Vec.y * Rc.bottom / 2.0f + Rc.bottom / 2.0f;

		g_VertBuffTransformed[i] = Vec;
	}

This topic is closed to new replies.

Advertisement