在我用 C++ 编写的测试程序中,我决定添加代码以在鼠标移到一边时转动。原始代码有效,但是当我将 glutPassiveMotionFunc 添加到程序中时,它给了我一个 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);
}