Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Move a schema using viewport?

  1. #1
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    1

    Move a schema using viewport?

    Hello

    I want to move the viewport of a window in order to make a schema seem like moving when pressing the left mouse button (i do not want to move it actually or redraw it in other coordinates). I know i have to use glutMouseFunc, glutMotionFunc and maybe gluOrtho2D but i am a bit stuck. Here is my code. Any help would be precious thanks

    Code :
    #include <stdlib.h>
    #include <stdio.h>
    #include <GL/glut.h>
     
    typedef GLfloat point2[2];  
    point2 vertices[5]={{154.5,0.0},{0.0,475.55},{404.5,769.45},{809.0,475.55},{654.5,0.0}}; 
    int i, j , k, disp_flag = 0;
    point2 p ={75.0,50.0};
    float R,G,B;
    int InitWwidth=500.0,InitWheight=500.0;
     
    void myinit(void)
    { 
    	glEnable(GL_BLEND);
     	glClearColor(1.0, 1.0, 1.0, 0.0); 
    	glColor3f(1.0, 0.0, 0.0); 
     
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0, 500.0, 0.0, 500.0);
        glMatrixMode(GL_MODELVIEW);
    }
     
    void display(void)
    {		
        glutSwapBuffers(); 
    	if (disp_flag == 0)
    	{
    	  disp_flag = 1;      
    	  glClear(GL_COLOR_BUFFER_BIT);	  
     
    	  for( k=0; k<50000; k++)
          {
    	     j=rand()%5;
     
    	     p[0] = ((p[0]+vertices[j][0])*3)/8; 
    	     p[1] = ((p[1]+vertices[j][1])*3)/8;
     
             glBegin(GL_POINTS);
    	       glVertex2fv(p); 
    	     glEnd();
    	  }
          glutSwapBuffers(); 
    	}
     }
     
    void mymenu(int selection)
    {    
         if(selection==1) 
    	 {		
    		glClear(GL_COLOR_BUFFER_BIT);
    		R=rand()%100;
    		G=rand()%100;
        	B=rand()%100;
    		R = R / 100; 
    		G = G / 100; 
    		B = B / 100; 
    		glColor3f(R, G, B); 
     
    		for( k=0; k<100000; k++)
    		{
    		   j=rand()%5;
     
    		   p[0] = ((p[0]+vertices[j][0])*3)/8; 
    		   p[1] = ((p[1]+vertices[j][1])*3)/8;
     
    		   glBegin(GL_POINTS);
    			 glVertex2fv(p); 
    		   glEnd();
    		}
    		glutSwapBuffers(); 
    	  }
    	 else if (selection==2)
    	 {
    		glClear(GL_COLOR_BUFFER_BIT);
    		R=rand()%100;
    		G=rand()%100;
        	B=rand()%100;
    		R = R / 100; 
    		G = G / 100; 
    		B = B / 100; 
    		glColor3f(R, G, B);
     
    		for( k=0; k<200000; k++)
    		{
    		   j=rand()%5;
     
    		   p[0] = ((p[0]+vertices[j][0])*3)/8; 
    		   p[1] = ((p[1]+vertices[j][1])*3)/8;
     
    		   glBegin(GL_POINTS);
    			 glVertex2fv(p); 
    		   glEnd();
    		}
    		glutSwapBuffers(); 
    	  }
    	 else if (selection==3)
    	 {				
        	glClear(GL_COLOR_BUFFER_BIT);
    		R=rand()%100;
    		G=rand()%100;
        	B=rand()%100;
    		R = R / 100; 
    		G = G / 100; 
    		B = B / 100; 
    		glColor3f(R, G, B);
     
    		for( k=0; k<300000; k++)
    		{
    		   j=rand()%5;
     
    		   p[0] = ((p[0]+vertices[j][0])*3)/8; 
    		   p[1] = ((p[1]+vertices[j][1])*3)/8;
     
    		   glBegin(GL_POINTS);
    			 glVertex2fv(p); 
    		   glEnd();
    		}
    		glutSwapBuffers(); 
    	  }	 
    	 else if (selection==4)
    	 {
    		 exit(0);
    	 }		 
    }
     
    void mymousecontrol(GLint button, GLint action, GLint xMouse, GLint yMouse)
    {
     
    }
     
    void processMouseActiveMotion(int x, int y)//an ksekinisei na kounietai to pontiki enw einai patimeno
    {
     
    }
     
    void main(int argc, char** argv)
    {	
    	glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
        glutInitWindowSize(InitWwidth,InitWheight); /* 500 x 500 pixel window */
        glutInitWindowPosition(0,0); /* place window top left on display */
        glutCreateWindow("Sierpinski Gasket"); /* window title */	
    	myinit(); /* set attributes */	
    	glutDisplayFunc(display); /* display callback invoked when window opened */
     
    	glutCreateMenu(mymenu);
    		glutAddMenuEntry("100000 points",1);
    		glutAddMenuEntry("200000 points",2);
    		glutAddMenuEntry("300000 points",3);
    		glutAddMenuEntry("Exit",4); 
     
    	glutAttachMenu(GLUT_RIGHT_BUTTON);	
    	glutMouseFunc(mymousecontrol);
    	glutMotionFunc(processMouseActiveMotion);	
     
    	glutMainLoop();	
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,109
    A few comments
    1) the display routine is meant to be called when ever the window needs updating so all your display logic should be in here
    2) usually glutSwapBuffers is only called at the end of this routine
    3) don't do drawing the menus just change states

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •