サンプルbezcurve.c

bezcurve.png

フルソースコード(gist)

GLfloat ctrlpoints[4][3] = {
    { -4.0, -4.0, 0.0 },{ -2.0, 4.0, 0.0 },
    { 2.0, -4.0, 0.0 },{ 4.0, 4.0, 0.0 } };
 
void reshape(GLFWwindow* window) {
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    float ratio = (width <= height) ? height / (float)width : width / (float)height;
 
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (width <= height)
        glOrtho(-5.0, 5.0, -5.0*ratio,5.0*ratio, -5.0, 5.0);
    else
        glOrtho(-5.0*ratio, 5.0*ratio, -5.0, 5.0, -5.0, 5.0);
}
void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
    glEnable(GL_MAP1_VERTEX_3);
}
void display() {
    glClear(GL_COLOR_BUFFER_BIT);
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
 
    int i;
 
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINE_STRIP);
    for (i = 0; i <= 30; i++)
        glEvalCoord1f((GLfloat)i / 30.0f);
    glEnd();
    /* 制御点をドットで描画します */
    glPointSize(5.0);
    glColor3f(1.0, 1.0, 0.0);
    glBegin(GL_POINTS);
    for (i = 0; i < 4; i++)
        glVertex3fv(&ctrlpoints[i][0]);
    glEnd();
}

opengl2-sample opengl-chapter12

サポートサイト Wikidot.com opengl2-sampleopengl-chapter12