【问题标题】:Moving multiple 2D shapes in OpenGL with key press通过按键在 OpenGL 中移动多个 2D 形状
【发布时间】:2022-01-06 20:40:44
【问题描述】:

我目前想要做到这一点,以便在我按下某个键时可以单独移动这些形状,但我不确定如何正确实现这一点;我已经看过这个问题How do I make a simple 2D shape move using the keyboard with GLUT,但我仍然不确定如何分配一个键来左右移动每个不同的形状。

//
//INCLUDE STATEMENTS FOR MS WINDOWS:
#include <windows.h>   // for MS Windows
#include <GL/glut.h>   // GLUT, include glu.h and gl.h
/* Initialize OpenGL Graphics */
void initGL() {
    // Set "clearing" or background color
     glClearColor ( 0.0f ,  0.0f ,  0.0f ,  1.0f ); // Black and opaque
}
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
     glClear ( GL_COLOR_BUFFER_BIT );   // Clear the color buffer with current clearing color
    
    // Define shapes enclosed within a pair of glBegin and glEnd
     glBegin ( GL_QUADS );              // Each set of 4 vertices form a quad
     glColor3f (1.0f , 0.0f , 0.0f );  // Red
     glVertex2f (- 0.6f ,  0.2f );     // Define vertices in counter-clockwise (CCW) order
     glVertex2f (- 0.2f ,  0.2f );     //   so that the normal (front-face) is facing you
    glVertex2f (- 0.2f ,  0.4f );
    glVertex2f (- 0.6f ,  0.4f );
     glEnd();
    
     glBegin ( GL_TRIANGLES );          // Each set of 3 vertices form a triangle
     glColor3f (0.0f , 0.0f , 1.0f );  // Blue
    glVertex2f ( - 0.4f , 0.5f );
    glVertex2f ( 0.0f , 0.5f );
    glVertex2f ( - 0.2f , 0.8f );
     glEnd();
    
     glBegin( GL_POLYGON );            // These vertices form a closed polygon
     glColor3f (1.0f , 1.0f , 0.0f );  // Yellow
    glVertex2f ( 0.2f ,  0.0f );
    glVertex2f ( 0.4f ,  0.0f );
    glVertex2f ( 0.5f ,  0.2f );
    glVertex2f ( 0.4f ,  0.4f );
    glVertex2f ( 0.2f ,  0.4f );
    glVertex2f ( 0.1f ,  0.2f );
     glEnd();
     
         glBegin( GL_POLYGON );            // These vertices form a closed polygon
     glColor3f (0.0f , 1.0f , 0.0f );  // Green
    glVertex2f ( 0.6f ,  0.3f );
    glVertex2f ( 0.8f ,  0.3f );
    glVertex2f ( 0.9f ,  0.5f );
    glVertex2f ( 0.7f ,  0.7f );
    glVertex2f ( 0.5f ,  0.5f );
     glEnd();
}
int main( int argc,  char** argv) {
glutInit(&argc, argv);            // Initialize GLUT
glutCreateWindow ( "Vertex, Primitive & Color" );  // Create window with the given title
glutInitWindowSize ( 320 ,  320 );   // Set the window's initial width & height
glutInitWindowPosition ( 50 ,  50 ); // Position the window's initial top-left corner
glutDisplayFunc ( display );       // Register callback handler for window re-paint event
initGL ();
glutMainLoop ();
return 0 ;
}

【问题讨论】:

    标签: c++ opengl 2d keypress glut


    【解决方案1】:

    您可以将各个 x/y 位置设置为一个变量,这样每当您按下某个键时,变量就会发生变化,并且形状会移动。例如:

    glBegin ( GL_QUADS );              // Each set of 4 vertices form a quad
         glColor3f (1.0f , 0.0f , 0.0f );  // Red
         glVertex2f (xPos - 0.6f ,  yPos + 0.2f );     // Define vertices in counter-clockwise (CCW) order
         glVertex2f (xPos - 0.2f ,  yPos + 0.2f );     //   so that the normal (front-face) is facing you
        glVertex2f (xPos - 0.2f ,  yPos + 0.4f );
        glVertex2f (xPos - 0.6f ,  yPos + 0.4f );
     glEnd();
     
     //draw loop or whatever
     
     while(true){
      if(GetAsyncKeyState('W')){
        yPos += .025;
      }
      if(GetAsyncKeyState('A')){
        xPos -= .025;
      }
      if(GetAsyncKeyState('S')){
        yPos -= .025;
      }
      if(GetAsyncKeyState('D')){
        xPos += .025;
      }
     }
    

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      相关资源
      最近更新 更多