前面我们已经做了一个有贴图的球,如果把世界地图贴上去,不就是地球仪了吗。
主cpp:
//50 地球仪
//左键(+ 移动)旋转,右键(+ 移动)缩放 1,2 切换地图
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glut32.lib")
#include
#include
#include
#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];
void reshape(int w, int h){
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, (GLfloat)w / (GLfloat)h, 6.5f, 100.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("map4.jpg",SkyTexture[0]);//航空图
BuildTexture("map5.jpg",SkyTexture[1]);//航海图
m_sky.InitSky(0,0,0,20,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, 25.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[0];
break;
}
glutPostRedisplay();
//printf("key::%d\n", key);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1200,640);
glutCreateWindow("HI");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse); //注册鼠标事件。
glutMotionFunc(move); //注册移动事件
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
效果图:
需要的世界地图:
如果谁知道哪儿有这种方的世界地图,望告知。