现在的位置: 首页 > 综合 > 正文

3-2

2013年03月28日 ⁄ 综合 ⁄ 共 1475字 ⁄ 字号 评论关闭

继续随时写代码作为练习的习惯吧,上一版<<OpenGL超级宝典>>时没怎么写代码,学<<OpenGL编程指南>>的时候,就随手写写吧.就像当初学数据结构一样,呵呵,当时为了学会,在纸上画了好多图啊,树啊,什么的,竟画圈了...这回好,直接写完程序就画出来了,哈哈,原来图形学用来计算机入门最好了.NND,要知道学矩阵,四元数什么的,也花了孙子我半年的时间,操操...想说老子了,还是自称孙子吧,这样比较幽默.~

//	3-6.cpp -- 2013-04-11-20:10
#include <gl/glut.h>
#include <stdlib.h>

static int year = 0 ;
static int month = 0 ;
static int day = 0 ;

void Init(void)
{
	glClearColor(0, 0, 0, 0) ;
	glShadeModel(GL_FLAT) ;
}

void Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT) ;
	glColor3f(1, 1, 0.7) ;

	glPushMatrix() ;

	//	画太阳
	glutWireSphere(1, 20, 16) ;

	//	定位地球
	glRotatef((GLfloat)year, 0, 1, 0) ;
	glTranslatef(2, 0, 0) ;
	glRotatef((GLfloat)month, 0, 1, 0) ;

	//	画地球
	glutWireSphere(0.6, 15, 16) ;

	glPushMatrix() ;
	//	定位月球
	glTranslatef(1, 0, 0) ;
	//	画月球
	glutWireSphere(0.2, 10, 8) ;
	glPopMatrix() ;

	glPopMatrix() ;

	glutSwapBuffers() ;
}

void Reshape(int width, int height)
{
	glViewport(0, 0, (GLsizei)width, (GLsizei)height) ;
	glMatrixMode(GL_PROJECTION) ;
	glLoadIdentity() ;
	gluPerspective(60, (GLfloat)width / (GLfloat)height, 1, 20) ;
	glMatrixMode(GL_MODELVIEW) ;
	glLoadIdentity() ;
	gluLookAt(0, 0,5, 0, 0, 0, 0, 1, 0) ;
}

void Keyboard(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 'd':
	case 'D':
		month = (month + 10) % 360 ;
		glutPostRedisplay() ;
		break ;
	case 'y':
	case 'Y':
		year = (year + 5) % 360 ;
		glutPostRedisplay() ;
		break ;
	case 27:
		exit(0) ;
		break ;
	default:
		break ;
	}
}

int main(int argc, char * * argv)
{
	glutInit(&argc, argv) ;
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) ;
	glutInitWindowSize(500, 500) ;
	glutInitWindowPosition(100, 100) ;
	glutCreateWindow(argv[0]) ;
	Init() ;
	glutDisplayFunc(Display) ;
	glutReshapeFunc(Reshape) ;
	glutKeyboardFunc(Keyboard) ;
	glutMainLoop() ;

	return 0 ;
}

抱歉!评论已关闭.