升级地球仪

前面的地球仪放大时,视点跑到内部去了,破坏了画面完整,现在来改进一下

1。视点跟踪球大小。能如意调整地图清晰度和视野

    gluLookAt(0.0, 0.0, m_sky.GetSkyR()*ArcBall->zoomRate+5.0,//视点跟踪球大小
        0.0, 0.0, 0.0,
        0.0, 1.0, 0.0);

2。全屏切换。充分利用屏幕空间。

		case 13://按回车切换全屏
			{
				static bool full=false;
				if (full)
				{
					glutReshapeWindow(width,height);//窗口
					full=false;
				}
				else
				{
					glutFullScreen();//全屏
					full=true;
				}
			}
			break;

完整cpp:

//51 改进地球仪

//左键(+ 移动)旋转,右键(+ 移动)缩放 1,2,3,4 切换地图,回车键(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, 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("world1.jpg",SkyTexture[0]);//世界地图
	BuildTexture("map3.jpg",SkyTexture[3]);//航空图
	BuildTexture("map5.jpg",SkyTexture[1]);//英文世界地图
	BuildTexture("world3.jpg",SkyTexture[2]);//航空航海图

	int R=45;//地图球半径

    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, 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;
}

效果图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值