glMatrixMode(GL_MODELVIEW)使用给出分段错误

时间:2016-06-10 21:54:24

标签: c++ opengl glut

我正在尝试为fps项目实现一个摄像头,在调用glMatrixMode(GL_MODELVIEW)时在main.cpp上使用我的Camera类时遇到问题。

以下是相关功能:

void Camera::init() {

    m_yaw = 0.0;
    m_pitch = 0.0;
    set_pos(0,0,0);
}

void Camera::set_pos(float x, float y, float z) {
    m_x = x;
    m_y = y;
    m_z = z;

    refresh();
}

void Camera::refresh() {
    m_lx = cos(m_yaw) * cos(m_pitch);
    m_ly = sin(m_pitch);
    m_lz = sin(m_yaw) * cos(m_pitch);

    m_strafe_lx = cos(m_yaw - M_PI_2);
    m_strafe_lz = sin(m_yaw - M_PI_2);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    printf("the segmentation fault is above!\n");
    gluLookAt(m_x, m_y, m_z, m_x + m_lx, m_y + m_ly, m_z + m_lz, 0.0, 1.0, 0.0);

    //printf("Camera: %f %f %f Direction vector: %f %f %f\n", m_x, m_y, m_z, m_lx, m_ly, m_lz);
}

1 个答案:

答案 0 :(得分:1)

如果你包含glew.h,你需要事先初始化,否则GL API调用被替换为空函数指针并导致段错误。