使GLUT应用程序在键盘按下时旋转

时间:2016-03-22 06:25:48

标签: opengl glut

我尝试制作一个GLUT应用程序,可以在按下的键上旋转对象,但似乎没有用。

#include <stdio.h>
#include <gl/glut.h>

GLfloat rotation = 90.0;
float posX = 0, posY = 0, posZ = 0;

void reshape(int width, int heigth){
    /* window ro reshape when made it bigger or smaller*/

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //clip the windows so its shortest side is 2.0
    if (width < heigth) {
        glOrtho(-2.0, 2.0, -2.0 * (GLfloat)heigth / (GLfloat)width, 2.0 * (GLfloat)heigth / (GLfloat)width, 2.0, 2.0);
    }
    else{
        glOrtho(-2.0, 2.0, -2.0 * (GLfloat)width / (GLfloat)heigth, 2.0 * (GLfloat)width / (GLfloat)heigth,2.0 , 2.0);
    }
    // set viewport to use the entire new window
    glViewport(0, 0, width, heigth);
}

void rect(){
    glBegin(GL_POLYGON);
    glVertex2f(-0.2, -0.2);
    glColor3f(1.0, 1.0, 0.0);
    glVertex2f(-0.2, 0.2);
    glColor3f(1.0, 0.0, 1.0);
    glVertex2f(0.2, 0.2);
    glColor3f(0.0, 1.0, 1.0);
    glVertex2f(1.2, -0.2);
    glColor3f(1.0, 1.0, 1.0);
    glEnd();



}

void display(){
    //Clear Window
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(posX,posY,posZ);
    rect();
    glPopMatrix();
    glFlush();
}


void init(){
    // set clear color to black
    glClearColor(0.0, 0.0, 0.0, 0.0);

    // set fill color to white
    glColor3f(1.0, 1.0, 1.0);

    //set up standard orthogonal view with clipping
    //box as cube of side 2 centered at origin
    //This is the default view and these statements could be removed
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

}
float move_unit = 10;
int deg = 0;
void keyboardown(int key, int x, int y)
{
    switch (key){
        case GLUT_KEY_RIGHT:
            glRotatef((deg+=move_unit), posX, posY, posZ);;
            glutPostRedisplay();
            break;

        case GLUT_KEY_LEFT:
            glRotatef(deg-=move_unit, posX, posY, posZ);;
        break;

        case GLUT_KEY_UP:
            glRotatef(deg-=move_unit, posX, posY, posZ);;
            break;

        case GLUT_KEY_DOWN:
            glRotatef(deg+=move_unit, posX, posY, posZ);;
        break;

        default:
         break;
    }
    glutPostRedisplay();
}


int main(int argc, char** argv){

    //initialize mode and open a windows in upper left corner of screen
    //Windows tittle is name of program

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Move Test");
    glutDisplayFunc(display);
    init();
    glutSpecialFunc(keyboardown);
    glutMainLoop();

}

有什么我做错了吗?

之前,我尝试使用GLUT_KEY_移动2d对象并且它有效,但是当我将命令更改为glrotatef时,它无法正常工作。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这里的问题是,在使用之前覆盖矩阵。在display中设置矩阵,但在glLoadIdentity();的开头调用keyboardown函数,它会重置矩阵并移除旋转。

要解决此问题,您可以,例如,将旋转角度存储在变量中。在display中增加/减少角度。在glRotatef函数中渲染时,您已将矩阵重置为已完成,然后通过使用先前存储的角度调用func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageTableViewCell let receiveTextSize: CGFloat = cell.receivedMessage.intrinsicContentSize().width let sendTextSize: CGFloat = cell.sentMessage.intrinsicContentSize().width cell.receivedMessage.alpha = 0 cell.sentMessage.alpha = 0 if messageReceived[indexPath.row] { if receiveTextSize >= 220 { cell.sentMessage.frame.size = CGSizeMake(cell.sentMessage.frame.width, 100) } cell.receivedMessage.text = messageContent[indexPath.row] as String cell.receivedMessage.alpha = 1 } else { if sendTextSize >= 220 { cell.sentMessage.frame.size = CGSizeMake(cell.sentMessage.frame.width, 100) cell.sentMessage.numberOfLines = 0 } cell.sentMessage.text = messageContent[indexPath.row] as String cell.sentMessage.alpha = 1 } cell.receivedMessage.sizeToFit() cell.receivedMessage.frame.size = CGSizeMake(100, 100) return cell } 来添加旋转。