/*************************************************************************
> 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
int width=400, height=400;
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};
float points[10][3] = {-1,-1,1,
1,-1,1,
1,-1,-1,
-1,-1,-1,
-1,1,1,
1,1,1,
1,1,-1,
-1,1,-1,
0,3,0,
3,0,0};
int faces[16][3]={1,2,5,
2,6,5,
2,10,6,
6,10,7,
2,3,10,
3,7,10,
5,6,9,
6,7,9,
7,8,9,
5,9,8,
1,5,4,
5,8,4,
1,4,2,
4,3,2,
8,7,4,
7,3,4
};
int points_list[10]={0};
float c = PI / 180.0f; //弧度和角度转换参数
int du = 90, oldmy = -1, oldmx = -1; //du是视点绕y轴的角度,opengl里默认y轴是上方向
float r = 1.5f, h = 0.0f; //r是视点绕y轴的半径,h是视点高度即在y轴上的坐标
GLfloat scale = 1.0;
void drawModel()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(255,255,255);
for(int i=0;i<16;i++)
{ glBegin(GL_POLYGON);
glVertex3f(points[ faces[i][0] -1][0] , points[faces[i][0]-1][1],points[faces[i][0]-1][2] );
glVertex3f(points[ faces[i][1] -1][0] , points[faces[i][1]-1][1],points[faces[i][1]-1][2] );
glVertex3f(points[ faces[i][2] -1][0] , points[faces[i][2]-1][1],points[faces[i][2]-1][2]);
glEnd();
}
glFlush();
}
void smooth()
{ for(int i=0;i<10;i++)
{ printf("point:%d->%.2f %.2f %.2f\n",i+1,points[i][0],points[i][1],points[i][2]);
//printf("change to:\n");
// points[i][0] = points[i][0] /2.0;
// points[i][1] = points[i][1] /3.0;
// points[i][2] = points[i][2] /4.0;
for(int j=0;j<16;j++)points_list[j]=0;
for(int j=0;j<16;j++)
{ if(faces[j][0]==i+1 || faces[j][1]==i+1 || faces[j][2]==i+1)
{ points_list[ faces[j][0]-1 ]=1;
points_list[ faces[j][1]-1 ]=1;
points_list[ faces[j][2]-1 ]=1;
}
}
printf("find:");
float x=0,y=0,z=0; //
int count = 0;
for(int l=0;l<16;l++)
{ if(points_list[l] == 1)
{ //printf("->%d ",l+1);
count++;
x += points[i][0] - points[l][0];
y += points[i][1] - points[l][1];
z += points[i][2] - points[l][2];
// printf("(%f %f %f)-(%f %f %f)=(%f %f %f)\n",points[i][0], points[i][1],points[i][2],
// points[l][0],points[l][1],points[l][2],
// x,y,z);
}
}
x = x/(count-1);
y = y/(count-1);
z = z/(count -1);
printf(" ->x:%f y:%f z:%f\n",x,y,z);
points[i][0] = points[i][0] /(1 + 0.1*fabs(x));
points[i][1] = points[i][1] /(1 + 0.1*fabs(y));
points[i][2] = points[i][2] /(1 + 0.1*fabs(z));
glutPostRedisplay();
}
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//printf("At:%.2f %.2f %.2f\n",r*cos(c*du),h,r*sin(c*du)); //这就是视点的坐标
glPushMatrix();
glLoadIdentity();
glRotatef(theta[0]/2, 1.0, 0.0, 0.0);
glRotatef(theta[1]/2, 0.0, 1.0, 0.0);
glRotatef(theta[2]/2, 0.0, 0.0, 1.0);
glScalef(scale, scale, scale);
glColor3f(1.0f, 0.0f, 0.0f);
drawModel();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
/* 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':
glTranslatef(0.0f,-20.0f,-40.0f);
puts("c");
break;
case 'e': case 'E':
break;
case 'b': case 'B':
break;
case 'i': case 'I':
break;
case 'h': case 'H':scale+=0.1;glutPostRedisplay();
break;
case 'n': case 'N':scale-=0.1;glutPostRedisplay();
break;
case 's': case 'S':
smooth();
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);
}
int main(int argc, char **argv)
{
/* Intialize the program */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
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();
return 0;
}
原图形:
转换之后: