简体   繁体   English

OpenGL中对象的旋转

[英]Rotation of an object in opengl

I need to rotate the rectangular object upon pressing the left arrow key. 我需要按向左箭头键旋转矩形对象。

I am not able to rotate it, or maybe it is. 我无法旋转它,或者也许是。 There may be a problem playing with the buffer. 使用缓冲区可能有问题。

Weather the rotatef() function is placed correctly or where the problem lies. 如果正确放置了rotatef()函数,或者问题出在哪里。

#include<stdlib.h>
#include<glut.h>
#include<stdio.h>
#include<math.h>
#include<time.h>


void rot()
{   glMatrixMode(GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();
    glPushMatrix();

    glRotatef(30,0.0,0.0,1.0);
    glBegin(GL_POLYGON);
                glVertex2i(50,50);
                glVertex2i(170,50);
                glVertex2i(170,100);
                glVertex2i(50,100);
                glEnd();


    glPopMatrix();
}

void shape()
{

    glColor3f(1.0,0.0,0.0);


                glBegin(GL_POLYGON);
                glVertex2i(50,50);
                glVertex2i(170,50);
                glVertex2i(170,100);
                glVertex2i(50,100);
                glEnd();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    shape();
    glutSwapBuffers();
    glFlush();
}
void init()
{
    glClearColor(1.0,1.0,1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glOrtho(0,700,0,700,-1,1);
    glMatrixMode(GL_MODELVIEW);
}


void Keys(int key,int x,int y)
{
    if(key==GLUT_KEY_LEFT)
        rot();
     glutPostRedisplay();
}

void main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(700,700);
    glutCreateWindow("tetris");

    glutDisplayFunc(display);

    glutSpecialFunc(Keys);
    init();
    glutMainLoop();
}

You post a redisplay immediately after drawing your rotated vertices. 您绘制旋转的顶点后立即发布重新显示。 So right after drawing the rotated vertices, in your display callback function, you clear the window and then draw your vertices without rotation. 因此,在绘制旋转的顶点之后,立即在显示回调函数中清除窗口,然后绘制不旋转的顶点。 So that is what you'll see (without rotation), until the next update. 这就是您看到的内容(无需旋转),直到下一次更新为止。

One way to proceed is: 一种处理方式是:

  1. Do all your drawing in your display function 在显示功能中完成所有绘图
  2. As reaction to a keypress, just update the angle ( I guess you want to rotate incrementally) and post a redisplay. 作为对按键的反应,只需更新角度(我想您想逐渐旋转)并发布重新显示。 Don't draw anything, your display function will use the updated angle. 不要画任何东西,您的显示功能将使用更新的角度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM