# 如何利用OpenGL画坐标轴指示图

## 利用OpenGL画坐标轴指示图

最开始是想在左下角位置画个坐标轴

```#include
using namespace std;

#include

//这个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);
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);
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"<
```

最终还是决定不在左下角画了，直接在模型上画出来坐标轴，用颜色区分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(0,0,500,500);//指定圆环绘制空间，从(0,0)位置开始，长宽分别为500

glViewport(0,300,200,200);//指定坐标轴的绘制空间，从(0,300)位置开始，长宽分别为200
```

```//设置投影效果//
glMatrixMode(GL_PROJECTION);
glOrtho(-500, 500, -500, 500, -500, 500); //指定了一个正方体区域，在这个区域内的图形才能正常显示

//设置模型视图矩阵，开始画图//
glMatrixMode(GL_MODELVIEW);
gluLookAt(0, 2, 0, 0, 0, 0, 0, 0, 1); //从(0,2,0)位置看向原点，z轴向上
```

```glRotatef(_xAngle, 1, 0, 0);
glRotatef(_yAngle, 0, 1, 0);
//传入的角度根据具体需求具体设定
```

```const int AXES_LEN = 300;
const int ARROW_LEN = 100;

//确定坐标系原点
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();
```

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