⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monitor.cpp

📁 浙江大学 RoboCup3D 2006 源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************** *   Copyright (C) 2004 - 2006 by ZJUBase                                  *
 *                                National Lab of Industrial Control Tech. * *                                Zhejiang University, China               *
*                                                                         * *   Team members:                                                         *
 *    Currently the team leader is,                                        * *           Hao JIANG (jianghao@iipc.zju.edu.cn; riveria@gmail.com)       *
 *    In the next season, the leader will be                               * *           Yifeng ZHANG (yfzhang@iipc.zju.edu.cn)                        *
 *    ZJUBase 3D agent is created by                                       * *           Dijun LUO (djluo@iipc.zju.edu.cn)                             *
 *    All the members who has ever contributed:                            * *           Jun JIANG                                                     *
 *           Xinfeng DU (xfdu@iipc.zju.edu.cn)                             *
 *           Yang ZHOU (yzhou@iipc.zju.edu.cn)                             *
 *           Zhipeng YANG                                                  *
 *           Xiang FAN                                                     *
 *                                                                         *
 *   Team Manager:                                                          *
 *      Ms. Rong XIONG (rxiong@iipc.zju.edu.cn)                            *
 *                                                                         *
 *   If you met any problems or you have something to discuss about        * *   ZJUBase. Please feel free to contact us through EMails given below.   * *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * *   This program is distributed in the hope that it will be useful,       * *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * *   GNU General Public License for more details.                          * *                                                                         * *   You should have received a copy of the GNU General Public License     * *   along with this program; if not, write to the                         * *   Free Software Foundation, Inc.,                                       * *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * ***************************************************************************/
#include "main.h"
#include "monitor.h"
#include "gamestate.h"
#include "logfileserver.h"
#include "commserver.h"
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

extern HWND hMainWnd;
/*
void display()
{
    vector<string> geoDebug;
    Monitor::Instance()->Display(geoDebug);
}

void mouseMotion(int x, int y)
{
    Monitor::Instance()->MouseMotion(x, y);
}

void keyboard(unsigned char key, int x, int y)
{
    Monitor::Instance()->Keyboard(key, x, y);
}

void specialkeys(int glutkey, int x, int y)
{
    Monitor::Instance()->SpecialKeys(glutkey, x, y);
}

void mouse(int button, int state, int x, int y)
{
    Monitor::Instance()->Mouse(button, state, x, y);
}

void reshape(int width, int height)
{
    Monitor::Instance()->Reshape(width, height);
}

void idle()
{
    Monitor::Instance()->Idle();
}
*/
bool Monitor::Run()
{
//    glutMainLoop();
    return true;
}

Monitor::Monitor()
{
    mWidth = DEFAULT_WIDTH;
    mHeight = DEFAULT_HEIGHT;
    mCamDelta = 0.7;
    mCameraMode = eFree;
    mDrawUnums = true;
    mDrawOverview = true;
    mServer = DEFAULT_HOST;
    mPort = DEFAULT_PORT;
    mSkip = 1;
    mCycle = -1;

    //JAN
    mLogserver = false;
    mSingleStep = false;
    mAdvance = 0;
    mBackward = false;

    FlagInfo fi;
    fi.mOffset = Vector3(0,0,0);
    fi.mRadius = 0.1;
    memcpy(fi.mColor, sSphereDefaultColor, sizeof(fi.mColor));

    // corner flags left team
    mDefaultFlagInfo = fi;
    fi.mOffset = Vector3(0,0,1.5);
    memcpy(fi.mColor, sTeamColorLeft, sizeof(fi.mColor));
    mFlagInfo[GameState::eFLAG_1_L] = fi;
    mFlagInfo[GameState::eFLAG_2_L] = fi;
    // goal flags left team
    fi.mOffset = Vector3(0,0,0.5);
    mFlagInfo[GameState::eGOAL_1_L] = fi;
    mFlagInfo[GameState::eGOAL_2_L] = fi;
    // goal flags right team
    memcpy(fi.mColor, sTeamColorRight, sizeof(fi.mColor));
    mFlagInfo[GameState::eGOAL_1_R] = fi;
    mFlagInfo[GameState::eGOAL_2_R] = fi;
    // corner flags right team
    fi.mOffset = Vector3(0,0,1.5);
    mFlagInfo[GameState::eFLAG_1_R] = fi;
    mFlagInfo[GameState::eFLAG_2_R] = fi;

}

bool Monitor::Init(int argc, char* argv[])
{
	if (argc == 1) {
		// connect to localhost
        mLogserver = false;
	} else if (argc == 3) {
		string opt(argv[1]), para(argv[2]);
		if (opt == "--logfile") {
			// load from a logfile
			mLogserver = true;
            mServer = para;
		} else if (opt == "--server") {
			// connect to a specific server
			mLogserver = false;
            mServer = para;
		}
	} else {
		return false;
	}

	if (instMonitor != NULL) {
		return false;
	}
	instMonitor = this;

	if (InitInternal(argc, argv) != eOK) {
		return false;
	}

	return true;
}

Monitor::EReturnType
Monitor::InitInternal(int argc, char* argv[])
{
    // init the commserver
    if (mLogserver == true) {
        // a log file
        mCommServer = new LogfileServer;
    } else {
        // a specific server
        mCommServer = new CommServer;
    }

    if (mCommServer->Init(mServer,mPort) == false) {
        return eErrInit;
    }
/*
    // init glut
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(mWidth, mHeight);
	glutCreateWindow("rcssmonitor3D--Windows");
    glutDisplayFunc(display);
    glutMotionFunc(mouseMotion);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(specialkeys);
    glutMouseFunc(mouse);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
*/
    //setup the GLserver with camera coordinates
    Vector3 pos(0.0,-48.0, 24.0);
    Vector3 lookAt(0.0,0.0,0.0);
    Vector3 up(0.0,0.0,1.0);
    mGLServer = GLServer(mWidth, mHeight, pos, lookAt, up, false);
    mGLServer.InitGL();

    return eOK;
}

void Monitor::Usage(int argc, char* argv[])
{
    cerr << "Usage: " << argv[0] << " --logfile (filename)" << endl;
    cerr << "       " << argv[0] << " --server (address)" << endl;
}

void
Monitor::DrawScene(int pass, vector<string>& geoDebug, bool showReal, bool monitorOnly, WM& wm)
{
    Vector3 pos;
    for (int i = static_cast<int>(GameState::eFLAG_1_L);
         i != static_cast<int>(GameState::eILLEGAL);
         ++i)
    {
        if (mGameState.GetFlag(static_cast<GameState::EFlagType>(i), pos))
        {
//            DrawFlag(static_cast<GameState::EFlagType>(i), pos, pass);
        }
    }

    int i = 0;
    int unum;
    TTeamIndex side;
    double size;
    mGLServer.SetWireframe(false);
    while (mGameState.GetPlayer(i, pos, side, unum, size))
    {
        bool noseen = false;
        if (!monitorOnly) {
            if (side == TI_LEFT && wm.noour[unum - 1] > 0) noseen = true;
            if (side == TI_RIGHT && wm.noopp[unum - 1] > 0) noseen = true;
        }
        if (noseen)
            DrawPlayer(-side, unum, pos, size, pass);
        else DrawPlayer(side, unum, pos, size, pass);
        ++i;
    }
    if (mGameState.GetBall(pos, size))
    {
        if (!monitorOnly && wm.noball > 0)
            DrawBall(pos, size, pass, Vector3(sTeamColorNoSeen[0] / 256., sTeamColorNoSeen[1] / 256., sTeamColorNoSeen[2] / 256.));
        else DrawBall(pos, size, pass);
        if (mCameraMode == eFollowBall && pass == 0)
        {
            mGLServer.SetLookAtPos(pos);
        }
    }
    Vector3 panTiltVector[2];
    if (mGameState.GetPanTilt(panTiltVector[0], panTiltVector[1])) {
        glColor4d(200. / 256., 200. / 256., 64. / 256., 1.0);
        glBegin(GL_LINES);
        glVertex3d(panTiltVector[0].x, panTiltVector[0].y, panTiltVector[0].z + size);
        glVertex3d(panTiltVector[1].x, panTiltVector[1].y, panTiltVector[1].z);
        glVertex3d(panTiltVector[0].x, panTiltVector[0].y, panTiltVector[0].z - size);
        glVertex3d(panTiltVector[1].x, panTiltVector[1].y, panTiltVector[1].z);
        glEnd();
    }

    if (showReal) {
        mGLServer.SetWireframe(true);
        GLdouble colorLeft[4], colorRight[4], colorBall[4];
        memcpy(colorBall, sBallColor, sizeof(colorBall));
        memcpy(colorLeft, sTeamColorLeft, sizeof(colorLeft));
        memcpy(colorRight, sTeamColorRight, sizeof(colorRight));
        sTeamColorLeft[0] = sTeamColorLeft[1] = sTeamColorLeft[2] = 0.2;
        sTeamColorRight[0] = sTeamColorRight[1] = sTeamColorRight[2] = 0.2;
        sBallColor[0] = sBallColor[1] = sBallColor[2] = 0.2;
        i = 0;
        while (mGameStateReal.GetPlayer(i, pos, side, unum, size))
        {
            DrawPlayer(side, 0, pos, size, pass);
            ++i;
        }
        if (mGameStateReal.GetBall(pos, size))
        {
            DrawBall(pos, size, pass);
        }
        mGLServer.SetWireframe(false);
        memcpy(sTeamColorLeft, colorLeft, sizeof(colorLeft));
        memcpy(sTeamColorRight, colorRight, sizeof(colorRight));
        memcpy(sBallColor, colorBall, sizeof(colorBall));
    }

// Draw Debug information
    double x,y,z,x1,y1,z1,x2,y2,z2,radius,ang1,ang2,text_x,text_y,text_z;
    int type;
    int r,g,b;
    char buff[1024];
    for(int i=0;i<(int)geoDebug.size();i++){
        buff[0]=0;
        char* tmp = const_cast<char*>(geoDebug[i].c_str());
        if (tmp[0] == 'S') {                // a sphere
            // S x y z radius r g b type str
            bool wireFrame = mGLServer.GetWireframe();
            sscanf(tmp,"S %lf %lf %lf %lf %d %d %d %d %s",&x,&y,&z,&radius,&r,&g,&b,&type,buff);
            if (type >= 0 && type <= 3) {
                if (type == 2 || type == 3) {   // wired sphere
                    mGLServer.SetWireframe(true);
                } else  {
                    mGLServer.SetWireframe(false);
                }
                DrawBall(Vector3(x, y, z), radius, pass, Vector3(r / 256., g / 256., b / 256.));
            }
            mGLServer.SetWireframe(wireFrame);
            text_x = x; text_y = y; text_z = z;
            // print out the text information
            glColor4f(0, 0, 0, 1.0);
            mGLServer.DrawText3D(buff, Vector3(text_x, text_y, text_z));
        } else if (tmp[0] == 'C') {         // a circle
            //  x y radius r g b type str
            sscanf(tmp,"C %lf %lf %lf %d %d %d %d %s",&x,&y,&radius,&r,&g,&b,&type,buff);
            glColor4f(r / 256., g / 256., b / 256., 1.0);
            if (type >= 0 && type <= 3) {
                if (type == 2 || type == 3) {   // an empty pie
                    glBegin(GL_LINE_LOOP);
                } else if (type == 1 || type == 0) {         // a solid pie
                    glBegin(GL_POLYGON);
                }
                const double eps = 1e-8;
                for (double ang = 0; ang < 360 + eps; ang += 1) {
                    double xx = x + radius*cosDeg(ang), yy = y + radius*sinDeg(ang);
                    glVertex3f(xx, yy, 0.1);
                }
                glEnd();
            }
            text_x = x; text_y = y; text_z = 0.1;
            // print out the text information
            glColor4f(0, 0, 0, 1.0);
            mGLServer.DrawText3D(buff, Vector3(text_x, text_y, text_z));
        } else if (tmp[0] == 'R') {         // a circle
            //  x1 y1 x2 y2 r g b type str
            sscanf(tmp,"R %lf %lf %lf %lf %d %d %d %d %s",&x1,&y1,&x2,&y2,&r,&g,&b,&type,buff);
            glColor4f(r / 256., g / 256., b / 256., 1.0);
            if (type >= 0 && type <= 3) {
                if (type == 2 || type == 3) {   // an empty pie
                    glBegin(GL_LINE_LOOP);
                } else if (type == 1 || type == 0) {         // a solid pie
                    glBegin(GL_POLYGON);
                }
                glVertex3f(x1, y1, 0.1);
                glVertex3f(x2, y1, 0.1);
                glVertex3f(x2, y2, 0.1);
                glVertex3f(x1, y2, 0.1);
                glVertex3f(x1, y1, 0.1);
                glEnd();
            }
            text_x = (x1 + x2) / 2; text_y = (y1 + y2) / 2; text_z = 0.1;
            // print out the text information
            glColor4f(0, 0, 0, 1.0);
            mGLServer.DrawText3D(buff, Vector3(text_x, text_y, text_z));
        } else if (tmp[0] == 'L') {         // a line
            // L x1 y1 z1 x2 y2 z2 r g b str
            sscanf(tmp,"L %lf %lf %lf %lf %lf %lf %d %d %d %s",&x1,&y1, &z1, &x2,&y2, &z2, &r,&g,&b,buff);
            glColor4f(r / 256., g / 256., b / 256., 1.0);
            glBegin(GL_LINES);
            glVertex3f(x1, y1, z1);
            glVertex3f(x2, y2, z2);
            glEnd();
            glBegin(GL_LINES);
            glColor4f(r / 256. * 0.3, g / 256. * 0.3, b / 256. * 0.3, 1.0);
            glVertex3f(x1, y1, 0.01);
            glVertex3f(x2, y2, 0.01);
            glEnd();
            text_x = (x1 + x2) / 2; text_y = (y1 + y2) / 2; text_z = (z1 + z2) / 2;
            // print out the text information
            glColor4f(0, 0, 0, 1.0);
            mGLServer.DrawText3D(buff, Vector3(text_x, text_y, text_z));
        } else if (tmp[0] == 'P') {         // a pie
            //  x y radius ang1 ang2 r g b type str
            sscanf(tmp,"P %lf %lf %lf %lf %lf %d %d %d %d %s",&x,&y,&radius,&ang1,&ang2,&r,&g,&b,&type,buff);
            glColor4f(r / 256., g / 256., b / 256., 1.0);
            if (type >= 0 && type <= 3) {
                if (type == 2 || type == 3) {   // an empty pie
                    glBegin(GL_LINE_LOOP);
                } else if (type == 1 || type == 0) {         // a solid pie
                    glBegin(GL_POLYGON);
                }
                glVertex3f(x, y, 0.1);
                const double eps = 1e-8;
                if (ang2 < ang1 + 1e-8) {
                    ang2 += 360;
                }
                for (double ang = ang1; ang < ang2 + eps; ang += 1) {
                    double xx = x + radius*cosDeg(ang), yy = y + radius*sinDeg(ang);
                    glVertex3f(xx, yy, 0.1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -