#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 = -0.3f;
float squareZ = 0.0f;

static int flag = 1;

void drawShape(void) {
    glTranslatef(squareX, squareY, squareZ);
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f(162, 50);
    glVertex2f(162, 10);
    glVertex2f(220, 10);
    glVertex2f(220, 50);
    glVertex2f(162, 50);
    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);
}

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

// make the square go up
void update(int value) {
    if (flag) {
        squareY += 1.0f;
        if (squareY > 400.0) {
            flag = 0;
        }
    }
    glutPostRedisplay();
    glutTimerFunc(25, update, 0);
}

// make the square go right
/* void update(int value) {
    if (flag) {
        squareX += 1.0f;
        if (squareX > 400.0) {
            flag = 0;
        }
    }
    glutPostRedisplay();
    glutTimerFunc(25, update, 0);
} */

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

我之前已经上传了此代码,但是这次我将广场一直拉到最上方。代码只是将正方形向上移动,但是一旦到达顶部,我不知道如何将其定位在左侧,因此我可以使其向右移动。我已经上传了一个演示,演示了下面的内容。

预习:

我接下来要做什么:

最佳答案

我建议使用矩形的起始位置初始化变量squareXsquareYsquareZ:

float squareX = 162.0f;
float squareY = 0.0f;
float squareZ = 0.0f;

不要绘制特定于矩形的位置,而是在位置(0,0)上绘制具有长度(widthheight)的矩形。让模型矩阵(由 glTranslatef 设置)完成定位工作:

void drawShape(void)
{
    float width = 58.0f;
    float height = 40.0f;

    glTranslatef(squareX, squareY, squareZ);
    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();
}

使用变量state,它指明了当前运动的方向:

int state = 1; // 0: stop; 1: move up; 2: move right

如果矩形到达某个位置,则必须更改state并设置新的起始位置。在最终位置,矩形可以停止,或者甚至可以重新启动该过程:
void update(int value)
{
    if (state == 1) // 1 : move up
    {
        squareY += 1.0f;
        if (squareY > 400.0)
        {
            state = 2;
            squareX = 0.0f;
            squareY = 180.0f;
        }
    }
    else if (state == 2) // 2 : move right
    {
      squareX += 1.0f;
      if (squareX > 400.0)
      {
          state = 0;

          // restart
          //state = 1;
          //squareX = 162.0f;
          //squareY = 0.0f;
      }
    }

    glutPostRedisplay();
    glutTimerFunc(25, update, 0);
}

关于c++ - 到达顶部后,将正方形放在左侧,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53884383/

10-10 12:39