#include <Windows.h>
#include <math.h>
#include <gl/glut.h>
void myinit()
{
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glOrtho(-5,5,-5,5,-5,5);
glMatrixMode(GL_MODELVIEW);
glRotatef(45,1,0,0);
}
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT);
double c,phi,phir,phir20,theta,thetar,x,y,z,thetar20;
double M_PI=3.141592653;
double n=1;
c=M_PI/180.0;
int num=4;
double fuhao[4][2]={(-1,-1),(1,-1),(-1,1),(1,1)};
double m=2;
while(num>0){
for(phi=-90.0; phi<=90.0; phi+=20.0)
{
phir=c*phi;
phir20=c*(phi+20);
glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_TRIANGLES);
for(theta=-180.0; theta<=180.0;theta+=20)
{
thetar=c*theta;
thetar20=c*(theta+20);
x=sin(thetar)*cos(phir)*n+fuhao[num-1][0]*m;
y=cos(thetar)*cos(phir)*n+fuhao[num-1][1]*m;
z=sin(phir)*n;
glVertex3d(x,y,z);
x=sin(thetar20)*cos(phir)*n+fuhao[num-1][0]*m;
y=cos(thetar20)*cos(phir)*n+fuhao[num-1][1]*m;
z=sin(phir)*n;
glVertex3d(x,y,z);
x=sin(thetar)*cos(phir20)*n+fuhao[num-1][0]*m;
y=cos(thetar)*cos(phir20)*n+fuhao[num-1][1]*m;
z=sin(phir20)*n;
glVertex3d(x,y,z);
}
glEnd();
}
num--;
}
glFlush();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("实验一");
glutDisplayFunc(mydisplay);
myinit();
glutMainLoop();
return 0;
}
想输出四个球,分别在四个象限上,可是。。。。。。麻烦各位帮忙看看修改一下。
#include <math.h>
#include <gl/glut.h>
void myinit()
{
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glOrtho(-5,5,-5,5,-5,5);
glMatrixMode(GL_MODELVIEW);
glRotatef(45,1,0,0);
}
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT);
double c,phi,phir,phir20,theta,thetar,x,y,z,thetar20;
double M_PI=3.141592653;
double n=1;
c=M_PI/180.0;
int num=4;
double fuhao[4][2]={(-1,-1),(1,-1),(-1,1),(1,1)};
double m=2;
while(num>0){
for(phi=-90.0; phi<=90.0; phi+=20.0)
{
phir=c*phi;
phir20=c*(phi+20);
glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_TRIANGLES);
for(theta=-180.0; theta<=180.0;theta+=20)
{
thetar=c*theta;
thetar20=c*(theta+20);
x=sin(thetar)*cos(phir)*n+fuhao[num-1][0]*m;
y=cos(thetar)*cos(phir)*n+fuhao[num-1][1]*m;
z=sin(phir)*n;
glVertex3d(x,y,z);
x=sin(thetar20)*cos(phir)*n+fuhao[num-1][0]*m;
y=cos(thetar20)*cos(phir)*n+fuhao[num-1][1]*m;
z=sin(phir)*n;
glVertex3d(x,y,z);
x=sin(thetar)*cos(phir20)*n+fuhao[num-1][0]*m;
y=cos(thetar)*cos(phir20)*n+fuhao[num-1][1]*m;
z=sin(phir20)*n;
glVertex3d(x,y,z);
}
glEnd();
}
num--;
}
glFlush();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("实验一");
glutDisplayFunc(mydisplay);
myinit();
glutMainLoop();
return 0;
}
想输出四个球,分别在四个象限上,可是。。。。。。麻烦各位帮忙看看修改一下。