开发者社区> 问答> 正文

最近入手了《OpenGL编程指南第九版》,可跟着书的源码编程序总是出错?报错

最近入手了《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)

这个应该是指针问题,可没道理呀!!!

展开
收起
爱吃鱼的程序员 2020-06-08 10:30:33 525 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    YourlastargumenttoglDrawArraysshouldbe4(numberofelements),not6(numberofint).

    You'retryingtodraw6elements,andaccessingpasttheboundsofyourarray.

    参考:https://stackoverflow.com/questions/10645484/opengl-segfaulting-on-gldrawarrays

    ps:善用搜索引擎 

    没事了,不知怎么的就弄好了,多谢费心回复 @qivip:把代码都贴出来还有另一个头文件的内容改了4还是没用,只有把它设为零才能运行(当然屏幕上什么都没有)。
    2020-06-08 10:30:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Python第五讲——关于爬虫如何做js逆向的思路 立即下载
Storm源码走读笔记 立即下载
数据+算法定义新世界 立即下载