PDA

View Full Version : after reshape some objects disappear



syncmaster
02-07-2008, 09:20 AM
Hello,
after glutDisplayFunc() done I start to work with glutMouseFunc() - here I draw some objects but if I make reshaping of the window by mouse then all the objects, which were drawn with glutMouseFunc(),
disappear. Wich were created before glutMouseFunc() call - they present on the window. What is wrong?
I made very simple version of my program which show the same mistake(second object disappear disappear after reshaping):
Regards/Oleg

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
int screenWidth = 800;
int screenHeight = 800;
const float winWidth = 130.;
const float winHeight = 130.;
void myMouse(int button, int state, int x, int y) {
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) exit(-1);
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
glColor3f(0.,0.,1.);
glBegin(GL_LINE_LOOP);
glVertex2f(50.,50.);
glVertex2f(50.,90.);
glVertex2f(90.,90.);
glVertex2f(90.,50.);
glEnd();
glFlush();
}
}
void reshape (int w, int h)
{
screenWidth = w;
screenHeight = h;
glViewport(0, 0, (GLsizei)screenWidth, (GLsizei)screenHeight);
}
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 130., 0.0, 130.);
glViewport(0, 0, (GLsizei)screenWidth, (GLsizei)screenHeight);
glDrawBuffer(GL_FRONT);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.,0.,1.);
glBegin(GL_LINE_LOOP);
glVertex2f(70.,70.);
glVertex2f(70.,110.);
glVertex2f(110.,110.);
glVertex2f(110.,70.);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow("Dot Plot of a Function");
glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay);
glutReshapeFunc(reshape);
myInit();
glutMainLoop();
return 0;
}

-NiCo-
02-07-2008, 09:40 AM
This is normal behavior.


Wich were created before glutMouseFunc() call - they present on the window.


You didn't create any objects before the glutMouseFunc() call, your application start at main().

Reshaping a window causes your displayfunc to be called, which apparently clears the screen draws the one rectangle. The other rectangle is only drawn when you press your left mouse button.

N.

syncmaster
02-07-2008, 11:43 PM
OK,
thank you.
After I sent my prevoius message I guessed that maybe glutDisplayFunc() function is called for each reshaping. I checked it then and realy found that display function is called MANY times during the reshaping. So I must change the structure of my program
to get the correct one.
Thank you.
Regards
Oleg

-NiCo-
02-08-2008, 12:57 AM
Indeed. As soon as either the width or the height of your window changes by as little as a pixel it needs to be redrawn so the display function is called. If you don't want this to happen you need to change a setting in the window system of your OS. e.g. set it so that you only see the rectangular silhouette of the new window size while reshaping until you release the mouse button.

N.