【问题标题】:How to translate and rotate a text drawing in OpenGl by keyboard如何通过键盘在 OpenGL 中翻译和旋转文本图形
【发布时间】:2021-06-28 19:36:03
【问题描述】:

我有一个使用 OpenGl 以原始方式编写的文本,我怎样才能对整个文本块进行平移、旋转和缩放,以便通过按一个键盘键来完成这些操作?

这是我引用文本的代码:

#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>

using namespace std;
void myInit(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glColor3f(0.0f, 0.0f, 0.0f);
    glLineWidth(5.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 1000.0, 0.0, 600.0);
}

float xposition = 100, Yposition = 50;

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POLYGON);
        glColor3f(1.0, 0.0, 1.0);
        glVertex2f(xposition, 100);
        glVertex2f(Yposition, 100);
        glVertex2f(Yposition , 50);
        glVertex2f(xposition, 50);
    glEnd();
    //The text I want to translate, rotate and scale
    glBegin(GL_LINE_STRIP); // S
        glVertex2i(270, 600);
        glVertex2i(200, 600);
        glVertex2i(280, 400);
        glVertex2i(200, 400);
    glEnd();

    glBegin(GL_LINES); //A
        glVertex2i(300, 400);
        glVertex2i(350, 600);
        glVertex2i(400, 400);
        glVertex2i(350, 600);
        glVertex2i(320, 490);
        glVertex2i(380, 490);
    glEnd();

    glBegin(GL_LINES); //F
        glVertex2i(420, 600);
        glVertex2i(420, 400);
        glVertex2i(420, 600);
        glVertex2i(480, 600);
        glVertex2i(420, 490);
        glVertex2i(470, 490);
    glEnd();

    glBegin(GL_LINES); //I
        glVertex2i(500, 600);
        glVertex2i(560, 600);
        glVertex2i(500, 400);
        glVertex2i(560, 400);
        glVertex2i(530, 600);
        glVertex2i(530, 400);
    glEnd();

    glBegin(GL_LINES); //A
        glVertex2i(580, 400);
        glVertex2i(630, 600);
        glVertex2i(680, 400);
        glVertex2i(630, 600);
        glVertex2i(600, 490);
        glVertex2i(660, 490);
    glEnd();

    glBegin(GL_LINES); //H
        glVertex2i(700, 400);
        glVertex2i(700, 600);
        glVertex2i(780, 400);
        glVertex2i(780, 600);
        glVertex2i(700, 490);
        glVertex2i(780, 490);
    glEnd();

    glBegin(GL_LINE_STRIP); // 2
        glVertex2i(300, 300);
        glVertex2i(330, 300);
        glVertex2i(300, 220);
        glVertex2i(330, 220);
    glEnd();

    glBegin(GL_LINE_STRIP); // 0
        glVertex2i(350, 270);
        glVertex2i(370, 270);
        glVertex2i(370, 230);
        glVertex2i(350, 230);
        glVertex2i(350, 270);
    glEnd();

    glBegin(GL_LINES);  //1
        glVertex2i(390, 300);
        glVertex2i(390, 220);
    glEnd();

    glBegin(GL_LINE_STRIP); //6
        glVertex2i(440, 300);
        glVertex2i(410, 300);
        glVertex2i(410, 220);
        glVertex2i(440, 220);
        glVertex2i(440, 270);
        glVertex2i(410, 270);
    glEnd();

    glBegin(GL_LINE_STRIP); // 0
        glVertex2i(460, 270);
        glVertex2i(480, 270);
        glVertex2i(480, 230);
        glVertex2i(460, 230);
        glVertex2i(460, 270);
    glEnd();

    glBegin(GL_LINE_STRIP); //6
        glVertex2i(525, 300);
        glVertex2i(495, 300);
        glVertex2i(495, 220);
        glVertex2i(525, 220);
        glVertex2i(525, 270);
        glVertex2i(495, 270);
    glEnd();

    glBegin(GL_LINE_STRIP); // 7
        glVertex2i(545, 300);
        glVertex2i(575, 300);
        glVertex2i(545, 220);
    glEnd();

    glBegin(GL_LINE_STRIP); //9
        glVertex2i(625, 240);
        glVertex2i(595, 240);
        glVertex2i(595, 300);
        glVertex2i(625, 300);
        glVertex2i(625, 220);
        glVertex2i(595, 220);
    glEnd();

    glBegin(GL_LINE_STRIP); // 0
        glVertex2i(645, 270);
        glVertex2i(665, 270);
        glVertex2i(665, 230);
        glVertex2i(645, 230);
        glVertex2i(645, 270);
    glEnd();
// The end of my text
    
glFlush();
}
void timer(int)
{
    glutPostRedisplay();
    glutTimerFunc(3000 / 60, timer, 0);
    if (xposition < 200 && Yposition<150);
    xposition += 10;
    Yposition += 10;
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(1000, 600);
    glutInitWindowPosition(200, 0);
    glutCreateWindow("PROGRAM TO DEMONSTRATE COLOR AND GL_LINES");
    glutDisplayFunc(myDisplay);
    glutTimerFunc(0, timer, 0);

    myInit();
    glutMainLoop();

    return 0;
}

【问题讨论】:

标签: c++ opengl glut


【解决方案1】:

您必须更改/设置GL_MODELVIEW 矩阵。
使用glRoatateglScaleglTranslate 操作当前模型视图矩阵。
使用glPushMatrix/glPopMatrix 保存和恢复矩阵堆栈上的矩阵。

例如:

float scale = 0.3f;
float angle = 0.0f;

void myDisplay(void)
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear(GL_COLOR_BUFFER_BIT);

    // draw object
    // [...]

    glPushMatrix();
    glTranslatef(500.0f, 400.0f, 0.0f);
    glRotatef(angle, 0.0f, 0.0f, 1.0f);
    angle += 3.0;
    glScalef(scale, scale, 1.0f);
    glTranslatef(-500.0f, -400.0f, 0.0f);

    // draw text
    // [...]
    
    glPopMatrix();

    glFlush();
}   

【讨论】:

    猜你喜欢
    • 2017-11-18
    • 2014-04-15
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多