首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >键入glBegin()后出现OpenGL分段错误

键入glBegin()后出现OpenGL分段错误
EN

Stack Overflow用户
提问于 2016-11-04 14:59:07
回答 1查看 169关注 0票数 2

我是OpenGL编程的新手,一切都很好,直到我突然收到分段错误,现在无法摆脱它。谁能找出分段的缺陷,给我解释一下?

注意:虽然这是一项任务,但我不是来这里寻找答案的。事实上,我甚至不会提及赋值细节,甚至不会发送完整的代码(不过,下面的代码也会导致Segfault值):

代码语言:javascript
复制
#  include <stdio.h>
#  include <stdlib.h>
#  ifdef __APPLE__
#  include <OpenGL/gl.h>
#  include <OpenGL/glu.h>
#  include <GLUT/glut.h>
#  else
#  include <GL/gl.h>
#  include <GL/glu.h>
#  include <GL/freeglut.h>
#  endif
#  include <iostream>
#  include <ctime>
#  include <cmath>
using namespace std;

#define GRID 50 

/* Eye location */
float eye[]={5,5,5};

void initDefaults(void){
    glClearColor(0, 0, 0, 0);
    glColor3f(1, 1, 1);
    glMatrixMode(GL_PROJECTION);
    glFrustum(-10,10,-10,10,0,100);
    gluPerspective(45,1,1,100);
}

/* TEMPORARY */
void createGrid(){  

//After checking, I narrowed down the segmentation fault to here.   
    glColor3f(0, 0, 0);
    printf("help");
    glBegin(GL_POINTS);
    glVertex3f(25,25,25);
    glEnd();
}

/* displays UI to user */
void display(void){

    //Clears the board upon (re)display
    glClearColor(1.0,1.0,1.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    //Enables backface culling
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    //Sets up the perspective matrix (viewing/camera)
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();
    gluLookAt(eye[0], eye[1], eye[2], GRID/2, GRID/2, 0, 0, 1, 0); 

    //I believe the segmentation fault occurs in here.
    //************************************
    createGrid();
    //************************************

    //Used for double-buffering
    glutSwapBuffers();
}

/* main function - program entry point */
int main(int argc, char** argv){

    glutInit(&argc, argv);                                      
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(200,200);
    glutCreateWindow("Terrain");                                
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
    glutDisplayFunc(display);                         
    gluOrtho2D(-1, GRID + 1, -1, GRID + 1);
    initDefaults();
    glutMainLoop();                                             
    return(0);                                                  
}
EN

回答 1

Stack Overflow用户

发布于 2016-11-04 17:08:50

仔细检查您的开发环境。您的代码(到目前为止)是完全正常的,您所经历的情况可能是由不匹配的库造成的。例如,如果您在Linux上进行开发,那么libGL.so必须与GPU驱动程序和版本完全匹配(或者使用一个纯粹的间接GLX上下文,这通常只发生在远程X11上)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40417004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档