【发布时间】:2018-03-05 12:06:16
【问题描述】:
我正在尝试绘制两个彼此相对的 2D 钻石。 所以我画了第一颗钻石,然后在使用后画了第二颗钻石:
glTranslated(0, -150, 0);
所以它可以正好出现在我的第一颗钻石下方。 但是,我遇到了一个问题,我无法翻转第二颗钻石,让它看起来像一面镜子。
这是我想要做的:
我在网上搜索了解决方案,他们都提到我应该 使用
glScalef(1.0f,-1.0f,1.0f);
但每次我使用它时,绘图都会消失。
功能
glRotatef(angle,x,y,z);
引起了我的注意,但我无法正确使用它,导致方向错误。
这是没有glRotate()时我的图像现在的样子:
所以我认为我需要适当的技术来使用这些功能。
注意:我使用了很多线循环和顶点来绘制。
#include <windows.h> // For MS Windows
#include <GL/glut.h> // (or others, depending on the system in use)
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); // Set display-window color to
white.
glMatrixMode(GL_PROJECTION); // Set projection parameters.
gluOrtho2D(0.0, 400.0, 0.0, 400.0);
}
void drawDiamond()
{
glBegin(GL_LINE_LOOP);
glVertex2f(125, 350);
glVertex2f(245, 350);
glVertex2f(290, 300);
glVertex2f(182, 200);
glVertex2f(75, 300);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(109, 333);
glVertex2f(138, 350);
glVertex2f(159, 337);
glVertex2f(123, 300);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(109, 333);
glVertex2f(123, 300);
glVertex2f(154, 225);
glVertex2f(92, 300);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(290, 300);
glVertex2f(75, 300);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(123, 300);
glVertex2f(159, 337);
glVertex2f(154, 300);
glVertex2f(171, 225);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(181, 300);
glVertex2f(159, 337);
glVertex2f(181, 350);
glVertex2f(209, 337);
glVertex2f(181, 300);
glVertex2f(171, 225);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(181, 300);
glVertex2f(209, 337);
glVertex2f(219, 300);
glVertex2f(195, 225);
glVertex2f(181, 300);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(209, 337);
glVertex2f(243, 300);
glVertex2f(195, 225);
glVertex2f(219, 300);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(209, 337);
glVertex2f(229, 350);
glVertex2f(260, 333);
glVertex2f(243, 300);
glVertex2f(209, 337);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(260, 333);
glVertex2f(278, 300);
glVertex2f(210, 225);
glVertex2f(243, 300);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(195, 225);
glVertex2f(182, 200);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(171, 225);
glVertex2f(182, 200);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f(0.0, 0.0, 0.0); // Set line segment color to blue.
// your code goes here
drawDiamond();
glTranslatef(0.0f, -150, 0.0f);
drawDiamond();
glFlush(); // Process all OpenGL routines as quickly as possible.
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition(50, 100); // Set top-left display-window
position.
glutInitWindowSize(400, 400); // Set display-window width and
height.
glutCreateWindow("Diamond Project"); // Create display window.
init(); // Execute initialization procedure.
glutDisplayFunc(display); // Send graphics to display window.
glutMainLoop(); // Display everything and wait.
}
【问题讨论】:
-
从外观上看,您使用的是旧版 OpenGL 1.0;如果可以的话,我建议使用现代 OpenGL 3.3+ 版并使用着色器。从那里您可以使用模型视图投影矩阵来执行所有需要的转换。还使用 GLM 数学库使事情变得更容易。
-
在我对SO: OpenGl draw a cylinder with 2 points glRotated 的回答中,我提到了
glRotatef()。顺便提一句。旋转约 180 度。和镜像不一样。如果旋转的项目是对称的,它看起来很相似。 -
您只需要围绕其中一个轴旋转对象并学习如何使用
glRotatef函数。 -
如果你镜像事物(例如使用
glScalef(1, -1, 1)),这会影响三角形的方向。 (您的模型被翻过来了。)因此,可能有必要将背面剔除反转为正面剔除(反之亦然)。
标签: c++ opengl graphics coordinate-transformation