Sign in to follow this  

Resize window deletes "scene"

This topic is 3370 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need some help with figuring out how to "redisplay an image in an OpenGL window. Below code draws rectangles. Resizing the OpenGL window, however, causes to "lose" the image/rectangles. I'm a beginner and still learning some basic commands. I tried "glutReshapeFunc(myReshape);" -- that doesn't do it (or maybe it's just positioned at the wrong place). Please help -- what code do I need to include to keep the drawn image after having resized the window? Thanks, EEH ***************************** #include <stdlib.h> #include <GL/glut.h> float xm, ym, xmm, ymm; float width = 500, height = 500; int first = 0; void mouse(int btn, int state, int x, int y) { if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { xm = x / width; ym = (height - y) / height; glColor3f(0.0, 0.0, 1.0); glLogicOp(GL_XOR); first = 0; } if (btn == GLUT_LEFT_BUTTON && state == GLUT_UP) { glRectf(xm, ym, xmm, ymm); glFlush(); glColor3f(0.0, 1.0, 0.0); xmm = x / width; ymm = (height - y) / height; glLogicOp(GL_COPY); glRectf(xm, ym, xmm, ymm); glutSwapBuffers(); glFlush(); } } void myReshape(int w, int h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 1, 0, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, w, h); width = w; height = h; } void move(int x, int y) { if (first == 1) { glRectf(xm, ym, xmm, ymm); glFlush(); } xmm = x / width; ymm = (height - y) / height; glRectf(xm, ym, xmm, ymm); glutSwapBuffers(); glFlush(); first = 1; } void myinit() { glClearColor (0.0, 0.0, 0.0, 1.0); } void display() { glClear(GL_COLOR_BUFFER_BIT); glutSwapBuffers(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(width, height); glutCreateWindow("Rubberband"); myinit (); glEnable(GL_COLOR_LOGIC_OP); glutReshapeFunc(myReshape); glutMouseFunc(mouse); glutMotionFunc(move); glutDisplayFunc(display); glutReshapeFunc(myReshape); // new glutMainLoop(); }

Share this post


Link to post
Share on other sites
I don't know how glut work (I use SDL instead) but you will need to redraw the scene when the window resizes. Typically you store your scene data and redraw it on a regular basic. You typically don't draw during you input routines.

A loop is usually used that calls each section of code: get input and make changes to data, check for window being closed/resized, etc, and at the end of each loop redraw the scene. It looks like glut is handling the loop for you. You likely just need to remove the drawing code from the mouse/move functions and place it into the display function.

Share this post


Link to post
Share on other sites

This topic is 3370 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this