一条摸鱼龙 2019-07-24 09:29 采纳率: 0%
浏览 360

OpenGL中glutMouseFunc函数问题

我想在MouseFunc中给一个结构体中成员赋值,但是一直报错Segmentation fault (core dumped),用gdb查了一下是这个赋值语句的问题,以下是代码

#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#include<malloc.h>
#include<stdlib.h>

struct PointD
{
        int x;
        int y;
        PointD* next;
};

struct PointU
{
        int x;
        int y;
        PointU* next;
};

struct PointD* D=NULL;
struct PointU* U=NULL;

void MouseClick(int btn,int state ,int x,int y)
{
        x=ReCoordinate(x);
        y=ReCoordinate(y);
        x=x-half;
        y=half-y;
        if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
         {
                 D->x=x;  //报错语句
                 D->y=y;//报错语句
                 PointD* p=NULL;
                 p=(PointD*)malloc(sizeof(PointD));
                 D->next=p;//报错语句
                 D=D->next;
                 pressed=1;
         }
       else if(btn==GLUT_LEFT_BUTTON && state==GLUT_UP && pressed==1)
        {
                U->x=x;
                U->y=y;
                PointU* q=NULL;
                q=(PointU*)malloc(sizeof(PointU));
                U->next=q;
                U=U->next;
                pressed=0;
                glutPostRedisplay;
        }
}
int main(int argc,char** argv)
{

        D=(struct PointD*)malloc(sizeof(PointD));
        U=(struct PointU*)malloc(sizeof(PointU));
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE|GLUT_DEPTH);
        glutInitWindowPosition(100,100);
        glutInitWindowSize(800,800);
        int half=400;
        glutCreateWindow("TEST");
        glMatrixMode(GL_PROJECTION);
        glOrtho(-half,half,-half,half,-half,half);
        glutDisplayFunc(&display);
        glutMouseFunc(MouseClick);
        glutMainLoop();
        return 0;
}

  • 写回答

1条回答

  • zqbnqsdsmd 2019-07-24 10:29
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程