gpt4 book ai didi

c++ - 键入 glBegin() 后出现 OpenGL 段错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:04 24 4
gpt4 key购买 nike

我是 OpenGL 编程的新手,一切都很好,直到我突然收到段错误,现在无法摆脱它。任何人都可以发现段错误并向我解释吗?

注意:虽然这是一项作业,但我不是来这里寻找答案的。事实上,我什至不会提及分配细节,甚至不会发送完整代码(尽管以下代码也会导致段错误):

#  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);
}

最佳答案

仔细检查您的开发环境。您的代码(到目前为止)非常好,您遇到的问题可能是由不匹配的库引起的。例如,如果您在 Linux 上进行开发,libGL.so 必须与 GPU 驱动程序和版本完全匹配(或者使用纯粹的间接 GLX 上下文,这通常只发生在远程 X11 上)。

关于c++ - 键入 glBegin() 后出现 OpenGL 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40417004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com