文章目录
- 自己最近学校开课程有一门计算机图形学,感觉快被其搞疯了,自己电脑扔学校里了,老家这老机器带不动程序,随时崩溃!有时候运行卡了半个小时没反应了,然后重启,自己就在这循环中没了耐性!头疼!感觉如果继续这种状态,这学期估计费了!自己测试并成功运行了几个小代码,有感兴趣的拿走不谢!
- #include <GL/glut.h> #include <math.h> #define PI 3.14159265358979323846 void init() { glClearColor(1.0f,1.0f,1.0f,1.0f); } void display(void) { GLfloat a=0.25; glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glBegin(GL_POLYGON); for(GLfloat t=0;t<2*PI;t+=0.01) { GLfloat x=a*(2*sin(t)-sin(2*t)),y=a*(2*cos(t)-cos(2*t)); glVertex2f(x,y); } glEnd(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(300,300); glutInitWindowPosition(300,300); glutCreateWindow("Heart"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }
- #include<GL/glut.h> /* 颜色立方体 */ //旋转角度 static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; //渲染回调函数 void RenderScene(void) { //清除缓冲区 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //保存矩阵 glPushMatrix(); //旋转 glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); //画六个面 glBegin(GL_QUADS); // 前 // 白色 glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)255); glVertex3f(50.0f,50.0f,50.0f); // 黄色 glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)0); glVertex3f(50.0f,-50.0f,50.0f); // 红色 glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)0); glVertex3f(-50.0f,-50.0f,50.0f); // 品红 glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)255); glVertex3f(-50.0f,50.0f,50.0f); // 后 // 蓝绿 glColor3f(0.0f, 1.0f, 1.0f); glVertex3f(50.0f,50.0f,-50.0f); // 绿色 glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(50.0f,-50.0f,-50.0f); // 黑色 glColor3f(0.0f, 0.0f, 0.0f); glVertex3f(-50.0f,-50.0f,-50.0f); // 蓝色 glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-50.0f,50.0f,-50.0f); // 顶面 // 蓝绿 glColor3f(0.0f, 1.0f, 1.0f); glVertex3f(50.0f,50.0f,-50.0f); // 白色 glColor3f(1.0f, 1.0f, 1.0f); glVertex3f(50.0f,50.0f,50.0f); // 品红 glColor3f(1.0f, 0.0f, 1.0f); glVertex3f(-50.0f,50.0f,50.0f); // 蓝色 glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-50.0f,50.0f,-50.0f); // 底面 // 绿色 glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(50.0f,-50.0f,-50.0f); // 黄色 glColor3f(1.0f, 1.0f, 0.0f); glVertex3f(50.0f,-50.0f,50.0f); // 红色 glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-50.0f,-50.0f,50.0f); // 黑色 glColor3f(0.0f, 0.0f, 0.0f); glVertex3f(-50.0f,-50.0f,-50.0f); // 左面 // 白色 glColor3f(1.0f, 1.0f, 1.0f); glVertex3f(50.0f,50.0f,50.0f); // 蓝绿色 glColor3f(0.0f, 1.0f, 1.0f); glVertex3f(50.0f,50.0f,-50.0f); // 滤色 glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(50.0f,-50.0f,-50.0f); // 黄色 glColor3f(1.0f, 1.0f, 0.0f); glVertex3f(50.0f,-50.0f,50.0f); // 右面 // 品红 glColor3f(1.0f, 0.0f, 1.0f); glVertex3f(-50.0f,50.0f,50.0f); // 蓝色 glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-50.0f,50.0f,-50.0f); // 黑色 glColor3f(0.0f, 0.0f, 0.0f); glVertex3f(-50.0f,-50.0f,-50.0f); // 红色 glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-50.0f,-50.0f,50.0f); glEnd(); glPopMatrix(); glutSwapBuffers(); } void SetupRC() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); glEnable(GL_DEPTH_TEST); glEnable(GL_DITHER); glShadeModel(GL_SMOOTH); } ///////////////////////////////////////////////// void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_UP) xRot-= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 5.0f; if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; if(key > 356.0f) xRot = 0.0f; if(key < -1.0f) xRot = 355.0f; if(key > 356.0f) yRot = 0.0f; if(key < -1.0f) yRot = 355.0f; // Refresh the Window glutPostRedisplay(); } void ChangeSize(int w, int h) { GLfloat fAspect; if(h == 0) h = 1; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); fAspect = (GLfloat)w / (GLfloat)h; gluPerspective(35.0f, fAspect, 1.0f, 1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -400.0f); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800,600); glutCreateWindow("RGB Cube"); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0; }
- #include <GL/glut.h> void drawstar(const GLfloat cx, const GLfloat cy, GLfloat angle) { const GLfloat point[2][12] = { { 0, 0.1, 0.4, 0.25, 0.4, 0.1, 0,-0.1, -0.4, -0.25, -0.4, -0.1 }, { 0.5, 0.3, 0.3, 0, -0.3, -0.3, -0.5, -0.3, -0.3, 0, 0.3, 0.3 } }; //确定每个点的颜色 GLfloat Color[3][12] = { 0 }; //六角星绘制 glBegin(GL_TRIANGLE_FAN); //开始绘制 { glColor3f(1, 1, 1); glVertex2f(0, 0); //中心点 for (int i = 0; i < 12; i++) { glVertex2f(point[0][i], point[1][i]);//顶点 } glVertex2f(point[0][0], point[1][0]); } glEnd(); } void display() { glClearColor(0, 0, 0, 1.0); glClear(GL_COLOR_BUFFER_BIT); drawstar(0, 0, 0); glFlush(); glutSwapBuffers(); //交换两个缓冲区指针 } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition(10, 10); glutInitWindowSize(400, 400); glutCreateWindow("SixStarGRB"); glutDisplayFunc(display); glutMainLoop(); //注册绘制回调函数 return 0; }
- #include <GL/glut.h> void drawstar() { //确定每个点的坐标 //(0,0.5),(0.1,0.3),.....,(-0.1,0.3) const GLfloat point[2][12] = { { 0, 0.1, 0.4, 0.25, 0.4, 0.1, 0,-0.1, -0.4, -0.25, -0.4, -0.1 }, { 0.5, 0.3, 0.3, 0, -0.3, -0.3, -0.5, -0.3, -0.3, 0, 0.3, 0.3 } }; //确定每个点的颜色 //最终会由得到的数字对应于12个顶点6种颜色的x,y,z对应的取值 GLfloat Color[3][12] = { 0 }; for (int i = 0; i < 12; i++) { if (i <= 3 || i >= 10) Color[0][i] = 1; // x = 0 1 2 3 10 11 if (i >= 6 && i <= 11) Color[1][i] = 1; // y = 6 7 8 9 10 11 if (i >= 2 && i <= 7) Color[2][i] = 1; // z = 2 3 4 5 6 7 } //六角星绘制 glBegin(GL_TRIANGLE_FAN); //开始绘制 { glColor3f(1, 1, 1); glVertex2f(0, 0); //中心点 for (int i = 0; i < 12; i++) { glColor3f(Color[0][i], Color[1][i], Color[2][i]);//选择对应顶点颜色 glVertex2f(point[0][i], point[1][i]);//依次会得到12个顶点的坐标值 } glColor3f(Color[0][0], Color[1][0], Color[2][0]); glVertex2f(point[0][0], point[1][0]); } glEnd(); } void display() { glClearColor(0, 0, 0, 1.0); glClear(GL_COLOR_BUFFER_BIT); drawstar(0, 0, 0); glFlush(); glutSwapBuffers(); //交换两个缓冲区指针 } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition(100, 100); glutInitWindowSize(500, 500); glutCreateWindow("SixStarGRB"); glutDisplayFunc(display); glutMainLoop(); //注册绘制回调函数 return 0; }
- #include <GL/glut.h> #include<math.h> #include<cstdio> /* 输入:中心坐标(cx,cy),旋转角度 angle(顺时针) */ void drawstar(const GLfloat cx, const GLfloat cy, GLfloat angle) { glTranslatef(cx, cy, 0); glRotatef(angle, 0, 0, -1); //顺时针旋转 //确定每个点的坐标 const GLfloat point[2][12] = { { 0, 0.01, 0.04, 0.025, 0.04, 0.01, 0,-0.01, -0.04, -0.025, -0.04, -0.01 }, { 0.05, 0.03, 0.03, 0, -0.03, -0.03, -0.05, -0.03, -0.03, 0, 0.03, 0.03 } }; //确定每个点的颜色 GLfloat Color[3][12] = { 0 }; for (int i = 0; i < 12; i++) { if (i <= 3 || i >= 10) Color[0][i] = 1; // 0 1 2 3 10 11 if (i >= 2 && i <= 7) Color[1][i] = 1; // 2 3 4 5 6 7 if (i >= 6 && i <= 11) Color[2][i] = 1; // 6 7 8 9 10 11 } //六角星绘制 glBegin(GL_TRIANGLE_FAN); //开始绘制 { glColor3f(1, 1, 1); glVertex2f(0, 0); //中心点 for (int i = 0; i < 12; i++) { glColor3f(Color[0][i], Color[1][i], Color[2][i]);//选择颜色 glVertex2f(point[0][i], point[1][i]);//顶点 } glColor3f(Color[0][0], Color[1][0], Color[2][0]); glVertex2f(point[0][0], point[1][0]); } glEnd(); glLoadIdentity();//重置当前的模型观察矩阵,该句执行完后,将焦点移动到了屏幕的中心 } void display() { glClearColor(0, 0, 0, 1.0); glClear(GL_COLOR_BUFFER_BIT); /* *不清楚啥原因 *感觉可能是机器精度有问题,sin和cos函数酸错了! */ // int theta = 0; // float x,y; // while(theta < 270){ // x = 0.75*cos(theta); // y = 0.75*sin(theta); // // drawstar(x,y,theta); // printf("%f,%fn",cos(theta),sin(theta)); // theta = theta + 90; // printf("(%f,%f,%d)n",x,y,theta); // } // drawstar(-0.75,0,0); // drawstar(0,0.75,30); // drawstar(0.75,0,180); // drawstar(0,-0.75,270); drawstar(-0.75,0,0); drawstar(-0.65,0.375,30); drawstar(-0.375,0.65,60); drawstar(0,0.75,90); drawstar(0.375,0.65,120); drawstar(0.65,0.375,150); drawstar(0.75,0,180); drawstar(0.65,-0.375,210); drawstar(0.375,-0.65,240); drawstar(0,-0.75,270); drawstar(-0.375,-0.75,300); drawstar(-0.65,-0.375,330); glFlush(); glutSwapBuffers(); //交换两个缓冲区指针 } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition(10, 10); glutInitWindowSize(500, 500); glutCreateWindow("SixStarRGBInCircle"); glutDisplayFunc(display); glutMainLoop(); //注册绘制回调函数 return 0; }
自己最近学校开课程有一门计算机图形学,感觉快被其搞疯了,自己电脑扔学校里了,老家这老机器带不动程序,随时崩溃!有时候运行卡了半个小时没反应了,然后重启,自己就在这循环中没了耐性!头疼!感觉如果继续这种状态,这学期估计费了!自己测试并成功运行了几个小代码,有感兴趣的拿走不谢!
自己最近学校开课程有一门计算机图形学,感觉快被其搞疯了,自己电脑扔学校里了,老家这老机器带不动程序,随时崩溃!有时候运行卡了半个小时没反应了,然后重启,自己就在这循环中没了耐性!头疼!感觉如果继续这种状态,这学期估计费了!自己测试并成功运行了几个小代码,有感兴趣的拿走不谢!

#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265358979323846
void init()
{
glClearColor(1.0f,1.0f,1.0f,1.0f);
}
void display(void)
{
GLfloat a=0.25;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_POLYGON);
for(GLfloat t=0;t<2*PI;t+=0.01)
{
GLfloat x=a*(2*sin(t)-sin(2*t)),y=a*(2*cos(t)-cos(2*t));
glVertex2f(x,y);
}
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(300,300);
glutInitWindowPosition(300,300);
glutCreateWindow("Heart");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

#include<GL/glut.h>
/* 颜色立方体 */
//旋转角度
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
//渲染回调函数
void RenderScene(void)
{
//清除缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//保存矩阵
glPushMatrix();
//旋转
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
//画六个面
glBegin(GL_QUADS);
// 前
// 白色
glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)255);
glVertex3f(50.0f,50.0f,50.0f);
// 黄色
glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)0);
glVertex3f(50.0f,-50.0f,50.0f);
// 红色
glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)0);
glVertex3f(-50.0f,-50.0f,50.0f);
// 品红
glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)255);
glVertex3f(-50.0f,50.0f,50.0f);
// 后
// 蓝绿
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);
// 绿色
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);
// 黑色
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);
// 蓝色
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);
// 顶面
// 蓝绿
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);
// 白色
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,50.0f);
// 品红
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,50.0f);
// 蓝色
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);
// 底面
// 绿色
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);
// 黄色
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,50.0f);
// 红色
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,50.0f);
// 黑色
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);
// 左面
// 白色
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,50.0f);
// 蓝绿色
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);
// 滤色
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);
// 黄色
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,50.0f);
// 右面
// 品红
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,50.0f);
// 蓝色
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);
// 黑色
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);
// 红色
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,50.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void SetupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glEnable(GL_DEPTH_TEST);
glEnable(GL_DITHER);
glShadeModel(GL_SMOOTH);
}
/////////////////////////////////////////////////
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat fAspect;
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
fAspect = (GLfloat)w / (GLfloat)h;
gluPerspective(35.0f, fAspect, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -400.0f);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("RGB Cube");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}

#include <GL/glut.h>
void drawstar(const GLfloat cx, const GLfloat cy, GLfloat angle)
{
const GLfloat point[2][12] =
{
{ 0, 0.1, 0.4, 0.25, 0.4, 0.1, 0,-0.1, -0.4, -0.25, -0.4, -0.1 },
{ 0.5, 0.3, 0.3, 0, -0.3, -0.3, -0.5, -0.3, -0.3, 0, 0.3, 0.3 }
};
//确定每个点的颜色
GLfloat Color[3][12] = { 0 };
//六角星绘制
glBegin(GL_TRIANGLE_FAN); //开始绘制
{
glColor3f(1, 1, 1);
glVertex2f(0, 0); //中心点
for (int i = 0; i < 12; i++)
{
glVertex2f(point[0][i], point[1][i]);//顶点
}
glVertex2f(point[0][0], point[1][0]);
}
glEnd();
}
void display()
{
glClearColor(0, 0, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
drawstar(0, 0, 0);
glFlush();
glutSwapBuffers(); //交换两个缓冲区指针
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(10, 10);
glutInitWindowSize(400, 400);
glutCreateWindow("SixStarGRB");
glutDisplayFunc(display);
glutMainLoop(); //注册绘制回调函数
return 0;
}


#include <GL/glut.h>
void drawstar()
{
//确定每个点的坐标
//(0,0.5),(0.1,0.3),.....,(-0.1,0.3)
const GLfloat point[2][12] =
{
{ 0, 0.1, 0.4, 0.25, 0.4, 0.1, 0,-0.1, -0.4, -0.25, -0.4, -0.1 },
{ 0.5, 0.3, 0.3, 0, -0.3, -0.3, -0.5, -0.3, -0.3, 0, 0.3, 0.3 }
};
//确定每个点的颜色
//最终会由得到的数字对应于12个顶点6种颜色的x,y,z对应的取值
GLfloat Color[3][12] = { 0 };
for (int i = 0; i < 12; i++)
{
if (i <= 3 || i >= 10)
Color[0][i] = 1; // x = 0 1 2 3 10 11
if (i >= 6 && i <= 11)
Color[1][i] = 1; // y = 6 7 8 9 10 11
if (i >= 2 && i <= 7)
Color[2][i] = 1; // z = 2 3 4 5 6 7
}
//六角星绘制
glBegin(GL_TRIANGLE_FAN); //开始绘制
{
glColor3f(1, 1, 1);
glVertex2f(0, 0); //中心点
for (int i = 0; i < 12; i++)
{
glColor3f(Color[0][i], Color[1][i], Color[2][i]);//选择对应顶点颜色
glVertex2f(point[0][i], point[1][i]);//依次会得到12个顶点的坐标值
}
glColor3f(Color[0][0], Color[1][0], Color[2][0]);
glVertex2f(point[0][0], point[1][0]);
}
glEnd();
}
void display()
{
glClearColor(0, 0, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
drawstar(0, 0, 0);
glFlush();
glutSwapBuffers(); //交换两个缓冲区指针
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("SixStarGRB");
glutDisplayFunc(display);
glutMainLoop(); //注册绘制回调函数
return 0;
}

#include <GL/glut.h>
#include<math.h>
#include<cstdio>
/* 输入:中心坐标(cx,cy),旋转角度 angle(顺时针) */
void drawstar(const GLfloat cx, const GLfloat cy, GLfloat angle)
{
glTranslatef(cx, cy, 0);
glRotatef(angle, 0, 0, -1); //顺时针旋转
//确定每个点的坐标
const GLfloat point[2][12] =
{
{ 0, 0.01, 0.04, 0.025, 0.04, 0.01, 0,-0.01, -0.04, -0.025, -0.04, -0.01 },
{ 0.05, 0.03, 0.03, 0, -0.03, -0.03, -0.05, -0.03, -0.03, 0, 0.03, 0.03 }
};
//确定每个点的颜色
GLfloat Color[3][12] = { 0 };
for (int i = 0; i < 12; i++)
{
if (i <= 3 || i >= 10)
Color[0][i] = 1; // 0 1 2 3 10 11
if (i >= 2 && i <= 7)
Color[1][i] = 1; // 2 3 4 5 6 7
if (i >= 6 && i <= 11)
Color[2][i] = 1; // 6 7 8 9 10 11
}
//六角星绘制
glBegin(GL_TRIANGLE_FAN); //开始绘制
{
glColor3f(1, 1, 1);
glVertex2f(0, 0); //中心点
for (int i = 0; i < 12; i++)
{
glColor3f(Color[0][i], Color[1][i], Color[2][i]);//选择颜色
glVertex2f(point[0][i], point[1][i]);//顶点
}
glColor3f(Color[0][0], Color[1][0], Color[2][0]);
glVertex2f(point[0][0], point[1][0]);
}
glEnd();
glLoadIdentity();//重置当前的模型观察矩阵,该句执行完后,将焦点移动到了屏幕的中心
}
void display()
{
glClearColor(0, 0, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
/*
*不清楚啥原因
*感觉可能是机器精度有问题,sin和cos函数酸错了!
*/
// int theta = 0;
// float x,y;
// while(theta < 270){
// x = 0.75*cos(theta);
// y = 0.75*sin(theta);
// // drawstar(x,y,theta);
// printf("%f,%fn",cos(theta),sin(theta));
// theta = theta + 90;
// printf("(%f,%f,%d)n",x,y,theta);
// }
// drawstar(-0.75,0,0);
// drawstar(0,0.75,30);
// drawstar(0.75,0,180);
// drawstar(0,-0.75,270);
drawstar(-0.75,0,0);
drawstar(-0.65,0.375,30);
drawstar(-0.375,0.65,60);
drawstar(0,0.75,90);
drawstar(0.375,0.65,120);
drawstar(0.65,0.375,150);
drawstar(0.75,0,180);
drawstar(0.65,-0.375,210);
drawstar(0.375,-0.65,240);
drawstar(0,-0.75,270);
drawstar(-0.375,-0.75,300);
drawstar(-0.65,-0.375,330);
glFlush();
glutSwapBuffers(); //交换两个缓冲区指针
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(10, 10);
glutInitWindowSize(500, 500);
glutCreateWindow("SixStarRGBInCircle");
glutDisplayFunc(display);
glutMainLoop(); //注册绘制回调函数
return 0;
}
