看一些文章,球紋理圖好象是用全景圖轉換來的。
用手機拍攝一組圖試試:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICO0gzMxkTNyIDOyIDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
然後用用ps中的photomerge拼接成:
再去掉兩頭重合部分:
直接載入前面的程式中也是可以的,顯示在球外視角:
如果用右鍵向縮小方向拉,圖像縮小後反向放大,視角就會進入球内部。
不過效果不是很好,為了更好的顯示,重新修改下cpp:
//52 全景球貼圖
//左鍵(+ 移動)旋轉,右鍵(+ 移動)縮放 1,2,3,4 切換地圖,Enter鍵(enter)全屏切換
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glut32.lib")
#include <GL/glut.h>
#include <GL/glu.h>
#include <math.h>
#include "ArcBall.h"
#include "Sky.h"
CSky m_sky;
int BuildTexture(char *szPathName, GLuint &texid);
//初始化,必須用全局變量的方式,不能用new
ArcBallT arcBall(600.0f,400.0f);
ArcBallT* ArcBall =&arcBall;// new ArcBallT(600.0f,400.0f);//&arcBall;
GLuint SkyTexture[10];//紋理
int width,height;//螢幕寬高
void reshape(int w, int h){
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, (GLfloat)w / (GLfloat)h, 6.5f, 150.0f);
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("map3.jpg",SkyTexture[3]);//航空圖
BuildTexture("map5.jpg",SkyTexture[1]);//英文世界地圖
BuildTexture("world3.jpg",SkyTexture[2]);//航空航海圖
int R=105;//地圖球半徑
m_sky.InitSky(0,0,0,R,SkyTexture[0]);//初始化天空球
}
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, 6.0,//m_sky.GetSkyR()*ArcBall->zoomRate+5.0,//視點跟蹤球大小
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
//glTranslatef(0,0,-3);
glScalef(-ArcBall->zoomRate, -ArcBall->zoomRate, -ArcBall->zoomRate);//2. 縮放
glMultMatrixf(ArcBall->Transform.M); //3. 旋轉
//起始位置轉到我國
//glRotatef(180, 1.0, 0.0, 0.0);
//glRotatef(105, 0.0, 1.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[0];
break;
case 13://按回車切換全屏
{
static bool full=false;
if (full)
{
glutReshapeWindow(width,height);//視窗
full=false;
}
else
{
glutFullScreen();//全屏
full=true;
}
}
break;
}
glutPostRedisplay();
//printf("key::%d\n", key);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(width=1200,height=640);
glutCreateWindow("HI");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse); //注冊滑鼠事件。
glutMotionFunc(move); //注冊移動事件
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
效果圖: