您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么利用OpenGL畫(huà)坐標(biāo)軸指示圖,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
最開(kāi)始是想在左下角位置畫(huà)個(gè)坐標(biāo)軸
后來(lái)在網(wǎng)上找了一個(gè),也是別人搬運(yùn)的,沒(méi)有出處。學(xué)習(xí)了一下,感覺(jué)不太方便
#include <iostream> using namespace std; #include<gl/glut.h> //這個(gè)N是用來(lái)計(jì)數(shù)的,為了驗(yàn)證兩個(gè)回調(diào)函數(shù)display和reshape誰(shuí)先執(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)按下移動(dòng) { 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; }
是這樣的效果,效果還行,只是這種方式不太方便嵌到代碼中
最終還是決定不在左下角畫(huà)了,直接在模型上畫(huà)出來(lái)坐標(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; }
第一,圖中圓環(huán)所在的指定區(qū)域與坐標(biāo)軸所在的區(qū)域是兩個(gè)相互獨(dú)立的空間,通過(guò)使用glViewport函數(shù)限定。
glViewport(0,0,500,500);//指定圓環(huán)繪制空間,從(0,0)位置開(kāi)始,長(zhǎng)寬分別為500 glViewport(0,300,200,200);//指定坐標(biāo)軸的繪制空間,從(0,300)位置開(kāi)始,長(zhǎng)寬分別為200
第二,設(shè)定投影效果、觀察坐標(biāo)及旋轉(zhuǎn)縮放等
//設(shè)置投影效果// glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-500, 500, -500, 500, -500, 500); //指定了一個(gè)正方體區(qū)域,在這個(gè)區(qū)域內(nèi)的圖形才能正常顯示 //設(shè)置模型視圖矩陣,開(kāi)始畫(huà)圖// glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 2, 0, 0, 0, 0, 0, 0, 1); //從(0,2,0)位置看向原點(diǎn),z軸向上
第二,考慮到實(shí)際應(yīng)用中我們需要對(duì)圓環(huán)進(jìn)行旋轉(zhuǎn),那坐標(biāo)系也應(yīng)該進(jìn)行旋轉(zhuǎn),這樣才能一一對(duì)應(yīng)上。
glRotatef(_xAngle, 1, 0, 0); glRotatef(_yAngle, 0, 1, 0); //傳入的角度根據(jù)具體需求具體設(shè)定
第三,繪制坐標(biāo)軸??梢詫⒆鴺?biāo)軸畫(huà)成一個(gè)上下底面同寬,長(zhǎng)度較長(zhǎng)的一個(gè)圓柱體;而坐標(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”字符,這是我目前遇到的問(wèn)題。我嘗試使用如下代碼:
glRasterPos3f(300, 0, 0); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'y');
以上就是怎么利用OpenGL畫(huà)坐標(biāo)軸指示圖,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。