为什么我得到一个SIGSEV错误,而试图使用一个过剩回调
Why do I get a SIGSEV error while trying to use a glut callback?
在我用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技术演示的第三方应用程序框架。
相关文章:
- 重写另一个方法 [C++] 使用的超类回调函数
- 使用一个回调函数从多个订阅者分配信息
- C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题
- 一个类的方法作为回调
- 通过回调功能,打破一个循环的最佳方法是什么?
- Delphi 如何在另一个函数中使用回调函数作为参数
- 在用户模式下从另一个进程回调,没有额外的线程
- 给出一个方法作为回调函数(gcc 4.7.1)
- 一个回调函数应该如何更新/返回值
- 将方法作为回调从一个类传递到另一个类
- 如何将方法作为回调传递给另一个类
- 设置一个回调函数,该函数是类的非静态成员函数
- C++,pthread和静态回调。 "this" 返回一个指针,指向派生的基类(第 2 部分)
- c++定时器实现-将一个成员函数分配给信号回调函数指针
- 如何使一个回调全局,所以我可以使用它的其他功能
- Std::ostream,为每行调用一个回调
- 我如何做一个回调从FireBreath到JavaScript
- DirectShow从内存中读取一个回调方法
- 创建一个回调结构传递给LuaJIT FFI
- 如何为IAsyncOperation指定一个回调方法