SkyBox.cpp
资源名称:虚拟地形建模.rar [点击查看]
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:5k
源码类别:
OpenGL
开发平台:
Visual C++
- // SkyBox.cpp: implementation of the CSkyBox class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "SkyBox.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CSkyBox::CSkyBox()
- {
- }
- CSkyBox::~CSkyBox()
- {
- }
- bool CSkyBox::InitSkyBox(int XLenght, int YLenght,int ZLenght,
- int CenterX,int CenterY,int CenterZ)
- {
- m_XLength=XLenght;
- m_YLength=YLenght;
- m_ZLength=ZLenght;
- m_CenterX=CenterX;
- m_CenterY=CenterY;
- m_CenterZ=CenterZ;
- texSky[0] = CreateTexture("Sky\skyb0.bmp");
- texSky[1] = CreateTexture("Sky\skyb1.bmp");
- texSky[2] = CreateTexture("Sky\skyb2.bmp");
- texSky[3] = CreateTexture("Sky\skyb3.bmp");
- texSky[4] = CreateTexture("Sky\skyb4.bmp");
- texSky[5] = CreateTexture("Sky\ground.bmp");
- return true;
- }
- void CSkyBox::DrawSkyBox()
- {
- int lx,ly,lz,cx,cy,cz;
- lx=m_XLength;
- ly=m_YLength;
- lz=m_ZLength;
- cx=m_CenterX;
- cy=m_CenterY;
- cz=m_CenterZ;
- float btmy=0.0f;//This variable indicate how much texture should be cut from 0 to btmy
- // glDisable(GL_FOG);
- ////////front
- glBindTexture(GL_TEXTURE_2D, texSky[0]);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glTexCoord2f(0,btmy);
- glVertex3i(-lx+cx, cy, -lz+cz);
- glTexCoord2f(1,btmy);
- glVertex3i(lx+cx, cy, -lz+cz);
- glTexCoord2f(1,1);
- glVertex3i(lx+cx, ly+cy, -lz+cz);
- glTexCoord2f(0,1);
- glVertex3i(-lx+cx, ly+cy, -lz+cz);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- /// ////////////////sky1
- /////////////////right
- glBindTexture(GL_TEXTURE_2D, texSky[1]);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glTexCoord2f(0,btmy);
- glVertex3i(lx+cx, cy, -lz+cz);
- glTexCoord2f(1,btmy);
- glVertex3i(lx+cx, cy, lz+cz);
- glTexCoord2f(1,1);
- glVertex3i(lx+cx, ly+cy, lz+cz);
- glTexCoord2f(0,1);
- glVertex3i(lx+cx, ly+cy, -lz+cz);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- /// ////////////////sky2
- //////////////back
- glBindTexture(GL_TEXTURE_2D, texSky[2]);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glTexCoord2f(0,btmy);
- glVertex3i(lx+cx, cy, lz+cz);
- glTexCoord2f(1,btmy);
- glVertex3i(-lx+cx, cy, lz+cz);
- glTexCoord2f(1,1);
- glVertex3i(-lx+cx, ly+cy, lz+cz);
- glTexCoord2f(0,1);
- glVertex3i(lx+cx, ly+cy, lz+cz);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- /// ////////////////sky3
- ////////////////left
- glBindTexture(GL_TEXTURE_2D, texSky[3]);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glTexCoord2f(0,btmy);
- glVertex3i(-lx+cx, cy, lz+cz);
- glTexCoord2f(1,btmy);
- glVertex3i(-lx+cx, cy, -lz+cz);
- glTexCoord2f(1,1);
- glVertex3i(-lx+cx, ly+cy, -lz+cz);
- glTexCoord2f(0,1);
- glVertex3i(-lx+cx, ly+cy, lz+cz);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- /// ////////////////sky4
- /////////////up
- glBindTexture(GL_TEXTURE_2D, texSky[4]);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3i(-lx+cx, ly+cy, -lz+cz);
- glTexCoord2f(1,0);
- glVertex3i(lx+cx,ly+cy, -lz+cz);
- glTexCoord2f(1,1);
- glVertex3i(lx+cx, ly+cy, lz+cz);
- glTexCoord2f(0,1);
- glVertex3i(-lx+cx, ly+cy, lz+cz);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- ///////地面
- glBindTexture(GL_TEXTURE_2D, texSky[5]);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3i(-lx+cx, cy, -lz+cz);
- glTexCoord2f(1,0);
- glVertex3i(lx+cx,cy, -lz+cz);
- glTexCoord2f(1,1);
- glVertex3i(lx+cx, cy, lz+cz);
- glTexCoord2f(0,1);
- glVertex3i(-lx+cx, cy, lz+cz);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- }
- GLuint CSkyBox::CreateTexture( CString filename )
- {
- GLuint texture;
- _AUX_RGBImageRec *Image;
- Image = auxDIBImageLoadA( (const char*) filename );
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Image->sizeX,
- Image->sizeY, GL_RGB,
- GL_UNSIGNED_BYTE, Image->data);
- delete Image;
- return texture;
- }