如何利用OpenGL畫坐標(biāo)軸指示圖
利用OpenGL畫坐標(biāo)軸指示圖
最開始是想在左下角位置畫個坐標(biāo)軸

后來在網(wǎng)上找了一個,也是別人搬運(yùn)的,沒有出處。學(xué)習(xí)了一下,感覺不太方便
#include <iostream>
using namespace std;
#include<gl/glut.h>
//這個N是用來計數(shù)的,為了驗證兩個回調(diào)函數(shù)display和reshape誰先執(zhí)行
//結(jié)果是reshape先執(zhí)行
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)//鼠標(biāo)按下移動
{
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);//最先調(diào)用,比display先
glutMainLoop();
return 0;
}是這樣的效果,效果還行,只是這種方式不太方便嵌到代碼中

最終還是決定不在左下角畫了,直接在模型上畫出來坐標(biāo)軸,用顏色區(qū)分xyz

頂點(diǎn)著色器如下,就是將三條線的頂點(diǎn)和顏色數(shù)組輸入到頂點(diǎn)著色器中,并與模型使用相同的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繪制三維坐標(biāo)系
第一,圖中圓環(huán)所在的指定區(qū)域與坐標(biāo)軸所在的區(qū)域是兩個相互獨(dú)立的空間,通過使用glViewport函數(shù)限定。
glViewport(0,0,500,500);//指定圓環(huán)繪制空間,從(0,0)位置開始,長寬分別為500 glViewport(0,300,200,200);//指定坐標(biāo)軸的繪制空間,從(0,300)位置開始,長寬分別為200
第二,設(shè)定投影效果、觀察坐標(biāo)及旋轉(zhuǎn)縮放等
//設(shè)置投影效果// glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-500, 500, -500, 500, -500, 500); //指定了一個正方體區(qū)域,在這個區(qū)域內(nèi)的圖形才能正常顯示 //設(shè)置模型視圖矩陣,開始畫圖// glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 2, 0, 0, 0, 0, 0, 0, 1); //從(0,2,0)位置看向原點(diǎn),z軸向上
第二,考慮到實際應(yīng)用中我們需要對圓環(huán)進(jìn)行旋轉(zhuǎn),那坐標(biāo)系也應(yīng)該進(jìn)行旋轉(zhuǎn),這樣才能一一對應(yīng)上。
glRotatef(_xAngle, 1, 0, 0); glRotatef(_yAngle, 0, 1, 0); //傳入的角度根據(jù)具體需求具體設(shè)定
第三,繪制坐標(biāo)軸??梢詫⒆鴺?biāo)軸畫成一個上下底面同寬,長度較長的一個圓柱體;而坐標(biāo)箭頭可以看成頭部很寬,底部寬度為0的圓柱體。
const int AXES_LEN = 300; const int ARROW_LEN = 100; const int ARROW_RADIUS = 30; GLUquadricObj *objCylinder = gluNewQuadric(); //確定坐標(biāo)系原點(diǎn) 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軸的是根據(jù)z軸旋轉(zhuǎn)得到的。
第四步,添加“xyz”字符,這是我目前遇到的問題。我嘗試使用如下代碼:
glRasterPos3f(300, 0, 0); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'y');
總結(jié)
到此這篇關(guān)于如何利用OpenGL畫坐標(biāo)軸指示圖的文章就介紹到這了,更多相關(guān)OpenGL畫坐標(biāo)軸指示圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言編程簡單卻重要的數(shù)據(jù)結(jié)構(gòu)順序表全面講解
這篇文章主要為大家介紹了C語言編程中非常簡單卻又非常重要的數(shù)據(jù)結(jié)構(gòu)順序表的全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
C語言實現(xiàn)進(jìn)制轉(zhuǎn)換函數(shù)的實例詳解
這篇文章主要介紹了C語言實現(xiàn)進(jìn)制轉(zhuǎn)換函數(shù)的實例詳解的相關(guān)資料,這里提供實現(xiàn)實例幫助大家實現(xiàn)改功能,需要的朋友可以參考下2017-08-08
C++中關(guān)于constexpr函數(shù)使用及說明
這篇文章主要介紹了C++中關(guān)于constexpr函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
UE4 Unlua 調(diào)用異步藍(lán)圖節(jié)點(diǎn)AIMoveTo函數(shù)示例詳解
這篇文章主要為大家介紹了UE4 Unlua 調(diào)用AIMoveTo函數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

