moving around and going near an object...

Hello everybody,

I’m having problems moving around with the glutfunctions…

my problem:

I have an object(a cube at the moment) placed in the origin (0,0,0).

i positioned myself with gluLookAt(1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0); right after having created the cube.

now, I would like to go forward, near the object or backward,moving away of the object.

I tried with gluLookAt(0.5,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0); which I thought should bring me near the object…but I obviously lost something on the way, because I don’t get anywhere near the obj…

anyone can explain me how to solve?

thx a lot

well this depends on a lot of stuff, you havent mention what type of
projection you have set up so i will give you two ways to do this…

with a glOrtho you would do the following:

  
GLfloat zoom = 0.0f;
GLfloat transx = 0.0f;
GLfloat transy = 0.0f;

glViewport(0,0,wide,high);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
   
glOrtho(-10.0/zoom,+10.0/zoom,-10.0/zoom,+10.0/zoom,-100.0,+100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();


glRotatef(rotx,1,0,0);
glRotatef(roty,0,1,0);

//drawing stuff here


with gluPerspective you would do the following


glViewport(0,0,wide,high);

 glMatrixMode (GL_PROJECTION);
 glLoadIdentity ();


gluPerspective(45.0, (float)(wide/high), 0.1, 200.0);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity ();

         glEnable(GL_DEPTH_TEST);

        glTranslatef(transx,transy,zoom);
        glRotatef(rotx,1,0,0);
        glRotatef(roty,0,1,0);


then you can use your mouse event callback to manipulate rotx,roty,and zoom, while mousebutton down and moving,although i don’t know if glut allows mouse scroll ability, this would be great for zooming…

first of all thanks for your answer, but I haven’t managed to solve my prob

ok…so, at the moment I haven’t specified any projection (I think I haven’t…

display function


void display(void) {
  glPushMatrix();
  glClear(GL_COLOR_BUFFER_BIT);	
  mesh();
  //gluLookAt (eyex,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0);
  glFlush();
  glPopMatrix();
}

here where I actually draw…


static void mesh() {	
	int i,f1,f2,f3,f4;

	glRotatef(mx / 2.0, 0, 1, 0); 				
	glRotatef(my / 2.0, 1, 0, 0); 				

	glTranslatef(-2*l, -2*l, -2*l); 				
	glScalef(1.0/dimension, 1.0/dimension, 1.0/dimension); 	
	glPushMatrix();
	
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
		
	for(i=1;i<counter_conn;i++){
 //here I draw...
}

reshape func…


void reshape(int w, int h) {
	glViewport(0, 0, (GLint) w, (GLint) h);
   
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if (w <= h)
		glOrtho (-1.0, 1.0, -1.0*(GLfloat)h/(GLfloat)w,1.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
	else
		glOrtho (-1.0*(GLfloat)w/(GLfloat)h,1.0*(GLfloat)w/(GLfloat)h,-1.0,1.0, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
}


and then I simply tried

gluLookAt (1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0);

associated with a key…I thougt this should position the eye in one place…(1,0,0)…but if I do so, I bounce around the object…

I tried using your code…but I haven’t solved…any other suggestions… :S

Don’t forget to call glLoadIdentity() before the gluLookAt(…) call otherwise the matrix created by the gluLookAt call is post-multiplied with the current modelview matrix.

ok, i be nice, but please look through code and see how it works, that way you will learn a bit more, copy exactly and paste and it should compile, make sure you link properly etc. the code is quite long but simple enough to understand, otherwise if you have trouble visit http://www.swiftless.com, i think he has a tutorial on using gluLookAt.


#define GLUT_DISABLE_ATEXIT_HACK

#if !defined(GLUT_WHEEL_UP)
#  define GLUT_WHEEL_UP   3
#  define GLUT_WHEEL_DOWN 4
#endif

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include<GL/glu.h>
#include <GL/gl.h>
#include <GL/glut.h>


/*func declare*/
void init();
static void resize(int width, int height);
static void display(void);
void mouse(int button, int state, int x, int y);
void motion( GLint x, GLint y );
void passive(GLint x, GLint y);
void keys(unsigned char key, int x, int y);


/*ui objects*/
void guipanel();
void renderview();
void rendergrid();
void drawbut();
void testrender();

/*flags*/
enum                actions {DRAGS, ROTATES, TRANSLATES, ZOOMS, NONE};
static GLint        action;

/*var declare*/
GLint wide = glutGet(GLUT_SCREEN_WIDTH)-1;
GLint high = glutGet(GLUT_SCREEN_HEIGHT)-50;
GLint gui_high = (high/3);


GLfloat zoom = 15.0f;
GLfloat otherzoom = 0.0f;
GLfloat transx = 0.0f;
GLfloat transy = 0.0f;

GLfloat rotx = 0.00f;
GLfloat roty = 0.001f;

GLint lastx=0;
GLint lasty=0;




///////////////////////////////////////////////////////////////////

void init()
{
    glEnable(GL_DEPTH_TEST);
}

//////////////////////////////////////////////////////////////////////////////

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);

    //this checks to give us an even screen coord to set world coords
    if(!(wide%2 == 0))
    {  wide -= 1;    }

    if(!(high%2 == 0))
    {  high -= 1;    }


    //there is no need to use floating points
    //but need to find a way to resolve world cooords

    glutInitWindowSize(wide,high);
    glutInitWindowPosition(0,0);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

    glutCreateWindow("mywin");


    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(keys);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutPassiveMotionFunc(passive);
    glutMainLoop();



    return 0;
}

/////////////////////////////////////////////////////////////////////////
void keys(unsigned char key, int x, int y)
{

    switch(key)
    {
        case (27):
            exit(0);
            break;

        case 'z':
            if(otherzoom < -12.00f)
            {
                otherzoom = -12.00f;
            }
            otherzoom -= 0.5f;
            glutPostRedisplay();
            break;

        case 'x':
            if(otherzoom > 50.0f)
            {
                otherzoom = 50.0f;
            }
            otherzoom += 0.5f;
            glutPostRedisplay();
            break;
    }



    if(!(GLUT_KEY_DOWN))
    {
        action = NONE;
    }

}


/////////////////////////////////////////////////////////////////////////

void mouse(int button, int state, int x, int y)
{
    lastx = x;
    lasty = y;
    if(state == GLUT_UP)
    {
       action = NONE;

    }




    else
    {
    switch (button)
    {

        case (GLUT_LEFT_BUTTON):
            if(state == GLUT_DOWN &&( ((high-y) < gui_high) && ((high-y) > gui_high-5) ))
            {
                action = DRAGS;
            }
            if(state == GLUT_DOWN &&((high-y) > gui_high  ))
            {
               action = ROTATES;
            }
                break;

        case (GLUT_MIDDLE_BUTTON):
            if(state == GLUT_DOWN &&((high-y) > gui_high  ))
            {
               action = TRANSLATES;
            }
                break;



    }



}

    glutPostRedisplay();

}
/////////////////////////////////////////////////////////////////////////

void motion( GLint x, GLint y )
{
    GLint diffx=x-lastx;
	GLint diffy=y-lasty;
	lastx=x;
	lasty=y;


    switch(action)
    {
        case DRAGS:

                gui_high = (high-y) ;
                if( ((high-y) > (high-30)) )
                {
                gui_high = (high-30);
                }
                if( y > (high-30))
                {
                gui_high = 30;
                }
                //main_high = y ;
                glutPostRedisplay();
                break;

        case ROTATES:

                rotx += (float) 0.5f * diffy;
                roty += (float) 0.5f * diffx;
                glutPostRedisplay();
                break;

        case TRANSLATES:

                transx += (float) 0.05f * diffx;
                transy -= (float) 0.05f * diffy;

                glutPostRedisplay();
                break;

        case ZOOMS:
                otherzoom -= (float) 0.05f;

                //transx += (float) 0.05f * diffx;
                //transy -= (float) 0.05f * diffy;

                glutPostRedisplay();
                break;

    }



}

/////////////////////////////////////////////////////////////////////////

void passive( GLint x, GLint y )
{

    if( ((high-y) < gui_high) && ((high-y) > gui_high-5) )
        {
             glutSetCursor(GLUT_CURSOR_UP_DOWN);
        }
    if(!( ((high-y) < gui_high) && ((high-y) > gui_high-5) ))
        {
             glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
        }

}



/////////////////////////////////////////////////////////////////////////

static void resize(int width, int height)
{



    glViewport(0,0,(GLsizei)(width),(GLsizei)(height));

            //to keep window fullscreen after resize event
            if(width < wide || height < high)
            {
                glutReshapeWindow(wide, high);

               //this is to keep window in correct position after resize event
               //for windows app you have to adjust the position slightly or you
               //will lose the taskbar
               //i am guessing that other os will accept the logical 0,0 coords
               //guessing is bad
               #ifdef _WIN32
               glutPositionWindow(1,50);
               #else
               glutPositionWindow(0,0);
               #endif
             }

    //main viewport
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,width,0,height);


   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();



}


//////////////////////////////////////////////////////////////////////////


static void display(void)
{



    //before any drawing occurs
    glClearColor(0.3,0.3,0.3,1.0);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );



    ///////////////////RENDER view



        glViewport(0,0,wide,high);

        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();

        gluPerspective(45.0,                  //The camera field of view
                   (float)(wide/high), //The width-to-height ratio
                   1.0,                   //The near z clipping coordinate
                   200.0);                //The far z clipping coordinate



        //you can uncomment and replace above with the following for ortho view
        //basically as mouse moves otherzoom will affect the clipping ratios of glOrtho
        //glOrtho(-10.0/otherzoom,+10.0/otherzoom,-10.0/otherzoom,+10.0/otherzoom,-100.0,+100.0);
        //BUT!!!!!!! you WILL have to figure out your own mouse events
        //also the code had a few bugs in at this point for switching between ortho and perpsective views 
        //so you might have to reorder some of the drawing functions
        //i never got round to fixing it as i stopped using glut for other alternatives      
        
        // i can't take full credit for all the code, i learnt a lot from various tutorials
        //esp at robthebloke.org
        
         

            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();

    glTranslatef(0.0,0.0,-zoom);
    glTranslatef(transx,transy,-otherzoom);
    glRotatef(rotx,1,0,0);
    glRotatef(roty,0,1,0);


    //renderview();
    rendergrid();

//////////////THE GUI PANEL
        glViewport(0,0,wide,gui_high);

        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();

        gluOrtho2D(0, wide, 0, gui_high);


           glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();


    guipanel();
    drawbut();




      ////////

      glutSwapBuffers();

}

////////////////////////////////////////////////////////////////////////

void guipanel()
{
     glColor3f(0.1,0.1,0.1);
     glBegin(GL_QUADS);
				glVertex2i(wide, (gui_high-5));
				glVertex2i(0,(gui_high-5));
				glVertex2i(0, gui_high);
				glVertex2i(wide, gui_high);
			glEnd();

     glColor3f(0.7,0.7,0.7);
        glBegin(GL_QUADS);
				glVertex2i(wide, 0);
				glVertex2i(0,0);
				glVertex2i(0, (gui_high-5));
				glVertex2i(wide, (gui_high-5));
			glEnd();

}

/////////////////////////////////////////////////////////////////////////////

void renderview()
{
glColor3f(0.3,0.3,0.3);
      glBegin(GL_QUADS);
				glVertex3i(2, 1,-1);
				glVertex3i(-2,1,-1);
				glVertex3i(-2, -1,-1);
				glVertex3i(2, -1,-1);
			glEnd();

}
/////////////////////////////////////////////////////////////////////////

void drawbut()
{
     glColor3f(0.5,0.5,0.5);
        glBegin(GL_QUADS);
				glVertex2i(50, (gui_high-50));
				glVertex2i(120,(gui_high-50));
				glVertex2i(120, (gui_high-90));
				glVertex2i(50, (gui_high-90));
			glEnd();

}

//////////////////////////////////////////////////////////////////////

void rendergrid()
{


    glBegin(GL_LINES);
    for(int i = (-10); i <= 10; i++)
    {
        glVertex3f(i,0,-10);
		glVertex3f(i,0,10);

		glVertex3f(10,0,i);
		glVertex3f(-10,0,i);
	}
	glColor3f(0.5,0.3,0.3);
        glVertex3f(0,10,0);
        glVertex3f(0,-10,0);
    glColor3f(0.3,0.5,0.3);
        glVertex3f(0,0,10);
        glVertex3f(0,0,-10);
    glColor3f(0.3,0.3,0.5);
        glVertex3f(10,0,0);
        glVertex3f(-10,0,0);
	glEnd();


}

//////////////////////////////////////////////////////////////////////


void testrender()
{
     glBegin(GL_QUADS);
      glColor3f(0.5,0.5,0.5);
      glVertex3f(-0.1f, 0.05f, -1.0f); //tr
      glVertex3f(-0.1f, -0.05f, -1.0f); //br
      glVertex3f(0.1f, -0.05f, -1.0f);  //bl
      glVertex3f(0.1f, 0.05f, -1.0f);  //tl

      glColor3f(0.4,0.4,0.4);
      glVertex3f(-0.1f, 0.05f, -1.0f);
      glVertex3f(-0.11f, 0.055f, -1.0f);
      glVertex3f(-0.11f, -0.055f, -1.0f);
      glVertex3f(-0.1f, -0.05f, -1.0f);

       glColor3f(0.4,0.4,0.4);
       glVertex3f(-0.1f, -0.05f, -1.0f); //br
      glVertex3f(0.1f, -0.05f, -1.0f);  //bl
      glVertex3f(0.11f, -0.055f, -1.0f);
      glVertex3f(-0.11f, -0.055f, -1.0f);

      glColor3f(0.6,0.6,0.6);
      glVertex3f(0.1f, -0.05f, -1.0f);
      glVertex3f(0.11f, -0.055f, -1.0f);
      glVertex3f(0.11f, 0.055f, -1.0f);
      glVertex3f(0.1f, 0.05f, -1.0f);

      glColor3f(0.7,0.7,0.7);
       glVertex3f(0.1f, 0.05f, -1.0f); //br
      glVertex3f(-0.1f, 0.05f, -1.0f);  //bl
      glVertex3f(-0.11f, 0.055f, -1.0f);
      glVertex3f(0.11f, 0.055f, -1.0f);


       glEnd();

}