前面哪个opengl程序实际上已经快成功了,只差一点点了。
发现只要前、后图对换,左、右图对换就可以了
所以这里除了对换外,把哪个要按2次键才能截到正确的图也改一下(延时)
具体看代码吧:
//53 全景球贴图 切分6图
//左键(+ 移动)旋转,右键(+ 移动)缩放 1,2,3,4 切换地图,回车键(enter)全屏切换,按w、a、s、d、f、x、切换视角并截图
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glut32.lib")
#include
#include
#include
#include
#include
#include "ArcBall.h"
#include "Sky.h"
CSky m_sky;
void setWidthHeight(GLint width ,GLint height);
void screenshot(char* FileName); //全窗口
int BuildTexture(char *szPathName, GLuint &texid);
//初始化,必须用全局变量的方式,不能用new
ArcBallT arcBall(600.0f,400.0f);
ArcBallT* ArcBall =&arcBall;// new ArcBallT(600.0f,400.0f);//&arcBall;
#define PI 3.141592654
GLuint SkyTexture[10];//纹理
int width,height;//屏幕宽高
int R=100;//地图球半径
std::string direction;//方向
bool Shot=false;
void reshape(int w, int h){
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//GLdouble zNear=R*cos(PI/2);
gluPerspective(90.0f, (GLfloat)w / (GLfloat)h, 6.5f, R*2);//zNear
glMatrixMode(GL_MODELVIEW);
////ball
ArcBall->setBounds((GLfloat)w, (GLfloat)h);//1. 设置窗口边界
}
void init(){
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
/* 启用纹理 */
glEnable(GL_TEXTURE_2D);
/* 初始化天空 */
//载入天空纹理图 //请放到一起(和其它图)
BuildTexture("m1.jpg",SkyTexture[0]);//塔下全景图
BuildTexture("mm2.jpg",SkyTexture[3]);//航空图
BuildTexture("mesh.jpg",SkyTexture[1]);//英文世界地图
BuildTexture("mm.jpg",SkyTexture[4]);//起居室图
BuildTexture("world3.jpg",SkyTexture[2]);//航空航海图
//int R=105;//地图球半径
m_sky.InitSky(0,0,0,R,SkyTexture[0]);//初始化天空球
direction="前";
}
void display (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 0.0,//m_sky.GetSkyR()*ArcBall->zoomRate+5.0,//视点跟踪球大小
0.0, 0.0,R,
0.0, 1.0, 0.0);
//glTranslatef(0,0,-3);
glScalef(ArcBall->zoomRate, ArcBall->zoomRate, ArcBall->zoomRate);//2. 缩放
glScalef(-1.0, -1.0, -1.0);//2. 缩放
glMultMatrixf(ArcBall->Transform.M); //3. 旋转
//位置转到...
if(direction=="后")
glRotatef(180, 0.0, 1.0, 0.0);
else if(direction=="左")
glRotatef(90, 0.0, 1.0, 0.0);
else if(direction=="右")
glRotatef(-90, 0.0, 1.0, 0.0);
else if(direction=="上")
glRotatef(90, 1.0, 0.0, 0.0);
else if(direction=="下")
glRotatef(-90, 1.0, 0.0, 0.0);
/* 绘制天空 */
m_sky.ShowSky() ;
glFlush ();
}
//移动
void move(int x, int y)
{
ArcBall->MousePt.s.X = x;
ArcBall->MousePt.s.Y = y;
ArcBall->upstate();
glutPostRedisplay();
}
//点击
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state==GLUT_DOWN){
ArcBall->isClicked = true;
move(x,y);
}
else if(button == GLUT_LEFT_BUTTON && state==GLUT_UP)
ArcBall->isClicked = false;
else if(button == GLUT_RIGHT_BUTTON && state==GLUT_DOWN){
ArcBall->isRClicked = true;
move(x,y);
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
ArcBall->isRClicked = false;
ArcBall->upstate();
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case '1':
m_sky.T=SkyTexture[1];
break;
case '2':
m_sky.T=SkyTexture[2];
break;
case '3':
m_sky.T=SkyTexture[3];
break;
case '4':
m_sky.T=SkyTexture[4];
break;
case '5':
m_sky.T=SkyTexture[0];
break;
case 13://按回车切换全屏
{
static bool full=false;
if (full)
{
glutReshapeWindow(width,height);//窗口
full=false;
}
else
{
glutFullScreen();//全屏
full=true;
}
}
break;
case 'w':
{
direction="上";
glutPostRedisplay();
Shot=true;
}
break;
case 'f':
{
direction="后";
glutPostRedisplay();
Shot=true;
}
break;
case 'x':
{
direction="下";
glutPostRedisplay();
Shot=true;
}
break;
case 'a':
{
direction="左";
glutPostRedisplay();
Shot=true;
}
break;
case 'd':
{
direction="右";
glutPostRedisplay();
Shot=true;
}
break;
case 's'://前
{
direction="前";
glutPostRedisplay();
Shot=true;
}
break;
}
glutPostRedisplay();
}
void saveShot()
{
static int count=0;
if(Shot){
if(count<2){//延时
count++;
return;
}
if(direction=="前"){
setWidthHeight(width,height);
screenshot("前.bmp"); //全窗口
system("bmp2jpg 前.bmp");//转化为jpg
}else
if(direction=="后")
{
setWidthHeight(width,height);
screenshot("后.bmp"); //全窗口
system("bmp2jpg 后.bmp");//转化为jpg
}else
if(direction=="左")
{
setWidthHeight(width,height);
screenshot("左.bmp"); //全窗口
system("bmp2jpg 左.bmp");//转化为jpg
}else
if(direction=="右")
{
setWidthHeight(width,height);
screenshot("右.bmp"); //全窗口
system("bmp2jpg 右.bmp");//转化为jpg
}else
if(direction=="上")
{
setWidthHeight(width,height);
screenshot("上.bmp"); //全窗口
system("bmp2jpg 上.bmp");//转化为jpg
}else
if(direction=="下")
{
setWidthHeight(width,height);
screenshot("下.bmp"); //全窗口
system("bmp2jpg 下.bmp");//转化为jpg
}
Shot=false;
count=0;
}
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
width=640;height=640;
R=width/(2*sin(45*PI/180));
glutInitWindowSize(width,height);
glutCreateWindow("HI");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse); //注册鼠标事件。
glutMotionFunc(move); //注册移动事件
glutKeyboardFunc(keyboard);
glutIdleFunc(saveShot); //在程序空闲的时候就会被调用的函数的函数名。
glutMainLoop();
return 0;
}
然后把这6张图片放到前面的天空盒程序中:
//char *bmpName[] = { "back","front","bottom","top","left","right"};
char *bmpName[] = { "后","前","下","上","左","右"};
每个连接都是正确的了。完成!