[英]How to translate and rotate a text drawing in OpenGl by keyboard
我有一个使用 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;
}
您必须更改/设置GL_MODELVIEW
矩阵。
使用glRoatate
、 glScale
和glTranslate
操作当前 model 视图矩阵。
使用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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.