如何使用 GLUT 在按键上的 textures/framebuffers 之间切换?

How to switch between textures/framebuffers on keypress using GLUT?

我有两个缓冲区,其中加载了 .rgb 个图像。原始图像存储在 frame_buffer 中,第二张图像存储在 temp_buffer 中。我想在 GLUT window 中的那两个图像之间切换,当用户按下按钮时,我如何扩展我的代码来执行此操作?

typedef struct pixel{
    GLbyte r;
    GLbyte g;
    GLbyte b;
}Pixel;

Pixel frame_buffer[WIDTH][HEIGHT]; //original image
Pixel temp_buffer[WIDTH][HEIGHT]; //embossed image

GLuint texture;

int window_id;


void keyboardFunction(unsigned char button, int x, int y){
    switch(button){
        case 'S':
        case 's':
            // WHEN USER PRESS 'S', NEW IMAGE FROM temp_buffer IS
            // LOADED, HOW CAN I DO THIS?
            break;
        default:
            break;
    }
    glutPostRedisplay();
}


// Initialize OpenGL state
void init() {
    // Texture setup
    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    // Other
    glClearColor(0, 0, 0, 0);
    gluOrtho2D(-1, 1, -1, 1);
    glLoadIdentity();
    glColor3f(1, 1, 1);

    loadInputFromUser();
    emboss();
}


void display() {
    // Copy frame_buffer to texture memory
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, frame_buffer);
    // Clear screen buffer
    glClear(GL_COLOR_BUFFER_BIT);
    // Render a quad
    glBegin(GL_QUADS);
        glTexCoord2f(1, 0); glVertex2f(1, 1);
        glTexCoord2f(1, 1); glVertex2f(1, -1);
        glTexCoord2f(0, 1); glVertex2f(-1, -1);
        glTexCoord2f(0, 0); glVertex2f(-1, 1);
    glEnd();
    // Display result
    glFlush();
    //glutPostRedisplay();
    glutSwapBuffers();
}


// Main entry function
int main(int argc, char **argv) {

    // Init GLUT
    glutInit(&argc, argv);
    glutInitWindowPosition(-1, -1);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("Convolution Filter");
    // Set up OpenGL state
    init();
    // Run the control loop
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboardFunction);
    glutReshapeFunc(changeViewPoint);

    GLenum err = glewInit();
    if (GLEW_OK != err){
        fprintf(stderr, "GLEW error");
        return 1;
    }
    glutMainLoop();
    return EXIT_SUCCESS;
}
  1. 原图存储在frame_buffer


  2. 使用浮雕过滤的图像存储在 temp_buffer 中(是的,我知道它并不完美 :D )


我想用按键在它们之间切换。

编辑以下片段:

Pixel frame_buffer[WIDTH][HEIGHT]; //original image
Pixel temp_buffer[WIDTH][HEIGHT]; //embossed image
int which_image = 0;

...

    case 'S':
    case 's':
        which_image ^= 1;
        break;

...

glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, which_image == 0 ? frame_buffer : temp_buffer);

...

glFlush();
glutPostRedisplay();
glutSwapBuffers();

话虽这么说,您当前为绘制的每一帧上传纹理 (glTexImage2D)。您应该只在启动期间和每次更改时上传一次。