欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[OpenGL]计算机图形学实验01:一个简单的球体

程序员文章站 2022-07-04 15:01:05
...
#include <Windows.h>
#include <gl\glut.h>
#include<math.h>

#define PI 3.14

void myDisplay2(){

	//画两个顶端的圆形区域
	glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
	glBegin(GL_TRIANGLE_FAN);
	glVertex3d(0.0,0.0,1.0);
	double c = PI/180;
	float z = sin(c*80);
	float x,y;
	for(float theta =-180.0;theta<=180.0;theta+=20.0){
		GLfloat thetar = theta*c;
		x= sin(thetar)*cos(c*80);
		y= cos(thetar)*cos(c*80);
		glVertex3d(x,y,z);
	}
	glEnd();
	//画两个顶端的圆形区域
	glBegin(GL_TRIANGLE_FAN);
	glVertex3d(0.0,0.0,-1.0);
	z = -sin(c*80);
	for(GLfloat theta =-180.0;theta<=180.0;theta+=20.0){
		GLfloat thetar = theta*c;
		x= sin(thetar)*cos(c*80);
		y= cos(thetar)*cos(c*80);
		glVertex3d(x,y,z);


	}
	glEnd();



	for(float phi=-80;phi<=80;phi+=20){
		float phir = c*phi ;
		float phir20 = c*(phi+20) ;
		glBegin(GL_QUAD_STRIP);
		for(GLfloat theta =-180.0;theta<=180.0;theta+=20.0){
			GLfloat thetar = theta*c;

			x= sin(thetar)*cos(phir);
			y= cos(thetar)*cos(phir);
			z=sin(phir);
			glVertex3d(x,y,z);
			x= sin(thetar)*cos(phir20);
			y= cos(thetar)*cos(phir20);
			z=sin(phir20);
			glVertex3d(x,y,z);
		}
		glEnd();

	}


	glFlush();

}

void myInit2(){

	//属性设置
	glClearColor(0.0,0.0,1.0,1.0);//白色背景
	glColor3f(0.0,0.0,1.0);//绘图颜色为蓝色

	//设置观察条件
	//在观察坐标中定义一个50.0*50.0的窗口,窗口左下角为观察坐标系的坐标原点。
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-5.0,5.0,5.0,-5.0);//左右下上,因为坐标轴原点在左下角

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//glTranslated(2,2,0);
	glRotated(30,1,0,0);
	glRotated(60,0,1,0);
	glRotated(80,0,0,1);
	//glRotated(20,1,0,0);
	//glutPostRedisplay();


}

void main(int argc,char** argv){
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

	glutInitWindowSize(500,500);
	glutInitWindowPosition(0,0);

	glutCreateWindow("DrawBall");


	glutDisplayFunc(myDisplay2);
	myInit2();
	glutMainLoop();


}