我无法使用用户输入使用 GLUT 更改三角形的 z 坐标?

I can't use user input to change z-coordinates of a triangle using GLUT?

本文关键字:三角形 坐标 GLUT 用户 输入      更新时间:2023-10-16

我试图使用GLUT和OpenGL来绘制一个三角形,它将根据用户输入改变其z坐标。但我不能让它工作;没有编译错误,但是当我按下这些键时三角形不会改变。有人知道为什么吗?

/////////////////////////////
/////OPENGL ASSIGNMENT 1/////
/////////////////////////////
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include <GL/glut.h>
using namespace std;


/////GLOBAL VARIABLES/////
float zCoord = -10.0;    
////Function Declarations/////
void initRendering();
void windowResize(int width, int height);
void keyBoardEvents(unsigned char key, int x, int y);   ///this is the function that         will use user input
void drawing();  /// Note: this is the function we will use to draw shapes using opengl   functions


////Function Definitions/////

 void initRendering()
{
     glEnable(GL_DEPTH_TEST);
}

void windowResize(int width, int height) 
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); 
glLoadIdentity();
gluPerspective(45.0, (double) width/ (double) height, 1.0, 200.0);       ////Note:     45.0 is the camera angle, w/h is the aspect ratio///          
}

void keyBoardEvents(unsigned char key, int x, int y) 
{    
switch (key)
{
case 27: exit(0);
    break;
case 'w': zCoord = zCoord + 5.0f;
    break;
case 's': zCoord = zCoord - 5.0f;
    break;
case 'r': zCoord = -5.0f;
    break;
}
if (zCoord < -30.0)       ////// here we ensure -5.0 > zCoord > -30.0
{
    zCoord = 30.0;
}
else if (zCoord > -5.0)
{
    zCoord = -5.0;
}
}


void drawing()     
{
//Standard OpenGL at the beginning of each drawing() function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 
///Specifically to this one, drawing the rectangle:
glBegin(GL_TRIANGLES);    ////here we say start grouping the next 3 statements as 3 vertices of a triangle
glVertex3f(-0.5f, 0.5f, (float)zCoord);
glVertex3f(-1.0f, 1.5f, (float)zCoord);
glVertex3f(-1.5f, 0.5f, (float)zCoord);
glEnd;
glutSwapBuffers();

}



int main(int argc, char** argv)      ////here we are passing some initial arguments to   main, that the user does not have to input
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE );     ///initiates things  like depth (DEPTH) and color (RGB)/////
glutInitWindowSize(400, 400);     /////the initial window size
glutCreateWindow("MentorAssignment_1");     /////here we using an OpenGL function to make a window of title MentorAssignment_1
initRendering();

//////HANDLER FUNCTIONS/////      ////These are functions that call other functions, like keyBoardEvents and drawing, every time glutMainLoop() runs
glutKeyboardFunc(keyBoardEvents);
glutDisplayFunc(drawing);
glutReshapeFunc(windowResize);
glutMainLoop(); ///The openGL function that ensures main runs infinitely! (until escape is pressed)
return 0;
}

您在glEnd之后缺少括号,这意味着您没有调用该函数,只是获取其地址。在glBegin()之后发出缓冲区交换可能会使OpenGL处于错误状态,并导致它忽略所有进一步的渲染命令。最终的效果是三角形似乎没有移动,只是因为它不再被重新绘制。

您需要在keyBoardEvents函数的末尾调用glutPostRedisplay()来指示GLUT应该重新绘制帧