Serge S
Serge S

Reputation: 25

GLUT zooming in and out by wheel

I have surfed a lot of sources and tried many varieties, but zooming still don't work. I can't change glprojection to gluPerspective, because in this case my prog don't draw anything. Here is approximate code.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "glut.h"

#define windowSize 900.0
double zoomFactor = 1;
void reshape (int w, int h)
{
glViewport (0.0, 0.0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-(GLdouble) w * zoomFactor, (GLdouble) w* zoomFactor, -(GLdouble) h* zoomFactor, (GLdouble) h* zoomFactor);
}
void mouse(int button, int state, int x, int y)
{
      if (button == 3 || button == 4 )
   { 
       if (state == GLUT_UP) zoomFactor += 0.05;
else zoomFactor -= 0.05;
   }
   else return;
   glutReshapeFunc(reshape);
}

Void display(){
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINES); 
glVertex2f ((0),(0)); 
glVertex2f ((100),(0));
 glEnd();
glBegin(GL_LINES); 
glVertex2f ((100),(0)); 
glVertex2f ((100),(100));
 glEnd();
glBegin(GL_LINES); 
glVertex2f ((0),(0)); 
glVertex2f ((100),(1000));
 glEnd();
glFlush();

}

int main(int argc,char** argv)
{
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(windowSize, windowSize);
glutInitWindowPosition(500,0);
glutCreateWindow("test");
glClearColor(0, 0.1,0.8,0.90);    
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 200, 200,0);   
glutDisplayFunc( display );    
glutMouseFunc(mouse);
glutMainLoop(;

return(0);
}

Upvotes: 0

Views: 3098

Answers (1)

datenwolf
datenwolf

Reputation: 162164

glutReshapeFunc only tells GLUT which function to call, when the window size changes. It makes very little sense to call it in response to an input event as it will not call the reshape function.

Anyway, your problem is, that you placed setting the viewport and the projection in the reshape function. To all the tutorial writers out there: Stop doing that, it just ingrains bad habits into newbies.

Now please speak with me: "Everything drawing related goes into the display function. The viewport is a drawing state and in every OpenGL program just a little bit more complex than 'Hello Triangle' the viewport is going to be changed several times drawing a single frame – for example when using framebuffer objects. I will thereby vow to never place calls to glViewport or projection matrix setup in the window reshape handler. I also vow to slap everyone in the face who writes code like this in my presence."

Move the whole stuff in your reshape function into display (either set global variables, or use glutGet(GLUT_WINDOW_{WIDTH,HEIGHT}) to get the window dimensions in the display function) and call glutPostRedisplay in your mouse wheel function to trigger a redraw.

Upvotes: 3

Related Questions