⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 earth.c

📁 图形学课件 图形学课件 图形学课件
💻 C
字号:
/* eatth.c  */

/* E. Angel, Interactive Computer Graphics */
/* A Top-Down Approach with OpenGL, Third Edition */
/* Addison-Wesley Longman, 2003 */


/* Rotating sphere with color interpolation */

/* Demonstration of use of quad strips and */
/* triangle fans for modeling a sphere by */
/* longitude and latitiude */

/* uses polygons in line mode */
 
/* simple mouse click interface to rotate */

#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>

#define INCRE_ANGLE 20
#define PI 3.14159

//------------------------------
void earth()
{
  double x, y, z, thet, phi;
  int nlat, nlong;
  double c;

  c=PI/180.0;

  x=y=0;
  z=1;

  glBegin(GL_TRIANGLE_FAN);
	glVertex3d(x,y,z);

	z=sin(c*80.0);
	for(thet=-180.0; thet<=180.0;thet+=INCRE_ANGLE)
	{
		x=sin(c*thet)*cos(c*80.0);
		y=cos(c*thet)*cos(c*80.0);
		glVertex3d(x,y,z);
	}
  glEnd();

  x=y=0;
  z=-1;
  glBegin(GL_TRIANGLE_FAN);
	glVertex3d(x,y,z);
	z=-sin(c*80.0);
	for(thet=-180.0; thet<=180.0;thet+=INCRE_ANGLE)
	{
		x=sin(c*thet)*cos(c*80.0);
		y=cos(c*thet)*cos(c*80.0);
		glVertex3d(x,y,z);
	}
  glEnd();

  for(phi=-80.0; phi<=80.0; phi+=INCRE_ANGLE)
  {
	glBegin(GL_QUAD_STRIP);
		for(thet=-180.0; thet<=180.0;thet+=INCRE_ANGLE)
		{
			x=sin(c*thet)*cos(c*phi);
			y=cos(c*thet)*cos(c*phi);
			z=sin(c*phi);
			glVertex3d(x,y,z);

			x=sin(c*thet)*cos(c*(phi+INCRE_ANGLE));
			y=cos(c*thet)*cos(c*(phi+INCRE_ANGLE));
			z=sin(c*(phi+INCRE_ANGLE));
			glVertex3d(x,y,z);
		}
	glEnd();
  }
}


//--------------------------------------------
static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;

void display(void)
{
/* display callback, clear frame buffer and z buffer,
   rotate cube and draw, swap buffers */

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glRotatef(theta[0], 1.0, 0.0, 0.0);
	glRotatef(theta[1], 0.0, 1.0, 0.0);
	glRotatef(theta[2], 0.0, 0.0, 1.0);

 earth();

 glFlush();
 glutSwapBuffers();
}

//-------------------------------
void spinEarth()
{

/* Idle callback, spin cube 2 degrees about selected axis */
	theta[axis] += 2.0;
	if( theta[axis] > 360.0 ) theta[axis] -= 360.0;

	glutPostRedisplay();
}

//-------------------------------
void mouse(int btn, int state, int x, int y)
{

/* mouse callback, selects an axis about which to rotate */
	if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) theta[0]+=2;
	if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) theta[1]+=2;
	if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) theta[2]+=2;

	glutPostRedisplay();
}

//-------------------------------
void myinit()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    
	glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glColor3f(0.0, 0.0, 0.0);
	glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
}

//-------------------------------
void main(int argc, char **argv)
{
    glutInit(&argc, argv);

/* need both double buffering and z buffer */

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("sphere");
    glutDisplayFunc(display);
	glutIdleFunc(NULL);
	glutMouseFunc(mouse);

	myinit();
    glutMainLoop();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -