可任意旋转的地球仪

前面我们已经做了一个有贴图的球,如果把世界地图贴上去,不就是地球仪了吗。

主cpp:

//50 地球仪

//左键(+ 移动)旋转,右键(+ 移动)缩放 1,2 切换地图

#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];

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;
}
效果图:

需要的世界地图:

如果谁知道哪儿有这种方的世界地图,望告知。



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值