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

📄 glserver.cpp

📁 浙江大学 RoboCup3D 2006 源代码
💻 CPP
字号:
/*************************************************************************** *   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 <gl/gl.h>
//#include <gl/glut.h>
#include "glserver.h"
#include <iostream>
using namespace std;
#include "glut"

GLServer::GLServer(int width, int height,
                   Vector3 camPos,
                   Vector3 lookAtPos,
                   Vector3 up, bool wire)
{
    mCamPos  = camPos;
    mLookAt  = lookAtPos;
    mUp      = up;

    mWidth     = width;
    mHeight    = height;
    mWireframe = wire;

    mCamera = Camera(camPos, lookAtPos, up);
}

//---------------------------------ApplyCamera--------------------------
//
//----------------------------------------------------------------------
void GLServer::ApplyCamera()
{
    //switch z-buffer on
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);

    //create a viewport
    glViewport(0,0,mWidth,mHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //setup camera angle, ratio, near and far clipping plane
    gluPerspective(45.0f,(GLdouble)mWidth/(GLdouble)mHeight,0.1f,200.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    mCamera.Look();
}

//---------------------------------initGL-------------------------------
//
// OpenGL initialisation function
//----------------------------------------------------------------------
void GLServer::InitGL (void)
{
    glDisable(GL_LIGHTING);
	glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
}

void GLServer::DrawTextPix(const char* text, Vector2 pix, ETextAlign ta)
{

    int width = GetTextWidth(text);
    switch (ta)
    {
    case eCENTER:
        pix[0] = (mWidth - width)/ 2.0 + pix[0];
        break;
    case eRIGHT:
        pix[0] = mWidth - width + pix[0];
        break;
    default: ;
    }

    const Vector2 pos(pix[0] / (double)mWidth * 2 - 1.0f,
                       1.0f - (pix[1] / (double)mHeight * 2));
    DrawText(text,pos);
}

int
GLServer::GetTextHeight() const
{
    // currently only GLUT_BITMAP_9_BY_15 is used
    return 15;
}

int
GLServer::GetTextWidth(const char* text) const
{
    // currently only GLUT_BITMAP_9_BY_15 is used
    int width = 0;
    for (char* s = (char*)text; *s; ++s)
    {
        width += glutBitmapWidth(GLUT_BITMAP_9_BY_15, *s);
    }
    return width;
}

void
GLServer::DrawText3D(const char* text, const Vector3& pos)
{
    glDisable (GL_DEPTH_TEST);
    glDisable (GL_TEXTURE_2D);

    glRasterPos3f(pos.x,pos.y,pos.z);
    for (const char* s = text; *s; ++s)
    {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s);
    }

    glEnable (GL_TEXTURE_2D);
    glEnable (GL_DEPTH_TEST);
}

void
GLServer::DrawText3D(const string& text, const Vector3& pos)
{
    glDisable (GL_DEPTH_TEST);
    glDisable (GL_TEXTURE_2D);

    glRasterPos3f(pos.x,pos.y,pos.z);
    for (unsigned int i = 0; i < text.length(); ++i)
    {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, text[i]);
    }

    glEnable (GL_TEXTURE_2D);
    glEnable (GL_DEPTH_TEST);
}

//--------------------------drawText-------------------------------------
//draws a given text string onto the screen at position pos;; (-1,1)
//is top left, (+1,-1) is bottom right of the viewport
//-----------------------------------------------------------------------
void GLServer::DrawText(const char* text, Vector2 pos)
{
    glDisable (GL_TEXTURE_2D);
    glDisable (GL_DEPTH_TEST);

    glMatrixMode (GL_PROJECTION);
    glPushMatrix ();
    glLoadIdentity ();
    glOrtho (0.0,mWidth,0.0,mHeight,-0.0,0.0);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();

    glRasterPos2f(pos[0],pos[1]);
    for (const char* s = text; *s; ++s)
        {
            glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s);
        }
}


void GLServer::DrawGroundRectangle(Vector3 pos, double szX, double szY,
                                   double angleDeg, double height)
{
    const int faceNum = 10;

    glPushMatrix();
    glRotatef(0,1,0,angleDeg);
    glTranslatef(pos[0],pos[1], pos[2]);
    glNormal3f(0,0,1);

    // store the sizes of our faces
    GLdouble deltaX = szX/faceNum;
    GLdouble deltaY = szY/faceNum;

    GLdouble x,y;

    for (int i=0; i<faceNum; i++)
        {
            y = i*deltaY;

            //draw face as Quadric strip
            glBegin(GL_QUAD_STRIP);
            for (int j=0; j<faceNum; j++)
                {
                    x = j*deltaX;
                    glVertex3f(x,y,height);
                    glVertex3f(x,y+deltaY,height);
                }
            glVertex3f(x+deltaX,y,height);
            glVertex3f(x+deltaX,y+deltaY,height);
            glEnd();
        }
    glPopMatrix();
}

void
GLServer::DrawCircle(const Vector3& pos, double radius,
                     double /*start_angle*/, double /*end_angle*/)
{
    glPushMatrix();
    glBegin(GL_LINE_LOOP);
    int num_lines = 36;

    for(int i =0;i<num_lines;i++)
    {
        double angle = i*2*pi/num_lines;
        glVertex3f(pos.x + radius*cos(angle), pos.y + radius*sin(angle), pos.z);
    }
    glEnd();
    glPopMatrix();
}

//------------------------drawWireBox-------------------------------------
//
// draws a wireframbox with given dimensions to position 'boxPos'
//-----------------------------------------------------------------------
void GLServer::DrawWireBox(Vector3 boxPos, Vector3 sz)
{
    glPushMatrix();
    glTranslatef(boxPos[0],boxPos[1], boxPos[2]);

    // ground plane
    glBegin(GL_LINE_LOOP);
    glVertex3f(0    ,0,0);
    glVertex3f(sz[0],0,0);
    glVertex3f(sz[0],0,sz[2]);
    glVertex3f(0    ,0,sz[2]);
    glEnd();

    // top plane
    glBegin(GL_LINE_LOOP);
    glVertex3f(0    ,sz[1],0);
    glVertex3f(sz[0],sz[1],0);
    glVertex3f(sz[0],sz[1],sz[2]);
    glVertex3f(0    ,sz[1],sz[2]);
    glEnd();

    // sides
    glBegin(GL_LINES);
    glVertex3f(0    ,0,0);
    glVertex3f(0    ,sz[1],0);
    glEnd();

    glBegin(GL_LINES);
    glVertex3f(sz[0],0,0);
    glVertex3f(sz[0],sz[1],0);
    glEnd();

    glBegin(GL_LINES);
    glVertex3f(sz[0],0,sz[2]);
    glVertex3f(sz[0],sz[1],sz[2]);
    glEnd();

    glBegin(GL_LINES);
    glVertex3f(0    ,0,sz[2]);
    glVertex3f(0    ,sz[1],sz[2]);
    glEnd();

    glPopMatrix();
}

//------------------------drawGoal-------------------------------------
//
// draws a goal with given dimensions to position 'goalPos'
//-----------------------------------------------------------------------
void GLServer::DrawGoal(Vector3 goalPos, Vector3 sz, double barRadius)
{

    GLUquadricObj *cyl;
    cyl = gluNewQuadric();

    glPushMatrix();
	if (goalPos[0] > 0) {
		glTranslatef(goalPos[0]-goalPos[0]*barRadius, goalPos[1], goalPos[2]);
	} else {
		glTranslatef(goalPos[0]+goalPos[0]*barRadius, goalPos[1], goalPos[2]);
	}


    // draw goal sides as cylinders
    glPushMatrix();
    glTranslatef(0, 0, sz[2] + barRadius);
    glRotatef(-90.0f, 1,0,0);
    gluCylinder(cyl,barRadius,barRadius,sz[1],15,15);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0, sz[1] + barRadius, 0);
    gluCylinder(cyl,barRadius,barRadius,sz[2],15,15);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0, -barRadius, 0);
    gluCylinder(cyl,barRadius,barRadius,sz[2],15,15);
    glPopMatrix();

    glPopMatrix();

    gluDeleteQuadric(cyl);

}

//------------------------drawSphere-------------------------------------
//
// draws a solid sphere with given radius to position 'spherePos'
//-----------------------------------------------------------------------
void GLServer::DrawSphere(Vector3 spherePos,double radius, int res)
{
    glPushMatrix();
    glTranslatef(spherePos[0],spherePos[1], spherePos[2]);
    if(!mWireframe) {
        glutSolidSphere(radius, res, res);
    }
    else {
        glutWireSphere(radius, res, res);
    }
    glPopMatrix();
}


//-----------------------DrawShadowOfSphere------------------------------
//
// draws the shadow of a sphere with given radius by scaling its size onto
// the ground (no y component)
//-----------------------------------------------------------------------
void GLServer::DrawShadowOfSphere(Vector3 spherePos,double radius)
{
    // distance between ground and shadows
    const double delta = 0.001f;

    glPushMatrix();
    glColor3f(0.0f, 0.0f, 0.0f);

    glTranslatef(spherePos[0], spherePos[1],delta);
    glScalef(1.0f, 1.0f, 0.0f);
    glutSolidSphere(radius, 10, 10);

    glPopMatrix();
}

⌨️ 快捷键说明

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