📄 glserver.cpp
字号:
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: glserver.cpp,v 1.14 2008/03/11 12:29:49 jboedeck Exp $ 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; version 2 of the License. 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., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "glserver.h"#include <iostream>#include <string>#ifdef __APPLE__#include <GLUT/glut.h>#else#include <GL/glut.h>#endifusing namespace salt;GLServer::GLServer(int width, int height, Vector3f camPos, Vector3f lookAtPos, Vector3f up, bool wire){ mCamPos = camPos; mLookAt = lookAtPos; mUp = up; mWidth = width; mHeight = height; mWireframe = wire; mCamera = Camera(camPos, lookAtPos, up);}// void// GLServer::InitIcons// {// mCamModeIcon[][][][eFree]// mCamModeIcon[][][][eFollowBall]// mCamModeIcon[][][][eCenterBall]// }// void// GLServer::InitTexture(const string &tFile)// {// if (!ReadTexture(tFile))// {// mTextureWidth = 128;// mTextureHeight = 128;// cout << "Texture: Loading " << tFile << " failed, using random image.\n";// int c;// for (int i = 0;i < mTextureHeight; i++) {// for (int j = 0;j < mTextureWidth; j++) {// c = ((((i&0x10)==0)^((j&0x10))==0))*15;// mTexture[i][j][0] = (GLubyte) 55+c;// mTexture[i][j][1] = (GLubyte) 156+c;// mTexture[i][j][2] = (GLubyte) 55+c;// mTexture[i][j][3] = (GLubyte) 255;// }// }// }// }// bool// GLServer::ReadTexture(const string &tFile)// {// // FIXME: This is a crude hack. No flexibility in the file allowed.// // rewrite with fileserver etc...// ifstream texturefile;// // absolute path to texturefile// string absTFile = "";// if (tFile[0] == '/')// {// // absolute path// absTFile = tFile;// texturefile.open(absTFile.c_str());// }// else// {// // relative path given, so we try ~/.rcssserver3d/ and// // $prefix/share/rcssserver3d/ as directories// char* home = getenv("HOME");// if (home)// {// absTFile = string(home)+"/.rcssserver3d/"+tFile;// texturefile.open(absTFile.c_str());// }// if (!texturefile)// {// // some macro magic (not at all)// absTFile = PREFIX "/share/" PACKAGE_NAME "/"+tFile;// texturefile.open(absTFile.c_str());// }// }// if (!texturefile)// {// cout << "Texture: could not open texture file" << endl;// return false;// }// string line;// //read magic no.// texturefile >> line;// if (line != "P6")// {// cout << "Texture: wrong magic number. No raw PPM" << endl;// return false;// }// // comment by gimp// texturefile >> line;// texturefile.ignore(INT_MAX, '\n');// // read the size// texturefile >> mTextureWidth >> mTextureHeight;// if ((mTextureWidth != 128) || (mTextureHeight > 128))// {// // texture has the wrong size// cout << "Texture: Wrong Size" << endl;// return false;// }// int numcolors;// texturefile >> numcolors;// if (numcolors > 255)// {// cout << "Texture: too many colors (" << numcolors << ")" << endl;// return false;// }// GLubyte r,g,b;// for (int i = 0;i < mTextureHeight; i++)// {// for (int j = 0;j < mTextureWidth; j++)// {// texturefile >> r >> g >> b;// mTexture[i][j][0] = (GLubyte) r;// mTexture[i][j][1] = (GLubyte) g;// mTexture[i][j][2] = (GLubyte) b;// mTexture[i][j][3] = (GLubyte) 255;// }// }// texturefile.close();// return true;// }//---------------------------------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,(GLfloat)mWidth/(GLfloat)mHeight,0.1f,10000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); mCamera.Look();}//---------------------------------initGL-------------------------------//// OpenGL initialisation function//----------------------------------------------------------------------void GLServer::InitGL (void){ glDisable(GL_LIGHTING); //JAN /*glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &mTexNames); glBindTexture(GL_TEXTURE_2D, mTexNames); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mTextureWidth, mTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mTexture); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); // 2d texture, 3 colors, width, height, RGB in that order, byte data, and the data. gluBuild2DMipmaps(GL_TEXTURE_2D, 4, mTextureWidth, mTextureHeight, GL_RGBA, GL_UNSIGNED_BYTE, mTexture);*/}void GLServer::DrawTextPix(const char* text, Vector2f 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 Vector2f pos(pix[0] / (float)mWidth * 2 - 1.0f, 1.0f - (pix[1] / (float)mHeight * 2)); DrawText(text,pos);}intGLServer::GetTextHeight() const{ // currently only GLUT_BITMAP_HELVETICA_18 is used return 18;}intGLServer::GetTextWidth(const char* text) const{ // currently GLUT_BITMAP_HELVETICA_18 is used int width = 0; for (const char* s = text; *s; ++s) { width += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, *s); } return width;}voidGLServer::DrawText3D(const char* text, const Vector3f& pos){ glDisable (GL_DEPTH_TEST); //glDisable (GL_TEXTURE_2D); glRasterPos3f(pos[0],pos[1],pos[2]); for (const char* s = text; *s; ++s) { glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s); } //glEnable (GL_TEXTURE_2D); glEnable (GL_DEPTH_TEST);}voidGLServer::DrawText3D(const std::string& text, const Vector3f& pos){ glDisable (GL_DEPTH_TEST); //glDisable (GL_TEXTURE_2D); glRasterPos3f(pos[0],pos[1],pos[2]); 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, Vector2f 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_HELVETICA_18, *s); }}void GLServer::DrawGroundRectangle(Vector3f pos, float szX, float szY, float angleDeg, float 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// GLfloat deltaX = szX/faceNum;// GLfloat deltaY = szY/faceNum; GLfloat x=0.0; GLfloat y=0.0; glBegin(GL_QUADS); glTexCoord2f(10.0, 10.0); glVertex3f(x,y,height); glTexCoord2f(0.0, 10.0); glVertex3f(x,y+szY,height); glTexCoord2f(0.0, 0.0); glVertex3f(x+szX,y+szY,height); glTexCoord2f(10.0, 0.0); glVertex3f(x+szX,y,height); glEnd(); glPopMatrix();}//====================================================================================// void GLServer::DrawGroundRectangle(Vector3f pos, float szX, float szY,// float angleDeg, float height)// {// const int faceNum = 2;// glPushMatrix();// glRotatef(0,1,0,angleDeg);// glTranslatef(pos[0],pos[1], pos[2]);// glNormal3f(0,0,1);// // store the sizes of our faces// GLfloat deltaX = szX/faceNum;// GLfloat deltaY = szY/faceNum;// GLfloat x,y;// glDisable(GL_LIGHTING);// 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;// glTexCoord2f(0.0, 0.0); glVertex3f(x,y,height);// glTexCoord2f(0.0, 1.0); glVertex3f(x,y+deltaY,height);// }// glTexCoord2f(1.0, 0.0); glVertex3f(x+deltaX,y,height);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -