如何利用OpenGL畫坐標軸指示圖

利用OpenGL畫坐標軸指示圖

 最開始是想在左下角位置畫個坐標軸

後來在網上找瞭一個,也是別人搬運的,沒有出處。學習瞭一下,感覺不太方便

#include <iostream>  
using namespace std;
 
#include<gl/glut.h>  
 
//這個N是用來計數的,為瞭驗證兩個回調函數display和reshape誰先執行
//結果是reshape先執行
int N = 0;
 
GLfloat transx, transy;
GLfloat scale;
 
int primw = 300;
int primh = 300;
GLfloat rotatex = 0, rotatey = 0;
GLint mousepx, mousepy;
 
void rend(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(8);
    glLineWidth(2);
    
    glPushMatrix();
    glTranslatef(transx, transy, 0);
    //glTranslatef(0, 0, 0);
    glRotatef(rotatex, 1, 0, 0);
    glRotatef(rotatey, 0, 1, 0);
    glBegin(GL_LINES);
    glColor3f(0, 1, 0);
    glVertex3f(0, 0, 0);
    glVertex3f(0, 2, 0);
    glColor3f(1, 0, 0);
    glVertex3f(0, 0, 0);
    glVertex3f(2, 0, 0);
    glColor3f(0, 0, 1);
    glVertex3f(0, 0, 0);
    glVertex3f(0, 0, 2);
    glEnd();
    glPopMatrix();
    glFlush();
    if (N < 3)
        cout << "rend" << endl;
    N++;
}
 
void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        gluOrtho2D(-10, 10, -10.0 / w * h, 10.0 / w * h);
    else
        gluOrtho2D(-10.0 / h * w, 10.0 / h * w, -10, 10);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    if (w <= h)
    {   /*  scale=(GLfloat)primw/w;*/
        transx = (50 - w / 2.0) * 20.0 / w;
        transy = (50 - h / 2.0) * 20.0 / w;
    }
    else
    {
        /*      scale=(GLfloat)primh/h;*/
        transx = (50 - w / 2.0) * 20.0 / h;
        transy = (50 - h / 2.0) * 20.0 / h;
    }
    if (N < 3)
        cout << "reshape" << endl;
    N++;
}
 
void motion(int x, int y)//鼠標按下移動
{
    int w, h;
    w = glutGet(GLUT_WINDOW_WIDTH);
    h = glutGet(GLUT_WINDOW_HEIGHT);
    if (0 <= x && x <= w && 0 <= y && y <= h)
    {
        rotatex = -(mousepy - y) / (GLfloat)h * 360;
        rotatey = -(mousepx - x) / (GLfloat)w * 360;
        /*      cout<<"rotatex:rotatey"<<rotatex<<" "<<rotatey<<endl;*/
        glutPostRedisplay();
    }
}
 
void mousedown(int mouse, int state, int x, int y)
{
    if (state == GLUT_DOWN)
    {
        mousepx = x;
        mousepy = y;
    }
    //  cout<<"mousepx:mousepy"<<endl;  
    //  cout<<mousepx<<"  "<<mousepy<<endl;
}
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(primw, primh);
    glutCreateWindow("coordination");
 
    glClearColor(1, 1, 1, 0);
    glutDisplayFunc(rend);
    glutMotionFunc(motion);
    glutMouseFunc(mousedown);
    glutReshapeFunc(reshape);//最先調用,比display先
    glutMainLoop();
    return 0;
}

是這樣的效果,效果還行,隻是這種方式不太方便嵌到代碼中

 最終還是決定不在左下角畫瞭,直接在模型上畫出來坐標軸,用顏色區分xyz

 頂點著色器如下,就是將三條線的頂點和顏色數組輸入到頂點著色器中,並與模型使用相同的MVP

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
 
uniform mat4 modelview;
uniform mat4 view;
uniform mat4 projection;
out vec3 color;
 
void main()
{
        gl_Position = projection * view * modelview * vec4(aPos, 1.0);
        color = aColor;
}

如何使用OpenGL繪制三維坐標系

第一,圖中圓環所在的指定區域與坐標軸所在的區域是兩個相互獨立的空間,通過使用glViewport函數限定。

glViewport(0,0,500,500);//指定圓環繪制空間,從(0,0)位置開始,長寬分別為500

glViewport(0,300,200,200);//指定坐標軸的繪制空間,從(0,300)位置開始,長寬分別為200

第二,設定投影效果、觀察坐標及旋轉縮放等

//設置投影效果//
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軸向上

第二,考慮到實際應用中我們需要對圓環進行旋轉,那坐標系也應該進行旋轉,這樣才能一一對應上。

glRotatef(_xAngle, 1, 0, 0);
glRotatef(_yAngle, 0, 1, 0);
//傳入的角度根據具體需求具體設定

第三,繪制坐標軸。可以將坐標軸畫成一個上下底面同寬,長度較長的一個圓柱體;而坐標箭頭可以看成頭部很寬,底部寬度為0的圓柱體。

const int AXES_LEN = 300;
const int ARROW_LEN = 100;
const int ARROW_RADIUS = 30;

GLUquadricObj *objCylinder = 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();

上述代碼中需要註意到的是x軸和y軸的是根據z軸旋轉得到的。

第四步,添加“xyz”字符,這是我目前遇到的問題。我嘗試使用如下代碼:

glRasterPos3f(300, 0, 0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'y');

 總結

到此這篇關於如何利用OpenGL畫坐標軸指示圖的文章就介紹到這瞭,更多相關OpenGL畫坐標軸指示圖內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: