📄 main.cpp
字号:
/* Copyright (C) Steven Woodcock, 2001.
* All rights reserved worldwide.
*
* This software is provided "as is" without express or implied
* warranties. You may freely copy and compile this source into
* applications you distribute provided that the copyright text
* below is included in the resulting source code, for example:
* "Portions Copyright (C) Steven Woodcock, 2001"
*/
//*********************************************************************
// Name: main.cpp
// Purpose: Driver routine for the Flocking With Teeth (Gems 2)
// demonstration. An OpenGL application pops up to show the
// user what's going on, and an output file is generated to the
// Windows desktop if various DEBUG options are specified
// (see readme.txt).
//*********************************************************************
//
// includes
//
#include <windows.h>
#include <iostream.h>
#include <time.h>
#include <mmsystem.h>
// flocking stuff
#include "CBox.h"
#include "CBoid.h"
#include "CFlock.h"
#include "CObstacle.h"
CBox *Box1;
CFlock *Flocks[MaxFlocks];
CBoid *Flies[MaxFlies];
CBoid *Sparrows[MaxSparrows];
CBoid *Hawks[MaxHawks];
CObstacle *Obstacles[MaxObstacles];
// flocking debug globals
bool gDrawAxes = FALSE;
bool gDrawPreyLine = TRUE;
bool gDrawPerceptionDist = FALSE;
bool gDrawKeepawayDist = FALSE;
bool gDrawSeparationDist = FALSE;
// flocking framerate control variables
float gFrameRate = 20.0f;
float gMilliSecsPerFrame = 1000.0f/gFrameRate;
int gLastTime = 0;
// flocking function prototypes
void Shutdown (void);
// GLUT stuff
#include "glut.h"
#include "mtxlib.h"
// GLUT globals
int gFrameNumber = 0;
vector3 gCameraPos, gLookatPos;
const float gKeyStep = 2.0f;
const unsigned int gWindowWidth = 640;
const unsigned int gWindowHeight = 480;
// GLUT function prototypes
void DisplayFunc (void);
void IdleFunc (void);
void Initialize3D (void);
void KeyboardFunc (unsigned char key, int x, int y);
void KeyboardSpecialFunc (int key, int x, int y);
void ReshapeFunc (int w, int h);
void SetPerspectiveCamera (void);
void SetOrthographicCamera (void);
/////////////////////
// Flocking Functions
/////////////////////
// main
// Makes it go.
int main (void)
{
int i;
// initialize the RNG
srand ((unsigned) time (NULL));
// build a world for our boids to flock around in
Box1 = new CBox (50.0, 50.0, 50.0);
// build some obstacles in said world
Obstacles[0] = new CObstacle (35.0, 5.0, 20.0, 20.0);
Obstacles[1] = new CObstacle (25.0, 3.0, 10.0, 15.0);
Obstacles[2] = new CObstacle (25.0, 3.0, 15.0, 10.0);
Obstacles[3] = new CObstacle (35.0, 5.0, -20.0, -20.0);
////////////////
// boid creation
////////////////
// When creating boids for the predator/prey world, you do so in
// the following steps:
// - Create boid
// - Set boid's type (Fly, Sparrow, or Hawk)
// - Set boid's perception range (NOTE: The constructors have defaults, but
// the demo shows hunting behavior better with specific values. Otherwise
// you could get Flies that can see farther than the Sparrows--and that
// means the Sparrows nearly always starve.
// - Set boid's prey (Fly, Sparrow, or Hawk) if any
// - Set boid's predator (Fly, Sparrow, or Hawk) if any
// - Indicate whether or not this boid can reproduce
// - Add the boid to a flock of like boids
//
// If so desired, you could also call each boid's SetHunger() method
// to set their hunger levels here, overriding the random value assigned
// when the boid was created. I did that with the Flies and the Hawk to
// show how it's done, The Sparrows I left with the random values instead.
// build some boids to move around in this world
myprintf("\nCreating boids\n");
char name[MaxNameLength];
for (i = 0; i < MaxFlies; i++) {
// create boid
sprintf(name, "Fly # %d",i);
Flies[i] = new CBoid(name);
// set its type
Flies[i]->SetType(Fly);
// set perception range (overrides defaults)
Flies[i]->SetPerceptionRange(5.0);
// set its prey and predator (if any)
Flies[i]->SetPreyOf(None);
Flies[i]->SetPredatorOf(Sparrow);
// set reproduction flag
Flies[i]->SetReproduction(TRUE);
}
for (i = 0; i < MaxSparrows; i++) {
// create boid
sprintf(name, "Sparrow # %d",i);
Sparrows[i] = new CBoid(name);
// set its type
Sparrows[i]->SetType(Sparrow);
// set perception range (overrides defaults)
Flies[i]->SetPerceptionRange(10.0);
// set its prey and predator (if any)
Sparrows[i]->SetPreyOf(Fly);
Sparrows[i]->SetPredatorOf(Hawk);
// set reproduction flag
Flies[i]->SetReproduction(FALSE);
}
for (i = 0; i < MaxHawks; i++) {
// create boid
sprintf(name, "Hawk # %d",i);
Hawks[i] = new CBoid(name);
// set its type
Hawks[i]->SetType(Hawk);
// set perception range (overrides defaults)
Flies[i]->SetPerceptionRange(25.0);
// set its prey and predator (if any)
Hawks[i]->SetPreyOf(Sparrow);
Hawks[i]->SetPredatorOf(None);
// set reproduction flag
Flies[i]->SetReproduction(FALSE);
}
// build some flocks for the boids to belong to
for (i = 0; i < MaxFlocks; i++) {
Flocks[i] = new CFlock();
}
myprintf("\nAdding boids to flocks\n");
// now assign the boids to the flocks
myprintf("\n Adding Flies\n");
for (i = 0; i < MaxFlies; i++) {
Flocks[0]->AddTo(Flies[i]);
}
myprintf("\n Adding Sparrows\n");
for (i = 0; i < MaxSparrows; i++) {
Flocks[1]->AddTo(Sparrows[i]);
}
myprintf("\n Adding Hawks\n");
for (i = 0; i < MaxHawks; i++) {
Flocks[2]->AddTo(Hawks[i]);
}
#ifdef FLOCK_DEBUG
for (i = 0; i < MaxFlocks; i++) {
myprintf("Flock %d count = %d\n",i,Flocks[i]->GetCount());
myprintf("Flock %d live count = %d\n",i,Flocks[i]->GetLiveCount());
myprintf("Flock %d dead count = %d\n",i,Flocks[i]->GetDeadCount());
}
myprintf("Total # of flocks = %d\n",CFlock::FlockCount);
#endif
myprintf("\nInitializing GL Code\n");
// initialize the GL code
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition(200, 0);
glutInitWindowSize(gWindowWidth, gWindowHeight);
glutCreateWindow("Flocking With Teeth App");
glutDisplayFunc(DisplayFunc);
glutKeyboardFunc(KeyboardFunc);
glutSpecialFunc(KeyboardSpecialFunc);
glutReshapeFunc(ReshapeFunc);
glutIdleFunc(IdleFunc);
Initialize3D();
glutSwapBuffers();
glutMainLoop();
// we're done...get outta here
return (0);
}
// Shutdown.
// Dumps out all the data about the final positions
// of the boids and then shuts down the demo.
void Shutdown (void)
{
int i;
// dump data about the boids at the end
myprintf("\n\n=========\n\n");
myprintf("Number of generations = %d\n",gFrameNumber);
for (i = 0; i < MaxFlocks; i++) {
Flocks[i]->PrintData();
}
myprintf("\nFinal camera postion = %f %f %f\n", gCameraPos.x,gCameraPos.y,gCameraPos.z);
// clean up all of our allocation pointers
delete Box1;
for (i = 0; i < MaxObstacles; i++) {
delete Obstacles[i];
}
for (i = 0; i < MaxFlocks; i++) {
delete Flocks[i];
}
for (i = 0; i < MaxFlies; i++) {
delete Flies[i];
}
for (i = 0; i < MaxSparrows; i++) {
delete Sparrows[i];
}
for (i = 0; i < MaxHawks; i++) {
delete Hawks[i];
}
// we're done...get outta here
exit(0);
}
/////////////////
// GLUT Functions
/////////////////
// StringLength()
//
// Returns the string length using font "font".
unsigned int StringLength(void* font, const char* str)
{
int stringSize = 0;
const char* c;
for (c=str; *c != '\0'; c++)
stringSize += glutBitmapWidth(font, *c);
return stringSize;
}
// DisplayString()
//
// Display a string using a specific font at the location (x,y)
// on the screen.
void DisplayString(int x, int y, void* font, const char* str)
{
const char* c;
// Position the cursor for font drawing
glRasterPos2i(x, y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -