-2

我正在尝试实现一个 VBO,其中我使用 glDrawArrays 来绘制三角形列表和 glReadPixels 来读取输出。glReadPixels 函数引发分段错误。

这是初始化和使用 VBO 的代码:

void hemicube::draw_escena(vec3f center, vec3f lookat, vec3f up, bool transp) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(center.x, center.y, center.z, lookat.x, lookat.y, lookat.z, up.x, up.y, up.z);
if(primeraVez){
glInfo glInfo;
glInfo.getInfo();

    primeraVez=false;
    GLfloat* vertices = new GLfloat[e.surfaces.size()*9]; // create vertex array
    GLfloat* normals = new GLfloat[e.surfaces.size()*9]; // create vertex array

    unsigned char* colors = new unsigned char[e.surfaces.size()*4*3]; // create vertex array

    for(unsigned int i=0; i<e.surfaces.size(); ++i) {
        if (!(transp==1 & e.materials[e.surfaces[i].material].transparency==1)){
            unsigned int code = ~i;
            unsigned char* temp= (unsigned char*)&code;
            colors[i*4]  = temp[0];
            colors[i*4+1]= temp[1];
            colors[i*4+2]= temp[2];
            colors[i*4+3]= temp[3];

            colors[i*4+4]  = temp[0];
            colors[i*4+5]= temp[1];
            colors[i*4+6]= temp[2];
            colors[i*4+7]= temp[3];

            colors[i*4+8]  = temp[0];
            colors[i*4+9]= temp[1];
            colors[i*4+10]= temp[2];
            colors[i*4+11]= temp[3];
            for(unsigned int j=0; j<e.surfaces[i].vertices.size(); ++j){
                memcpy(vertices+(i*9+j*3),&e.vertices[e.surfaces[i].vertices[0]].x,3*sizeof(GLfloat));
            }
        }
    }

    glGenBuffersARB(1, &vboId);
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertices)+sizeof(colors), 0, GL_STATIC_DRAW_ARB);
    glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(vertices), vertices);                             
    glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertices), sizeof(normals), normals);                // copy normals after vertices
    glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertices)+sizeof(normals), sizeof(colors), colors);  // copy colours after normals

    free(vertices);
    free(colors);
}

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);
// enable vertex arrays
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glNormalPointer(GL_FLOAT, 0, (void*)(e.surfaces.size()*9*sizeof(GLfloat)));
glColorPointer(4, GL_UNSIGNED_BYTE, 0, (void*)(e.surfaces.size()*9*sizeof(GLfloat)*2));
glVertexPointer(3, GL_FLOAT, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, e.surfaces.size()*3);

glDisableClientState(GL_VERTEX_ARRAY);  // disable vertex arrays
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);


glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

}

这是调用该函数并尝试读取结果的代码:

void hemicube::calc_form_factors(vec3f center, vec3f normal, vec3f up, vector<double>& form_factors) {
vec3f right = cross_prod(normal, up);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, 1, 0.001, 2000);
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);


// front
glViewport(resolution/2, resolution/2, resolution, resolution);
draw_escena(center-0.0001*normal, center+normal-0.0001*normal, up, 1);

// up
glViewport(resolution/2, resolution*3/2, resolution, resolution);
draw_escena(center-0.0001*normal, up+center-0.0001*normal, -normal, 1);

// right
glViewport(resolution*3/2, resolution/2, resolution, resolution);
draw_escena(center-0.0001*normal, center+right-0.0001*normal, up, 1);

// left
glViewport(-resolution/2, resolution/2, resolution, resolution);
draw_escena(center-0.0001*normal, center-right-0.0001*normal, up, 1);

// down
glViewport(resolution/2,-resolution/2,resolution, resolution);
draw_escena(center-0.0001*normal, center-up-0.0001*normal, normal, 1);

glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);

glReadPixels(0, 0, resolution*2, resolution*2, GL_RGBA, GL_UNSIGNED_BYTE, &pixel_buffer[0]);
}

关于为什么它不起作用的任何想法?

编辑 像素缓冲区声明为:

std::vector<unsigned int> pixel_buffer;

并以大小 4*resolution*resolution 初始化

4

1 回答 1

0

发现错误:

我错过了一个

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);

之后

glGenBuffersARB(1, &vboId);

此外,我以错误的方式处理大小,因为它们不是静态数组而是指针。

谢谢大家,何塞。

于 2015-10-28T15:20:47.290 回答