我在一个可以根据键盘输入向右或向左移动的区域中制作一个球。这是完整的代码:
#include <windows.h> //for Sleep(...) functionality
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
GLfloat angle = 0.0;
static int myWindow;
void keyboard_Handler(unsigned char key, int x, int y);
float ball_posX = 0.0;
void ball(void)
{
glColor4f(1.0f,0.0f,0.0f,0.6f); //set ball colour
glTranslatef(ball_posX, 0.0, 2); //moving it toward the screen a bit on creation
glutSolidSphere(0.25,100,100); //this is the damn ball
}
void floor(void)
{
//floors are a flat surface slightly facing down to support the logic that the ball will slide
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
for (GLfloat i = -2.5; i <= 2.5; i += 0.25)
{
glVertex3f(i, -0.25, 2.5);
glVertex3f(i, -0.25, -2.5);
glVertex3f(2.5, -0.25, i);
glVertex3f(-2.5, -0.25, i);
}
glEnd();
}
void init(void)
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
float cam_posX = 0.0;
float cam_posY = 0.5;
float cam_posZ = 5.0;
void display(void)
{
glClearColor(1.0,1.0,1.0,1.0); //to add background color (white)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the buffer for colour and depth
glLoadIdentity();
gluLookAt(cam_posX, cam_posY, cam_posZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //camera! (cam position X, cam position Y, cam position Z, cam target X, cam target Y, cam target Z, up position X, up position Y, up position Z)
floor();
ball();
glutSwapBuffers();
angle += 0.05; //to affect the glRotate function
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); // Set up display buffer
glutInitWindowSize(500, 500); //window's size
glutInitWindowPosition(100, 100); //window's position
myWindow = glutCreateWindow("Hendra Ganteng, read Controls.txt, I insist!"); //window's title
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard_Handler);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
void keyboard_Handler(unsigned char key, int x, int y)
{
switch(key)
{
//CAMERA
case 'a':
if(cam_posX > -3)
{
cam_posX = cam_posX - 1;
}
break;
case 'd':
if(cam_posX < 3)
{
cam_posX = cam_posX + 1;
}
break;
case 's':
if(cam_posY > 0.5)
{
cam_posY = cam_posY - 1;
}
break;
case 'w':
if(cam_posY < 3)
{
cam_posY = cam_posY + 1;
}
break;
case 'x':
if(cam_posZ == 5.0)
{
cam_posZ = 4.0;
}
else
cam_posZ = 5.0;
break;
//BALL
case ',':
if(ball_posX > -2)
{
for(int i=0; i<5; i++)
{
Sleep(200);
ball_posX = ball_posX - 0.2;
}
}
break;
case '.':
if(ball_posX < 2)
{
ball_posX = ball_posX + 1;
}
break;
}
}
但是,如果我更改其翻译,我的球将无法顺利移动。我向左移动时尝试了“睡眠”方法,但不是每0.2秒缓慢移动直到它到达目的地,它等待1秒然后毫不拖延地翻译。那么如何使翻译顺利进行呢?
答案 0 :(得分:0)
试试这个: 为main中的glutIdleFunc再做一个函数。我们将此函数称为idle()。 然后在空闲时移动显示功能的最后一行。
angle += 0.05; //to affect the glRotate function
您的代码中也没有glRotate调用。 试试这个
void display(void)
{
glClearColor(1.0,1.0,1.0,1.0); //to add background color (white)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the buffer for colour and depth
glLoadIdentity();
glRotate(angle, 0.0, 1.0, 0.0);//for horizontal rotation
gluLookAt(cam_posX, cam_posY, cam_posZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //camera! (cam position X, cam position Y, cam position Z, cam target X, cam target Y, cam target Z, up position X, up position Y, up position Z)
floor();
ball();
glutSwapBuffers();
}
您的新idle()函数将执行任何变量计算。