【发布时间】: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