如何使用GLUT在按键上切换纹理/帧缓冲区?

时间:2016-11-22 19:31:39

标签: c opengl textures glut framebuffer

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

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

    中的原始图像

    enter image description here

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

    enter image description here

  3. 我想用钥匙扣在它们之间切换。

1 个答案:

答案 0 :(得分:2)

编辑以下代码段:

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)。您应该在启动期间和每次更改时仅上传一次。