五色五角星

武汉大学图形学上机检查作业

要求:用opengl画一个五角星,五个角有不同颜色

/*************************************************************************
	> File Name: Star.c
	> Author: halfopen
	> Mail: halfopen@yeah.net
	> Created Time: 2015年11月11日 星期四 17时12分46秒
 ************************************************************************/

#include<stdio.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include<math.h>

#define PI 3.14159262423

static double	y=sin(PI/5)*tan(PI/5)-cos(PI/5); 
static GLint axis = 2;
static GLfloat theta[] = {0.0,0.0,0.0};
static double colors[][3] = {0.0, 255.0, 0.0,  
			255.0, 0.0, 0.0,  
			0.0,0.0, 255,  
			0,255.0, 255.0,  
			125.0,   125.0, 120.0
};

static double stars[10][2]={0,y,  
			-sin(PI/5),-cos(PI/5),
			cos(PI/10)*y,sin(PI/10)*y,
			-cos(PI/10),sin(PI/10),
			sin(PI/5)*y,-cos(PI/5)*y,
			0,1,
			-sin(PI/5)*y,-cos(PI/5)*y,
			cos(PI/10),sin(PI/10),
			-cos(PI/10)*y,sin(PI/10)*y,
			sin(PI/5),-cos(PI/5)
};

int width=400, height=400;

void drawStar()
{
		glClear(GL_COLOR_BUFFER_BIT); 
		glBegin(GL_POLYGON); 
		double px,py;
		glColor3f(255,255,255);
		glVertex2f(0.0 , 0.0 );
		for(int i=0;i<10;++i)
		{			
			px = stars[i][0];
			py = stars[i][1];

			int j = i/2;
			//printf("%d\n",j);
			if(i==0)glColor3f(255,255,0);
			else if(i%2)glColor3f(colors[j][0],colors[j][1],colors[j][2]);
			
			glVertex2f(px , py );
			//getchar();		
		}
		glColor3f(255,255,0);
		glVertex2f(0.0,y);
		//glVertex2f(0,0);
		glEnd();
		glFlush();
}

static void display(void)
{
 		
 		drawStar();
		glRotatef(theta[0]/10, 1.0, 0.0, 0.0);
		glRotatef(theta[1]/10, 0.0, 1.0, 0.0);
		glRotatef(theta[2]/10, 0.0, 0.0, 1.0);		
}


/* This routine inputs new control points */
static void mouse(int button, int state, int x, int y)
{
        glFlush();

}

void init(void) 
{
		glClearColor(0.0,0.0,0.0,0.0);     
	
}

/* This routine handles keystroke commands */
void keyboard(unsigned char key, int x, int y)
{	
        switch (key)
    		{
				case 'j':
					axis = 0;
					theta[axis] += 2.0;
					if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
					glutPostRedisplay();break;
				case 'k':
					axis = 1;
					theta[axis] += 2.0;
					if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
					glutPostRedisplay();break;
				case 'l':
					axis = 2;
					theta[axis] += 2.0;
					if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
					glutPostRedisplay();break;
                case 'q': case 'Q':
                    exit(0);
                    break;
                case 'c': case 'C':
                    break;
                case 'e': case 'E':
                    break;
                case 'b': case 'B':
                    break;
                case 'i': case 'I':
                    break;
                case 's': case 'S':
                    break;
            
    }

}

void spinCube()
{
		theta[axis] += 2.0;
		if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
		glutPostRedisplay();
}

void reshape(int w, int h)
{
        width = w;
        height = h;

        /* Set the transformations */
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
        glMatrixMode(GL_MODELVIEW);
        glViewport(0, 0, w, h);

}
main(int argc, char **argv)
{
        /* Intialize the program */
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB);
        glutInitWindowSize(width, height);
        glutCreateWindow("star");

        /* Register the callbacks */
        glutDisplayFunc(display);

        glutMouseFunc(mouse);
        glutKeyboardFunc(keyboard);
        glutReshapeFunc(reshape);
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glEnable(GL_MAP1_VERTEX_3);

		init();
		glutMainLoop();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值