Exemplo n.º 1
0
void KEYMAN::DoHeldKeys(float timefactor, float fps, CAMERA & cam)
{
	//fps camera
	
	float playermovespeedz = 6.0f;
	float playermovespeedx = 3.0f;
	
	//camera movement
	
	VERTEX moveamount;
	
	if (keys[GetKey("UP")])
		moveamount.z += playermovespeedz;
	if (keys[GetKey("PAGEUP")])
		moveamount.z += 50.0*playermovespeedz;
	if (keys[GetKey("INSERT")])
		moveamount.z -= 50.0*playermovespeedz;
	if (keys[GetKey("DOWN")])
		moveamount.z += -playermovespeedz;
	if (keys[GetKey("RIGHT")])
		moveamount.x += -playermovespeedx;
	if (keys[GetKey("LEFT")])
		moveamount.x += playermovespeedx;
	
	moveamount.Scale(3.0f);
	
	moveamount.Scale(timefactor/fps);

	if (freecam)
		cam.MoveRelative(moveamount.x, moveamount.y, moveamount.z);
	VERTEX campos = cam.GetPosition();
	cam.SetPosition(campos);
}
Exemplo n.º 2
0
void TERRAIN::Render(CAMERA &camera)
{
	//Set render states		
	m_pDevice->SetRenderState(D3DRS_LIGHTING, false);
	m_pDevice->SetRenderState(D3DRS_ZWRITEENABLE, true);	
	
	m_pDevice->SetTexture(0, m_pAlphaMap);
	m_pDevice->SetTexture(1, m_diffuseMaps[0]);		//Grass
	m_pDevice->SetTexture(2, m_diffuseMaps[1]);		//Mountain
	m_pDevice->SetTexture(3, m_diffuseMaps[2]);		//Snow
	m_pDevice->SetTexture(4, m_pLightMap);			//Lightmap
	m_pDevice->SetMaterial(&m_mtrl);

	D3DXMATRIX world, vp = camera.GetViewMatrix() * camera.GetProjectionMatrix();
	D3DXMatrixIdentity(&world);
	m_pDevice->SetTransform(D3DTS_WORLD, &world);
	
	//Set vertex shader variables
	m_terrainVS.SetMatrix(m_vsMatW, world);
	m_terrainVS.SetMatrix(m_vsMatVP, vp);
	m_terrainVS.SetVector3(m_vsDirToSun, m_dirToSun);

	m_terrainVS.Begin();
	m_terrainPS.Begin();
		
	for(int p=0;p<(int)m_patches.size();p++)
		if(!camera.Cull(m_patches[p]->m_BBox))
			m_patches[p]->Render();

	m_terrainPS.End();
	m_terrainVS.End();

	m_pDevice->SetTexture(1, NULL);
	m_pDevice->SetTexture(2, NULL);
	m_pDevice->SetTexture(3, NULL);
	m_pDevice->SetTexture(4, NULL);

	//Render Objects
	m_objectsVS.SetMatrix(m_objMatW, world);
	m_objectsVS.SetMatrix(m_objMatVP, vp);
	m_objectsVS.SetVector3(m_objDirToSun, m_dirToSun);
	m_objectsVS.SetVector3(m_objMapSize, D3DXVECTOR3((float)m_size.x, (float)m_size.y, 0.0f));

	m_pDevice->SetTexture(1, m_pLightMap);		//Lightmap
	
	m_objectsVS.Begin();
	m_objectsPS.Begin();

	for(int i=0;i<(int)m_objects.size();i++)
		if(!camera.Cull(m_objects[i].m_BBox))
		{
			D3DXMATRIX m = m_objects[i].m_meshInstance.GetWorldMatrix();
			m_objectsVS.SetMatrix(m_objMatW, m);
			m_objects[i].Render();
		}

	m_objectsVS.End();
	m_objectsPS.End();
}
Exemplo n.º 3
0
CAMERA * LoadCamera(
	const PTree & cfg,
	const float camerabounce,
	std::ostream & error_output)
{
	std::string type, name;
	if (!cfg.get("type", type, error_output)) return 0;
	if (!cfg.get("name", name, error_output)) return 0;

	MATHVECTOR<float, 3> position;
	MATHVECTOR<float, 3> lookat;
	float fov = 0.0;
	float stiffness = 0.0;
	cfg.get("fov", fov);
	cfg.get("stiffness", stiffness);
	cfg.get("position", position);
	if (!cfg.get("lookat", lookat))
	{
		lookat = position + direction::Forward;
	}

	CAMERA * cam;
	if (type == "mount")
	{
		CAMERA_MOUNT * c = new CAMERA_MOUNT(name);
		c->SetEffectStrength(camerabounce);
		c->SetStiffness(stiffness);
		c->SetOffset(position, lookat);
		cam = c;
	}
	else if (type == "chase")
	{
		CAMERA_CHASE * c = new CAMERA_CHASE(name);
		c->SetOffset(position);
		cam = c;
	}
	else if (type == "orbit")
	{
		CAMERA_ORBIT * c = new CAMERA_ORBIT(name);
		c->SetOffset(position);
		cam = c;
	}
	else if (type == "free")
	{
		CAMERA_FREE * c = new CAMERA_FREE(name);
		c->SetOffset(position);
		cam = c;
	}
	else
	{
		error_output << "Unknown camera type " << type << std::endl;
		return 0;
	}
	cam->SetFOV(fov);
	return cam;
}
Exemplo n.º 4
0
HRESULT APPLICATION::Update(float deltaTime)
{
	//Set World matrix
	D3DXMATRIX  matWorld;
	D3DXMatrixIdentity(&matWorld);
	m_pDevice->SetTransform(D3DTS_WORLD, &matWorld);

	//Update m_mouse
	m_mouse.Update();

	//Update m_camera
	m_camera.Update(m_mouse, deltaTime);

	//Change intersection test type
	if(KEYDOWN(VK_SPACE))
	{
		Sleep(300);
		m_intersectType++;
		if(m_intersectType > 2)m_intersectType = 0;
	}	

	if(KEYDOWN(VK_ESCAPE))
		Quit();

	return S_OK;
}	
Exemplo n.º 5
0
int main(int argc, char* argv[]) {
	cout << "Starting program" << endl;

	//Signal to exit program.
	struct sigaction signalHandler;	
	signalHandler.sa_handler = terminate;
	sigemptyset(&signalHandler.sa_mask);
	signalHandler.sa_flags = 0;
	
	sigaction(SIGTERM, &signalHandler, NULL);
	sigaction(SIGINT,  &signalHandler, NULL);

	//Main program
	CAMERA cam = CAMERA();
	cam.setup();
	cam.start();
	
	ObjectLocation object_data;
	
	while(!exitProgram) {
		
		cout << "\t\t\t\t" << "Framerate: " << cam.getFramerate() << endl;
		
		boost::this_thread::sleep(boost::posix_time::milliseconds(500));
	}
	cam.stop();
	cam.close();
	return 0;
}
Exemplo n.º 6
0
/*
	motion():
	Mouse Movement Events
*/
void GAME::motion(int x, int y)
{
	/* Do Region Checks for the Right Viewport */

	/* Rotate */
	if (_mouseDown[MOUSE_LEFT] == true)
	{
		int dx, dy;
		float mul = 0.5f;

		dx = x - _mouseLast.x;
		dy = y - _mouseLast.y;
	

		CAMERA *c = getCamera(x, y);

		if (c)
		{
			c->rotateTheta( c->rotateTheta() + dx * mul );
			c->rotatePhi( c->rotatePhi() + dy * mul );
		}
	}


	/* Zoom */
	if (_mouseDown[MOUSE_RIGHT] == true)
	{
		int dx;
		float d;
		float mul = 0.1f;
		dx = x - _mouseLast.x;

		d = dx * mul;

		
		CAMERA *c = getCamera(x, y);

		/* Zoom */
		if (c)
			c->zoom( c->zoom() + d );
	}

	/* Set Last Position */
	_mouseLast.x = x;
	_mouseLast.y = y;
}
Exemplo n.º 7
0
HRESULT APPLICATION::Update(float deltaTime)
{
	//Control camera
	D3DXMATRIX  matWorld;
	D3DXMatrixIdentity(&matWorld);
	m_pDevice->SetTransform(D3DTS_WORLD, &matWorld);

	//Update mouse
	m_mouse.Update();

	//Update camera
	m_camera.Update(m_mouse, deltaTime);

	if(KEYDOWN(VK_ESCAPE))
		Quit();

	return S_OK;
}	
Exemplo n.º 8
0
// realize camera
template<class CAMERA> void realizeCamera(const CAMERA& _camera)
{
  glMatrixMode(GL_MODELVIEW);
  // realize coordinates
  gluLookAt(
      _camera.eye().x(),
      _camera.eye().y(),
      _camera.eye().z(),
      _camera.center().x(),
      _camera.center().y(),
      _camera.center().z(),
      _camera.up().x(),
      _camera.up().y(),
      _camera.up().z()
      );
}
Exemplo n.º 9
0
/*************
 * DESCRIPTION:   read a camera from scene file
 * INPUT:         iff      iff handler
 *                where    where to insert after
 *                dir      insert direction
 * OUTPUT:        created object
 *************/
OBJECT *ParseCamera(struct IFFHandle *iff, OBJECT *where, int dir)
{
	CAMERA *camera;
	struct ContextNode *cn;
	long error = 0;
	ULONG flags;

	camera = new CAMERA;
	if(!camera)
		return NULL;

	if(!camera->ReadAxis(iff))
	{
		delete camera;
		return NULL;
	}

	while(!error)
	{
		error = ParseIFF(iff, IFFPARSE_RAWSTEP);
		if(error < 0 && error != IFFERR_EOC)
		{
			delete camera;
			return NULL;
		}

		// Get a pointer to the context node describing the current context
		cn = CurrentChunk(iff);
		if(!cn)
			continue;

		if((cn->cn_ID == ID_FORM) && (cn->cn_Type == ID_CAMR))
			break;

		if(error == IFFERR_EOC)
		{
			error = 0;
			continue;
		}

		switch (cn->cn_ID)
		{
			case ID_NAME:
				if(!camera->ReadName(iff))
				{
					delete camera;
					return NULL;
				}
				break;
			case ID_TRCK:
				if(!camera->ReadTrack(iff))
				{
					delete camera;
					return NULL;
				}
				break;
			case ID_FLGS:
				if(rscn_version < 200)
				{
					if(!ReadULONG(iff, &flags, 1))
					{
						delete camera;
						return NULL;
					}
					if(flags & 1)
						camera->flags |= OBJECT_CAMERA_VFOV;
					else
						camera->flags &= ~OBJECT_CAMERA_VFOV;
					if(flags & 4)
						camera->flags |= OBJECT_CAMERA_FOCUSTRACK;
					else
						camera->flags &= ~OBJECT_CAMERA_FOCUSTRACK;
				}
				else
				{
					if(!camera->ReadFlags(iff))
					{
						delete camera;
						return NULL;
					}
				}
				break;
			case ID_FDST:
				if(!ReadFloat(iff,&camera->focaldist,1))
				{
					delete camera;
					return NULL;
				}
				break;
			case ID_APER:
				if(!ReadFloat(iff,&camera->aperture,1))
					return NULL;
				break;
			case ID_FDOV:
				if(!ReadFloat(iff,&camera->hfov,1))
				{
					delete camera;
					return NULL;
				}
				if(!ReadFloat(iff,&camera->vfov,1))
				{
					delete camera;
					return NULL;
				}
				break;
		}
	}

	SetVector(&camera->bboxmin, -camera->size.z*.5, -camera->size.z*.5, -camera->size.z);
	SetVector(&camera->bboxmax, camera->size.z*.5, camera->size.z*1.3, camera->size.z*1.5);

	camera->Insert(where,dir);

	return (OBJECT*)camera;
}
Exemplo n.º 10
0
HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
{
	debug.Print("Application initiated");

	//Create Window Class
	WNDCLASS wc;
	memset(&wc, 0, sizeof(WNDCLASS));
	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
	wc.hInstance     = hInstance;
	wc.lpszClassName = "D3DWND";

	//Register Class and Create new Window
	RegisterClass(&wc);
	m_mainWindow = CreateWindow("D3DWND", "Example 5.6: Frustum Culling", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
	SetCursor(NULL);
	ShowWindow(m_mainWindow, SW_SHOW);
	UpdateWindow(m_mainWindow);

	//Create IDirect3D9 Interface
	IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

    if(d3d9 == NULL)
	{
		debug.Print("Direct3DCreate9() - FAILED");
		return E_FAIL;
	}

	//Check that the Device supports what we need from it
	D3DCAPS9 caps;
	d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);

	//Hardware Vertex Processing or not?
	int vp = 0;
	if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
		vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	//Check vertex & pixelshader versions
	if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
	{
		debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
	}

	//Set D3DPRESENT_PARAMETERS
	D3DPRESENT_PARAMETERS d3dpp;
	d3dpp.BackBufferWidth            = width;
	d3dpp.BackBufferHeight           = height;
	d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
	d3dpp.BackBufferCount            = 1;
	d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
	d3dpp.MultiSampleQuality         = 0;
	d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
	d3dpp.hDeviceWindow              = m_mainWindow;
	d3dpp.Windowed                   = windowed;
	d3dpp.EnableAutoDepthStencil     = true; 
	d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
	d3dpp.Flags                      = 0;
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;

	//Create the IDirect3DDevice9
	if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
								 vp, &d3dpp, &m_pDevice)))
	{
		debug.Print("Failed to create IDirect3DDevice9");
		return E_FAIL;
	}

	//Release IDirect3D9 interface
	d3d9->Release();

	D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
				   DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
				   DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);

	//Create m_light
	::ZeroMemory(&m_light, sizeof(m_light));
	m_light.Type      = D3DLIGHT_DIRECTIONAL;
	m_light.Ambient   = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
	m_light.Diffuse   = D3DXCOLOR(0.9f, 0.9f, 0.9f, 1.0f);
	m_light.Specular  = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
	m_light.Direction = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
	m_pDevice->SetLight(0, &m_light);
	m_pDevice->LightEnable(0, true);

	//Set sampler state
	for(int i=0;i<4;i++)
	{
		m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
	}

	//Init camera
	m_camera.Init(m_pDevice);	

	//Load objects
	LoadObjectResources(m_pDevice);

	//Create 2D Line
	D3DXCreateLine(m_pDevice, &m_pLine);

	//Create city
	m_city.Init(INTPOINT(25, 25));
	m_camera.m_focus = m_city.GetCenter();

	//Init mouse
	m_mouse.InitMouse(m_pDevice, m_mainWindow);

	return S_OK;
}
Exemplo n.º 11
0
bool LoadWorld()
{
	UnloadWorld();
	
	editordata.helppage = 0;
	editordata.numbezinput = 0;
	editordata.mousebounce[1] = false;
	
	//teststrip = track.AddNewRoad();
	
	//begin loading world
	
	LoadingScreen("Loading...\nConfiguration files");
	
	CONFIGFILE setupfile;
	setupfile.Load(settings.GetDataDir() + "/tracks/editor.config");
	setupfile.GetParam("active track", editordata.activetrack);
	if (editordata.activetrack == "")
		editordata.activetrack = "default";
	
	objects.LoadObjectsFromFolder(settings.GetDataDir() + "/tracks/" + 
			editordata.activetrack + "/objects/");
	
	track.Load(editordata.activetrack);
	
	//float aangle[] = {0, 1, 0, 0};
	//track.GetStartOrientation(0).GetAxisAngle(aangle);
	//cam.Rotate(aangle[0], aangle[1], aangle[2], aangle[3]);
	VERTEX start = track.GetStart(0);
	cam.Move(-start.x, -start.y, -start.z);
	cam.Update();
	cam.LoadVelocityIdentity();
	
	//car_file = "s2000";
	//ifstream csfile;
	//car_file = "";
	//csfile.open((settings.GetSettingsDir() + "/selected_car").c_str());
	//car_paint = 0;
/*	if (csfile)
	{
		state.SetCarName(0, utility.sGetLine(csfile));
		state.SetCarPaint(0, utility.iGetParam(csfile));
		csfile.close();
	}
*/
	
	//LoadingScreen("Loading...\nLoading scenery objects");
	//objects.LoadObjectsFromFolder(settings.GetDataDir() + "/tracks/" + state.GetTrackName() + "/objects");
	
	/*trees.DeleteAll();
	int numtrees = 200;
	for (i = 0; i < numtrees/2; i++)
	{
		VERTEX tp;
		tp.x = ((float) rand()/RAND_MAX)*(param[0]/2.0)+param[0]/4.0;
		tp.z = ((float) rand()/RAND_MAX)*(param[2]/2.0)+param[2]/4.0;
		tp.x += param[3];
		tp.z += param[4];
		tp.y = terrain.GetHeight(tp.x, tp.z);
		trees.Add(tp, 40.0, 0, 5);
	}
	
	for (i = 0; i < numtrees/2; i++)
	{
		VERTEX tp;
		tp.x = ((float) rand()/RAND_MAX)*(param[0]/2.0)+param[0]/4.0;
		tp.z = ((float) rand()/RAND_MAX)*(param[2]/2.0)+param[2]/4.0;
		tp.x += param[3];
		tp.z += param[4];
		tp.y = terrain.GetHeight(tp.x, tp.z);
		trees.Add(tp, 60.0, 1, 5);
	}*/
	
	LoadingScreen("Loading...\nDone");
	
	mq1.AddMessage("Editor started, press H for help");
	
	return true;
}
Exemplo n.º 12
0
void Update()
{
	//cam.position.DebugPrint();
	
	bool mainloop = false;
	
	//std::cout << "Mainloop" << std::endl;
	
	//if unpaused and getting a reasonable framerate
	//if (fps > MIN_FPS && timefactor != 0.0f)
	if (fps > 0.0f && timefactor != 0.0f)
	{
		double time_increment = timefactor / fps;
		//std::cout << "Time elapsed: " << execution_time << " + " << time_increment << std::endl;
		
		execution_time += time_increment;
		//int num_updates = (int) (execution_time / FRAME_TIME);
//		double this_frame = (double) num_updates * FRAME_TIME;
		
		mouse.Update(cam, SCREEN_WIDTH, SCREEN_HEIGHT, timefactor, fps);

		cam.dir = mouse.GetDir();
		
		while (execution_time > FRAME_TIME)
		{
			//std::cout << "Frame start: " << execution_time << std::endl;
			
			#ifdef PERFORMANCE_PROFILE
			suseconds_t t1, t2;
			t1 = GetMicroSeconds();
			t1 = GetMicroSeconds();
			#endif
			
			//multiplay.Update(FRAME_TIME);
			
			//weather tick
			
			#ifdef PERFORMANCE_PROFILE
			t2 = GetMicroSeconds();
			cout << "multiplay.Update() ticks: " << t2-t1 << endl;
			t1 = GetMicroSeconds();
			#endif
			
			cam.Update();
			
			//handle input
			//keyman.DoHeldKeys(timefactor, fps, cam);
			keyman.DoHeldKeys(1.0, 1.0/FRAME_TIME, cam);
			
			#ifdef PERFORMANCE_PROFILE
			t1 = GetMicroSeconds();
			t1 = GetMicroSeconds();
			#endif

			#ifdef PERFORMANCE_PROFILE
			t2 = GetMicroSeconds();
			cout << "world update ticks: " << t2-t1 << endl;
			t1 = GetMicroSeconds();
			#endif
			
			keyman.DoOneTimeKeys(cam);
			
			//particle.Update(timefactor, fps);
			//particle.Update(1.0, 1.0/FRAME_TIME);
			
			cam.ExtractFrustum();
			
			//float timepassed = (timefactor/fps)/86400.0;
			float timepassed = (FRAME_TIME)/86400.0;
			/*if (keyman.keys[keyman.GetKey("AccelTimeVFast")])
				timefactor = 10000.0;
			else if (keyman.keys[keyman.GetKey("AccelTimeFast")])
				timefactor = 1000.0;
			else
				timefactor = 1.0;*/
			//float timepassed = (timefactor/fps)/60.0;
			//float timepassed = (timefactor/fps)/10.0;
			abs_time += timepassed;
			day_time += timepassed;
			if (day_time > 1.0f)
				day_time -= 1.0f;
			
			#ifdef PERFORMANCE_PROFILE
			t2 = GetMicroSeconds();
			cout << "Time increment ticks: " << t2-t1 << endl;
			t1 = GetMicroSeconds();
			#endif
			
			mainloop = true;
			
			//terrain.Update(cam, timefactor, fps, day_time);
			//terrain.Update(cam, 1.0, 1.0/FRAME_TIME, day_time);
			
			execution_time -= FRAME_TIME;
			//num_updates++;
			
			//replay.IncrementFrame();
		}
	}
	else
	{
		//print "paused"
	}
	
	if (pauserequest)
	{
		if (timefactor != 0.0f)
			timefactor = 0.0f;
		pauserequest = false;
	}
	if (unpauserequest)
	{
		if (timefactor == 0.0f)
			timefactor = 1.0f;
		unpauserequest = false;
	}

	/*if (!mainloop)
		multiplay.Update(0);*/ //don't need this anymore because we're stopping things from being paused in multiplayer mode
}
Exemplo n.º 13
0
/* Here goes our drawing code */
int drawGLScene()
{
	#ifdef PERFORMANCE_PROFILE
	suseconds_t t1, t2;
	t1 = GetMicroSeconds();
	t1 = GetMicroSeconds();
	#endif
	
	//glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	#ifdef PERFORMANCE_PROFILE
	t2 = GetMicroSeconds();
	cout << "RenderReflectedScene() ticks: " << t2-t1 << endl;
	t1 = GetMicroSeconds();
	#endif
	
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity( );
	gluPerspective( 45.0f, (float)SCREEN_WIDTH/SCREEN_HEIGHT, 0.1f, settings.GetViewDistance() );
	glMatrixMode( GL_MODELVIEW );
	
	glSetup();
	
	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); 
	//glClearColor( 255.0f, 0.0f, 0.0f, 0.0f ); 
	
	#ifdef PERFORMANCE_PROFILE
	t2 = GetMicroSeconds();
	cout << "glSetup() ticks: " << t2-t1 << endl;
	t1 = GetMicroSeconds();
	#endif
	
	utility.Tex2D(3, false);
	utility.Tex2D(2, false);
	utility.Tex2D(1, false);
	utility.Tex2D(0, true);
	
	glStencilMask(~0);
	glClearStencil(0);
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);		// Clear Screen And Depth Buffer
	
	glDisable(GL_STENCIL_TEST);

	
	GLdouble temp_matrix[16];
	int i;
	
	

	cam.PutTransformInto(temp_matrix);
	glLoadMatrixd(temp_matrix);
	
	//reset sun position so it's in the correct frame
	float lp[4];
	lp[0] = LightPosition[0];
	lp[1] = LightPosition[1];
	lp[2] = LightPosition[2];
	lp[3] = 0;
	
	//lighting hardcoded to reasonable position
	lp[0] = 3;
	lp[1] = 3;
	lp[2] = 3;
	lp[3] = 0;
	glLightfv( GL_LIGHT1, GL_POSITION, lp );
	
	#ifdef PERFORMANCE_PROFILE
	t2 = GetMicroSeconds();
	cout << "Matrix setup and light setup ticks: " << t2-t1 << endl;
	t1 = GetMicroSeconds();
	#endif
	
	// model the highlighted vertex belongs to
	OBJECTMODEL * highlighted_model = NULL;
	//if (state.GetGameState() != STATE_INITIALMENU)
	{
		
//		bool normal = true;
		
		glClear (GL_DEPTH_BUFFER_BIT);
		
		#ifdef PERFORMANCE_PROFILE
		t2 = GetMicroSeconds();
		cout << "DrawSky() ticks: " << t2-t1 << endl;
		t1 = GetMicroSeconds();
		#endif
		
		glTranslatef(cam.GetPosition().x,cam.GetPosition().y,cam.GetPosition().z);
	
		/*GLdouble equation[4];
		VERTEX earthnormal = temp - cam.position;
		earthnormal.y += EARTH_RADIUS;
		earthnormal = earthnormal.normalize();
		equation[0] = earthnormal.x;
		equation[1] = -earthnormal.y;
		equation[2] = earthnormal.z;
		equation[3] = (waterheight);
		glClipPlane(GL_CLIP_PLANE1, equation);
		if (underwater)
			glEnable(GL_CLIP_PLANE1);
		else
			glDisable(GL_CLIP_PLANE1);
		*/
		
				
		#ifdef PERFORMANCE_PROFILE
		t2 = GetMicroSeconds();
		cout << "terrain.SetFrustum() ticks: " << t2-t1 << endl;
		t1 = GetMicroSeconds();
		#endif
		
		//glPolygonOffset(1.0,1.0);
		//glPolygonOffset(0.0,10.0);
		//glEnable(GL_POLYGON_OFFSET_FILL);
	
		/*if (normal)
		{
			//GLfloat LightAmbient2[]  = { 0.3f, 0.3f, 0.3f, 1.0f };
			//glLightfv( GL_LIGHT1, GL_AMBIENT, LightAmbient2 );
			glDisable(GL_STENCIL_TEST);
			
			#ifdef PERFORMANCE_PROFILE
			t2 = GetMicroSeconds();
			cout << "terrain.Draw() ticks: " << t2-t1 << endl;
			t1 = GetMicroSeconds();
			#endif
			
			utility.SelectTU(3);
			glDisable(GL_TEXTURE_2D);
			utility.SelectTU(0);
			glEnable(GL_TEXTURE_2D);

			#ifdef PERFORMANCE_PROFILE
			t2 = GetMicroSeconds();
			cout << "trees.Draw() ticks: " << t2-t1 << endl;
			t1 = GetMicroSeconds();
			#endif
			objects.Draw();
			#ifdef PERFORMANCE_PROFILE
			t2 = GetMicroSeconds();
			cout << "objects.Draw() ticks: " << t2-t1 << endl;
			t1 = GetMicroSeconds();
			#endif
			
			//glLightfv( GL_LIGHT1, GL_AMBIENT, LightAmbient );
		}
		
		glDisable(GL_POLYGON_OFFSET_FILL);*/
		
		//glClear (GL_DEPTH_BUFFER_BIT);
		
		//glDisable(GL_LIGHTING);
		objects.Draw();
		
		#ifdef PERFORMANCE_PROFILE
		t2 = GetMicroSeconds();
		cout << "normal draw done" << t2-t1 << endl;
		t1 = GetMicroSeconds();
		#endif
	
		/*
		//draw trees
		foliage.Draw(terrain, cam);
		
		#ifdef PERFORMANCE_PROFILE
		t2 = GetMicroSeconds();
		cout << "foliage.Draw() ticks: " << t2-t1 << endl;
		t1 = GetMicroSeconds();
		#endif
	
		//glDisable(GL_CLIP_PLANE1);
		ships.Draw(false);
	
		terrain.Draw(cam, 1.0f, false, false, true, false, timefactor, fps, day_time);
		//terrain.Draw(cam, 1.0f, false, false, true, false, timefactor, fps, day_time);
		
		//rain is drawn over everything else
		if (!underwater)
			backdrop.DrawRain(day_time);
	
		
		#ifdef PERFORMANCE_PROFILE
		t2 = GetMicroSeconds();
		cout << "DrawRain() ticks: " << t2-t1 << endl;
		t1 = GetMicroSeconds();
		#endif
		
		
		glDisable(GL_CLIP_PLANE1);
	*/	
	
		
		//glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
		/*if (0)
		{	
			//experimental bezier stuff
			BEZIER patch;
			VERTEX fl, fr, bl, br;
			//fl.Set(20,40,0);
			fl.Set(5,10,0);
			fr = fl;
			bl = fl;
			br = fl;
			
			fr.x += 15;
			fr.y += 3;
			
			br.x += 12;
			br.z += 8;
			
			bl.z += 10;
			bl.y -= 2;
			
			patch.SetFromCorners(fl, fr, bl, br);
			
			BEZIER nextpatch;
			VERTEX offset;
			offset.x += -7;
			offset.z += -10;
			fl = fl + offset;
			fr = fr + offset;
			bl = bl + offset;
			br = br + offset;
			
			nextpatch.SetFromCorners(fl, fr, bl, br);
			
			//patch.Attach(nextpatch);
			
			BEZIER thirdpatch;
			offset.zero();
			offset.y += -3;
			offset.z += -10;
			fl = fl + offset;
			fr = fr + offset;
			bl = bl + offset;
			br = br + offset;
			
			thirdpatch.SetFromCorners(fl, fr, bl, br);
			
			//nextpatch.Attach(thirdpatch);
			
			TRACK track;
			ROADSTRIP * teststrip = track.AddNewRoad();
			teststrip->Add(patch);
			teststrip->Add(nextpatch);
			teststrip->Add(thirdpatch);
			
			//teststrip.DeleteLastPatch();
					
			track.VisualizeRoads(true, true);
			
			VERTEX down;
			down.y = -1;
			VERTEX colpoint;
			if (patch.Collide(cam.position.ScaleR(-1.0), down, colpoint))
			{
				//colpoint.DebugPrint();
			}
		}*/
		
		VERTEX camray;
		camray.z = -1;
		camray = cam.dir.ReturnConjugate().RotateVec(camray);
		/*camray.z = 1;
		camray = cam.dir.RotateVec(camray);*/
		VERTEX selvert;
		bool highlightedvert = false;
		if (objects.FindClosestVert(cam.position.ScaleR(-1.0), camray, selvert, highlighted_model))
		{
			/*cam.position.ScaleR(-1.0).DebugPrint();
			selvert.DebugPrint();
			cout << endl;*/
			
			//draw a highlighted vert
			
			highlightedvert = true;
			
			glPushAttrib(GL_ALL_ATTRIB_BITS);
			glDisable(GL_LIGHTING);
			glDisable(GL_TEXTURE_2D);
			glDisable(GL_DEPTH_TEST);
			//glEnable(GL_BLEND);
			//glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
			glPointSize(4.0);
			//glColor4f(1,1,0,0.75);
			glColor4f(1,1,0,1);
			glBegin(GL_POINTS);
			glVertex3fv(selvert.v3());
			glEnd();
			glDisable(GL_BLEND);
			glColor4f(1,1,1,1);
			glPopAttrib();
		}
		
		//left click
		if (highlightedvert && mouse.ButtonDown(1) && !editordata.mousebounce[1])
		{
			int oldnumbezinput = editordata.numbezinput;
			
			//left click on a highlighted vert
			if (vertmode == TWOVERTS)
			{
				if (editordata.numbezinput == 0)
					editordata.numbezinput = 3;
				else if (editordata.numbezinput <= 3)
					editordata.numbezinput = 4;
				else if (editordata.numbezinput <= 4)
					editordata.numbezinput = 7;
				else if (editordata.numbezinput <= 7)
					editordata.numbezinput = 8;
			}
			else
			{
				editordata.numbezinput++;
				if (vertmode == THREEVERTS && (editordata.numbezinput == 2 || editordata.numbezinput == 6))
					editordata.numbezinput++;
			}
			
			if (editordata.numbezinput >= 8)
			{
				//create bezier patch
				BEZIER patch;
				
				if (vertmode == TWOVERTS)
					patch.SetFromCorners(editordata.bezinput[4], selvert, editordata.bezinput[0], editordata.bezinput[3]);
				else
				{
					//copy the front and back selected rows to the patch, then tell it to do the math to find the other points
					// plus center and radius calculations
					
					editordata.bezinput[7] = selvert;
					
					for (int i = 0; i < 4; i++)
						patch.points[3][i] = editordata.bezinput[i];
					for (int i = 0; i < 4; i++)
						patch.points[0][i] = editordata.bezinput[i+4];
					
					/*if (vertmode == THREEVERTS)
					{
						//recalculate the middle two verts
						for (int i = 0; i < 4; i += 3)
						{
							if ((patch.points[i][1] - patch.points[i][2]).len() < 0.0001)
							{
								VERTEX leftslope = patch.points[i][1] - patch.points[i][0];
								VERTEX rightslope = patch.points[i][2] - patch.points[i][3];
								patch.points[i][1] = patch.points[i][0] + leftslope.ScaleR(0.5);
								patch.points[i][2] = patch.points[i][3] + rightslope.ScaleR(0.5);
							}
						}
					}*/
					
					patch.CalculateMiddleRows();
				}
				
				if (activestrip == NULL)
				{
					activestrip = track.AddNewRoad();
					mq1.AddMessage("New road created to hold new patch.");
				}
				
				activestrip->Add(patch);
				
				//editordata.numbezinput = 0;
				editordata.numbezinput = 4;
				
				editordata.bezinput[0] = patch.points[0][0];
				editordata.bezinput[1] = patch.points[0][1];
				editordata.bezinput[2] = patch.points[0][2];
				editordata.bezinput[3] = patch.points[0][3];
			}
			else
			{
				editordata.bezinput[oldnumbezinput] = selvert;
				if (vertmode == THREEVERTS && (oldnumbezinput == 1 || oldnumbezinput == 5))
					editordata.bezinput[oldnumbezinput+1] = selvert;
			}
		}
		
		editordata.mousebounce[1] = mouse.ButtonDown(1);
		
		glPushAttrib(GL_ALL_ATTRIB_BITS);
		glDisable(GL_LIGHTING);
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_DEPTH_TEST);
		glPointSize(4.0);
		glColor4f(1,0,0,1);
		glBegin(GL_POINTS);
		for (i = 0; i < editordata.numbezinput; i++)
		{
			if (vertmode != TWOVERTS || i == 0 || i == 3 || i == 4 || i == 7)
			{
				glVertex3fv(editordata.bezinput[i].v3());
			}
		}
		glEnd();
		glColor4f(1,1,1,1);
		glPopAttrib();
		
		track.VisualizeRoads(true, false, activestrip);
		
		for (int l = 0; l < track.NumLapSeqs(); l++)
		{
			BEZIER * lapbez = track.GetLapSeq(l);
			if (lapbez != NULL)
			{
				VERTEX reddish;
				reddish.x = 0.5;
				reddish.y = 0.2;
				reddish.z = 0.2;
				lapbez->Visualize(true, false, reddish);
			}
		}
		
		
		//image in the framebuffer is now complete.
		
		
		/*//add a brightness/contrast adjustment
		glClear (GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		glPushAttrib(GL_ALL_ATTRIB_BITS);
		glDisable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		glDisable(GL_FOG);
		glDisable(GL_LIGHTING);
		glTranslatef(0,0,-40.0f);
		glBlendFunc(GL_ONE, GL_SRC_ALPHA);
		
		float rd = (float) weathersystem.GetRainDrops();
		
		float rainscale = 0.7f;
		float clearscale = 1.1f;
		float rainbias = 0.035f;
		float clearbias = 0.0f;
		float rainmax = 50.0f;
		
		float rainfactor = (rd/rainmax);
		if (rainfactor > 1.0f)
			rainfactor = 1.0f;
	
		float scale = rainfactor*rainscale+(1.0f-rainfactor)*clearscale;
		float bias = rainfactor*rainbias+(1.0f-rainfactor)*clearbias;
	
	
		glColor4f(bias, bias, bias, scale);
		float x1, y1, x2, y2;
		x1 = -30;
		y1 = -30;
		x2 = 30;
		y2 = 30;
		
		if (scale > 1.0) 
		{
			float remainingScale;
			
			remainingScale = scale;
			glBlendFunc(GL_DST_COLOR, GL_ONE);
			if (remainingScale > 2.0) 
			{
				glColor4f(1, 1, 1, 1);
				while (remainingScale > 2.0) 
				{
					glRectf(x1,y1,x2,y2);
					remainingScale /= 2.0;
				}
			}
			glColor4f(remainingScale - 1,
				remainingScale - 1, remainingScale - 1, 1);
			glRectf(x1,y1,x2,y2);
			glBlendFunc(GL_ONE, GL_ONE);
			if (bias != 0)
			{
				if (bias > 0) 
				{
					glColor4f(bias, bias, bias, 0.0);
				} 
				else 
				{
					glColor4f(-bias, -bias, -bias, 0.0);
					//can't do bias < 0
				}
				glRectf(x1,y1,x2,y2);
			}
		}
		else 
		{
			if (bias > 0) 
			{
				glColor4f(bias, bias, bias, scale);
			}
			else 
			{
				glColor4f(-bias, -bias, -bias, scale);
				//can't do bias < 0
			}
			glBlendFunc(GL_ONE, GL_SRC_ALPHA);
			glRectf(x1,y1,x2,y2);
		}
		
		glPopAttrib();*/
	
		//timer.Draw();
	}
	
	float w, h;
	w = 0.02;
	h = w*(4/3.0);
	utility.Tex2D(0,true);
	utility.Draw2D(0.5-w, 0.5-h, 0.5+w, 0.5+h, editordata.cursortex);
	
	if (fps > 0.0f)
		mq1.Draw(timefactor, fps, font);
	
	
	#ifdef PERFORMANCE_PROFILE
	t2 = GetMicroSeconds();
	cout << "Brightness/contrast adjustment ticks: " << t2-t1 << endl;
	t1 = GetMicroSeconds();
	#endif

	
	
    Frames++;
	frameno++;
	if (frameno >= 30011)
		frameno -= 30011;
	lfps[lfpspos] = pfps;
	lfpspos++;
	if (lfpspos >= AVERAGEFRAMES)
	{
		lfpspos = lfpspos % AVERAGEFRAMES;
		lfpsfull = true;
	}
	
	float tfps = 0.0f;
	int tnum = 0;
	for (i = 0; i < AVERAGEFRAMES; i++)
	{
		if (!(!lfpsfull && i >= lfpspos))
		{
			tfps += lfps[i];
			tnum++;
		}
	}
	fps = std::min(1000.0f,tfps / (float) tnum);
	
	/*lfps += pfps;
    {
		//const int freq = (int) MIN_FPS;
		const int freq = 60;
		if (Frames >= freq)
		{
			fps = lfps / freq;
			Frames = 0;
			lfps = 0;
		}
	}*/
	
	char tempchar[1024];
	sprintf(tempchar, "Frames per second: %f\n", fps);
	
	//font.Print(0.5,0,tempchar,0,0,1,1,0);
	if (showfps)
		font.Print( 0.75, 0.0, tempchar, 1, 5, 1.0 );
	
	// print camera position
	VERTEX pos = cam.GetPosition();
	sprintf(tempchar, "Position: %0.2f, %0.2f, %0.2f\n", -pos.x, -pos.y, -pos.z);
	font.Print( 0.75, 0.025, tempchar, 1, 5, 1.0 );
	
	//VERTEX ang = cam.dir.GetEulerZYX();
	//sprintf(tempchar, "Angle: %0.2f, %0.2f, %0.2f\n", 180/M_PI*ang.x, -180/M_PI*ang.y, 180/M_PI*ang.z);
	//font.Print( 0.75, 0.05, tempchar, 1, 5, 1.0 );
	
	// print object name the highlighted vertex belongs to
	if (highlighted_model)
	{
		string modelname = highlighted_model->name;
		sprintf(tempchar, "Model: %s\n", modelname.c_str());
		font.Print( 0.75, 0.05, tempchar, 1, 5, 1.0 );
	}
	
	#ifdef PERFORMANCE_PROFILE
	t2 = GetMicroSeconds();
	cout << "font.Print() ticks: " << t2-t1 << endl;
	t1 = GetMicroSeconds();
	#endif
	
	
	//draw help screen(s)
	if (editordata.helppage > 2)
		editordata.helppage = 0;
	if (editordata.helppage == 1)
	{
		font.Print(0.1, 0.1, "VDrift Track Editor Keys (press H again for more help)\n"
	"Mouse left click\n"
	"Arrow keys\n"
	"Page Up\n"
	"A, F\n"
	"L\n"
	"K\n"
	"N\n"
	"R\n"
	"S\n"
	"I\n"
	"E\n"
	"- (minus)\n"
	"2, 3, 4\n"
	"BACKSPACE\n"
	"ESCAPE\n"
	, 1, 5, 1.0);
		font.Print(0.3, 0.1, "\n"
	"Select highligted vertex\n"
	"Move around\n"
	"Move forward very fast\n"
	"Automatically try to create the next bezier patch on this road (F: 25 at a time)\n"
	"Add the current camera position to the list of car start locations\n"
	"Remove the last start location from the list\n"
	"Create a new road (the new road is created once you add patches to it)\n"
	"Select the road under the cursor\n"
	"Save the track\n"
	"Print the object that owns the selected vertex to the console\n"
	"Mark a road segment as part of a lap sequence\n"
	"Clear all lap sequences\n"
	"Select vertices 2 at a time, 3 at a time, 4 at a time\n"
	"Delete the last bezier patch on this road\n"
	"Quit the editor\n"
	, 1, 5, 1.0);
	}
	if (editordata.helppage == 2)
		font.Print(0.1, 0.1, "VDrift Track Editor Textual Help\n"
	"The editor starts in vertex selection mode.  Vertices are highlighted yellow as the mouse pointer\n"
	"gets close to them.  Press the left mouse button to select the highlighted vertex.  It will turn\n"
	"red.  Select a vertex on the left side of the road, then a vertex on the right side of the road,\n"
	"with left and right determined by the direction you'd like to create the road in.  You can now select\n"
	"the next two vertices (left and then right), and a green box will be created to indicate a bezier\n"
	"patch has been created there.  Notice that the last two vertices you select are still colored red\n"
	"(selected).  You can now select the next two vertices (left and right), and continue to create bezier\n"
	"patches around the track.  Once you have created a bezier patch, you can alternatively press A to\n"
	"have the editor try to automatically select the next two vertices and auto-create the next patch.\n"
	"This works well in straight areas, not so well in curvy areas.  Press BACKSPACE at any time to delete\n"
	"the last patch that was created."
	, 1, 5, 1.0);


	/* Draw it to the screen */
    SDL_GL_SwapBuffers( );

    
		GLint t = SDL_GetTicks();
		//if (t - T0 >= 50) 
		{
			GLfloat seconds = (t - T0) / 1000.0;
			pfps = 1 / seconds;
			//printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
			T0 = t;
			//Frames = 0;
		}
		
	#ifdef PERFORMANCE_PROFILE
	t2 = GetMicroSeconds();
	cout << "SwapBuffers() ticks: " << t2-t1 << endl;
	t1 = GetMicroSeconds();
	cout << endl;
	#endif
		
    return( TRUE );
}