void Material::bindMaterial(Transform &T, Camera &camera)
{
	glUseProgram(shaderProgram);

	// MATRICES FROM TRANSFORM
	GLint loc = glGetUniformLocation(shaderProgram, "uObjectWorldM");
	if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(T.transform));
	//
	loc = glGetUniformLocation(shaderProgram, "uObjectWorldInverseM");
    //printMat(T.invTransform);
	if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(T.invTransform));
	//
	glm::mat4x4 objectWorldViewPerspect = camera.worldViewProject * T.transform;
	loc = glGetUniformLocation(shaderProgram, "uObjectPerpsectM");
	if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(objectWorldViewPerspect));

	// MATERIAL COLORS
	for (int i = 0; i < (int) colors.size(); i++) {
		if (colors[i].id == -1) {
			loc = glGetUniformLocation(shaderProgram, colors[i].name.c_str());
			colors[i].id = loc;
		}
		if (colors[i].id >= 0) {
			glUniform4fv(colors[i].id, 1, &colors[i].val[0]);
		}
	}
    
    loc = glGetUniformLocation(shaderProgram, "uViewPosition");
    if(loc != -1)
    {
        glm::vec4 positionVec(camera.center, 0);
        glUniform4fv(loc, 1, &positionVec[0]);
    }
    
    loc = glGetUniformLocation(shaderProgram, "uViewDirection");
    if(loc != -1)
    {
        glm::vec4 directionVec(glm::normalize(camera.eye - camera.center), 0);
        glUniform4fv(loc, 1, &directionVec[0]);
    }
    

	// MATERIAL TEXTURES
	for (int i = 0; i < (int) textures.size(); i++) {
		if (textures[i].id == -1) {
			loc = glGetUniformLocation(shaderProgram, textures[i].name.c_str());
			textures[i].id = loc;
		}
		if (textures[i].id >= 0) {
			//printf("\n%d %d\n", textures[i].id, textures[i].val->samplerId);
			glActiveTexture(GL_TEXTURE0 + i);
			glUniform1i(textures[i].id, i);
			glBindTexture(GL_TEXTURE_2D, textures[i].val->textureId);
			glBindSampler(textures[i].id, textures[i].val->samplerId);
		}
	}
}
void DEMGenerator::convert(int screenX, int screenY, int* indexX, int* worldY, int* indexZ) {

	GLint viewport[4];						// Where The Viewport Values Will Be Stored
	GLdouble projection[16];				// Where The 16 Doubles Of The Projection Matrix Are To Be Stored
	GLdouble modelview[16];					// Where The 16 Doubles Of The Modelview Matrix Are To Be Stored
	GLfloat windowX, windowY;

	glGetIntegerv(GL_VIEWPORT, viewport);
	glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
	glGetDoublev(GL_PROJECTION_MATRIX, projection);

	windowX = screenX;
	windowY = viewport[3] - screenY;

	GLdouble pos1X, pos1Y, pos1Z;
	GLdouble pos2X, pos2Y, pos2Z;

	gluUnProject( windowX, windowY, 0.0, modelview, projection, viewport, &pos1X, &pos1Y, &pos1Z);
	gluUnProject( windowX, windowY, 1.0, modelview, projection, viewport, &pos2X, &pos2Y, &pos2Z);

	Vec3f directionVec(pos1X - pos2X, pos1Y - pos2Y, pos1Z - pos2Z);
	Vec3f directionOrigin(pos1X, pos1Y, pos1Z);

	float t = -directionOrigin.y / directionVec.y;

	float x = directionOrigin.x + (t * directionVec.x);
	float z = directionOrigin.z + (t * directionVec.z);

	float offset = this->gridWidth * this->cellSize / 2;

	x += offset;
	z += offset;

	x /= this->cellSize;
	z /= this->cellSize;

	(*indexX) = x;
	(*worldY) = 0;
	(*indexZ) = z;
}
Example #3
0
int main(int argc, char *argv[]) 
{
  // Initialize MPI
#ifdef HAVE_MPI
  MPI_Init(&argc,&argv);
#endif

  // Create a communicator for Epetra objects
#ifdef HAVE_MPI
  Epetra_MpiComm Comm( MPI_COMM_WORLD );
#else
  Epetra_SerialComm Comm;
#endif
 
  bool verbose = false;

  if (argc > 1)
    if (argv[1][0]=='-' && argv[1][1]=='v')
      verbose = true;

  // Get the process ID and the total number of processors
  int MyPID = Comm.MyPID();
#ifdef HAVE_MPI
  int NumProc = Comm.NumProc();
#endif

  // define the parameters of the nonlinear PDE problem
  int nx = 5;
  int ny = 6;  
  double lambda = 1.0;

  PDEProblem Problem(nx,ny,lambda,&Comm);
 
  // starting solution, here a zero vector
  Epetra_Vector InitialGuess(Problem.GetMatrix()->Map());
  InitialGuess.PutScalar(0.0);

  // random vector upon which to apply each operator being tested
  Epetra_Vector directionVec(Problem.GetMatrix()->Map());
  directionVec.Random();

  // Set up the problem interface
  Teuchos::RCP<SimpleProblemInterface> interface = 
    Teuchos::rcp(new SimpleProblemInterface(&Problem) );
  
  // Set up theolver options parameter list
  Teuchos::RCP<Teuchos::ParameterList> noxParamsPtr = Teuchos::rcp(new Teuchos::ParameterList);
  Teuchos::ParameterList & noxParams = *(noxParamsPtr.get());

  // Set the nonlinear solver method
  noxParams.set("Nonlinear Solver", "Line Search Based");

  // Set up the printing utilities
  // Only print output if the "-v" flag is set on the command line
  Teuchos::ParameterList& printParams = noxParams.sublist("Printing");
  printParams.set("MyPID", MyPID); 
  printParams.set("Output Precision", 5);
  printParams.set("Output Processor", 0);
  if( verbose )
    printParams.set("Output Information", 
		NOX::Utils::OuterIteration + 
		NOX::Utils::OuterIterationStatusTest + 
		NOX::Utils::InnerIteration +
		NOX::Utils::Parameters + 
		NOX::Utils::Details + 
		NOX::Utils::Warning +
		NOX::Utils::TestDetails);
  else
    printParams.set("Output Information", NOX::Utils::Error +
		NOX::Utils::TestDetails);

  NOX::Utils printing(printParams);


  // Identify the test problem
  if (printing.isPrintType(NOX::Utils::TestDetails))
    printing.out() << "Starting epetra/NOX_Operators/NOX_Operators.exe" << std::endl;

  // Identify processor information
#ifdef HAVE_MPI
  if (printing.isPrintType(NOX::Utils::TestDetails)) {
    printing.out() << "Parallel Run" << std::endl;
    printing.out() << "Number of processors = " << NumProc << std::endl;
    printing.out() << "Print Process = " << MyPID << std::endl;
  }
  Comm.Barrier();
  if (printing.isPrintType(NOX::Utils::TestDetails))
    printing.out() << "Process " << MyPID << " is alive!" << std::endl;
  Comm.Barrier();
#else
  if (printing.isPrintType(NOX::Utils::TestDetails))
    printing.out() << "Serial Run" << std::endl;
#endif

  int status = 0;

  Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;

  // Need a NOX::Epetra::Vector for constructor
  NOX::Epetra::Vector noxInitGuess(InitialGuess, NOX::DeepCopy);

  // Analytic matrix
  Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( Problem.GetMatrix(), false );

  Epetra_Vector A_resultVec(Problem.GetMatrix()->Map());
  interface->computeJacobian( InitialGuess, *A );
  A->Apply( directionVec, A_resultVec );

  // FD operator
  Teuchos::RCP<Epetra_CrsGraph> graph = Teuchos::rcp( const_cast<Epetra_CrsGraph*>(&A->Graph()), false );
  Teuchos::RCP<NOX::Epetra::FiniteDifference> FD = Teuchos::rcp(
    new NOX::Epetra::FiniteDifference(printParams, iReq, noxInitGuess, graph) );
  
  Epetra_Vector FD_resultVec(Problem.GetMatrix()->Map());
  FD->computeJacobian(InitialGuess, *FD);
  FD->Apply( directionVec, FD_resultVec );

  // Matrix-Free operator
  Teuchos::RCP<NOX::Epetra::MatrixFree> MF = Teuchos::rcp(
    new NOX::Epetra::MatrixFree(printParams, iReq, noxInitGuess) );

  Epetra_Vector MF_resultVec(Problem.GetMatrix()->Map());
  MF->computeJacobian(InitialGuess, *MF);
  MF->Apply( directionVec, MF_resultVec );

  // Need NOX::Epetra::Vectors for tests
  NOX::Epetra::Vector noxAvec ( A_resultVec , NOX::DeepCopy );
  NOX::Epetra::Vector noxFDvec( FD_resultVec, NOX::DeepCopy );
  NOX::Epetra::Vector noxMFvec( MF_resultVec, NOX::DeepCopy );

  // Create a TestCompare class
  NOX::Epetra::TestCompare tester( printing.out(), printing);
  double abstol = 1.e-4;
  double reltol = 1.e-4 ;
  //NOX::TestCompare::CompareType aComp = NOX::TestCompare::Absolute;

  status += tester.testVector( noxFDvec, noxAvec, reltol, abstol,
                              "Finite-Difference Operator Apply Test" );
  status += tester.testVector( noxMFvec, noxAvec, reltol, abstol,
                              "Matrix-Free Operator Apply Test" );

  // Summarize test results  
  if( status == 0 )
    printing.out() << "Test passed!" << std::endl;
  else 
    printing.out() << "Test failed!" << std::endl;

#ifdef HAVE_MPI
  MPI_Finalize();
#endif

  // Final return value (0 = successfull, non-zero = failure)
  return status;

}