为什么我得到一个SIGSEV错误,而试图使用一个过剩回调

Why do I get a SIGSEV error while trying to use a glut callback?

本文关键字:一个 回调 SIGSEV 为什么 错误      更新时间:2023-10-16

在我用c++编写的测试程序中,我决定添加当鼠标移动到一边时转向的代码。原始代码可以工作,但是当我将glutpassivemotionfuncc添加到程序中时,它给了我一个SIGSEV错误。有这个函数的程序可以很好地编译,没有这个函数的程序可以编译并运行。

/**************************
 * Includes
 *
 **************************/
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include<gl/glut.h>
#include <math.h>

/**************************
 * Function Declarations
 *
 **************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
void mousePos(int mx, int my);
void room1();
float x = 0.0f;
float y = 0.0f;
int omx = 0;
int omy = 0;
float theta = 0.0f;
boolean gof = false;
boolean gob = false;
boolean gol = false;
boolean gor = false;
/**************************
 * WinMain
 *
 **************************/
int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);
    /* create main window */
    hWnd = CreateWindow (
      "GLSample", "WalkRoom", 
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      100, 100, 512, 512,
      NULL, NULL, hInstance, NULL);
    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);
    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */
            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            //glClearDepth(0.0);
            glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            //glFogi (GL_FOG_MODE, GL_LINEAR);
            glPushMatrix ();
            glRotatef (theta, 0.0f, 1.0f, 0.0f);
            glTranslatef (y, 0.0f, x);
            glBegin (GL_QUADS);
            room1();
            glEnd ();
            glPopMatrix ();
            SwapBuffers (hDC);
            glutPassiveMotionFunc(mousePos);
            if(gol){
                theta -= 0.5f;
            }
            if(gor){
                theta += 0.5f;
            }
            if(gof){
                x += cos((acos(-1.0) * theta) / 180) / 8;
                y -= sin((acos(-1.0) * theta) / 180) / 8;
            }
            if(gob){
                x -= cos((acos(-1.0) * theta) / 180) / 8;
                y += sin((acos(-1.0) * theta) / 180) / 8;
            }
            Sleep (1);
        }
    }
    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);
    /* destroy the window explicitly */
    DestroyWindow (hWnd);
    return msg.wParam;
}
void mousePos(int mx, int my)
{
//  theta += mx - omx;
//  omx = mx;
}
void room1()
{
    glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f (-1.0f, 1.0f, -5.0f);
    glColor3f (1.0f, 1.0f, 1.0f);   glVertex3f (1.0f, 1.0f, -5.0f);
    glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (1.0f, -1.0f, -5.0f);
    glColor3f (0.0f, 0.0f, 0.0f);   glVertex3f (-1.0f, -1.0f, -5.0f);
    glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f (-1.0f, 1.0f, -5.0f);
    glColor3f (1.0f, 1.0f, 1.0f);   glVertex3f (-1.0f, 1.0f, -4.0f);
    glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (-1.0f, -1.0f, -4.0f);
    glColor3f (0.0f, 0.0f, 0.0f);   glVertex3f (-1.0f, -1.0f, -5.0f);
    glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f (1.0f, 1.0f, -4.0f);
    glColor3f (1.0f, 1.0f, 1.0f);   glVertex3f (1.0f, 1.0f, -5.0f);
    glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (1.0f, -1.0f, -5.0f);
    glColor3f (0.0f, 0.0f, 0.0f);   glVertex3f (1.0f, -1.0f, -4.0f);
    glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f (-1.0f, -1.0f, -4.0f);
    glColor3f (1.0f, 1.0f, 1.0f);   glVertex3f (1.0f, -1.0f, -4.0f);
    glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (1.0f, -1.0f, -5.0f);
    glColor3f (0.0f, 0.0f, 0.0f);   glVertex3f (-1.0f, -1.0f, -5.0f);
    glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f (-1.0f, 1.0f, -5.0f);
    glColor3f (1.0f, 1.0f, 1.0f);   glVertex3f (1.0f, 1.0f, -5.0f);
    glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (1.0f, 1.0f, -4.0f);
    glColor3f (0.0f, 0.0f, 0.0f);   glVertex3f (-1.0f, 1.0f, -4.0f);
}

/********************
 * Window Procedure
 *
 ********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;
    case WM_DESTROY:
        return 0;
    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        case VK_LEFT:
            gol = true;
            return 0;
        case VK_RIGHT:
            gor = true;
            return 0;
        case VK_UP:
            gof = true;
            return 0;
        case VK_DOWN:
            gob = true;
            return 0;
        }
        return 0;
    case WM_KEYUP:
        switch (wParam)
        {
        case VK_LEFT:
            gol = false;
            return 0;
        case VK_RIGHT:
            gor = false;
            return 0;
        case VK_UP:
            gof = false;
            return 0;
        case VK_DOWN:
            gob = false;
            return 0;
        }
    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}

/*******************
 * Enable OpenGL
 *
 *******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;
    /* get the device context (DC) */
    *hDC = GetDC (hWnd);
    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);
    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );
    gluPerspective(45.0f,(GLfloat)512/(GLfloat)512, 0.5f, 3000.0f);
    glEnable(GL_DEPTH_TEST);
}

/******************
 * Disable OpenGL
 *
 ******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}

为什么你调用glutPassiveMotionFunc,当你的程序使用本地Win32 API?如果你想使用GLUT事件回调,你必须让GLUT处理窗口创建和事件循环。

顺便说一句:GLUT不是OpenGL的一部分。这是一个用于简单OpenGL技术演示的第三方应用程序框架。