Sign in to follow this  

gluLookAt problem

This topic is 4053 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'm trying to create a program that has multiple views of the same set of object (some simple shapes). So far I've managed to successfully create each subwindow, and manipulating the shapes in any window moves them around for all windows. What I can't seem to get is gluLookAt to alter the viewing angle for my different subwindows. Any ideas?

#include <GL/glut.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
using std::string;

void translate(float x, float y, float z);
void readFile(string s);
static float vAngle, hAngle, mouseX, mouseY = 0;
static float zoom = 1.3f;
static float pan = 0.0;
unsigned char Buttons[2] = {0};
static int w = 600;
static int h = 600;
static int border = 6;
float x=0.0f,y=1.75f,z=5.0f;
float lx=0.0f,ly=0.0f,lz=-1.0f;
int mainWindow, subWindow1, subWindow2, subWindow3;

void display(void)
{
  glutSetWindow(mainWindow);
  glClear(GL_COLOR_BUFFER_BIT);
  glutSwapBuffers();
}

void subdisplay(int currentWindow)
{
    //clear screen and buffers
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(90, (GLfloat)w/(GLfloat)h, 0.3, 50);
  //draw objects
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(pan,0, -zoom);
  glRotatef(hAngle, 0.0f, 1.0f, 0.0f);
  glRotatef(vAngle, 0.0f, 0.0f, 1.0f);
  translate(-0.5, 0.0, 0.0);
  glColor3f(0.0f, 1.0f, 0.0f);
  glutSolidSphere(0.2, 20, 16);
  glPopMatrix();
  translate(0.0, 0.0, 0.0);
  glColor3f(1.0f, 0.0f, 0.0f);
  glutSolidCube(0.2f);
  glPopMatrix();
  translate(0.5, 0.0, 0.0);
  glColor3f(0.0f, 0.0f, 1.0f);  
  glutSolidTeapot(0.1f);
  glPopMatrix();
  //flush buffers
  glutSwapBuffers();
  glFlush();
}

void display_window1() {
	glutSetWindow(subWindow1);
        glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,1.0f,0.0f);
	subdisplay(subWindow1);
}

void display_window2() {
	glutSetWindow(subWindow2);
        glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluLookAt(x, y+15, z, x ,y - 1,z,lx,0,lz);
	subdisplay(subWindow2);
}

void display_window3() {
	glutSetWindow(subWindow3);
        glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluLookAt(x-lz*10 , y, z+lx*10,x ,y ,z ,0.0f,1.0f,0.0f);
	subdisplay(subWindow3);
}

void translate(float x, float y, float z)
{
  glPushMatrix();
    glTranslatef(x,y,z); // position object
}

void orient(int x, int y)
{
  int xDiff = (x - mouseX);
  int yDiff = (y - mouseY);
  mouseX = x;
  mouseY = y;
  if( Buttons[1] )
  {
    zoom += (float) 0.1f * yDiff;
    pan += (float) 0.1f * xDiff;
    if (zoom < 1.3)
    {
      zoom = 1.3;
    }
    else if (zoom > 48)
    {
      zoom = 48;
    }
  }
  else if( Buttons[0] )
  {
    vAngle += (float) yDiff;
    hAngle += (float) xDiff;		
  }
  glutPostRedisplay();
}

void clicker(int button, int state, int x, int y)
{
  mouseX = x;
  mouseY = y;
  switch(button)
  {
    case GLUT_LEFT_BUTTON:
      Buttons[0] = ((GLUT_DOWN==state)?1:0);
      break;
    case GLUT_RIGHT_BUTTON:
      Buttons[1] = ((GLUT_DOWN==state)?1:0);
      break;
    default:
      break;		
  }
}

void renderSceneAll() 
{
  display_window1();
  display_window2();
  display_window3();
}

void readFile(string s)
{
  ifstream fin(s.c_str());
  if (!fin.good())
  {
    cout << "Cannot find " + s << endl;
  }
  else
  {
    char ch;
    while ((ch = fin.get()) != '\n')
    {      
      cout << ch;
    }    
    cout << endl;
  }
  fin.close();
}

void init(void)
{
  GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  GLfloat mat_shininess[] = {50.0};
  GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
  GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
  GLfloat lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  glShadeModel(GL_SMOOTH); // smooth shading
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
  glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
	
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_DEPTH_TEST); // depth testing

  glEnable(GL_COLOR_MATERIAL);
  //glEnable(GL_LINE_SMOOTH);
  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

  glDepthFunc(GL_LEQUAL);	// depth testing type
}

int main (int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  glutInitWindowPosition(100, 100);
  glutInitWindowSize(w, h);
  mainWindow = glutCreateWindow("Graphics Coursework");
  glutDisplayFunc(display);
  glutIdleFunc(renderSceneAll);
  init();

  subWindow1 = glutCreateSubWindow(mainWindow,border,border,w-2*border, h/2 - border*3/2);
	       glutDisplayFunc(display_window1);
               glutMouseFunc(clicker);
               glutMotionFunc(orient);
	       init();

  subWindow2 = glutCreateSubWindow(mainWindow,border,(h+border)/2,w/2-border*3/2, h/2 - border*3/2);
               glutDisplayFunc(display_window2);
               glutMouseFunc(clicker);
               glutMotionFunc(orient);
               init();

  subWindow3 = glutCreateSubWindow(mainWindow,(w+border)/2,(h+border)/2,w/2-border*3/2,h/2 - border*3/2);
               glutDisplayFunc(display_window3);
               glutMouseFunc(clicker);
               glutMotionFunc(orient);
               init();

  glutMainLoop();
  return 0;
}

Share this post


Link to post
Share on other sites
For each subwindow, you're first calling gluLookAt while operating on the PROJECTION stack (which is wrong), and then in the display() call you're removing it with glLoadIdentity.

Share this post


Link to post
Share on other sites

This topic is 4053 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