D50.1.0 如何使用OpenGL绘制三维坐标系

论坛 期权论坛     
匿名技术用户   2021-1-15 13:41   0   0
<div class="markdown-here-wrapper" style="font-size:17px;letter-spacing:.1em;line-height:1.8em;">
<p style="margin-left:5px;">如何在屏幕左下角绘制直角坐标系?</p>
<ul style="list-style-type:circle;margin-left:5px;"><li>第一步,指定屏幕绘制区域</li><li>第二步,设定投影效果、观察坐标及旋转缩放等</li><li>第三步,绘制坐标轴,绘制箭头</li><li>第四步,添加“xyz”字符</li></ul>
<p style="margin-left:5px;"><img alt="" class="blockcode" height="306" src="https://201907.oss-cn-shanghai.aliyuncs.com/cs/5606289-75994562373bf2d5fd90a664c045ad06.jpg" width="536"></p>
<p style="margin-left:5px;"><img alt="wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw&#61;&#61;" height="15" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw&#61;&#61;" title="点击并拖拽以移动" width="15"></p>
<p style="margin-left:5px;"><img alt="" class="blockcode" height="307" src="https://201907.oss-cn-shanghai.aliyuncs.com/cs/5606289-824dbc2ff8504dfe3b5fbeec8cd88558.jpg" width="537"></p>
<p style="margin-left:5px;"><img alt="wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw&#61;&#61;" height="15" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw&#61;&#61;" title="点击并拖拽以移动" width="15"></p>
<p style="margin-left:5px;">第一,图中圆环所在的指定区域与坐标轴所在的区域是两个相互独立的空间,通过使用glViewport函数限定。</p>
<pre style="font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;font-size:15px;line-height:1.4em;margin-left:5px;"><code style="font-size:14px;font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;">glViewport(0,0,500,500);//指定圆环绘制空间,从(0,0)位置开始,长宽分别为500

glViewport(0,300,200,200);//指定坐标轴的绘制空间,从(0,300)位置开始,长宽分别为200
</code></pre>
<p style="margin-left:5px;">第二,设定投影效果、观察坐标及旋转缩放等</p>
<pre style="font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;font-size:15px;line-height:1.4em;margin-left:5px;"><code style="font-size:14px;font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;">//设置投影效果//
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-500, 500, -500, 500, -500, 500);  //指定了一个正方体区域,在这个区域内的图形才能正常显示

//设置模型视图矩阵,开始画图//
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 2, 0, 0, 0, 0, 0, 0, 1);      //从(0,2,0)位置看向原点,z轴向上
</code></pre>
<p style="margin-left:5px;">第二,考虑到实际应用中我们需要对圆环进行旋转,那坐标系也应该进行旋转,这样才能一一对应上。</p>
<pre style="font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;font-size:15px;line-height:1.4em;margin-left:5px;"><code style="font-size:14px;font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;">glRotatef(_xAngle, 1, 0, 0);
glRotatef(_yAngle, 0, 1, 0);
//传入的角度根据具体需求具体设定
</code></pre>
<p style="margin-left:5px;">第三,绘制坐标轴。可以将坐标轴画成一个上下底面同宽,长度较长的一个圆柱体;而坐标箭头可以看成头部很宽,底部宽度为0的圆柱体。</p>
<pre style="font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;font-size:15px;line-height:1.4em;margin-left:5px;"><code style="font-size:14px;font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;">const int AXES_LEN &#61; 300;
const int ARROW_LEN &#61; 100;
const int ARROW_RADIUS &#61; 30;

GLUquadricObj *objCylinder &#61; gluNewQuadric();
//确定坐标系原点
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glutSolidSphere(15, 20, 20);
glPopMatrix();

glPushMatrix();
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidSphere(0.25, 6, 6);
gluCylinder(objCylinder, 10, 10, AXES_LEN, 10, 5);            //z
glTranslatef(0, 0, AXES_LEN);
gluCylinder(objCylinder, ARROW_RADIUS, 0, ARROW_LEN, 10, 5);  //z arrow
glPopMatrix();

glPushMatrix();
glColor3f(0.0f, 1.0f, 0.0f);
glRotatef(90, 1.0, 0.0, 0.0);
gluCylinder(objCylinder, 10, 10, AXES_LEN, 10, 5);             //Y
glTranslatef(0, 0, AXES_LEN);
gluCylinder(objCylinder, ARROW_RADIUS, 0, ARROW_LEN, 10, 5);   //Y arrow
glPopMatrix();

glPushMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
glRotatef(90, 0.0, 1.0, 0.0);
gluCylinder(objCylinder, 10, 10, AXES_LEN, 10, 5);              //X
glTranslatef(0, 0, AXES_LEN);
gluCylinder(objCylinder, ARROW_RADIUS, 0, ARROW_LEN, 10, 5);    //X arrow
glPopMatrix();
</code></pre>
<p style="margin-left:5px;">上述代码中需要注意到的是x轴和y轴的是根据z轴旋转得到的。</p>
<p style="margin-left:5px;">第四步,添加“xyz”字符,这是我目前遇到的问题。我尝试使用如下代码:</p>
<pre style="font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;font-size:15px;line-height:1.4em;margin-left:5px;"><code style="font-size:14px;font-family:Roboto, &#39;Courier New&#39;, Consolas, Inconsolata, Courier, monospace;">glRasterPos3f(300, 0, 0);
glutBitmap
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP