1
#include <stdio.h> // this library is for standard input and output
#include "glut.h" // this library is for glut the OpenGL Utility Toolkit
#include <math.h>

float squareX = 0.0f;
float squareY = 200.0f;

static int flag = 1;

void drawShape(void) {
    float width = 58.0f;
    float height = 40.0f;
    glTranslatef(squareX, squareY, 0);
    // test
    // glScalef(0.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f(0, 0);
    glVertex2f(width, 0);
    glVertex2f(width, height);
    glVertex2f(0, height);
    glVertex2f(0, 0);
    glEnd();
}

void initRendering() {
    glEnable(GL_DEPTH_TEST);
}

// called when the window is resized
void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f);
}

int state = 1;

void update(int value) {
    if (state == 1) { // 1 : move right
        squareX += 1.0f;
        if (squareX > 400.0) {
            state = 0;
        }
    }
    glutPostRedisplay();
    glutTimerFunc(25, update, 0);
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    drawShape();
    glutSwapBuffers();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Moving Square");
    initRendering();
    glutDisplayFunc(display);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25, update, 0);
    glutMainLoop();
    return(0);
}

正方形を右に移動しながら大きくしたい。下の 2 番目の GIF を参照してください。正方形を大きくする必要があることはわかってglScalefいますが、移動中に大きくする方法がわかりません。

コードのプレビュー:

右に移動する正方形

これと同様のことを行うために必要です(品質については申し訳ありませんが、GIFは自分で作成しました):

正方形が大きくなる

4

1 に答える 1