我无法使用用户输入使用 GLUT 更改三角形的 z 坐标?
I can't use user input to change z-coordinates of a triangle using GLUT?
我试图使用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应该重新绘制帧
相关文章:
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 绘制旋转的三角形
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 如何用for循环在c++中生成单词三角形
- 如何使用递归打印修改后的星号三角形图案
- C++ 创建带有 for 循环的三角形
- 绘制顺时针三角形,重新排序点
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Eigen c++ 三角形来自
- 为我的 c++ 类介绍制作一个三角形分类器.我有几个问题
- 使用对象文件读取三角形数据网格
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- 如何在三角形曲面细分评估着色器中插值 UV 映射坐标?
- 为什么在三角形带中使用z坐标开gl
- 三角形内的点:重心坐标
- DirectX 9.0(世界坐标移动我的对象(三角形)动画
- 在openGL中改变三角形坐标
- 重心坐标夹紧 3D 三角形
- 我无法使用用户输入使用 GLUT 更改三角形的 z 坐标?