示例#1
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 );
}