在 GLUT 中使用键进行旋转

Rotation using keys in GLUT

我创建了一个使用箭头键移动方块的工具。现在,每次按下 'r' 键时,我都希望我的正方形在 y 轴上旋转 90 度。我已经为允许我的方块移动的箭头键添加了 case 语句。

这是我的程序;

#include "include\freeglut.h"   // OpenGL toolkit - in the local shared folder
#include <cmath>
#include <iostream>

//set up some constants
#define X_CENTRE 0.0      /* centre point of square */
#define Y_CENTRE 0.0
#define LENGTH   5.0      /* lengths of sides of square */


//forward declaration - best in the header
void drawStar(GLfloat radius, GLfloat x, GLfloat y);
void drawSquare(GLfloat length, GLfloat x, GLfloat y);
void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y);
void drawCar(GLfloat length, GLfloat x, GLfloat y);
void greenSquare(GLfloat length, GLfloat x, GLfloat y);


GLfloat red = 1.0, green = 1.0, blue = 1.0;
GLint   xmove = -13.0, ymove = -13.5, zmove = 0.0;

static GLfloat spin = 0.0;

GLboolean square = false;









/* reshape callback function
   executed when window is moved or resized. This function should be used in Tutorial 1 */
void reshape(int width, int height)
{
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
    glLoadIdentity();

  //  glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0);   //sets the x,y,z plane from -1 to 1
    if (width <= height) //if aspect is less or equal to 1
        glOrtho(-10.0, 10.0, -10.0/aspect, 10.0/aspect, -1.0, 1.0);
    else // aspect is greater than 1
        glOrtho(-10.0 * aspect, 10.0* aspect, -10.0 , 10.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
}


/* display callback function
   called whenever contents of window need to be re-displayed */
   //this is the all important drawing method - all drawing code goes in here
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);     /* clear window */
    /*glColor3f(1.0, 1.0, 1.0);        /* white drawing objects */

    glColor3f(0.0, 0.0, 1.0);          /* blue drawing objects */
    /* define object to be drawn as a square polygon */



    glColor3f(0.3, -3.0, 4.0);
    drawSquare(2, -3, 8);

    glColor3f(0.3, -1.0, 2.0);
    drawSquare(2, -3, 8);

    glColor3f(1.0, 0.0, 0.0);
    drawSquare(2, -5, -6);

    glColor3f(0.0, 1.0, 0.0);
    drawSquare(2, 0.0, 0.0);

    glColor3f(2.0, 3.0, 1.1);
    drawSquare(2, 6.0, 0.0);

    glColor3f(0.3, -1.0, 2.0);
    drawSquare(2, -6, 5);

    glColor3f(0.7, -1.0, 2.0);
    drawSquare(2, 5, -5);

    glColor3f(0.0, 0.0, 1.0);
    drawSquare(2, 4, 6);

    glColor3f(1.0, 0.0, 0.0);
    drawSquare(5, -8, -12);

    glColor3f(0.0, 1.0, 0.0);
    greenSquare(5,8,10.5);

    if (!square) {
        glColor3f(0.75, 0.5, 0.25);
        drawCar(1, 0, 0);
    }
    


    glFlush();     /* execute drawing commands in buffer */
}


void myMenu(GLint id) {

    if (id == 1) {
        square = 1.0;
    }
    else {
        square = 0.0;

    }
    glutPostRedisplay();
}









void drawCar(GLfloat length, GLfloat x, GLfloat y) {
    glPushMatrix();
    glTranslatef(xmove, ymove, zmove);
    //glBegin(GL_TRIANGLES);
    ////specify the vertices (points in 3D space) of the shape - note that these are 2D points
    //glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE - LENGTH / 3);
    //glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE + LENGTH / 3);
    //glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE + LENGTH / 3);
    //glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE - LENGTH / 3);
    //glEnd();

   glRectf(3, 4, 6, 2);
    glPopMatrix();
    glFlush();
   
}

//void spinDisplay(void) {
//    spin = spin + 90.0;
//    
//    glutPostRedisplay();
//}
//
//
//void rotate (unsigned char ,GLint x,GLint y){
//    switch (x) {
//    case 'r':
//        glRotatef(spin, 1.0, 90.0, 90.0);
//     break;
//    }
//
//
//
//}

void keyInput(int z, int x, int y)
{
    switch (z)
    {
    case GLUT_KEY_UP:
        ymove++;
        if (ymove >= 5) ymove = 5.0;

        break;
    case GLUT_KEY_DOWN:
        ymove--;
        //  if (ymove <= -5) ymove = 0.0;
        break;
    case GLUT_KEY_RIGHT:
        xmove++;
        //  if (xmove >= 5) xmove = 0.0;
        break;
    case GLUT_KEY_LEFT:
        xmove--;
        //  if (xmove <= -5) xmove = 0.0;
        break;
    }
        
    glutPostRedisplay();
}

void greenSquare(GLfloat length, GLfloat x, GLfloat y) {
    //x1,y1 is the top left-hand corner coordinate
    GLfloat x1, y1, x2, y2, x3, y3, x4, y4;

    //glColor3f(0.1, 1.0, 0.2);
    x1 = x - length / 2;
    y1 = y + length / 2;
    x2 = x + length / 2;
    y2 = y + length / 2;
    x3 = x + length / 2;
    y3 = y - length / 2;
    x4 = x - length / 2;
    y4 = y - length / 2;

    glBegin(GL_POLYGON);
    glVertex2f(x1, y1);
    glVertex2f(x2, y2);
    glVertex2f(x3, y3);
    glVertex2f(x4, y4);
    glEnd();

    glFlush();
}




void drawSquare(GLfloat length, GLfloat x, GLfloat y)
{
    //x1,y1 is the top left-hand corner coordinate
    GLfloat x1, y1, x2, y2, x3, y3, x4, y4;

    //glColor3f(0.1, 1.0, 0.2);
    x1 = x - length / 2;
    y1 = y + length / 2;
    x2 = x + length / 2;
    y2 = y + length / 2;
    x3 = x + length / 2;
    y3 = y - length / 2;
    x4 = x - length / 2;
    y4 = y - length / 2;

    glBegin(GL_POLYGON);
    glVertex2f(x1, y1);
    glVertex2f(x2, y2);
    glVertex2f(x3, y3);
    glVertex2f(x4, y4);
    glEnd();

    glFlush();
}

void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y)
{
    //x1,y1 is the top left-hand corner coordinate
    GLfloat x1, y1, x2, y2, x3, y3, x4, y4;

    //This example is for a rectangle 
    x1 = x - lengthX / 2;
    y1 = y + lengthY / 2;
    x2 = x + lengthX / 2;
    y2 = y + lengthY / 2;
    x3 = x + lengthX / 2;
    y3 = y - lengthY / 2;
    x4 = x - lengthX / 2;
    y4 = y - lengthY / 2;

    glBegin(GL_POLYGON);
    glVertex2f(x1, y1);
    glVertex2f(x2, y2);
    glVertex2f(x3, y3);
    glVertex2f(x4, y4);
    glEnd();

    glFlush();
}

//Draws a 5 pointed star using lines
void drawStar(GLfloat radius, GLfloat x, GLfloat y)
{
    //x1,y1 is the top coordinate
    //glColor3f(0.0, 1.0, 1.0);

    GLfloat x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;

    x1 = x;
    y1 = y + radius;
    x2 = x + 0.90 * radius;
    y2 = y + 0.40 * radius;
    x3 = x + 0.65 * radius;
    y3 = y - 0.55 * radius;
    x4 = x - 0.65 * radius;
    y4 = y - 0.55 * radius;
    x5 = x - 0.90 * radius;
    y5 = y + 0.40 * radius;

    glLineWidth(1.0);
    glBegin(GL_LINES);
    glVertex2f(x1, y1);
    glVertex2f(x3, y3);
    glVertex2f(x1, y1);
    glVertex2f(x4, y4);
    glVertex2f(x2, y2);
    glVertex2f(x4, y4);
    glVertex2f(x2, y2);
    glVertex2f(x5, y5);
    glVertex2f(x3, y3);
    glVertex2f(x5, y5);
    glEnd();


    glFlush();
}


/* graphics initialisation */
void init(void)
{
      
    glClearColor(0.0, 0.0, 0.0, 0.0);  //Setting up the background color 
}


int main(int argc, char** argv)
{


    /* window management code ... */
/* initialises GLUT and processes any command line arguments */
    glutInit(&argc, argv);

    /* use single-buffered window and RGBA colour model */
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);    
    glutInitWindowSize(750, 750);

    /* window upper left corner at (100, 100) */
    glutInitWindowPosition(100, 100);

    /* creates an OpenGL window with command argument in its title bar */
    glutCreateWindow("Car game");
    // glutKeyboardFunc(rotate);
    glutSpecialFunc(keyInput);

   //Pop menu using with right click 
    glutCreateMenu(myMenu);
    glutAddMenuEntry("Hide Square", 1);
    glutAddMenuEntry("Show Square", 2);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
   
    

    init();
    
    

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

使用 glRotatef 使汽车绕 z 轴 drawCar 旋转。 glRotatef 的第一个参数是以度为单位的角度。参数 2 到 4 指定旋转轴向量的 x、y 和 z 分量:

GLfloat xmove = -8.5, ymove = -9.0, zmove = 0.0;
GLfloat spin = 0.0;
void drawCar(GLfloat length, GLfloat x, GLfloat y) 
{
    glPushMatrix();
    glTranslatef(xmove, ymove, zmove);
    glRotatef(spin, 0, 0, 1);
    glRectf(-1.5, -1, 1.5, 1);
    glPopMatrix();
    glFlush(); 
}

rotate

中改变旋转
void rotate(unsigned char key, GLint x, GLint y){
    switch (key) {
    case 'r':
        spin = spin + 90.0;
        break;
    }
    glutPostRedisplay();
}
int main(int argc, char** argv)
{
    // [...]

    glutKeyboardFunc(rotate);

    // [...]
}