2013-05-03 111 views
0

在我用C++编写的测试程序中,我决定在鼠标移动到一边时添加代码。原代码工作,但当我添加glutPassiveMotionFunc程序,它给了我一个SIGSEV错误。该程序编译良好的功能,编译和运行没有它。为什么我在尝试使用过量回调时遇到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); 
} 
+1

考虑到这是一个Windows程序,我怀疑你得到了一个实际的“分段错误”。但是,如果您可以在调试器中运行该程序来查看它崩溃的_where_,它将会有所帮助。 – 2013-05-03 16:58:11

回答

2

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

BTW:GLUT不是OpenGL的一部分。这是简单的OpenGL技术演示的第三方应用程序框架。

相关问题