用贝塞尔曲线画“心”,用六个点控制就可以了(最下面一个点超出了屏幕范围,没显示)。
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
GLfloat ctrlpoints[7][3] = {
{0.0, 0.0, 0.0 },
{-15.0, 20.0, 0.0},
{-50.0, 0.0, 0.0},
{0.0, -110.0, 0.0},
{50.0, 0.0, 0.0},
{15.0, 20.0, 0.0},
{0.0, 0.0, 0.0 },
};
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 7, &ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
glShadeModel(GL_SMOOTH);
}
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
/* 显示控制点 */
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < 7; i++)
glVertex3fv(&ctrlpoints[0]);
glEnd();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(400,400);
glutCreateWindow("OpenGL Heart");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}
文章评论(0条评论)
登录后参与讨论