C++ 如何在OpenGL中将对象旋转一定的度数?是否有内置命令或必须使用公式?

C++ 如何在OpenGL中将对象旋转一定的度数?是否有内置命令或必须使用公式?,c++,math,opengl,graphics,formula,C++,Math,Opengl,Graphics,Formula,如何在OpenGL中将对象旋转一定的度数?是否有内置命令或必须使用公式?我在这个问题上纠缠了好几天。这是一个程序,在我的鼠标移动时在它下面画一个形状 假设我有一个绘图功能: glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glColor3f ( 1, 1, 1 ); glBegin (toggle_type ); //Where cur_x and cur_y is the current mouse location that gets auto-updated

如何在OpenGL中将对象旋转一定的度数?是否有内置命令或必须使用公式?我在这个问题上纠缠了好几天。这是一个程序,在我的鼠标移动时在它下面画一个形状

假设我有一个绘图功能:

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColor3f ( 1, 1, 1 );
glBegin (toggle_type );

//Where cur_x and cur_y is the current mouse location that gets auto-updated
//ratiox is 0.7 and ratioy is 0.6
if (toggle_type==GL_QUADS) //rectangle from (-length, -length) to (length,length) 
{
    glVertex2f ( cur_x- length*ratiox, cur_y + length*ratioy );
    glVertex2f ( cur_x+ length*ratiox, cur_y + length*ratioy );
    glVertex2f ( cur_x+ length*ratiox, cur_y- length*ratioy );
    glVertex2f ( cur_x-  length*ratiox, cur_y- length*ratioy );
}
else if (toggle_type==GL_TRIANGLES)//triangle with vertices (-length, -length), (length, -length), (0, length).
{
    glVertex2f ( cur_x- length, cur_y - length );
    glVertex2f ( cur_x+length, cur_y - length );
    glVertex2f ( cur_x, cur_y + length );
}

else if (toggle_type==GL_LINES) //line brush with vertices (0,-length), (0,length)
{

    glVertex2f ( cur_x, cur_y - length );
    glVertex2f ( cur_x, cur_y + length );
}

我不能在使用glBegin之前先使用glRotatef(),可以吗?我想把它绕着我的鼠标旋转一定的角度。没有内置的功能吗?如果没有,我应该使用什么公式?

您需要学习如何使用OpenGL转换:glTranslate、glRotate和glScale

翻译的意思是“移动物体”。缩放的意思是“使物体变大或变小”。旋转的意思是它听起来的意思

使用OpenGL转换,可以帮助您在每次发出转换时更改坐标系

所以要做到这一点,我们假设您正在围绕光标绘制一个框。首先平移到鼠标光标的位置。那是你想画画的地方。然后围绕光标旋转坐标系,这样您就可以轻松地绘制长方体,而无需使用正弦、余弦和角度进行有趣的操作。然后,将对象缩放到您想要的大小——这会缩小或扩展坐标系。最后,只要在原点(0,0)周围画一个一个单位的框,它就会以您想要的位置、旋转和大小显示在屏幕上

如果在平移之前尝试旋转,将得到错误的结果。其技术原因是OpenGL post将变换矩阵乘以顶点向量


是的,您应该在begin/end块之外执行所有变换。“开始/结束”块仅用于指定顶点、法线等。

我只是在玩freeglut,看看多个窗口是否可以工作(似乎可以!),我使用旋转来查看显示中的一些更改:

#include <cstdio>
#include <cassert>
#include <GL/freeglut.h>

#define DEGREES_X_SEC 10.0
int w_dc, w_ds;
float yRotationAngle;

void DrawSphere()
{
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
    glRotatef(yRotationAngle, .3f, .3f, .3f);
    glutWireSphere(.3, 20, 20);
    glFlush();
    glutSwapBuffers();
}
void DrawCube()
{
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
    glRotatef(yRotationAngle, .2f, .2f, .2f);
    glutWireCube(.5);
    glFlush();
    glutSwapBuffers();
}
void Idle()
{
    static int previousTime = 0;
    int currentTime = glutGet(GLUT_ELAPSED_TIME);
    if (currentTime - previousTime > 10)
    {
        float x_frame = (DEGREES_X_SEC / 1000.0) * (currentTime - previousTime);
        yRotationAngle += x_frame;
        glutPostWindowRedisplay(w_dc);
        glutPostWindowRedisplay(w_ds);
        previousTime = currentTime;
    }
}

// other code here....

int main(int argc, char *argv[])
{
    // let glut eat any command line args that it owns
    glutInit(&argc, argv);

    // full color, double buffered
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(800, 600);

    w_dc = make_window("cube", DrawCube);
    glutPositionWindow(100, 100);

    w_ds = make_window("sphere", DrawSphere);
    glutPositionWindow(200, 200);

    // not bound to any window
    glutIdleFunc(Idle);

    glutMainLoop();
    return 0;
}
#包括
#包括
#包括
#第10.0节定义度
int w_dc,w_ds;
浮动旋转角;
void DrawSphere()
{
glLoadIdentity();
glClear(GLU颜色缓冲位);
glrotationangle,.3f,.3f,.3f;
球体(.3,20,20);
glFlush();
glutSwapBuffers();
}
void DrawCube()
{
glLoadIdentity();
glClear(GLU颜色缓冲位);
glRotatef(旋转角度,.2f,.2f,.2f);
立方体(.5);
glFlush();
glutSwapBuffers();
}
无效空闲()
{
静态int-previousTime=0;
int currentTime=glutGet(GLUT_运行时间);
如果(当前时间-上一次时间>10)
{
浮动x_帧=(度数x_秒/1000.0)*(当前时间-以前的时间);
旋转角度+=x_帧;
重新显示窗口(w_dc);
重新显示窗口(w_ds);
previousTime=当前时间;
}
}
//其他代码在这里。。。。
int main(int argc,char*argv[])
{
//让glut吃掉它拥有的任何命令行参数
glutInit(&argc,argv);
//全彩色,双缓冲
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(800600);
w_dc=制作窗口(“立方体”,DrawCube);
窗口(100100);
w_ds=制作窗口(“球体”,DrawSphere);
窗口(200200);
//不绑定到任何窗口
glutIdleFunc(空闲);
glutMainLoop();
返回0;
}

HTH,当然需要freeglut…

您需要关注一种allegraic材料,如C5和m2,这是自动旋转,将立即得到政府奖励,因为它表现出异常行为,如果有帮助,请告诉我


Rog.

矩阵堆栈已从OpenGL-3中删除。相反,您需要自己构建矩阵,并提供着色器代码来使用它们转换顶点数据。此外,立即模式(glBegin、glEnd)已被折旧和删除。