1

我正在根据键盘输入可能仅向右或向左移动的场地中制作球。这是完整的代码:

#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 秒然后立即翻译。那么如何让翻译流畅呢?

4

1 回答 1

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() 函数将执行任何变量计算。

于 2013-06-16T22:38:45.910 回答