如何使用OpenGL在点的方向(不是GL_POINTS
,而是坐标)移动2D对象?
为了更好地理解我的代码:
我已经将大部分代码拆分为不同的源代码,但实际上创建形状和设置场景的是以下代码:
void setupScene(int clearColor[]) {
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
//glClearColor(250, 250, 250, 1.0); // Set the cleared screen colour to black.
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window.
// Set up the orthographic projection so that coordinates (0, 0) are in the top left.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10);
// Back to the modelview so we can draw stuff.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer.
}
void drawScene() {
setupScene((int[]){250, 250, 250, 1});
triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT);
glBegin(GL_QUADS);
glColor3f(RGB(80), RGB(80), RGB(80));
glPushMatrix();
glTranslatef(400, 400, 0);
glVertex2d(200, 100);
glVertex2d(100, 100);
glVertex2d(100, 200);
glVertex2d(200, 200);
glPopMatrix();
glEnd();
glutSwapBuffers(); // Send the scene to the screen.
}
void update(int value) {
glutPostRedisplay(); // Tell GLUT that the display has changed.
glutTimerFunc(25, update, 0); // Tell GLUT to call update again in 25 milliseconds.
}
2条答案
按热度按时间daupos2t1#
你需要转换modelview矩阵。假设你已经在modelview模式下了:
[编辑]
@paddy:像这样的东西?我试过了,但是方块不动。pastebin.com/2PCsy5kC
试着显式地选择模型视图矩阵,你的例子并没有告诉我们它当前处于哪种模式:
通常在渲染开始时重置所有内容...因此,您进入GL_PROJECTION模式,调用
glLoadIdentity()
重置该模式并设置摄影机,然后对GL_MODELVIEW矩阵也执行此操作。ryhaxcpt2#
Thanks @paddy,我试图理解
glTranslatef
的用法,并提供了解决方案。以下是工作代码,它将创建一个100x100的正方形,并将其移动到400x200: