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;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用OpenGL坐标系动态曲线的基本步骤: 1. 在Visual Studio 2019中创建一个空项目,并添加OpenGL的头文件和库文件。 2. 创建一个窗口并初始化OpenGL,可以使用glut库来实现。在窗口中创建一个OpenGL视口。 3. 定义一个坐标系,在OpenGL中使用glOrtho函数来设置视口大小和坐标系范围。 4. 在OpenGL中使用glBegin和glEnd函数来绘制曲线。使用glColor函数来设置线条颜色。使用glVertex2f函数来定义坐标点。 5. 使用glFlush函数来刷新绘缓冲区。 6. 在每次绘制之前,清空绘缓冲区,使用glClear函数来实现。 7. 使用glut库的glutTimerFunc函数来定时刷新曲线,实现动态效果。 下面是一个简单的示例代码,使用OpenGL和glut库绘制一个简单的坐标系动态曲线: ``` #include <GL/glut.h> #include <math.h> #define PI 3.14159265358979323846 float x = -10.0f, y = 0.0f; float dx = 0.1f, dy = 0.0f; void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-15.0, 15.0, -10.0, 10.0, -1.0, 1.0); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); // 绘制坐标系 glColor3f(1.0, 1.0, 1.0); glBegin(GL_LINES); glVertex2f(-15.0, 0.0); glVertex2f(15.0, 0.0); glVertex2f(0.0, -10.0); glVertex2f(0.0, 10.0); glEnd(); // 绘制曲线 glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINE_STRIP); for (float x = -10.0f; x <= 10.0f; x += 0.1f) { float y = sin(x * PI / 2.0f); glVertex2f(x, y); } glEnd(); // 刷新缓冲区 glFlush(); } void timer(int value) { // 更新坐标位置 x += dx; y += dy; if (x > 10.0f || x < -10.0f) { dx = -dx; } if (y > 1.0f || y < -1.0f) { dy = -dy; } glutPostRedisplay(); glutTimerFunc(10, timer, 0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(800, 600); glutInitWindowPosition(100, 100); glutCreateWindow("Dynamic Curve Graph"); init(); glutDisplayFunc(display); glutTimerFunc(10, timer, 0); glutMainLoop(); return 0; } ``` 在这个示例代码中,我们使用sin函数绘制一个正弦曲线,并使用定时器来更新曲线的坐标位置,实现动态效果。同时,我们也绘制了一个简单的坐标系,使曲线更加直观。您可以根据需要修改绘制的曲线和坐标系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值