绘制地球

绘制地球_第1张图片

#include 
#include 

#define PI 3.1415926535f
#define A 0.31415926535f
#define B 0.31415926535f
#define R 0.5f

struct MyPoint
{
	float x, y, z;
};

MyPoint pts[11][20];

// 使用OpenGL的接口,来绘制
static void display()
{
	glClear(GL_COLOR_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity(); // 初始化,清空
	gluPerspective(60, 1, 0.1f, 10.0f);

	gluLookAt(0, 1, 1, 0, 0, 0, 1, 0, 0);

	for (int i = 0; i <= PI/A; ++i)
	{
		float y = cosf(A*i) * R;
		float t = sinf(A*i) * R;
		
		for (int j = 0; j < PI/B*2; ++j)
		{
			float x = t * cosf(j*B);
			float z = t * sinf(j*B);

			pts[i][j].x = x;
			pts[i][j].y = y;
			pts[i][j].z = z;
		}
	}
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
//	glPolygonMode(GL_BACK, GL_LINE);
	glPolygonMode(GL_FRONT, GL_LINE);

	glBegin(GL_QUADS);
	for (int i = 0; i < PI / A; ++i)
	{
		for (int j = 0; j < PI / B * 2; ++j)
		{
			glVertex3f(pts[i][j].x, pts[i][j].y, pts[i][j].z);
			glVertex3f(pts[i+1][j].x, pts[i+1][j].y, pts[i+1][j].z);
			glVertex3f(pts[i + 1][(j + 1) % 20].x, pts[i + 1][(j + 1) % 20].y, pts[i + 1][(j + 1) % 20].z);
			glVertex3f(pts[i][(j + 1) % 20].x, pts[i][(j + 1) % 20].y, pts[i][(j + 1) % 20].z);
		}
	}
	glEnd();

	
	glFlush();
}

int main(int argc, char*argv[])
{
	// 初始化环境
	glutInit(&argc, argv);

	// 创建一个窗口, 参数的参照物是系统桌面的左上角
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hello OpenGL");

	// 设置显示函数
	glutDisplayFunc(display);

	// 进入主循环
	glutMainLoop();

	return 0;
}

你可能感兴趣的:(图形&图像&引擎)