Пример #1
0
// return m0 * m1
MATRIX4 operator *(const MATRIX4 & m0, const MATRIX4 & m1)
{
	MATRIX4 result;

	for (int i = 0; i < m0.Dimension(); i++)
		for (int j = 0; j < m0.Dimension(); j++) {
			result[i][j] = 0;
			for (int k = 0; k < m0.Dimension(); k++)
				result[i][j] += m0(i,k) * m1(k,j);
		};

	return(result);
}
Пример #2
0
void RENDER_INPUT::PushShadowMatrices()
{
	glActiveTexture(GL_TEXTURE3);
	glMatrixMode(GL_TEXTURE);
	MATRIX4<float> m;
	glGetFloatv (GL_TEXTURE_MATRIX, m.GetArray());
	m = m.Inverse();

	//std::cout << m << std::endl;

	for (int i = 0; i < 3; i++)
	{
		glActiveTexture(GL_TEXTURE4+i);
		glPushMatrix();
		glMultMatrixf(m.GetArray());
	}

	glActiveTexture(GL_TEXTURE0);
	glMatrixMode(GL_MODELVIEW);
}
Пример #3
0
void GRAPHICS_GL2::ChangeDisplay(
	const int width, const int height,
	std::ostream & error_output)
{
	glViewport(0, 0, (GLint)width, (GLint)height);

	GLfloat ratio = (GLfloat)width / (GLfloat)height;
	MATRIX4<float> m;

	glMatrixMode(GL_PROJECTION);
	m.Perspective(45.0f, ratio, 0.1f, 100.0f);
	glLoadMatrixf(m.GetArray());

	glMatrixMode(GL_MODELVIEW);
	m.LoadIdentity();
	glLoadMatrixf(m.GetArray());

	GLUTIL::CheckForOpenGLErrors("ChangeDisplay", error_output);

	w = width;
	h = height;
}
/*
This function sets rot_parent_current data member.
Rotation from this bone local coordinate system 
to the coordinate system of its parent
*/
void Skeleton::compute_rotation_parent_child(Bone *parent, Bone *child)
{
  if(child != NULL)
  { 
    MATRIX4 Rz = MATRIX4::RotationZ(-parent->axis_z * M_PI / 180.0);
    MATRIX4 Ry = MATRIX4::RotationY(-parent->axis_y * M_PI / 180.0);
    MATRIX4 Rx = MATRIX4::RotationX(-parent->axis_x * M_PI / 180.0);

    MATRIX4 tmp1 = Rx * Ry * Rz;
    
    Rz = MATRIX4::RotationZ(child->axis_z * M_PI / 180.0);
    Ry = MATRIX4::RotationY(child->axis_y * M_PI / 180.0);
    Rx = MATRIX4::RotationX(child->axis_x * M_PI / 180.0);

    MATRIX4 tmp2 = Rz * Ry * Rx;

    MATRIX4 tmp = tmp1 * tmp2;
    tmp = tmp.transpose();

    for (int x = 0; x < 4; x++)
      for (int y = 0; y < 4; y++)
        child->rot_parent_current[x][y] = tmp[x][y];
  }
}
void RENDER_INPUT_POSTPROCESS::Render(GLSTATEMANAGER & glstate, std::ostream & error_output)
{
	assert(shader);

	OPENGL_UTILITY::CheckForOpenGLErrors("postprocess begin", error_output);

	glstate.SetColorMask(writecolor, writealpha);
	glstate.SetDepthMask(writedepth);

	if (clearcolor && cleardepth)
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	else if (clearcolor)
		glClear(GL_COLOR_BUFFER_BIT);
	else if (cleardepth)
		glClear(GL_DEPTH_BUFFER_BIT);

	shader->Enable();

	OPENGL_UTILITY::CheckForOpenGLErrors("postprocess shader enable", error_output);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho( 0, 1, 0, 1, -1, 1 );
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	glColor4f(1,1,1,1);
	glstate.SetColor(1,1,1,1);

	assert(blendmode != BLENDMODE::ALPHATEST);
	switch (blendmode)
	{
		case BLENDMODE::DISABLED:
		{
			glstate.Disable(GL_ALPHA_TEST);
			glstate.Disable(GL_BLEND);
			glstate.Disable(GL_SAMPLE_ALPHA_TO_COVERAGE);
		}
		break;

		case BLENDMODE::ADD:
		{
			glstate.Disable(GL_ALPHA_TEST);
			glstate.Enable(GL_BLEND);
			glstate.Disable(GL_SAMPLE_ALPHA_TO_COVERAGE);
			glstate.SetBlendFunc(GL_ONE, GL_ONE);
		}
		break;

		case BLENDMODE::ALPHABLEND:
		{
			glstate.Disable(GL_ALPHA_TEST);
			glstate.Enable(GL_BLEND);
			glstate.Disable(GL_SAMPLE_ALPHA_TO_COVERAGE);
			glstate.SetBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		}
		break;

		case BLENDMODE::PREMULTIPLIED_ALPHA:
		{
			glstate.Disable(GL_ALPHA_TEST);
			glstate.Enable(GL_BLEND);
			glstate.Disable(GL_SAMPLE_ALPHA_TO_COVERAGE);
			glstate.SetBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
		}
		break;

		default:
		assert(0);
		break;
	}

	if (writedepth || depth_mode != GL_ALWAYS)
		glstate.Enable(GL_DEPTH_TEST);
	else
		glstate.Disable(GL_DEPTH_TEST);
	glDepthFunc( depth_mode );
	glstate.Enable(GL_TEXTURE_2D);

	glActiveTexture(GL_TEXTURE0);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);

	OPENGL_UTILITY::CheckForOpenGLErrors("postprocess flag set", error_output);

	// put the camera transform into texture3
	glActiveTexture(GL_TEXTURE3);
	glMatrixMode(GL_TEXTURE);
	glLoadIdentity();
	float temp_matrix[16];
	(cam_rotation).GetMatrix4(temp_matrix);
	glLoadMatrixf(temp_matrix);
	glTranslatef(-cam_position[0],-cam_position[1],-cam_position[2]);
	glActiveTexture(GL_TEXTURE0);
	glMatrixMode(GL_MODELVIEW);

	//std::cout << "postprocess: " << std::endl;
	PushShadowMatrices();

	OPENGL_UTILITY::CheckForOpenGLErrors("shader parameter upload", error_output);

	float maxu = 1.f;
	float maxv = 1.f;

	int num_nonnull = 0;
	for (unsigned int i = 0; i < source_textures.size(); i++)
	{
		//std::cout << i << ": " << source_textures[i] << std::endl;
		glActiveTexture(GL_TEXTURE0+i);
		if (source_textures[i])
		{
			source_textures[i]->Activate();
			num_nonnull++;
			if (source_textures[i]->IsRect())
			{
				maxu = source_textures[i]->GetW();
				maxv = source_textures[i]->GetH();
			}
		}
	}
	if (num_nonnull <= 0)
	{
		error_output << "Out of the " << source_textures.size() << " input textures provided as inputs to this postprocess stage, zero are available. This stage will have no effect." << std::endl;
		return;
	}
	glActiveTexture(GL_TEXTURE0);

	OPENGL_UTILITY::CheckForOpenGLErrors("postprocess texture set", error_output);

	// build the frustum corners
	float ratio = w/h;
	std::vector <MATHVECTOR <float, 3> > frustum_corners(4);
	frustum_corners[0].Set(-lod_far,-lod_far,-lod_far);	//BL
	frustum_corners[1].Set(lod_far,-lod_far,-lod_far);	//BR
	frustum_corners[2].Set(lod_far,lod_far,-lod_far);	//TR
	frustum_corners[3].Set(-lod_far,lod_far,-lod_far);	//TL
	MATRIX4 <float> invproj;
	invproj.InvPerspective(camfov, ratio, 0.1, lod_far);
	for (int i = 0; i < 4; i++)
	{
		invproj.TransformVectorOut(frustum_corners[i][0], frustum_corners[i][1], frustum_corners[i][2]);
		frustum_corners[i][2] = -lod_far;
	}

	// send shader parameters
	{
		MATHVECTOR <float, 3> lightvec = lightposition;
		cam_rotation.RotateVector(lightvec);
		shader->UploadActiveShaderParameter3f("directlight_eyespace_direction", lightvec);
		shader->UploadActiveShaderParameter1f("contrast", contrast);
		shader->UploadActiveShaderParameter1f("znear", 0.1);
		//std::cout << lightvec << std::endl;
		shader->UploadActiveShaderParameter3f("frustum_corner_bl", frustum_corners[0]);
		shader->UploadActiveShaderParameter3f("frustum_corner_br_delta", frustum_corners[1]-frustum_corners[0]);
		shader->UploadActiveShaderParameter3f("frustum_corner_tl_delta", frustum_corners[3]-frustum_corners[0]);
	}


	glBegin(GL_QUADS);

	// send the UV corners in UV set 0, send the frustum corners in UV set 1

	glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
	glMultiTexCoord3f(GL_TEXTURE1, frustum_corners[0][0], frustum_corners[0][1], frustum_corners[0][2]);
	glVertex3f( 0.0f,  0.0f,  0.0f);

	glMultiTexCoord2f(GL_TEXTURE0, maxu, 0.0f);
	glMultiTexCoord3f(GL_TEXTURE1, frustum_corners[1][0], frustum_corners[1][1], frustum_corners[1][2]);
	glVertex3f( 1.0f,  0.0f,  0.0f);

	glMultiTexCoord2f(GL_TEXTURE0, maxu, maxv);
	glMultiTexCoord3f(GL_TEXTURE1, frustum_corners[2][0], frustum_corners[2][1], frustum_corners[2][2]);
	glVertex3f( 1.0f,  1.0f,  0.0f);

	glMultiTexCoord2f(GL_TEXTURE0, 0.0f, maxv);
	glMultiTexCoord3f(GL_TEXTURE1, frustum_corners[3][0], frustum_corners[3][1], frustum_corners[3][2]);
	glVertex3f( 0.0f,  1.0f,  0.0f);

	glEnd();

	OPENGL_UTILITY::CheckForOpenGLErrors("postprocess draw", error_output);

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();

	PopShadowMatrices();

	glstate.Enable(GL_DEPTH_TEST);
	glstate.Disable(GL_TEXTURE_2D);

	for (unsigned int i = 0; i < source_textures.size(); i++)
	{
		//std::cout << i << ": " << source_textures[i] << std::endl;
		glActiveTexture(GL_TEXTURE0+i);
		if (source_textures[i])
			source_textures[i]->Deactivate();
	}
	glActiveTexture(GL_TEXTURE0);

	OPENGL_UTILITY::CheckForOpenGLErrors("postprocess end", error_output);
}
Пример #6
0
void GRAPHICS_GL2::SetupScene(
	float fov, float new_view_distance,
	const MATHVECTOR <float, 3> cam_position,
	const QUATERNION <float> & cam_rotation,
	const MATHVECTOR <float, 3> & dynamic_reflection_sample_pos)
{
	// setup the default camera from the passed-in parameters
	{
		GRAPHICS_CAMERA & cam = cameras["default"];
		cam.fov = fov;
		cam.pos = cam_position;
		cam.orient = cam_rotation;
		cam.view_distance = new_view_distance;
		cam.w = w;
		cam.h = h;
	}

	// create a camera for the skybox with a long view distance
	{
		GRAPHICS_CAMERA & cam = cameras["skybox"];
		cam = cameras["default"];
		cam.view_distance = 10000.0;
	}

	// create a camera for 3d ui elements that has a fixed FOV
	{
		GRAPHICS_CAMERA & cam = cameras["ui3d"];
		cam.fov = 45;
		cam.pos = cam_position;
		cam.orient = cam_rotation;
		cam.view_distance = new_view_distance;
		cam.w = w;
		cam.h = h;
	}

	// create a camera for the dynamic reflections
	{
		GRAPHICS_CAMERA & cam = cameras["dynamic_reflection"];
		cam.pos = dynamic_reflection_sample_pos;
		cam.fov = 90; // this gets automatically overridden with the correct fov (which is 90 anyway)
		cam.orient.LoadIdentity(); // this gets automatically rotated for each cube side
		cam.view_distance = 100.f;
		cam.w = 1.f; // this gets automatically overridden with the cubemap dimensions
		cam.h = 1.f; // this gets automatically overridden with the cubemap dimensions
	}

	// create a camera for the dynamic reflection skybox
	{
		GRAPHICS_CAMERA & cam = cameras["dynamic_reflection_skybox"];
		cam = cameras["dynamic_reflection"];
		cam.view_distance = 10000.f;
	}

	// create an ortho camera for 2d drawing
	{
		GRAPHICS_CAMERA & cam = cameras["2d"];

		// this is the glOrtho call we want: glOrtho( 0, 1, 1, 0, -1, 1 );
		cam.orthomode = true;
		cam.orthomin = MATHVECTOR <float, 3> (0, 1, -1);
		cam.orthomax = MATHVECTOR <float, 3> (1, 0, 1);
	}

	// put the default camera transform into texture3, needed by shaders only
	MATRIX4<float> viewMatrix;
	cam_rotation.GetMatrix4(viewMatrix);
	float translate[4] = {-cam_position[0], -cam_position[1], -cam_position[2], 0};
	viewMatrix.MultiplyVector4(translate);
	viewMatrix.Translate(translate[0], translate[1], translate[2]);

	glActiveTexture(GL_TEXTURE3);
	glMatrixMode(GL_TEXTURE);
	glLoadMatrixf(viewMatrix.GetArray());

	// create cameras for shadow passes
	if (shadows)
	{
		MATRIX4<float> viewMatrixInv = viewMatrix.Inverse();

		std::vector <std::string> shadow_names;
		shadow_names.push_back("near");
		shadow_names.push_back("medium");
		shadow_names.push_back("far");

		for (int i = 0; i < 3; i++)
		{
			float shadow_radius = (1<<i)*closeshadow+(i)*20.0; //5,30,60

			MATHVECTOR <float, 3> shadowbox(1,1,1);
			shadowbox = shadowbox * (shadow_radius*sqrt(2.0));
			MATHVECTOR <float, 3> shadowoffset(0,0,-1);
			shadowoffset = shadowoffset * shadow_radius;
			(-cam_rotation).RotateVector(shadowoffset);
			shadowbox[2] += 60.0;

			GRAPHICS_CAMERA & cam = cameras["shadows_"+shadow_names[i]];
			cam = cameras["default"];
			cam.orthomode = true;
			cam.orthomin = -shadowbox;
			cam.orthomax = shadowbox;
			cam.pos = cam.pos + shadowoffset;
			cam.orient = lightdirection;

			// go through and extract the clip matrix, storing it in a texture matrix
			// premultiply the clip matrix with default camera view inverse matrix
			renderscene.SetOrtho(cam.orthomin, cam.orthomax);
			renderscene.SetCameraInfo(cam.pos, cam.orient, cam.fov, cam.view_distance, cam.w, cam.h);

			MATRIX4<float> clipmat;
			clipmat.Scale(0.5f);
			clipmat.Translate(0.5f, 0.5f, 0.5f);
			clipmat = renderscene.GetProjMatrix().Multiply(clipmat);
			clipmat = renderscene.GetViewMatrix().Multiply(clipmat);
			clipmat = viewMatrixInv.Multiply(clipmat);

			glActiveTexture(GL_TEXTURE4+i);
			glLoadMatrixf(clipmat.GetArray());
		}
	}

	glMatrixMode(GL_MODELVIEW);
	glActiveTexture(GL_TEXTURE0);
}