最近入手了《OpenGL编程指南第九版》,可跟着书的源码编程序总是出错? 400 报错
最近入手了《OpenGL编程指南第九版》,可跟着书的源码编程序总是出错。我是在CLion上构建项目的,下面是源码,为了配合CLion做了一些修改:
#include "gl3.h" #include "gl3w.h" #include <GLFW/glfw3.h> #include "LoadShaders.h" #include <iostream> #define BUFFER_OFFSET(a) ((void*)(a)) enum VAO_IDs { // vertex-array object IDs Triangles, NumVAOs // Triangles = 0, NumVAOs = 1 }; enum Buffer_IDs { ArrayBuffer, NumBuffers }; enum Attrib_IDs { vPosition = 0 }; GLuint VAOs[NumVAOs]; GLuint Buffers[NumBuffers]; const GLuint NumVertices = 6; //----------------------------------------------------------------------------- // // init // void init(void) { static const GLfloat vertices[NumVertices][2] = { {-0.90f, -0.90f}, {0.85f, -0.90f}, {-0.90f, 0.85f}, // Triangle 1 {0.90f, -0.85f}, {0.90f, 0.90f}, {-0.85f, 0.90f} // Triangle 2 }; glGenVertexArrays(NumVAOs, VAOs); // get vertex-array objects ids glBindVertexArray(VAOs[Triangles]); glGenBuffers(NumBuffers, Buffers);// get buffers ids // store the data to the buffer. glBufferStorage(Buffers[ArrayBuffer], sizeof(vertices), vertices, 0); ShaderInfo shaders[] = { {GL_VERTEX_SHADER, "triangles.vert"}, {GL_FRAGMENT_SHADER, "triangles.frag"}, {GL_NONE, NULL} }; GLuint program = LoadShaders(shaders); glUseProgram(program); glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]); glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glEnableVertexAttribArray(vPosition); } //---------------------------------------------------------------------------- // // display // void display(void) { static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f}; glClearBufferfv(GL_COLOR, 0, black); glBindVertexArray(VAOs[Triangles]); glDrawArrays(GL_TRIANGLES, 0, NumVertices);//////这一步出错!!!!!!!!!!! } //---------------------------------------------------------------------------- // // main // int main(int argc, char **argv) { glfwInit(); GLFWwindow *window = glfwCreateWindow(800, 600, "Triangles", NULL, NULL); glfwMakeContextCurrent(window); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); }
在 glDrawArrays(GL_TRIANGLES, 0, NumVertices) 这一步总是报错
Signal: SIGSEGV (Segmentation fault)
这个应该是指针问题,可没道理呀!!!
Your last argument to glDrawArrays should be 4 (number of elements), not 6 (number of int).
You're trying to draw 6 elements , and accessing past the bounds of your array.
参考:https://stackoverflow.com/questions/10645484/opengl-segfaulting-on-gldrawarrays
ps:善用搜索引擎
######没事了,不知怎么的就弄好了,多谢费心######回复 @qivip : 把代码都贴出来 还有另一个头文件的内容######改了4还是没用,只有把它设为零才能运行(当然屏幕上什么都没有)。版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。