Peter B. Peter B. - 1 year ago 114
C Question

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

I have two buffers with

.rgb
images loaded. Original image is stored in
frame_buffer
and second one is stored in
temp_buffer
. I want to switch between those two images in GLUT window, when user press a button, how can I extend my code to do this?

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. original image stored in
    frame_buffer


    enter image description here


  2. image filtered with emboss stored in
    temp_buffer
    (yeah I know its not perfect :D )

    enter image description here




I want to switch between them with keybutton.

Answer Source

Edit the following snippets:

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();

That being said, you currently upload the texture (glTexImage2D) for each frame drawn. You should rather upload it only once during startup and each time it changes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download