1

我目前正在为程序 Igor Pro 构建一个 C++ 插件。该插件采用三个点数组(X、Y、Z)并返回一个包含数据像素值的矩阵(float array[windowsize][windowsize])。

我已经通过使用 VBO 在 OpenGL (glew) 中开发了一个解决方案,但我觉得这不是最佳解决方案。

1) 你认为我应该使用 FBO 或 PBO 的屏幕外渲染吗?我想让它尽可能快地运行。

2)另外,我只想运行一次 glutMainLoop 。我应该换成 FreeGLUT 吗?

/*  
        RenderTriangle.c
*/
#include "VC10\triangulation.h"
#include "XOPStandardHeaders.h"         // Include ANSI headers, Mac headers, IgorXOP.h, XOP.h and XOPSupport.h
#include "glui.h"

// Prototypes
HOST_IMPORT int main(IORecHandle ioRecHandle);

// Global Variables
const int windowSize = 512;
GLuint ID;
int size,el_size;
float* waveX=NULL;
float* waveY=NULL;
float* waveZ=NULL;
waveHndl waveH;

// Custom error codes
#define OLD_IGOR 1 + FIRST_XOP_ERR
#define NON_EXISTENT_WAVE 2 + FIRST_XOP_ERR
#define REQUIRES_SP_OR_DP_WAVE 3 + FIRST_XOP_ERR

#pragma pack(2)     // All structures passed to Igor are two-byte aligned.
typedef struct FitParams {
    waveHndl waveX; 
    waveHndl waveY;
    waveHndl waveZ;
    double result;
} FitParams, *FitParamsPtr;
#pragma pack()      // Restore default structure alignment

// Prototypes of the OpenGL callbacks
void init(void);
void display(void);
void idle(void);

void init(void){

    float z_val=0;
    int point=0;

    //Loading data from the text files and applying the Delaunay algorithm
    triangulateio tri_data=triangulationWave(waveX,waveY,waveZ);
    vector<float>data_vector(tri_data.numberoftriangles*3*6); //3 points with 6 coordinates (position and color)

    size=data_vector.size();

    double zMin=tri_data.pointattributelist[N_ELEMENTS],zMax=tri_data.pointattributelist[N_ELEMENTS+1];
    for (int i_tri = 0; i_tri < tri_data.numberoftriangles; i_tri++) {

        for (int i_point = 0; i_point < 3; i_point++) {

            z_val=tri_data.pointattributelist[tri_data.trianglelist[i_tri * 3 + i_point]];
            z_val=1-((z_val-zMin)/(zMax-zMin)); //normalize and invertion

            point=6*(i_tri *3 + i_point);

            data_vector[point+0]=tri_data.pointlist[2*tri_data.trianglelist[i_tri * 3 + i_point]];
            data_vector[point+1]=tri_data.pointlist[2*tri_data.trianglelist[i_tri * 3 + i_point]+1];
            data_vector[point+2]=z_val;
            data_vector[point+3]=z_val;
            data_vector[point+4]=z_val;
            data_vector[point+5]=z_val;
        }
    }

    glewInit();

    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glDepthFunc(GL_LEQUAL);
    glDepthRange(-2.0f, 2.0f);
    glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
    glShadeModel(GL_SMOOTH);
    glEnableClientState(GL_VERTEX_ARRAY);
    glGenBuffers(1,&ID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,size*sizeof(float), &data_vector[0], GL_STATIC_DRAW);

    el_size=6*sizeof(float);
}
void display(void){

    int zoom = 1;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2*zoom, 2*zoom,2*zoom,-2*zoom, -5.0f, 5.0f);
    glPushMatrix ();
    glBindBuffer(GL_ARRAY_BUFFER, ID);

    // Pointer for the position of the points
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, el_size, 0);

    // Pointer for the color of the points
    glEnableClientState(GL_COLOR_ARRAY);
    glColorPointer(3,GL_FLOAT, el_size,(void*)(el_size/2));

    //Draw callback
    glDrawArrays(GL_TRIANGLES,0,size/6);

    glPopMatrix ();
    glFlush();
}

// Idle callback function for OpenGL API
void idle(void){

    float* pixels = (float*)malloc(windowSize*windowSize*sizeof(float));
    glReadPixels(0, 0, windowSize, windowSize, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);

    //
    //char waveName[MAX_OBJ_NAME+1]; 
    //CountInt dimensionSizes[MAX_DIMENSIONS+1]; 
    //float* wp;
    //strcpy(waveName, "wave0");
    //MemClear(dimensionSizes, sizeof(dimensionSizes));
    //dimensionSizes[ROWS] = windowSize*windowSize; // Make 1D wave 
    //wp = (float*)WaveData(waveH); // Get a pointer to the wave data 

    //for(int i = 0;i < windowSize*windowSize; i++){    
    //  *wp++ =pixels[i];                 
    //}

    //glDeleteBuffers(1,&ID);
}

// Main function for the XOP
extern "C" int
RenderTriangle(FitParamsPtr p)
{
    waveX=(float*)WaveData(p->waveX);
    waveY=(float*)WaveData(p->waveY);
    waveZ=(float*)WaveData(p->waveZ);

    char *myargv [1];
    int myargc=1;
    myargv [0]=strdup ("RenderTriangle");
    glutInit(&myargc,myargv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_DEPTH | GLUT_RGB);
    glutInitWindowSize(windowSize,windowSize);
    glutCreateWindow("OpenGL");
    init();
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutMainLoop();
    p->result = 1;

    return 0;
}

static XOPIORecResult
RegisterFunction()
{
    int funcIndex;

    funcIndex = (int)GetXOPItem(0);         // Which function invoked ?
    switch (funcIndex) {
        case 0:                                 // y = RenderTriangle(w,x) (curve fitting function).
            return (XOPIORecResult)RenderTriangle;  // This function is called using the direct method.
            break;
    }
    return 0;
}

/*  XOPEntry()

    This is the entry point from the host application to the XOP for all
    messages after the INIT message.
*/
extern "C" void
XOPEntry(void)
{   
    XOPIORecResult result = 0;

    switch (GetXOPMessage()) {
        case FUNCADDRS:
            result = RegisterFunction();    // This tells Igor the address of our function.
            break;
    }
    SetXOPResult(result);
}

/*  main(ioRecHandle)

    This is the initial entry point at which the host application calls XOP.
    The message sent by the host must be INIT.
    main() does any necessary initialization and then sets the XOPEntry field of the
    ioRecHandle to the address to be called for future messages.
*/
HOST_IMPORT int
main(IORecHandle ioRecHandle)
{   
    XOPInit(ioRecHandle);                           // Do standard XOP initialization.
    SetXOPEntry(XOPEntry);                          // Set entry point for future calls.

    if (igorVersion < 600) {
        SetXOPResult(OLD_IGOR);
        return EXIT_FAILURE;
    }

    SetXOPResult(0);
    return EXIT_SUCCESS;
}
4

1 回答 1

0
  • VBO 用于传输顶点数据。

您正在通过 VBO 传输点云数据,没有比这更好的了。

  • PBO 用于传输像素数据。

您可以使用 PBO 从 OpenGL 帧缓冲区异步读取数据。

  • 可以绘制 FBO。

您应该使用 FBO 将您的目标帧缓冲区与任何窗口操作隔离开来。

2)另外,我只想运行一次 glutMainLoop 。我应该换成 FreeGLUT 吗?

您根本不必运行主循环。您只需要创建一个窗口即可;在主循环启动之前它不会出现。您需要窗口来获取 OpenGL 上下文,但您可以放心地省略主循环。

无需注册任何回调(它们不会被调用),只需按顺序执行您的 OpenGL 命令。如果您的库被多次调用,您不必重新创建窗口,但可能需要调用 glutSetWindow 以使上下文成为当前的。

  1. 设置一个 FBO 来绘制
  2. 画到 FBO
  3. glReadPixels
    • 进入 PBO 和 glMapBuffer 和 memcpy。
    • 或直接放入适当大小的缓冲区。
于 2013-01-16T19:18:56.243 回答