📄 main.cpp
字号:
// Draw the font letter-by-letter
for (c=str; *c != '\0'; c++)
glutBitmapCharacter(font, *c);
}
// DrawBox()
//
// Draw a 2D box at the specified coordinates.
void DrawBox(float ulx, float uly, float lrx, float lry)
{
glBegin(GL_QUADS);
glVertex2f(ulx, uly);
glVertex2f(ulx, lry);
glVertex2f(lrx, lry);
glVertex2f(lrx, uly);
glEnd();
}
// DisplayFunc()
// Handles the display window
void DisplayFunc(void)
{
char infoString1[] = "Move camera with arrow keys";
char quitString[] = "Press <ESC> to Quit";
int i,stringSize;
// Clear the frame buffer and Z buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up a Perspective Projection camera
SetPerspectiveCamera();
// Set up current camera
gluLookAt(gCameraPos.x, gCameraPos.y, gCameraPos.z,
gLookatPos.x, gLookatPos.y, gLookatPos.z,
0, 1, 0); // Up vector
// Draw some 3D
glBlendFunc(GL_ONE, GL_ZERO);
// Draw the worldbox
Box1->Draw();
// Draw the obstacles
for (i = 0; i < MaxObstacles; i++) {
Obstacles[i]->Draw();
}
// Draw the boids
for (i = 0; i < CFlock::FlockCount; i++) {
CFlock::ListOfFlocks[i]->Draw();
}
/////////////////////
// Draw some 2D stuff
/////////////////////
// Set up an Orthographic Perspective camera
SetOrthographicCamera();
// Draw a box for info string 1
stringSize = StringLength(GLUT_BITMAP_TIMES_ROMAN_24, infoString1);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(0x0f, 0x3f, 0xff, 0x7f);
DrawBox(0, 0, 20+stringSize, 34);
// Display info string 1
glBlendFunc(GL_ONE, GL_ZERO);
glColor4ub(0xff, 0xff, 0xff, 0x7f);
DisplayString(10, 10,
GLUT_BITMAP_TIMES_ROMAN_24, infoString1);
// Draw a box for the quit string
stringSize = StringLength(GLUT_BITMAP_TIMES_ROMAN_24, quitString);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(0xff, 0x3f, 0x0f, 0x7f);
DrawBox(gWindowWidth-20-stringSize, 0, gWindowWidth, 34);
// Display the quit string
glBlendFunc(GL_ONE, GL_ZERO);
glColor4ub(0xff, 0xff, 0xff, 0x7f);
DisplayString(gWindowWidth-10-stringSize, 10,
GLUT_BITMAP_TIMES_ROMAN_24, quitString);
// Swap frame buffers
glutSwapBuffers();
// Keep a frame count
gFrameNumber++;
}
// KeyboardFunc()
// Looks for the "ESCAPE" key, by which we quit the app
void KeyboardFunc(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
Shutdown();
break;
}
}
// KeyboardSpecialFunc()
// This function handles the arrow keys to move the camera around.
void KeyboardSpecialFunc(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_UP:
gCameraPos.z -= gKeyStep;
break;
case GLUT_KEY_DOWN:
gCameraPos.z += gKeyStep;
break;
case GLUT_KEY_LEFT:
gCameraPos.x -= gKeyStep;
break;
case GLUT_KEY_RIGHT:
gCameraPos.x += gKeyStep;
break;
case GLUT_KEY_F1:
gCameraPos.y += gKeyStep;
break;
case GLUT_KEY_F2:
gCameraPos.y -= gKeyStep;
break;
case GLUT_KEY_F3:
gLookatPos.x += gKeyStep;
break;
case GLUT_KEY_F4:
gLookatPos.x -= gKeyStep;
break;
case GLUT_KEY_F5:
gLookatPos.y += gKeyStep;
break;
case GLUT_KEY_F6:
gLookatPos.y -= gKeyStep;
break;
case GLUT_KEY_F7:
gLookatPos.z += gKeyStep;
break;
case GLUT_KEY_F8:
gLookatPos.z -= gKeyStep;
break;
case GLUT_KEY_F9:
if (gDrawAxes == TRUE) {
gDrawAxes = FALSE;
} else {
gDrawAxes = TRUE;
}
break;
case GLUT_KEY_F10:
if (gDrawPerceptionDist == TRUE) {
gDrawPerceptionDist = FALSE;
} else {
gDrawPerceptionDist = TRUE;
}
break;
case GLUT_KEY_F11:
if (gDrawKeepawayDist == TRUE) {
gDrawKeepawayDist = FALSE;
} else {
gDrawKeepawayDist = TRUE;
}
break;
case GLUT_KEY_F12:
if (gDrawSeparationDist == TRUE) {
gDrawSeparationDist = FALSE;
} else {
gDrawSeparationDist = TRUE;
}
break;
}
}
// Initialize3D()
// Finishes setting up the GLUT library.
void Initialize3D (void)
{
// Enable Gouraud shading
glShadeModel(GL_SMOOTH);
// Set clear color and alpha
glClearColor(0.1f, 0.2f, 0.2f, 0.2f);
// Enable blending of source pixel data with frame buffer
glEnable(GL_BLEND);
// Render front faces filled
glFrontFace(GL_CCW);
glPolygonMode(GL_FRONT, GL_FILL);
// Initialize camera and lookat positions
gCameraPos = vector3(0.0f, 0.0f, Box1->GetBoxLength() * 2.0f);
gLookatPos = vector3(0.0f, 0.0f, 0.0f);
}
// IdleFunc()
// Idle function for between frames
void IdleFunc(void)
{
int i;
// Call the display routine next time through the main loop
glutPostRedisplay();
// Test: Time to update the boids?
if ((timeGetTime() - gLastTime) >= gMilliSecsPerFrame) {
// yes--update the boids
for (i = 0; i < CFlock::FlockCount; i++) {
CFlock::ListOfFlocks[i]->Update();
}
// save the current time
gLastTime = timeGetTime();
}
}
// ReshapeFunc()
// This function is called when the user changes the window size.
void ReshapeFunc(int w, int h)
{
// Modify the viewport for the new window settings
glViewport (0, 0, w, h);
}
// SetPerspectiveCamera()
// Sets up a perspective (3D) projection
void SetPerspectiveCamera(void)
{
// Enable Z buffer
glEnable(GL_DEPTH_TEST);
// Set the projection matrix
// Two ways shown here-- with the OpenGL routine or the mtxlib
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#if 0
glFrustum (-1.6, 1.6, -1.6, 1.6, 4, 512);
#else
matrix44 perspMatrix = FrustumMatrix44(-1.6, 1.6, -1.6, 1.6, 4, 512);
glLoadMatrixf((float*)&perspMatrix);
#endif
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
#if 1
// Turn backface culling off
glDisable(GL_CULL_FACE);
#else
// Turn backface culling on
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
#endif
}
// SetOrthographicCamera()
// Sets up an orthographic (2D) projection
void SetOrthographicCamera(void)
{
// Disable Z buffer
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 320, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Disable backface culling
glDisable(GL_CULL_FACE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -