Exemplo n.º 1
0
void VisSurface::build(){
    sharedInfo->release();
    
    glGenBuffers(1, &(sharedInfo->vtx_buf_id));
    glGenBuffers(1, &(sharedInfo->idx_buf_id));
    
    glBindBuffer(GL_ARRAY_BUFFER, sharedInfo->vtx_buf_id);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sharedInfo->idx_buf_id);
    
    //prepare for data generation
    Vec2i dicing   = sharedInfo->dicing;
    Vec2d dicingf  = Vec2d(dicing.x - 1, dicing.y - 1);
    int num_floats = dicing.x * dicing.y * 3;
    int num_idxs   = dicing.x * (dicing.y-1) * 2;
    float *vtxData = new float[num_floats];
    unsigned int *idxData = new unsigned int[num_idxs];
    
    //generate pt data
    for (int y = 0; y < dicing.y; y++){
        for (int x = 0; x < dicing.x; x++){
            Vec2d st = sharedInfo->region.remap(Vec2d(x,y) / dicingf);
            Vec3d pt = surface->eval(st);
            unsigned int vtx_num = (unsigned int)(dicing.x) * y + x;
            vtxData[3*vtx_num]   = pt.x;
            vtxData[3*vtx_num+1] = pt.y;
            vtxData[3*vtx_num+2] = pt.z;
            if (y < dicing.y - 1){
                idxData[vtx_num * 2] = vtx_num;
                idxData[vtx_num * 2 + 1] = vtx_num + dicing.x;
            }
        }
    }
    
    //TODO: normals (interleave; it's better)
    
    //send the data
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * num_floats, vtxData, GL_STATIC_DRAW);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * num_idxs, idxData, GL_STATIC_DRAW);
    
    sharedInfo->valid = true;
    delete [] vtxData;
    delete [] idxData;
}