2008-11-05 13:40
void CVCITYView::DrawTree()//绘制树 { ///////////////////////////// /////////画树程序//////////// ///////////////////////////// CTree tree; tree.getTree(tree); AUX_RGBImageRec* myimage; unsigned char *image; myimage=auxDIBImageLoad("tree1.bmp"); //makeTexture(myimage); int width,height; width=myimage->sizeX; height=myimage->sizeY; FILE *fp; fp=fopen("tree1.bmp","rb"); if(!fp) return; fseek(fp,54,SEEK_SET);////读取24位真彩色位图 image=(unsigned char *)malloc(width*height*3); int rc; rc=fread(image,sizeof(unsigned char),width*height*3,fp); fclose(fp); BYTE texture[256][256][4];//注意:每个像素占用4个字节,不是原来的3个。 for(int i=0;i<width;i++) { for(int j=0;j<height;j++) { //把颜色值写入 texture[i][j][0] = (GLubyte)*(image+i*width*3+j*3); texture[i][j][1] = (GLubyte)*(image+i*width*3+j*3+1); texture[i][j][2] = (GLubyte)*(image+i*width*3+j*3+2); //设置alpha值,假设白色为透明色 if(texture[i][j][0]==255 && texture[i][j][1]==255 && texture[i][j][2]==255) texture[i][j][3] = 0;//透明,设为 0 else texture[i][j][3] = 255;//不透明,设为 255, 也就是以后用的1.0 } } //映射纹理 glPixelStorei(GL_UNPACK_ALIGNMENT,1); glTexImage2D(GL_TEXTURE_2D,0,4,width,height,0, GL_RGBA, GL_UNSIGNED_BYTE,texture); gluBuild2DMipmaps(GL_TEXTURE_2D,4,width,height,GL_RGBA,GL_UNSIGNED_BYTE,texture); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER ,0.9);//0.5可以换成任何在0~1之间的数 for(i=1;i<198;i++) { glBegin(GL_QUADS); glTexCoord2d(0,0); glVertex3d(tree.TreePoint[i].x-0.5,tree.TreePoint[i].y,0.0); glTexCoord2d(1,0); glVertex3d(tree.TreePoint[i].x+0.5,tree.TreePoint[i].y,0.0); glTexCoord2d(1,1); glVertex3d(tree.TreePoint[i].x+0.5,tree.TreePoint[i].y,3); glTexCoord2d(0,1); glVertex3d(tree.TreePoint[i].x-0.5,tree.TreePoint[i].y,3); glEnd(); } glDisable(GL_TEXTURE_2D); glDisable(GL_ALPHA_TEST); glDisable(GL_BLEND); } |