Esempio n. 1
0
void display(void)
{
	handle_keyboard(&cam, &lookAtPoint, &upVector, &movement_speed);

	// clear the screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	mat4 total, modelView, camMatrix;

	printError("pre display");

	glUseProgram(program);

	// Build matrix
	camMatrix = lookAtv(cam,lookAtPoint,upVector);

	modelView = IdentityMatrix();
	total = Mult(camMatrix, modelView);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);

	glBindTexture(GL_TEXTURE_2D, tex1);		// Bind Our Texture tex1
	DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord");

	printError("display 2");

	glutSwapBuffers();
}
Esempio n. 2
0
void display(void)
{

	checkKeyDowns();
	// clear the screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	mat4 total, modelView, camMatrix;
	
	printError("pre display");
	
	glUseProgram(program);

	// Build matrix
	
	/*
	vec3 cam = {0, 5, 8};
	vec3 lookAtPoint = {2, 0, 2};
	camMatrix = lookAt(cam.x, cam.y, cam.z,
				lookAtPoint.x, lookAtPoint.y, lookAtPoint.z,
				0.0, 1.0, 0.0);*/
	camMatrix = lookAtv(p,l,v);
	modelView = IdentityMatrix();
	total = Mult(camMatrix, modelView);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
	
	glBindTexture(GL_TEXTURE_2D, tex1);		// Bind Our Texture tex1
	DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord");

	printError("display 2");
	
	glutSwapBuffers();
}
Esempio n. 3
0
mat4 lookAt(GLfloat px, GLfloat py, GLfloat pz, 
			GLfloat lx, GLfloat ly, GLfloat lz,
			GLfloat vx, GLfloat vy, GLfloat vz)
{
	vec3 p, l, v;
	
	p = SetVector(px, py, pz);
	l = SetVector(lx, ly, lz);
	v = SetVector(vx, vy, vz);
	
	return lookAtv(p, l, v);
}
Esempio n. 4
0
void Camera::update()
{
  // Calc. patch coordinate to adjust our height.

  float xPosition,zPosition;    
  if(position.x < 0.0) {
    xPosition = blendedSize-1*(fmod(-1*position.x,(float)(blendedSize)));
  }
  else {
    xPosition = fmod(position.x,(float)(blendedSize));  
  }
  if(position.z < 0.0) {
    zPosition = blendedSize-1*(fmod(-1*position.z,(float)(blendedSize)));
  }
  else {
    zPosition = fmod(position.z,(float)(blendedSize));  
  }

  int tempPatchXIndex = floor((position.x - xPosition)/(float)(blendedSize));
  int tempPatchZIndex = floor((position.z - zPosition)/(float)(blendedSize));

  if(tempPatchXIndex != actualPatchXIndex || tempPatchZIndex != actualPatchZIndex){     
    actualPatchXIndex = tempPatchXIndex;        
    actualPatchZIndex = tempPatchZIndex;        
    for(int i = 0; i < gridSize; i++){
      if(terrainVector->at(i).at(0)->yGrid == actualPatchZIndex){
	actualPatchRow = terrainVector->at(i);
      }
    }
    for(int i = 0; i < gridSize; i++){
      if(actualPatchRow.at(i)->xGrid == actualPatchXIndex){
	actualPatch = actualPatchRow.at(i);
      }
    }
  }
  float actualY = actualPatch->calcHeight(xPosition,zPosition);
  float yDiff = position.y - (actualY + groundOffset);
  if(!flying){
    position.y += -yDiff/2.0;
    lookAtPoint.y += -yDiff/2.0;
  }
  else{
    if(yDiff < 0.0){
      position.y += -yDiff/2.0;
      lookAtPoint.y += -yDiff/2.0;
    }
  }
      
  cameraMatrix = lookAtv(position,lookAtPoint,upVector);
  if(!lockFrustum)
    frustumPlanes->update(this);
}
Esempio n. 5
0
//Constructor
Camera::Camera(vec3 pos, GLfloat vel, GLfloat sens, vector<vector<TerrainPatch*>> * terrain, int sizePatch, int overlap,int sizeGrid)
{
  vec3 r = vec3(0.5,0,0);
  position = pos;
  lookAtPoint = VectorAdd(position,r);
  upVector = vec3(0.0,1.0,0.0);
  
  //Terrain information
  terrainVector = terrain;
  patchSize = sizePatch;
  patchOverlap = overlap;
  blendedSize = patchSize-patchOverlap;
  gridSize = sizeGrid;
  //Set inital patch not to equal to starting patch for comparison in  
  actualPatchXIndex = 100;
  actualPatchZIndex = 100;
  groundOffset = 10;
  flying = false;


  //cameraMatrix = lookAtv(position,lookAtPoint,upVector);
  velocity = 1.5;

  projectionNear = 0.8;
#if LOWGRAPHICS == 1
  projectionFar = 800.0;  
#else 
  projectionFar = 1700.0;
#endif
  projectionRight = 0.5/0.75; // for wide screen
  projectionLeft = -0.5/0.75; // for wide screen
  projectionTop = 0.5;
  projectionBottom = -0.5;

  warpPointer=true;
  lockFrustum=false;
  initKeymapManager();

  cameraMatrix = lookAtv(position,lookAtPoint,upVector);
  velocity = vel;
  sensitivity = sens;

  projectionMatrix = frustum(projectionLeft, projectionRight, projectionBottom, projectionTop,projectionNear, projectionFar);

  frustumPlanes = new Frustum(this);

  timer = 30;
  followFlock = false;
  birdView = true;
  flockIndex = -1; // Is set to zero first time we choose to follow a flock.
}
Esempio n. 6
0
void display(void)
{
  printError("pre display");

  // clear the screen (using chosen color earlier)
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glClearColor(1,1,1,0);

  // Set rotation matrix
  phi = ( phi < 2*PI ) ? phi+PI/50 : phi-2*PI+PI/50;

  vec3 trans = {1,0,-3};
  vec3 trans2 = {-1,0,-3};
  vec3 lookAtPoint = {0,0,-3};
  vec3 cameraLocation = {3.0f*cos(phi),0.0f,-3+3.0f*sin(phi)};
  vec3 upVector = {0,1,0};

  mat4 translation = T(trans.x,trans.y,trans.z);
  mat4 rotations = Mult(Ry(phi),Mult(Rx(phi), Rz(phi)));

  mat4 lookAtMatrix = lookAtv(cameraLocation,lookAtPoint,upVector);
  transformMatrix = Mult(translation,rotations);

  // Send translMatrix to Vertex
  glUniformMatrix4fv(glGetUniformLocation(program, "transformMatrix"), 1, GL_TRUE,  transformMatrix.m);
  // Send lookAt-vector to vertex shader
  glUniformMatrix4fv(glGetUniformLocation(program, "lookAtMatrix"), 1, GL_TRUE,  lookAtMatrix.m);

  DrawModel(bunny, program, "in_Position", "in_Normal", "inTexCoord");

  // Model 2
  transformMatrix = T(trans2.x, trans2.y, trans2.z);
  glUniformMatrix4fv(glGetUniformLocation(program, "transformMatrix"), 1, GL_TRUE,  transformMatrix.m);
  DrawModel(bunny_model2, program, "in_Position", "in_Normal", "inTexCoord");

  printError("display");

  glutSwapBuffers(); // Swap buffer so that GPU can use the buffer we uploaded to it and we can write to another
}
Esempio n. 7
0
mat4 Camera::getCameraMatrix() const
{
	return lookAtv(getPosition(), getPosition() + _viewDirection, _upDirection);
}
Esempio n. 8
0
void display(void)
{
  printError("pre display");

  // clear the screen (using chosen color earlier)
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glClearColor(1,1,1,0);

  // Set rotation matrix
  phi = ( phi < 2*PI ) ? phi+PI/100 : phi-2*PI+PI/100;

  // Translations for wings/blades
  mat4 rotation_wings = Ry(PI/2);

  // ---------------------------
  // Movement of camera with keyboard
  handle_keyboard(&cameraLocation, &lookAtPoint, &upVector, &movement_speed);

  // Move skybox after cameraLocation is changed by input handling
  skybox_transform = move_skybox(&cameraLocation);
  // Also move camera
  mat4 lookAtMatrix = lookAtv(cameraLocation,lookAtPoint,upVector);
  // ---------------------------


  glUseProgram(skybox_shaders);
  glUniformMatrix4fv(glGetUniformLocation(skybox_shaders, "lookAtMatrix"), 1, GL_TRUE,  lookAtMatrix.m);

  // Draw skybox
  glDisable(GL_DEPTH_TEST);
  glBindTexture(GL_TEXTURE_2D, skybox_tex);
  glUniformMatrix4fv(glGetUniformLocation(skybox_shaders, "transformMatrix"), 1, GL_TRUE,  skybox_transform.m);
  glUniform1i(glGetUniformLocation(skybox_shaders, "tex"), 0);
  DrawModel(skybox, ground_shaders, "in_Position", NULL, "inTexCoord");


  // Draw and texture ground
  glUseProgram(ground_shaders);
  glUniformMatrix4fv(glGetUniformLocation(ground_shaders, "lookAtMatrix"), 1, GL_TRUE,  lookAtMatrix.m);
  glEnable(GL_DEPTH_TEST);
  glBindTexture(GL_TEXTURE_2D, ground_tex);
  glUniform1i(glGetUniformLocation(ground_shaders, "tex"), 0);
  DrawModel(ground, ground_shaders, "in_Position", NULL, "inTexCoord");

  // Model program
  glUseProgram(program);
  glUniform3f(glGetUniformLocation(program, "camera_position"), cameraLocation.x, cameraLocation.y, cameraLocation.z);
  glUniformMatrix4fv(glGetUniformLocation(program, "lookAtMatrix"), 1, GL_TRUE, lookAtMatrix.m);

  // Draw teapot
  glUniformMatrix4fv(glGetUniformLocation(program, "transformMatrix"), 1, GL_TRUE,  trans_teapot.m);
  DrawModel(teapot, program, "in_Position", "in_Normal", NULL);

  // Draw roof
  glUniformMatrix4fv(glGetUniformLocation(program, "transformMatrix"), 1, GL_TRUE,  trans_roof.m);
  DrawModel(roof, program, "in_Position", "in_Normal", NULL);

  // Draw balcony
  glUniformMatrix4fv(glGetUniformLocation(program, "transformMatrix"), 1, GL_TRUE,  trans_balcony.m);
  DrawModel(balcony, program, "in_Position", "in_Normal", NULL);

  // Draw windmill
  glUniformMatrix4fv(glGetUniformLocation(program, "transformMatrix"), 1, GL_TRUE,  trans_mill.m);
  DrawModel(mill, program, "in_Position", "in_Normal", NULL);

  // Model 2
  for (size_t i = 0; i < 4; i++) {
    transformMatrix = Mult(trans_wings_up, Mult(trans_wings, Rx(phi+i*(PI/2))));
    glUniformMatrix4fv(glGetUniformLocation(program, "transformMatrix"), 1, GL_TRUE,  transformMatrix.m);
    DrawModel(wing, program, "in_Position", "in_Normal", NULL);
  }

  printError("display");

  glutSwapBuffers(); // Swap buffer so that GPU can use the buffer we uploaded to it and we can write to another
}
Esempio n. 9
0
void display(void)
{ 
	if (glutKeyIsDown('a')) { 
	 x=x-0.3;
	}
	if (glutKeyIsDown('d')) { 
	 x=x+0.3;
	}
	if (glutKeyIsDown('w')) { 
	 y=y+0.3;
	}
	if (glutKeyIsDown('s')) { 
	 y=y-0.3;
	}
	if (glutKeyIsDown('l')) { 
	 ry=ry-0.3;
	}
	if (glutKeyIsDown('j')) { 
	 ry=ry+0.3;
	}
	if (glutKeyIsDown('i')) { 
	 rx=rx-0.3;
	}
	if (glutKeyIsDown('k')) { 
	 rx=rx+0.3;
	}


        
 	a += 0.1;
	mat4 rot, trans, trans2, transCam, transCam2, rot2, rotTot, totCam, total, bladeRotMat, windMillPos;

	//clear the screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	rot = Ry(ry/10);
	transCam = T(0, 0, 16);
	transCam2 = T(0,0,-16);
	totCam = Mult(rot, transCam); 
	totCam = Mult(transCam2, totCam); 

	vec3 p = SetVector(x, y+8, 10);
	vec4 s = vec3tovec4(p);
	vec4 d = MultVec4(totCam, s);
	vec3 q = vec4tovec3(d);
	vec3 l = SetVector(0,8,-16);
	vec3 v = SetVector(0,1,0);
	 
	camera = lookAtv(q,l,v);
	glUniformMatrix4fv(glGetUniformLocation(program, "camMatrix"), 1, GL_TRUE, camera.m);

	// walls
rot = Ry(0);
	windMillPos = T(0,0,-16);
	total = Mult(windMillPos, rot);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
	DrawModel(walls, program,"in_Position" , "in_Normal", "inTexCoord");

	// Blade rotation matrix
	trans = T(0, -9, 16);
	trans2 = T(0, 9, -16);
	rot = Rz(a/6);
	bladeRotMat = Mult(rot, trans);
	bladeRotMat = Mult(trans2, bladeRotMat);

	// Bladener
	rot = Ry(1.57);
	trans = T(0, 9, -11);
	total = Mult(trans, rot);
	total = Mult(bladeRotMat, total);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
	DrawModel(blade, program,"in_Position" , "in_Normal", "inTexCoord");
	// Bladupp
	rot = Ry(1.57);
	rot2 = Rz(3.14);
	rotTot = Mult(rot2, rot);
	trans = T(0, 9, -11);
	total = Mult(trans, rotTot);
	total = Mult(bladeRotMat, total);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
	DrawModel(blade, program,"in_Position" , "in_Normal", "inTexCoord");
	// BladeV
	rot = Ry(1.57);
	rot2 = Rz(1.57);
	rotTot = Mult(rot2, rot);
	trans = T(0, 9, -11);
	total = Mult(trans, rotTot);
	total = Mult(bladeRotMat, total);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
	DrawModel(blade, program,"in_Position" , "in_Normal", "inTexCoord");
	// BladeH
	rot = Ry(1.57);
	rot2 = Rz(-1.57);
	rotTot = Mult(rot2, rot);
	trans = T(0, 9, -11);
	total = Mult(trans, rotTot);
	total = Mult(bladeRotMat, total);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
	DrawModel(blade, program,"in_Position" , "in_Normal", "inTexCoord");
	
	

	//roof model
	rot  = Ry(0/6);
	trans = T(0,0,-16);
	total = Mult(trans, rot);

	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
	DrawModel(roof, program,"in_Position" , "in_Normal", "inTexCoord");


	printError("display");
	glutSwapBuffers();
}
Esempio n. 10
0
void display(void)
{
	checkKeyDowns();
	printError("pre display");
	
	GLfloat t = (GLfloat)glutGet(GLUT_ELAPSED_TIME);
	
    mat4 projection = frustum(left, right, bottom, top, near, far);
        
    mat4 view = lookAtv(p, l, v);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    vec3 nollV = SetVector(0.0f, 0.0f, 0.0f);
    
    drawSkybox(view, projection);
    
   glUseProgram(program);

	int j;
	for (j = 0; j < nrOfMills * nrOfMills; j++)
	{
   	 	UploadObjectMatricesVec(windmills[j].pos, 0.0f, view, projection, program);       
  	 	DrawModel(m, program, "in_Position", "in_Normal", "inTexCoord");
   	
   		UploadObjectMatricesVec(windmills[j].pos, 0.0f, view, projection, program);    
  		DrawModel(m2, program, "in_Position", "in_Normal", "inTexCoord");
   
   		UploadObjectMatricesVec(windmills[j].pos, 0.0f, view, projection, program);    
   		DrawModel(m3, program, "in_Position", "in_Normal", "inTexCoord");

   
   		vec3 rAxis = SetVector(1.0f, 0.0f, 0.0f);
   
   		int i;
   		for (i = 0; i < 4; i++)
   		{
   			float x = windmills[j].pos.x;
   			float y = windmills[j].pos.y;
   			float z = windmills[j].pos.z;
   			UploadObjectMatricesArbRot(x + 5.0f, y + 9.0f, z + 0.0f, rAxis, 1.6f * i + t / 720, view, projection, program);    
   			DrawModel(blade, program, "in_Position", "in_Normal", NULL);
   		}
   
	}
    
   glUseProgram(terrainProgram);
   mat4 totMatrix = T(0.0, 0.0, 0.0);
   totMatrix = Mult(view, totMatrix);
   totMatrix = Mult(projection, totMatrix);
   glUniformMatrix4fv(glGetUniformLocation(terrainProgram, "totMatrix"), 1, GL_TRUE, totMatrix.m);

   glBindVertexArray(vertexArrayObjID);	// Select VAO
	glDrawArrays(GL_TRIANGLES, 0, 6*3);	// draw object
	
   //mat4 totMatrix = Mult(view, transMatrix);
   //totMatrix = Mult(projection, totMatrix);
   //glUniformMatrix4fv(glGetUniformLocation(terrainProgram, "totMatrix"), 1, GL_TRUE, totMatrix.m);
   
	printError("display");
	glutSwapBuffers();
}
Esempio n. 11
0
void display(void)
{
	ballXPos = ballXPos + 0.03;
	ballZPos = ballZPos + 0.03;
	int d = floor(ballXPos * 5.0);
	int e = floor(ballZPos * 5.0);

	dx = ballXPos * 5.0 - d;
	dz = ballZPos * 5.0 - e; 
	
	
	if(dx+dz<1){
		dyx = tm->vertexArray[(d+1 + e * texWidth)*3+1] - tm->vertexArray[(d + e * texWidth)*3+1];	
		dyz = tm->vertexArray[(d + (e+1) * texWidth)*3+1] - tm->vertexArray[(d + e * texWidth)*3+1];
		ballYPos = dyx*dx+dyz*dz + tm->vertexArray[(d + e * texWidth)*3+1];
		
	} 
	else{
		dyx = tm->vertexArray[(d+1 + e * texWidth)*3+1] - tm->vertexArray[((d+1) + (e+1) * texWidth)*3+1];	
		dyz = tm->vertexArray[(d + (e+1) * texWidth)*3+1] - tm->vertexArray[((d+1) + (e+1) * texWidth)*3+1];
		dx = (ceil(ballXPos * 5.0)-ballXPos * 5.0);
		dz = (ceil(ballZPos * 5.0)-ballZPos * 5.0);
		ballYPos = dyx*dx+dyz*dz + tm->vertexArray[((d+1) + (e+1) * texWidth)*3+1];
	}
	
	
		

	
	
	//printf("%lf\n",ballYPos);

	
	vec3 lookingDir = Normalize(VectorSub(l, p));
	vec3 walkSideways = Normalize(CrossProduct(lookingDir, v));
	
	if (glutKeyIsDown('a')) { 
	 p = VectorSub(p, walkSideways);
	 l = VectorSub(l, walkSideways);
	}
	
	if (glutKeyIsDown('d')) { 
	  p = VectorAdd(p, walkSideways);
	 l = VectorAdd(l, walkSideways);
	}

	if (glutKeyIsDown('w')) { 	
		p = VectorAdd(p, lookingDir);
		l = VectorAdd(l, lookingDir);
		} 
		
	if (glutKeyIsDown('s')) { 
		p = VectorSub(p, lookingDir);
		l = VectorSub(l, lookingDir);
	 }
		
	if (glutKeyIsDown('l')) { 
		trans = T(-p.x, -p.y, -p.z);
		trans2 = T(p.x, p.y, p.z);
		rot = Ry(-0.05);
		l = MultVec3(trans2, MultVec3(rot, MultVec3(trans, l)));
	}
	
	if (glutKeyIsDown('j')) { 
		trans = T(-p.x, -p.y, -p.z);
		trans2 = T(p.x, p.y, p.z);
		rot = Ry(0.05);
		l = MultVec3(trans2, MultVec3(rot, MultVec3(trans, l)));
	}
	
	if (glutKeyIsDown('i')) { 
	 trans = T(-p.x, -p.y, -p.z);
		trans2 = T(p.x, p.y, p.z);
		rotateUpDown = ArbRotate(walkSideways, 0.05);
		l = MultVec3(trans2, MultVec3(rotateUpDown, MultVec3(trans, l)));
	}
	if (glutKeyIsDown('k')) { 
	 trans = T(-p.x, -p.y, -p.z);
		trans2 = T(p.x, p.y, p.z);
		rotateUpDown = ArbRotate(walkSideways, -0.05);
		l = MultVec3(trans2, MultVec3(rotateUpDown, MultVec3(trans, l)));
	}


	// clear the screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
	printError("pre display");
	
	glUseProgram(program);

	// Build matrix
	camMatrix = lookAtv(p,l,v);
	modelView = IdentityMatrix();
	total = Mult(camMatrix, modelView);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, modelView.m);
	glUniformMatrix4fv(glGetUniformLocation(program, "cameraMatrix"), 1, GL_TRUE, camMatrix.m);
	DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord");

	//Draw sphere
	modelToWorld = T(ballXPos, ballYPos, ballZPos);
	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, modelToWorld.m);
	DrawModel(sphere, program, "inPosition", "inNormal", NULL);


	printError("display 2");
	
	glutSwapBuffers();
}
Esempio n. 12
0
void display(void)
{

  // Clear the screen____________________________________


if((cam.x < goalx + 2 && cam.x > goalx - 2) && (cam.z < goalz + 2 && cam.z > goalz - 2))
{
	
	third_person = true;

}
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



  // Variables
    
  mat4 total, modelView, camMatrix;
  GLfloat t =(GLfloat)glutGet(GLUT_ELAPSED_TIME)/1000; //Scale with constant to make animation slower.
 


  //Read input_____________________________________________
  if (third_person)
    {

	goal_found = true;

      if (glutKeyIsDown('t'))
	{ 
	  third_person = false;  
	}
  
  vec3 pos =  {200, 200, 150};// SetVector(keyx,keyy,keyz);
      //Mouse control_____________________________________________
      //fmin and fmax doesn't really make sense but looks good. 
      GLfloat mousexlocal = 0;
      GLfloat mouseylocal = 0;

      if (mousex < 350 ||  mousex > 250)
	{
	  mousexlocal = 300-mousex;
	}
      else
	{
	  mousexlocal = 0;
	}

      deltaphi = deltaphi + deltaphi*-mousexlocal;
      deltaphi = fmin(0.0001,deltaphi);
      deltaphi = fmax(deltaphi,2 * PI - 0.0001);
      forward = MultVec3(Ry(deltaphi*-mousexlocal), forward);

      vec3 crossvec = CrossProduct(forward,up);

      if (mousey < 350 || mousey > 250)
	{
	  mouseylocal = 300-mousey;
	}  
      else
	{
	  mouseylocal = 0;
	}

      deltatheta = deltatheta + deltatheta* -mouseylocal;
      deltatheta = fmin(0.0001,deltatheta);
      deltatheta = fmax(deltatheta,2*PI - 0.0001);
      forward = MultVec3( ArbRotate(crossvec,deltatheta*-mouseylocal), forward);

      //Create camera matrix_____________________________________________
      camMatrix = lookAtv(pos,VectorAdd(forward,pos),up);
    }




// FIRST PERSON VIEW --------------------------------------------------------
  else
    {
      
      vec3 moveVec;
      
      if (glutKeyIsDown('w')) // forwards, obviously
	{
	  vec3 oldcam = cam; //Save old position to check if its legal.
	  moveVec = ScalarMult(VectorSub(cam, SetVector(lookAtPoint.x, cam.y, lookAtPoint.z)), 0.1); // VectorSub makes a vector in the direction we're looking.
	  cam = VectorSub(cam, moveVec); // The new camera position is the old camera position + the move vector
	  cam.y = getHeight(&ttex,cam.x, cam.z)+2; // Get the correct height from the new position  
	  GLfloat maze_height = getHeight(&ttexm, cam.x, cam.z); // Get the height of the maze at the new (possible) camera position
	  GLfloat ground_height = getHeight(&ttex, cam.x, cam.z); // Get the height of the ground at the new (possible) camera position


	  //Check if the new position is legal
	    if ((ground_height - maze_height) < ground_height) // If 0, shouldn't move ever: OK!
						   // When does it start to move??
	      {
		printf("woot");
		cam = oldcam;
	      }
	}
       else if (glutKeyIsDown('t'))
	 {
	   third_person = true;
	 }

       GLfloat mousexlocal = 0;
      GLfloat mouseylocal = 0;

      if (mousex < 350 ||  mousex > 250)
	{
	  mousexlocal = 300-mousex;
	}
      else
	{
	  mousexlocal = 0;
	}

      deltaphi = deltaphi + deltaphi*-mousexlocal;
      deltaphi = fmin(0.0001,deltaphi);
      deltaphi = fmax(deltaphi,2 * PI - 0.0001);
      forward = MultVec3(Ry(deltaphi*-mousexlocal), forward);

      vec3 crossvec = CrossProduct(forward,up);

      if (mousey < 350 || mousey > 250)
	{
	  mouseylocal = 300-mousey;
	}  
      else
	{
	  mouseylocal = 0;
	}

      deltatheta = deltatheta + deltatheta* -mouseylocal;
      deltatheta = fmin(0.0001,deltatheta);
      deltatheta = fmax(deltatheta,2*PI - 0.0001);
      forward = MultVec3( ArbRotate(crossvec,deltatheta*-mouseylocal), forward);
      lookAtPoint = VectorAdd(forward,cam);
      //Create camera matrix_____________________________________________
      camMatrix = lookAtv(cam,lookAtPoint,up);

    }

  
  // --------------------------------------------------------//
  // SKYBOX
  // --------------------------------------------------------//

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	glUseProgram(programSky);
	glActiveTexture(GL_TEXTURE2);
	//glBindTexture(GL_TEXTURE_2D, tex2);
	trans = T(0.0, 0.0, 0.0);

       	rot = Rx(0);
	total = Mult(trans, rot);
       	glUniformMatrix4fv(glGetUniformLocation(programSky, "mdlMatrix"), 1, GL_TRUE, total.m);

	lookSky = camMatrix;

	lookSky.m[3] = 0;
	lookSky.m[7] = 0;
	lookSky.m[11] = 0;

	glUniformMatrix4fv(glGetUniformLocation(programSky, "lookMatrix"), 1, GL_TRUE, lookSky.m);
	glUniformMatrix4fv(glGetUniformLocation(programSky, "projectionMatrix"), 1, GL_TRUE, projectionMatrix.m);

	glUniform1i(glGetUniformLocation(programSky, "texUnit2"), 2); // Texture unit 2
	DrawModel(skybox, programSky, "in_Position", NULL, "inTexCoord");


	glUseProgram(program);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	//------------------------------------------------------//
	//------------------------------------------------------//



 
  modelView = IdentityMatrix();
  total = Mult(camMatrix, modelView);

  glActiveTexture(GL_TEXTURE1);
  glUseProgram(program);
  glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
  glBindTexture(GL_TEXTURE_2D, tex2);		// Bind Our Texture tex1
  DrawModel(tm, program,"inPosition", "inNormal", "inTexCoord");

  glUseProgram(program);
  glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 0, GL_TRUE, total.m);
  glBindTexture(GL_TEXTURE_2D, tex1);		// Bind Our Texture tex1
  DrawModel(mm, program,"inPosition", "inNormal", "inTexCoord");

  GLfloat y = getHeight(&ttexm, 200, 147);
  mat4 trans = T(goalx, 2, goalz);
  mat4 scale = S(1, 1, 1);
 total = Mult(total, trans);
 total = Mult(total, scale);

 
 if (goal_found)
{
	char* string = "GOOD JOB!! You won :D";
	sfDrawString(100, 100, string);
	//while(true){};
}



  glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
  glBindTexture(GL_TEXTURE_2D, tex3);		// Bind Our Texture tex1


  DrawModel(goal, program,"inPosition", "inNormal", "inTexCoord");


  



  printError("display 2");
	
  glutSwapBuffers();
  printf("x %f\n", cam.x);
  printf("y %f\n", cam.y);
  printf("z %f\n", cam.z);
  
  /* if ( (cam.x < 95) && (cam.x > 85) && */
  /*      (cam.z < 85) && (cam.z > 75)) */

  
}
Esempio n. 13
0
void display(void)
{
  //Read input_____________________________________________
   GLfloat t =(GLfloat)glutGet(GLUT_ELAPSED_TIME)/1000; //Scale with constant to make animation slower.


if (glutKeyIsDown('w'))
    { 
      keyz = keyz +1;
    }
  else if (glutKeyIsDown('s'))
    { 
      keyz = keyz -1;
    }
  else if (glutKeyIsDown('a'))
    { 
      keyx = keyx +1;
    }
  else if (glutKeyIsDown('d'))
    { 
      keyx = keyx -1;
    }
  else if (glutKeyIsDown('e'))
    { 
      keyy = keyy +1;
    }
  else if (glutKeyIsDown('q'))
    { 
      keyy = keyy -1;
    }
  else if (glutKeyIsDown('r'))
    { 
      exit(0);
    }
  // Clear the screen____________________________________
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
  mat4 total, modelView, camMatrix;
	
  printError("pre display");

  vec3 pos = SetVector(keyx,keyy,keyz);
  vec3 up = SetVector(0,1,0);

  //Mouse control_____________________________________________
  //fmin and fmax doesn't really make sense but looks good. 
  GLfloat mousexlocal = 0;
  GLfloat mouseylocal = 0;

  if (mousex < 350 ||  mousex > 250)
    {
      mousexlocal = 300-mousex;
    }
  else
    {
      mousexlocal = 0;
    }

  deltaphi = deltaphi + deltaphi*-mousexlocal;
  deltaphi = fmin(0.0001,deltaphi);
  deltaphi = fmax(deltaphi,2 * PI - 0.0001);
  forward = MultVec3(Ry(deltaphi*-mousexlocal), forward);

  vec3 crossvec = CrossProduct(forward,up);

  if (mousey < 350 || mousey > 250)
    {
      mouseylocal = 300-mousey;
    }  
  else
    {
      mouseylocal = 0;
    }

  deltatheta = deltatheta + deltatheta* -mouseylocal;
  deltatheta = fmin(0.0001,deltatheta);
  deltatheta = fmax(deltatheta,2*PI - 0.0001);
  forward = MultVec3( ArbRotate(crossvec,deltatheta*-mouseylocal), forward);

  //Create camera matrix_____________________________________________
  

  GLfloat x = cos(t)*5 + 35;//Mult(Ry(2*5),x);
  GLfloat z = sin(t)*5 + 75;//Mult(Ry
  //GLfloat z = 65;
  GLfloat y = getHeight(&ttex,x,z) + 0.8;
  
  //  pos.y = pos.yy;

  camMatrix = lookAtv(pos,VectorAdd(forward,pos),up);

  modelView = IdentityMatrix();
  total = Mult(camMatrix, modelView);

  glUseProgram(program);
  glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
  glBindTexture(GL_TEXTURE_2D, tex1);		// Bind Our Texture tex1
  DrawModel(tm, program,"inPosition", "inNormal", "inTexCoord");


  //total = Mult(Ry(2*t),T(10,0,0));
  total = T(x,y,z);
  total = Mult(camMatrix,total);

  glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
  glBindTexture(GL_TEXTURE_2D, tex2);		// Bind Our Texture tex1
  DrawModel(bunny, program, "inPosition", "inNormal", "inTexCoord");

  printError("display 2");
	
  glutSwapBuffers();
}