📄 glserver.cpp
字号:
// glTexCoord2f(1.0, 1.0); glVertex3f(x+deltaX,y+deltaY,height);// glEnd();// }// glPopMatrix();// }//====================================================================================voidGLServer::DrawCircle(const salt::Vector3f& pos, float radius, float /*start_angle*/, float /*end_angle*/){ glPushMatrix(); glBegin(GL_LINE_LOOP); int num_lines = 60; for(int i =0;i<num_lines;i++) { double angle = i*2*M_PI/num_lines; glVertex3f(pos[0] + radius*cos(angle), pos[1] + radius*sin(angle), pos[2]); } glEnd(); glPopMatrix();}voidGLServer::DrawArbitraryLine(const salt::Vector3f& startPos, const salt::Vector3f& endPos){ glPushMatrix(); glBegin(GL_LINES); glVertex3f(startPos[0],startPos[1],startPos[2]); glVertex3f(endPos[0], endPos[1],endPos[2]); glEnd(); glPopMatrix();}//------------------------drawWireBox-------------------------------------//// draws a wireframbox with given dimensions to position 'boxPos'//-----------------------------------------------------------------------void GLServer::DrawWireBox(Vector3f boxPos, Vector3f 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(Vector3f goalPos, Vector3f sz, float barRadius){ GLUquadricObj *cyl; cyl = gluNewQuadric(); glPushMatrix(); glTranslatef(goalPos[0]-gSign(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(Vector3f spherePos,float radius, int res, bool wireframe){ glLineWidth(2); glPushMatrix(); glTranslatef(spherePos[0],spherePos[1], spherePos[2]); //if(!mWireframe) if(!wireframe){ glutSolidSphere(radius, res, res); // WAS res }else{ glutWireSphere(radius, 8, 8); //WAS res } glPopMatrix(); glLineWidth(1);}//-----------------------DrawShadowOfSphere------------------------------//// draws the shadow of a sphere with given radius by scaling its size onto// the ground (no y component)//-----------------------------------------------------------------------void GLServer::DrawShadowOfSphere(Vector3f spherePos,float radius){ // distance between ground and shadows const float 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();}//-----------------------------------------------------------------------------// work in n progress//-----------------------------------------------------------------------------// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only.// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info.// if mesa ever gets glaux, let me know.boolGLServer::BMPImageLoad(const char *filename, Image *image) { FILE *file; unsigned long size; // size of the image in bytes. unsigned long i; // standard counter. unsigned short int planes; // number of planes in image (must be 1) unsigned short int bpp; // number of bits per pixel (must be 24) char temp; // used to convert bgr to rgb color. // make sure the file is there. if ((file = fopen(filename, "rb"))==NULL) { printf("File Not Found : %s\n",filename); printf("\nIf you don't have the texture files, please download them at\n"); printf("http://www.uni-koblenz.de/~murray/robocup/rcssmonitor-lite-textures.tgz\n"); printf("and unpack them to your $HOME/.rcssserver3d directory.\n\n"); return 0; } // seek through the bmp header, up to the width/height: fseek(file, 18, SEEK_CUR); // read the width if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { printf("Error reading width from %s.\n", filename); return 0; } // read the height if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { printf("Error reading height from %s.\n", filename); return 0; } // calculate the size (assuming 24 bits or 3 bytes per pixel). size = image->sizeX * image->sizeY * 3; // read the planes if ((fread(&planes, 2, 1, file)) != 1) { printf("Error reading planes from %s.\n", filename); return 0; } if (planes != 1) { printf("Planes from %s is not 1: %u\n", filename, planes); return 0; } // read the bpp if ((i = fread(&bpp, 2, 1, file)) != 1) { printf("Error reading bpp from %s.\n", filename); return 0; } if (bpp != 24) { printf("Bpp from %s is not 24: %u\n", filename, bpp); return 0; } // seek past the rest of the bitmap header. fseek(file, 24, SEEK_CUR); // read the data. image->data = (char *) malloc(size); if (image->data == NULL) { printf("Error allocating memory for color-corrected image data"); return 0; } if ((i = fread(image->data, size, 1, file)) != 1) { printf("Error reading image data from %s.\n", filename); return 0; } for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb) temp = image->data[i]; image->data[i] = image->data[i+2]; image->data[i+2] = temp; } // we're done. return 1;}// Load Bitmaps And Convert To TexturesGLvoidGLServer::InitTexture(const string &tFile){ // Load Texture Image *image1; string pkgname("." PACKAGE_NAME); string path = string(getenv("HOME")) + "/" + pkgname + "/"; // allocate space for texture image1 = (Image *) malloc(sizeof(Image)); if (image1 == NULL) { printf("Error allocating space for image"); exit(0); } if (!BMPImageLoad((path + string("grass.bmp")).c_str(), image1)) { exit(1); } // Load skymap Image *skytex[6]; // allocate space for texture for(int i = 0; i < 6 ; i++) { skytex[i] = (Image *) malloc(sizeof(Image)); if (skytex[i] == NULL) { printf("Error allocating space for image"); exit(0); } } if (!BMPImageLoad((path + string("bottom.bmp")).c_str(), skytex[0]) || !BMPImageLoad((path + string("top.bmp")).c_str(), skytex[1]) || !BMPImageLoad((path + string("left.bmp")).c_str(), skytex[2]) || !BMPImageLoad((path + string("right.bmp")).c_str(), skytex[3]) || !BMPImageLoad((path + string("front.bmp")).c_str(), skytex[4]) || !BMPImageLoad((path + string("back.bmp")).c_str(), skytex[5])) { exit(1); } // Create Textures glGenTextures(7, &mTexNames[0]); // texture 1 (poor quality scaling) glBindTexture(GL_TEXTURE_2D, mTexNames[0]); // 2d texture (x and y size) // cheap scaling when image bigger than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST); // cheap scaling when image smalled than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image1->sizeX, image1->sizeY, GL_RGB, GL_UNSIGNED_BYTE, image1->data); for(int i = 0; i < 6; i++) { // texture 2 (mipmapped scaling) glBindTexture(GL_TEXTURE_2D, mTexNames[i+1]); // 2d texture (x and y size) // scale linearly when image bigger than texture //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST); //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); // scale linearly + mipmap when image smalled than texture glTexImage2D(GL_TEXTURE_2D, 0, 3, skytex[i]->sizeX, skytex[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, skytex[i]->data); // 2d texture, 3 colors, width, height, RGB in that order, byte data, and the data. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, skytex[i]->sizeX, skytex[i]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, skytex[i]->data); }}voidGLServer::DrawSkyBackground(float x, float y, float z, float width, float height, float length){ x = x - width / 2; y = y - height / 2; z = z - length / 2; glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); glBindTexture(GL_TEXTURE_2D, mTexNames[1]); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0.0f, 0.0f); glVertex3f(x,y,z); glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y,z); glTexCoord2f(0.0f, 1.0f); glVertex3f(x,y + height, z); glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z); glEnd(); glBindTexture(GL_TEXTURE_2D, mTexNames[2]); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(1.0f, 1.0f); glVertex3f(x,y,z + length); glTexCoord2f(0.0f, 1.0f); glVertex3f(x,y + height, z + length); glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, z + length); glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y + height, z + length); glEnd(); glBindTexture(GL_TEXTURE_2D, mTexNames[3]); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(1.0f, 0.0f); glVertex3f(x,y,z); glTexCoord2f(1.0f, 1.0f); glVertex3f(x,y, z + length); glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y,z); glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y,z + length); glEnd(); glBindTexture(GL_TEXTURE_2D, mTexNames[4]); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0.0f, 0.0f); glVertex3f(x,y + height,z); glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y + height, z); glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z + length); glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z + length); glEnd(); glBindTexture(GL_TEXTURE_2D, mTexNames[5]); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z); glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y + height, z); glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y, z + length); glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length); glEnd(); glBindTexture(GL_TEXTURE_2D, mTexNames[6]); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(1.0f, -1.0f); glVertex3f(x + width, y, z); glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, z + length); glTexCoord2f(0.0f, -1.0f); glVertex3f(x + width, y + height, z); glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y + height, z + length); glEnd(); glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D, mTexNames[0]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -