opengl中对象的旋转

Rotation of an object in opengl

本文关键字:旋转 对象 opengl      更新时间:2023-10-16

我需要在按下左箭头键时旋转矩形对象。

我不能旋转它,也许是。可能是缓冲区有问题。

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();
}

绘制旋转顶点后立即发布重新显示。因此,在绘制旋转的顶点后,在显示回调函数中,您可以清除窗口,然后在不旋转的情况下绘制顶点。这就是您将看到的(不旋转),直到下一次更新。

一种方法是:

  1. 在显示功能中绘制所有图形
  2. 作为对按键的反应,只需更新角度(我想你想增量旋转)并重新显示。不要画任何东西,您的显示功能将使用更新后的角度